mirror of
				https://github.com/esp8266/Arduino.git
				synced 2025-10-27 05:56:11 +03:00 
			
		
		
		
	Merge pull request #1226 from Links2004/httpClient
HTTP client move stream buffer the heap
This commit is contained in:
		| @@ -60,7 +60,6 @@ void loop() { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         http.end(); |         http.end(); | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     delay(1000); |     delay(1000); | ||||||
|   | |||||||
| @@ -30,7 +30,6 @@ | |||||||
|  |  | ||||||
| #include "ESP8266HTTPClient.h" | #include "ESP8266HTTPClient.h" | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * constractor |  * constractor | ||||||
|  */ |  */ | ||||||
| @@ -335,8 +334,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { | |||||||
|         return HTTPC_ERROR_SEND_HEADER_FAILED; |         return HTTPC_ERROR_SEND_HEADER_FAILED; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // create buffer for read |     size_t buff_size = HTTP_TCP_BUFFER_SIZE; | ||||||
|     uint8_t buff[1460] = { 0 }; |  | ||||||
|  |  | ||||||
|     int len = size; |     int len = size; | ||||||
|     int bytesWritten = 0; |     int bytesWritten = 0; | ||||||
| @@ -345,6 +343,16 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { | |||||||
|         len = -1; |         len = -1; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE | ||||||
|  |     if((len > 0) && (len < HTTP_TCP_BUFFER_SIZE)) { | ||||||
|  |         buff_size = len; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // create buffer for read | ||||||
|  |     uint8_t * buff = (uint8_t *) malloc(buff_size); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     if(buff) { | ||||||
|         // read all data from stream and send it to server |         // read all data from stream and send it to server | ||||||
|         while(connected() && stream->available() && (len > 0 || len == -1)) { |         while(connected() && stream->available() && (len > 0 || len == -1)) { | ||||||
|  |  | ||||||
| @@ -352,7 +360,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { | |||||||
|             size_t s = stream->available(); |             size_t s = stream->available(); | ||||||
|  |  | ||||||
|             if(s) { |             if(s) { | ||||||
|             int c = stream->readBytes(buff, ((s > sizeof(buff)) ? sizeof(buff) : s)); |                 int c = stream->readBytes(buff, ((s > buff_size) ? buff_size : s)); | ||||||
|  |  | ||||||
|                 // write it to Stream |                 // write it to Stream | ||||||
|                 bytesWritten += _tcp->write((const uint8_t *) buff, c); |                 bytesWritten += _tcp->write((const uint8_t *) buff, c); | ||||||
| @@ -367,6 +375,8 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         free(buff); | ||||||
|  |  | ||||||
|         if(size && (int) size != bytesWritten) { |         if(size && (int) size != bytesWritten) { | ||||||
|             DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); |             DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); | ||||||
|             DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!"); |             DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!"); | ||||||
| @@ -375,6 +385,11 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { | |||||||
|             DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten); |             DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |     } else { | ||||||
|  |         DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE); | ||||||
|  |         return HTTPC_ERROR_TOO_LESS_RAM; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     // handle Server Response (Header) |     // handle Server Response (Header) | ||||||
|     return handleHeaderResponse(); |     return handleHeaderResponse(); | ||||||
| } | } | ||||||
| @@ -434,9 +449,17 @@ int HTTPClient::writeToStream(Stream * stream) { | |||||||
|     int len = _size; |     int len = _size; | ||||||
|     int bytesWritten = 0; |     int bytesWritten = 0; | ||||||
|  |  | ||||||
|     // create buffer for read |     size_t buff_size = HTTP_TCP_BUFFER_SIZE; | ||||||
|     uint8_t buff[1460] = { 0 }; |  | ||||||
|  |  | ||||||
|  |     // if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE | ||||||
|  |     if((len > 0) && (len < HTTP_TCP_BUFFER_SIZE)) { | ||||||
|  |         buff_size = len; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     // create buffer for read | ||||||
|  |     uint8_t * buff = (uint8_t *) malloc(buff_size); | ||||||
|  |  | ||||||
|  |     if(buff) { | ||||||
|         // read all data from server |         // read all data from server | ||||||
|         while(connected() && (len > 0 || len == -1)) { |         while(connected() && (len > 0 || len == -1)) { | ||||||
|  |  | ||||||
| @@ -444,7 +467,7 @@ int HTTPClient::writeToStream(Stream * stream) { | |||||||
|             size_t size = _tcp->available(); |             size_t size = _tcp->available(); | ||||||
|  |  | ||||||
|             if(size) { |             if(size) { | ||||||
|             int c = _tcp->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); |                 int c = _tcp->readBytes(buff, ((size > buff_size) ? buff_size : size)); | ||||||
|  |  | ||||||
|                 // write it to Stream |                 // write it to Stream | ||||||
|                 bytesWritten += stream->write(buff, c); |                 bytesWritten += stream->write(buff, c); | ||||||
| @@ -459,12 +482,19 @@ int HTTPClient::writeToStream(Stream * stream) { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         free(buff); | ||||||
|  |  | ||||||
|         DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] connection closed or file end (written: %d).\n", bytesWritten); |         DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] connection closed or file end (written: %d).\n", bytesWritten); | ||||||
|  |  | ||||||
|         if(_size && _size != bytesWritten) { |         if(_size && _size != bytesWritten) { | ||||||
|             DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); |             DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |     } else { | ||||||
|  |         DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE); | ||||||
|  |         return HTTPC_ERROR_TOO_LESS_RAM; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     end(); |     end(); | ||||||
|     return bytesWritten; |     return bytesWritten; | ||||||
| } | } | ||||||
| @@ -509,12 +539,13 @@ String HTTPClient::errorToString(int error) { | |||||||
|             return String("no stream"); |             return String("no stream"); | ||||||
|         case HTTPC_ERROR_NO_HTTP_SERVER: |         case HTTPC_ERROR_NO_HTTP_SERVER: | ||||||
|             return String("no HTTP server"); |             return String("no HTTP server"); | ||||||
|  |         case HTTPC_ERROR_TOO_LESS_RAM: | ||||||
|  |             return String("too less ram"); | ||||||
|         default: |         default: | ||||||
|             return String(); |             return String(); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * adds Header to the request |  * adds Header to the request | ||||||
|  * @param name |  * @param name | ||||||
|   | |||||||
| @@ -41,6 +41,10 @@ | |||||||
| #define HTTPC_ERROR_CONNECTION_LOST     (-5) | #define HTTPC_ERROR_CONNECTION_LOST     (-5) | ||||||
| #define HTTPC_ERROR_NO_STREAM           (-6) | #define HTTPC_ERROR_NO_STREAM           (-6) | ||||||
| #define HTTPC_ERROR_NO_HTTP_SERVER      (-7) | #define HTTPC_ERROR_NO_HTTP_SERVER      (-7) | ||||||
|  | #define HTTPC_ERROR_TOO_LESS_RAM        (-8) | ||||||
|  |  | ||||||
|  | /// size for the stream handling | ||||||
|  | #define HTTP_TCP_BUFFER_SIZE (1460) | ||||||
|  |  | ||||||
| /// HTTP codes see RFC7231 | /// HTTP codes see RFC7231 | ||||||
| typedef enum { | typedef enum { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user