This library is used to make HTTP and HTTPS calls from mbed OS 5 applications.
Fork of mbed-http by
Revision 10:b017c7d2cf23, committed 2017-03-28
- Comitter:
- Jan Jongboom
- Date:
- Tue Mar 28 13:33:14 2017 +0200
- Parent:
- 9:1289162d9530
- Child:
- 11:96e4dcb9c0c2
- Commit message:
- Don't rely on strlen for request length, as it prevents sending \0 over the line. Patch via https://developer.mbed.org/users/ihere1/code/mbed-http/rev/ec9b75c349aa
Changed in this revision
--- a/source/http_request.h Thu Mar 02 13:14:36 2017 +0100 +++ b/source/http_request.h Tue Mar 28 13:33:14 2017 +0200 @@ -104,8 +104,8 @@ return NULL; } - char* request = request_builder->build(body, body_size); - size_t request_size = strlen(request); + size_t request_size = 0; + char* request = request_builder->build(body, body_size, request_size); nsapi_size_or_error_t send_result = socket.send(request, request_size);
--- a/source/http_request_builder.h Thu Mar 02 13:14:36 2017 +0100 +++ b/source/http_request_builder.h Tue Mar 28 13:33:14 2017 +0200 @@ -46,19 +46,19 @@ } } - char* build(const void* body, size_t body_size) { + char* build(const void* body, size_t body_size, size_t &size) { const char* method_str = http_method_str(method); - if (body_size > 0) { + if (method == HTTP_POST || method == HTTP_PUT || method == HTTP_DELETE || body_size > 0) { char buffer[10]; snprintf(buffer, 10, "%d", body_size); set_header("Content-Length", string(buffer)); } - size_t size = 0; + size = 0; // first line is METHOD PATH+QUERY HTTP/1.1\r\n - size += strlen(method_str) + 1 + strlen(parsed_url->path()) + strlen(parsed_url->query()) + 1 + 8 + 2; + size += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2; // after that we'll do the headers typedef map<string, string>::iterator it_type; @@ -80,14 +80,12 @@ char* req = (char*)calloc(size + 1, 1); char* originalReq = req; - 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; + if (strlen(parsed_url->query())) { + sprintf(req, "%s %s?%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query()); + } else { + sprintf(req, "%s %s%s HTTP/1.1\r\n", method_str, parsed_url->path(), parsed_url->query()); } - 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; - } + req += strlen(method_str) + 1 + strlen(parsed_url->path()) + (strlen(parsed_url->query()) ? strlen(parsed_url->query()) + 1 : 0) + 1 + 8 + 2; typedef map<string, string>::iterator it_type; for(it_type it = headers.begin(); it != headers.end(); it++) { @@ -100,7 +98,7 @@ req += 2; if (body_size > 0) { - sprintf(req, "%s", (char*)body); + memcpy(req, body, body_size); } req += body_size;
--- a/source/https_request.h Thu Mar 02 13:14:36 2017 +0100 +++ b/source/https_request.h Tue Mar 28 13:33:14 2017 +0200 @@ -196,8 +196,8 @@ return NULL; } - char* request = _request_builder->build(body, body_size); - size_t request_size = strlen(request); + size_t request_size = 0; + char* request = _request_builder->build(body, body_size, request_size); ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) request, request_size);