[HTTP] Fixed memory leak

This commit is contained in:
jgromes 2019-07-23 20:22:35 +02:00
parent 5ea9bc89fd
commit c43ba03d7d

View file

@ -22,7 +22,7 @@ int16_t HTTPClient::get(const char* url, String& response) {
host = new char[hostEnd - hostStart]; host = new char[hostEnd - hostStart];
strncpy(host, hostStart + 1, hostEnd - hostStart - 1); strncpy(host, hostStart + 1, hostEnd - hostStart - 1);
host[hostEnd - hostStart - 1] = 0x00; host[hostEnd - hostStart - 1] = 0x00;
// find the endpoint string // find the endpoint string
endpoint = new char[url + strlen(url) - hostEnd + 1]; endpoint = new char[url + strlen(url) - hostEnd + 1];
strcpy(endpoint, hostEnd); strcpy(endpoint, hostEnd);
@ -32,12 +32,12 @@ int16_t HTTPClient::get(const char* url, String& response) {
host = new char[hostEnd - url + 1]; host = new char[hostEnd - url + 1];
strncpy(host, url, hostEnd - url); strncpy(host, url, hostEnd - url);
host[hostEnd - url] = 0x00; host[hostEnd - url] = 0x00;
// find the endpoint string // find the endpoint string
endpoint = new char[url + strlen(url) - hostEnd + 1]; endpoint = new char[url + strlen(url) - hostEnd + 1];
strcpy(endpoint, hostEnd); strcpy(endpoint, hostEnd);
} }
// build the GET request // build the GET request
char* request = new char[strlen(endpoint) + strlen(host) + 25]; char* request = new char[strlen(endpoint) + strlen(host) + 25];
strcpy(request, "GET "); strcpy(request, "GET ");
@ -45,9 +45,9 @@ int16_t HTTPClient::get(const char* url, String& response) {
strcat(request, " HTTP/1.1\r\nHost: "); strcat(request, " HTTP/1.1\r\nHost: ");
strcat(request, host); strcat(request, host);
strcat(request, "\r\n\r\n"); strcat(request, "\r\n\r\n");
delete[] endpoint; delete[] endpoint;
// create TCP connection // create TCP connection
int16_t state = _tl->openTransportConnection(host, "TCP", _port); int16_t state = _tl->openTransportConnection(host, "TCP", _port);
delete[] host; delete[] host;
@ -55,22 +55,22 @@ int16_t HTTPClient::get(const char* url, String& response) {
delete[] request; delete[] request;
return(state); return(state);
} }
// send the GET request // send the GET request
state = _tl->send(request); state = _tl->send(request);
delete[] request; delete[] request;
if(state != ERR_NONE) { if(state != ERR_NONE) {
return(state); return(state);
} }
//delay(1000); //delay(1000);
// get the response length // get the response length
size_t numBytes = _tl->getNumBytes(); size_t numBytes = _tl->getNumBytes();
if(numBytes == 0) { if(numBytes == 0) {
return(ERR_RESPONSE_MALFORMED_AT); return(ERR_RESPONSE_MALFORMED_AT);
} }
// read the response // read the response
char* raw = new char[numBytes]; char* raw = new char[numBytes];
size_t rawLength = _tl->receive((uint8_t*)raw, numBytes); size_t rawLength = _tl->receive((uint8_t*)raw, numBytes);
@ -78,14 +78,14 @@ int16_t HTTPClient::get(const char* url, String& response) {
delete[] raw; delete[] raw;
return(ERR_RESPONSE_MALFORMED); return(ERR_RESPONSE_MALFORMED);
} }
// close the tl connection // close the tl connection
state = _tl->closeTransportConnection(); state = _tl->closeTransportConnection();
if(state != ERR_NONE) { if(state != ERR_NONE) {
delete[] raw; delete[] raw;
return(state); return(state);
} }
// get the response body // get the response body
char* responseStart = strstr(raw, "\r\n"); char* responseStart = strstr(raw, "\r\n");
if(responseStart == NULL) { if(responseStart == NULL) {
@ -97,7 +97,7 @@ int16_t HTTPClient::get(const char* url, String& response) {
responseStr[raw + rawLength - responseStart - 2] = 0x00; responseStr[raw + rawLength - responseStart - 2] = 0x00;
response = String(responseStr); response = String(responseStr);
delete[] responseStr; delete[] responseStr;
// return the HTTP status code // return the HTTP status code
char* statusStart = strchr(raw, ' '); char* statusStart = strchr(raw, ' ');
delete[] raw; delete[] raw;
@ -123,7 +123,7 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response,
host = new char[hostEnd - hostStart]; host = new char[hostEnd - hostStart];
strncpy(host, hostStart + 1, hostEnd - hostStart - 1); strncpy(host, hostStart + 1, hostEnd - hostStart - 1);
host[hostEnd - hostStart - 1] = 0x00; host[hostEnd - hostStart - 1] = 0x00;
// find the endpoint string // find the endpoint string
endpoint = new char[url + strlen(url) - hostEnd + 1]; endpoint = new char[url + strlen(url) - hostEnd + 1];
strcpy(endpoint, hostEnd); strcpy(endpoint, hostEnd);
@ -133,12 +133,12 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response,
host = new char[hostEnd - url + 1]; host = new char[hostEnd - url + 1];
strncpy(host, url, hostEnd - url); strncpy(host, url, hostEnd - url);
host[hostEnd - url] = 0x00; host[hostEnd - url] = 0x00;
// find the endpoint string // find the endpoint string
endpoint = new char[url + strlen(url) - hostEnd + 1]; endpoint = new char[url + strlen(url) - hostEnd + 1];
strcpy(endpoint, hostEnd); strcpy(endpoint, hostEnd);
} }
// build the POST request // build the POST request
char contentLengthStr[8]; char contentLengthStr[8];
itoa(strlen(content), contentLengthStr, 10); itoa(strlen(content), contentLengthStr, 10);
@ -154,29 +154,30 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response,
strcat(request, "\r\n\r\n"); strcat(request, "\r\n\r\n");
strcat(request, content); strcat(request, content);
strcat(request, "\r\n\r\n"); strcat(request, "\r\n\r\n");
delete[] endpoint; delete[] endpoint;
// create TCP connection // create TCP connection
int16_t state = _tl->openTransportConnection(host, "TCP", _port); int16_t state = _tl->openTransportConnection(host, "TCP", _port);
delete[] host; delete[] host;
if(state != ERR_NONE) { if(state != ERR_NONE) {
delete[] request;
return(state); return(state);
} }
// send the POST request // send the POST request
state = _tl->send(request); state = _tl->send(request);
delete[] request; delete[] request;
if(state != ERR_NONE) { if(state != ERR_NONE) {
return(state); return(state);
} }
// get the response length // get the response length
size_t numBytes = _tl->getNumBytes(); size_t numBytes = _tl->getNumBytes();
if(numBytes == 0) { if(numBytes == 0) {
return(ERR_RESPONSE_MALFORMED_AT); return(ERR_RESPONSE_MALFORMED_AT);
} }
// read the response // read the response
char* raw = new char[numBytes]; char* raw = new char[numBytes];
size_t rawLength = _tl->receive((uint8_t*)raw, numBytes); size_t rawLength = _tl->receive((uint8_t*)raw, numBytes);
@ -184,14 +185,14 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response,
delete[] raw; delete[] raw;
return(ERR_RESPONSE_MALFORMED); return(ERR_RESPONSE_MALFORMED);
} }
// close the tl connection // close the tl connection
state = _tl->closeTransportConnection(); state = _tl->closeTransportConnection();
if(state != ERR_NONE) { if(state != ERR_NONE) {
delete[] raw; delete[] raw;
return(state); return(state);
} }
// get the response body // get the response body
char* responseStart = strstr(raw, "\r\n"); char* responseStart = strstr(raw, "\r\n");
if(responseStart == NULL) { if(responseStart == NULL) {
@ -203,7 +204,7 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response,
responseStr[raw + rawLength - responseStart - 2] = 0x00; responseStr[raw + rawLength - responseStart - 2] = 0x00;
response = String(responseStr); response = String(responseStr);
delete[] responseStr; delete[] responseStr;
// return the HTTP status code // return the HTTP status code
char* statusStart = strchr(raw, ' '); char* statusStart = strchr(raw, ' ');
delete[] raw; delete[] raw;