4 #include "http_parsed_url.h"
5 #include "http_parser.h"
15 switch (status_code) {
19 return "Switching Protocols";
29 return "Non-Authoritative Information";
33 return "Reset Content";
35 return "Partial Content";
37 return "Multi-Status";
39 return "Already Reported";
43 return "Multiple Choices";
45 return "Moved Permanently";
51 return "Not Modified";
55 return "Temporary Redirect";
57 return "Permanent Redirect";
61 return "Unauthorized";
63 return "Payment Required";
69 return "Method Not Allowed";
71 return "Not Acceptable";
73 return "Proxy Authentication Required";
75 return "Request Timeout";
81 return "Length Required";
83 return "Precondition Failed";
85 return "Payload Too Large";
87 return "URI Too Long";
89 return "Unsupported Media Type";
91 return "Range Not Satisfiable";
93 return "Expectation Failed";
95 return "Misdirected Request";
97 return "Unprocessable Entity";
101 return "Failed Dependency";
103 return "Upgrade Required";
105 return "Precondition Required";
107 return "Too Many Requests";
109 return "Request Header Fields Too Large";
111 return "Unavailable For Legal Reasons";
113 return "Internal Server Error";
115 return "Not Implemented";
117 return "Bad Gateway";
119 return "Service Unavailable";
121 return "Gateway Timeout";
123 return "HTTP Version Not Supported";
125 return "Variant Also Negotiates";
127 return "Insufficient Storage";
129 return "Loop Detected";
131 return "Not Extended";
133 return "Network Authentication Required";
142 : status_code(a_status_code),
150 map<string, string>::iterator it = headers.find(key);
152 if (it != headers.end()) {
155 headers.insert(headers.end(), pair<string, string>(key, value));
159 char *
build(
const void *body,
size_t body_size,
size_t *size) {
161 snprintf(buffer,
sizeof(buffer),
"%d", body_size);
162 set_header(
"Content-Length",
string(buffer));
164 char status_code_buffer[5];
165 snprintf(status_code_buffer,
sizeof(status_code_buffer),
"%d",
172 8 + 1 + strlen(status_code_buffer) + 1 + strlen(status_message) + 2;
175 typedef map<string, string>::iterator it_type;
176 for (it_type it = headers.begin(); it != headers.end(); it++) {
178 *size += it->first.length() + 1 + 1 + it->second.length() + 2;
188 char *res = (
char *)calloc(*size + 1, 1);
189 char *originalRes = res;
192 sprintf(res,
"HTTP/1.1 %s %s\r\n", status_code_buffer, status_message);
194 typedef map<string, string>::iterator it_type;
195 for (it_type it = headers.begin(); it != headers.end(); it++) {
197 res += sprintf(res,
"%s: %s\r\n", it->first.c_str(), it->second.c_str());
200 res += sprintf(res,
"\r\n");
203 memcpy(res, body, body_size);
208 printf(
"\nhttp_response_builder#build\n");
209 printf(
"----- BEGIN RESPONSE -----\n");
210 printf(
"%s", originalRes);
211 printf(
"----- END RESPONSE -----\n");
217 nsapi_error_t
send(TCPSocket *socket,
const void *body,
size_t body_size) {
219 return NSAPI_ERROR_NO_SOCKET;
222 char *response = build(body, body_size, &res_size);
224 nsapi_error_t r = socket->send(response, res_size);
nsapi_error_t send(TCPSocket *socket, const void *body, size_t body_size)
void set_header(string key, string value)
map< string, string > headers
char * build(const void *body, size_t body_size, size_t *size)
const char * status_message
HttpResponseBuilder(uint16_t a_status_code)
static const char * get_http_status_string(uint16_t status_code)