fix the problem http request with '\0' and fixx query string in url
Fork of mbed-http by
Revision 10:ec9b75c349aa, committed 2017-03-03
- Comitter:
- ihere1
- Date:
- Fri Mar 03 10:51:56 2017 +0000
- Parent:
- 9:1289162d9530
- Commit message:
- fix the problem http request with '\0' and fix query string in url
Changed in this revision
diff -r 1289162d9530 -r ec9b75c349aa source/http_request.h --- a/source/http_request.h Thu Mar 02 13:14:36 2017 +0100 +++ b/source/http_request.h Fri Mar 03 10:51:56 2017 +0000 @@ -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);
diff -r 1289162d9530 -r ec9b75c349aa source/http_request_builder.h --- a/source/http_request_builder.h Thu Mar 02 13:14:36 2017 +0100 +++ b/source/http_request_builder.h Fri Mar 03 10:51:56 2017 +0000 @@ -46,19 +46,17 @@ } } - 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) { char buffer[10]; snprintf(buffer, 10, "%d", body_size); set_header("Content-Length", string(buffer)); } - size_t 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 +78,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 +96,7 @@ req += 2; if (body_size > 0) { - sprintf(req, "%s", (char*)body); + memcpy(req, body, body_size); } req += body_size;
diff -r 1289162d9530 -r ec9b75c349aa source/https_request.h --- a/source/https_request.h Thu Mar 02 13:14:36 2017 +0100 +++ b/source/https_request.h Fri Mar 03 10:51:56 2017 +0000 @@ -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);