Skip to content

Commit

Permalink
[HTTP] Fixed incorrect string length
Browse files Browse the repository at this point in the history
  • Loading branch information
jgromes committed Sep 9, 2019
1 parent 14df1d8 commit 4eef30a
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/protocols/HTTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int16_t HTTPClient::get(const char* url, String& response) {
char* hostEnd = strchr(hostStart + 1, '/');
host = new char[hostEnd - hostStart];
strncpy(host, hostStart + 1, hostEnd - hostStart - 1);
host[hostEnd - hostStart - 1] = 0x00;
host[hostEnd - hostStart - 1] = '\0';

// find the endpoint string
endpoint = new char[url + strlen(url) - hostEnd + 1];
Expand All @@ -31,15 +31,15 @@ int16_t HTTPClient::get(const char* url, String& response) {
char* hostEnd = strchr(url, '/');
host = new char[hostEnd - url + 1];
strncpy(host, url, hostEnd - url);
host[hostEnd - url] = 0x00;
host[hostEnd - url] = '\0';

// find the endpoint string
endpoint = new char[url + strlen(url) - hostEnd + 1];
strcpy(endpoint, hostEnd);
}

// build the GET request
char* request = new char[strlen(endpoint) + strlen(host) + 25];
char* request = new char[strlen(endpoint) + strlen(host) + 25 + 1];
strcpy(request, "GET ");
strcat(request, endpoint);
strcat(request, " HTTP/1.1\r\nHost: ");
Expand Down Expand Up @@ -72,7 +72,7 @@ int16_t HTTPClient::get(const char* url, String& response) {
}

// read the response
char* raw = new char[numBytes];
char* raw = new char[numBytes + 1];
size_t rawLength = _tl->receive((uint8_t*)raw, numBytes);
if(rawLength == 0) {
delete[] raw;
Expand All @@ -92,9 +92,9 @@ int16_t HTTPClient::get(const char* url, String& response) {
delete[] raw;
return(ERR_RESPONSE_MALFORMED);
}
char* responseStr = new char[raw + rawLength - responseStart - 1];
char* responseStr = new char[raw + rawLength - responseStart - 1 + 1];
strncpy(responseStr, responseStart + 2, raw + rawLength - responseStart - 1);
responseStr[raw + rawLength - responseStart - 2] = 0x00;
responseStr[raw + rawLength - responseStart - 2] = '\0';
response = String(responseStr);
delete[] responseStr;

Expand Down Expand Up @@ -122,7 +122,7 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response,
char* hostEnd = strchr(hostStart + 1, '/');
host = new char[hostEnd - hostStart];
strncpy(host, hostStart + 1, hostEnd - hostStart - 1);
host[hostEnd - hostStart - 1] = 0x00;
host[hostEnd - hostStart - 1] = '\0';

// find the endpoint string
endpoint = new char[url + strlen(url) - hostEnd + 1];
Expand All @@ -132,7 +132,7 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response,
char* hostEnd = strchr(url, '/');
host = new char[hostEnd - url + 1];
strncpy(host, url, hostEnd - url);
host[hostEnd - url] = 0x00;
host[hostEnd - url] = '\0';

// find the endpoint string
endpoint = new char[url + strlen(url) - hostEnd + 1];
Expand All @@ -142,7 +142,7 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response,
// build the POST request
char contentLengthStr[8];
itoa(strlen(content), contentLengthStr, 10);
char* request = new char[strlen(endpoint) + strlen(host) + strlen(contentType) + strlen(contentLengthStr) + strlen(content) + 64];
char* request = new char[strlen(endpoint) + strlen(host) + strlen(contentType) + strlen(contentLengthStr) + strlen(content) + 64 + 1];
strcpy(request, "POST ");
strcat(request, endpoint);
strcat(request, " HTTP/1.1\r\nHost: ");
Expand Down

0 comments on commit 4eef30a

Please sign in to comment.