[HTTP] Fixed incorrect string length

pull/41/head
jgromes 2019-09-09 10:04:07 +02:00
rodzic 14df1d8c52
commit 4eef30a2a4
1 zmienionych plików z 9 dodań i 9 usunięć

Wyświetl plik

@ -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];
@ -31,7 +31,7 @@ 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];
@ -39,7 +39,7 @@ int16_t HTTPClient::get(const char* url, String& response) {
}
// 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: ");
@ -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;
@ -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;
@ -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];
@ -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];
@ -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: ");