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 3:8a6b003e3874, committed 2017-02-23
- Comitter:
- Jan Jongboom
- Date:
- Thu Feb 23 13:28:07 2017 +0100
- Parent:
- 2:959baaa89148
- Child:
- 4:539df159e058
- Commit message:
- Rely on Content-Length header to determine when request is processed
Changed in this revision
--- a/source/http_request.h Thu Feb 16 13:55:05 2017 +0100
+++ b/source/http_request.h Thu Feb 23 13:28:07 2017 +0100
@@ -127,6 +127,7 @@
// TCPSocket::recv is called until we don't have any data anymore
nsapi_size_or_error_t recv_ret;
while ((recv_ret = socket.recv(recv_buffer, HTTP_RECEIVE_BUFFER_SIZE)) > 0) {
+
// Pass the chunk into the http_parser
size_t nparsed = parser.execute((const char*)recv_buffer, recv_ret);
if (nparsed != recv_ret) {
@@ -135,8 +136,8 @@
free(recv_buffer);
return NULL;
}
- // No more chunks? break out of this loop
- if (recv_ret < HTTP_RECEIVE_BUFFER_SIZE) {
+
+ if (response->is_body_complete()) {
break;
}
}
--- a/source/http_response.h Thu Feb 16 13:55:05 2017 +0100
+++ b/source/http_response.h Thu Feb 23 13:28:07 2017 +0100
@@ -29,6 +29,8 @@
status_code = 0;
concat_header_field = false;
concat_header_value = false;
+ expected_content_length = 0;
+ body_length = 0;
}
void set_status(int a_status_code, string a_status_message) {
@@ -72,6 +74,16 @@
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());
+ break;
+ }
+ }
+ }
+
size_t get_headers_length() {
return header_fields.size();
}
@@ -92,7 +104,25 @@
return body;
}
+ void increase_body_length(size_t length) {
+ body_length += length;
+ }
+
+ bool is_body_complete() {
+ return body_length == expected_content_length;
+ }
+
private:
+ // from http://stackoverflow.com/questions/5820810/case-insensitive-string-comp-in-c
+ int strcicmp(char const *a, char const *b) {
+ for (;; a++, b++) {
+ int d = tolower(*a) - tolower(*b);
+ if (d != 0 || !*a) {
+ return d;
+ }
+ }
+ }
+
int status_code;
string status_message;
@@ -102,6 +132,9 @@
bool concat_header_field;
bool concat_header_value;
+ size_t expected_content_length;
+
string body;
+ size_t body_length;
};
#endif
--- a/source/http_response_parser.h Thu Feb 16 13:55:05 2017 +0100
+++ b/source/http_response_parser.h Thu Feb 23 13:28:07 2017 +0100
@@ -90,11 +90,14 @@
return 0;
}
- static int on_headers_complete(http_parser* parser) {
+ int on_headers_complete(http_parser* parser) {
+ response->set_headers_complete();
return 0;
}
int on_body(http_parser* parser, const char *at, size_t length) {
+ response->increase_body_length(length);
+
if (body_callback) {
body_callback(at, length);
return 0;
