Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-http by
Revision 7:2e3eedb9ca5c, committed 2017-02-24
- Comitter:
- Jan Jongboom
- Date:
- Fri Feb 24 11:20:16 2017 +0100
- Parent:
- 6:112d72c60e07
- Child:
- 8:6156404278bb
- Commit message:
- Add support for chunked encoding. Fix bug handle querystrings. HttpResponse should own header strings.
Changed in this revision
--- a/source/http_request.h Thu Feb 23 14:11:47 2017 +0100
+++ b/source/http_request.h Fri Feb 24 11:20:16 2017 +0100
@@ -137,7 +137,7 @@
return NULL;
}
- if (response->is_body_complete()) {
+ if (response->is_message_complete()) {
break;
}
}
--- a/source/http_request_builder.h Thu Feb 23 14:11:47 2017 +0100
+++ b/source/http_request_builder.h Fri Feb 24 11:20:16 2017 +0100
@@ -80,8 +80,14 @@
char* req = (char*)calloc(size + 1, 1);
char* originalReq = req;
- sprintf(req, "%s %s%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
- req += strlen(method_str) + 1 + strlen(parsed_url->path()) + strlen(parsed_url->query()) + 1 + 8 + 2;
+ if (strcmp(parsed_url->query(), "") == 0) {
+ sprintf(req, "%s %s HTTP/1.1\r\n", method_str, parsed_url->path());
+ req += strlen(method_str) + 1 + strlen(parsed_url->path()) + 1 + 8 + 2;
+ }
+ else {
+ sprintf(req, "%s %s?%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query());
+ req += strlen(method_str) + 1 + strlen(parsed_url->path()) + 1 + strlen(parsed_url->query()) + 1 + 8 + 2;
+ }
typedef map<string, string>::iterator it_type;
for(it_type it = headers.begin(); it != headers.end(); it++) {
--- a/source/http_response.h Thu Feb 23 14:11:47 2017 +0100
+++ b/source/http_response.h Fri Feb 24 11:20:16 2017 +0100
@@ -30,6 +30,8 @@
concat_header_field = false;
concat_header_value = false;
expected_content_length = 0;
+ is_chunked = false;
+ is_message_completed = false;
body_length = 0;
body_offset = 0;
body = NULL;
@@ -39,6 +41,11 @@
if (body != NULL) {
free(body);
}
+
+ for (size_t ix = 0; ix < header_fields.size(); ix++) {
+ delete header_fields[ix];
+ delete header_values[ix];
+ }
}
void set_status(int a_status_code, string a_status_message) {
@@ -59,10 +66,10 @@
// headers can be chunked
if (concat_header_field) {
- header_fields[header_fields.size() - 1] = header_fields[header_fields.size() - 1] + field;
+ *header_fields[header_fields.size() - 1] = (*header_fields[header_fields.size() - 1]) + field;
}
else {
- header_fields.push_back(field);
+ header_fields.push_back(new string(field));
}
concat_header_field = true;
@@ -73,20 +80,19 @@
// headers can be chunked
if (concat_header_value) {
- header_values[header_values.size() - 1] = header_values[header_values.size() - 1] + value;
+ *header_values[header_values.size() - 1] = (*header_values[header_values.size() - 1]) + value;
}
else {
- header_values.push_back(value);
+ header_values.push_back(new string(value));
}
concat_header_value = true;
}
void set_headers_complete() {
- // @todo: chunked encoding
for (size_t ix = 0; ix < header_fields.size(); ix++) {
- if (strcicmp(header_fields[ix].c_str(), "content-length") == 0) {
- expected_content_length = (size_t)atoi(header_values[ix].c_str());
+ if (strcicmp(header_fields[ix]->c_str(), "content-length") == 0) {
+ expected_content_length = (size_t)atoi(header_values[ix]->c_str());
break;
}
}
@@ -96,20 +102,29 @@
return header_fields.size();
}
- vector<string> get_headers_fields() {
+ vector<string*> get_headers_fields() {
return header_fields;
}
- vector<string> get_headers_values() {
+ vector<string*> get_headers_values() {
return header_values;
}
void set_body(const char *at, size_t length) {
// only malloc when this fn is called, so we don't alloc when body callback's are enabled
- if (body == NULL) {
+ if (body == NULL && !is_chunked) {
body = (char*)malloc(expected_content_length);
}
+ if (is_chunked) {
+ if (body == NULL) {
+ body = (char*)malloc(length);
+ }
+ else {
+ body = (char*)realloc(body, body_offset + length);
+ }
+ }
+
memcpy(body + body_offset, at, length);
body_offset += length;
@@ -132,8 +147,16 @@
return body_offset;
}
- bool is_body_complete() {
- return body_length == expected_content_length;
+ bool is_message_complete() {
+ return is_message_completed;
+ }
+
+ void set_chunked() {
+ is_chunked = true;
+ }
+
+ void set_message_complete() {
+ is_message_completed = true;
}
private:
@@ -150,14 +173,18 @@
int status_code;
string status_message;
- vector<string> header_fields;
- vector<string> header_values;
+ vector<string*> header_fields;
+ vector<string*> header_values;
bool concat_header_field;
bool concat_header_value;
size_t expected_content_length;
+ bool is_chunked;
+
+ bool is_message_completed;
+
char * body;
size_t body_length;
size_t body_offset;
--- a/source/http_response_parser.h Thu Feb 23 14:11:47 2017 +0100
+++ b/source/http_response_parser.h Fri Feb 24 11:20:16 2017 +0100
@@ -108,16 +108,18 @@
}
int on_message_complete(http_parser* parser) {
+ response->set_message_complete();
+
return 0;
}
int on_chunk_header(http_parser* parser) {
- // ?? Don't know when this is used
+ response->set_chunked();
+
return 0;
}
int on_chunk_complete(http_parser* parser) {
- // ?? Don't know when this is used
return 0;
}
@@ -159,7 +161,7 @@
}
static int on_chunk_complete_callback(http_parser* parser) {
- return ((HttpResponseParser*)parser->data)->on_chunk_complete_callback(parser);
+ return ((HttpResponseParser*)parser->data)->on_chunk_complete(parser);
}
HttpResponse* response;
--- a/source/https_request.h Thu Feb 23 14:11:47 2017 +0100
+++ b/source/https_request.h Fri Feb 24 11:20:16 2017 +0100
@@ -257,7 +257,7 @@
return NULL;
}
- if (_response->is_body_complete()) {
+ if (_response->is_message_complete()) {
break;
}
}
