greenhouse
Public Member Functions | Private Attributes
HttpResponseBuilder Class Reference

#include <http_response_builder.h>

Collaboration diagram for HttpResponseBuilder:
Collaboration graph
[legend]

Public Member Functions

 HttpResponseBuilder (uint16_t a_status_code)
 
void set_header (string key, string value)
 
char * build (const void *body, size_t body_size, size_t *size)
 
nsapi_error_t send (TCPSocket *socket, const void *body, size_t body_size)
 

Private Attributes

uint16_t status_code
 
const char * status_message
 
map< string, string > headers
 

Detailed Description

Definition at line 139 of file http_response_builder.h.

Constructor & Destructor Documentation

◆ HttpResponseBuilder()

HttpResponseBuilder::HttpResponseBuilder ( uint16_t  a_status_code)
inline

Definition at line 141 of file http_response_builder.h.

142  : status_code(a_status_code),
143  status_message(get_http_status_string(a_status_code)) {}
static const char * get_http_status_string(uint16_t status_code)

Member Function Documentation

◆ build()

char* HttpResponseBuilder::build ( const void *  body,
size_t  body_size,
size_t *  size 
)
inline

Definition at line 159 of file http_response_builder.h.

159  {
160  char buffer[10];
161  snprintf(buffer, sizeof(buffer), "%d", body_size);
162  set_header("Content-Length", string(buffer));
163 
164  char status_code_buffer[5];
165  snprintf(status_code_buffer, sizeof(status_code_buffer), "%d",
166  status_code /* max 5 digits */);
167 
168  *size = 0;
169 
170  // first line is HTTP/1.1 200 OK\r\n
171  *size +=
172  8 + 1 + strlen(status_code_buffer) + 1 + strlen(status_message) + 2;
173 
174  // after that we'll do the headers
175  typedef map<string, string>::iterator it_type;
176  for (it_type it = headers.begin(); it != headers.end(); it++) {
177  // line is KEY: VALUE\r\n
178  *size += it->first.length() + 1 + 1 + it->second.length() + 2;
179  }
180 
181  // then the body, first an extra newline
182  *size += 2;
183 
184  // body
185  *size += body_size;
186 
187  // Now let's print it
188  char *res = (char *)calloc(*size + 1, 1);
189  char *originalRes = res;
190 
191  res +=
192  sprintf(res, "HTTP/1.1 %s %s\r\n", status_code_buffer, status_message);
193 
194  typedef map<string, string>::iterator it_type;
195  for (it_type it = headers.begin(); it != headers.end(); it++) {
196  // line is KEY: VALUE\r\n
197  res += sprintf(res, "%s: %s\r\n", it->first.c_str(), it->second.c_str());
198  }
199 
200  res += sprintf(res, "\r\n");
201 
202  if (body_size > 0) {
203  memcpy(res, body, body_size);
204  }
205  res += body_size;
206 
207 #ifdef DEBUG
208  printf("\nhttp_response_builder#build\n");
209  printf("----- BEGIN RESPONSE -----\n");
210  printf("%s", originalRes);
211  printf("----- END RESPONSE -----\n");
212 #endif
213 
214  return originalRes;
215  }
void set_header(string key, string value)
map< string, string > headers

◆ send()

nsapi_error_t HttpResponseBuilder::send ( TCPSocket *  socket,
const void *  body,
size_t  body_size 
)
inline

Definition at line 217 of file http_response_builder.h.

217  {
218  if (!socket)
219  return NSAPI_ERROR_NO_SOCKET;
220 
221  size_t res_size;
222  char *response = build(body, body_size, &res_size);
223 
224  nsapi_error_t r = socket->send(response, res_size);
225 
226  free(response);
227 
228  return r;
229  }
char * build(const void *body, size_t body_size, size_t *size)

Referenced by WebServer::tick().

Here is the caller graph for this function:

◆ set_header()

void HttpResponseBuilder::set_header ( string  key,
string  value 
)
inline

Set a header for the request If the key already exists, it will be overwritten...

Definition at line 149 of file http_response_builder.h.

149  {
150  map<string, string>::iterator it = headers.find(key);
151 
152  if (it != headers.end()) {
153  it->second = value;
154  } else {
155  headers.insert(headers.end(), pair<string, string>(key, value));
156  }
157  }

Field Documentation

◆ headers

map<string, string> HttpResponseBuilder::headers
private

Definition at line 234 of file http_response_builder.h.

◆ status_code

uint16_t HttpResponseBuilder::status_code
private

Definition at line 232 of file http_response_builder.h.

◆ status_message

const char* HttpResponseBuilder::status_message
private

Definition at line 233 of file http_response_builder.h.


The documentation for this class was generated from the following file: