@@ -36,7 +36,12 @@ httpClient::httpClient() {
3636 _tcp = NULL ;
3737 _tcps = NULL ;
3838
39+ _port = 0 ;
40+
3941 _reuse = false ;
42+ _https = false ;
43+
44+ _userAgent = " ESP8266httpClient" ;
4045
4146 _headerKeysCount = 0 ;
4247 _currentHeaders = NULL ;
@@ -74,7 +79,7 @@ httpClient::~httpClient() {
7479 * @param httpsFingerprint const char *
7580 */
7681void httpClient::begin (const char *url, const char * httpsFingerprint) {
77- begin (String (url), String (httpsFingerprint));
82+ begin (String (url), String (httpsFingerprint));
7883}
7984
8085/* *
@@ -108,7 +113,7 @@ void httpClient::begin(String url, String httpsFingerprint) {
108113 _host = url.substring (0 , index); // hostname
109114 url.remove (0 , (index + 1 )); // remove hostname + :
110115
111- index = url.indexOf (' /' );
116+ index = url.indexOf (' /' );
112117 _port = url.substring (0 , index).toInt (); // get port
113118 url.remove (0 , index); // remove port
114119 hasPort = true ;
@@ -197,7 +202,6 @@ bool httpClient::connected() {
197202 return false ;
198203}
199204
200-
201205/* *
202206 * try to reuse the connection to the server
203207 * keep-alive
@@ -207,6 +211,14 @@ void httpClient::setReuse(bool reuse) {
207211 _reuse = reuse;
208212}
209213
214+ /* *
215+ * set User Agent
216+ * @param userAgent const char *
217+ */
218+ void httpClient::setUserAgent (const char * userAgent) {
219+ _userAgent = userAgent;
220+ }
221+
210222/* *
211223 * send a GET request
212224 * @return http code
@@ -237,7 +249,7 @@ int httpClient::POST(String payload) {
237249 * @return -1 if no info or > 0 when Content-Length is set by server
238250 */
239251int httpClient::sendRequest (const char * type, uint8_t * payload, size_t size) {
240- // connect ro server
252+ // connect to server
241253 if (!connect ()) {
242254 return HTTPC_ERROR_CONNECTION_REFUSED;
243255 }
@@ -262,6 +274,77 @@ int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
262274 return handleHeaderResponse ();
263275}
264276
277+ /* *
278+ * sendRequest
279+ * @param type const char * "GET", "POST", ....
280+ * @param stream Stream * data stream for the message body
281+ * @param size size_t size for the message body if 0 not Content-Length is send
282+ * @return -1 if no info or > 0 when Content-Length is set by server
283+ */
284+ int httpClient::sendRequest (const char * type, Stream * stream, size_t size) {
285+
286+ if (!stream) {
287+ return HTTPC_ERROR_NO_STREAM;
288+ }
289+
290+ // connect to server
291+ if (!connect ()) {
292+ return HTTPC_ERROR_CONNECTION_REFUSED;
293+ }
294+
295+ if (size > 0 ) {
296+ addHeader (" Content-Length" , String (size));
297+ }
298+
299+ // send Header
300+ if (!sendHeader (type)) {
301+ return HTTPC_ERROR_SEND_HEADER_FAILED;
302+ }
303+
304+ // create buffer for read
305+ uint8_t buff[1460 ] = { 0 };
306+
307+ int len = size;
308+ int bytesWritten = 0 ;
309+
310+ if (len == 0 ) {
311+ len = -1 ;
312+ }
313+
314+ // read all data from stream and send it to server
315+ while (connected () && stream->available () && (len > 0 || len == -1 )) {
316+
317+ // get available data size
318+ size_t s = stream->available ();
319+
320+ if (s) {
321+ int c = stream->readBytes (buff, ((s > sizeof (buff)) ? sizeof (buff) : s));
322+
323+ // write it to Stream
324+ bytesWritten += _tcp->write ((const uint8_t *)buff, c);
325+
326+ if (len > 0 ) {
327+ len -= c;
328+ }
329+
330+ delay (0 );
331+ } else {
332+ delay (1 );
333+ }
334+ }
335+
336+ if (size && (int )size != bytesWritten) {
337+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n " , bytesWritten, _size);
338+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!" );
339+ return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
340+ } else {
341+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload written: %d\n " , bytesWritten);
342+ }
343+
344+ // handle Server Response (Header)
345+ return handleHeaderResponse ();
346+ }
347+
265348/* *
266349 * size of message body / payload
267350 * @return -1 if no info or > 0 when Content-Length is set by server
@@ -345,7 +428,7 @@ int httpClient::writeToStream(Stream * stream) {
345428 DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] connection closed or file end (written: %d).\n " , bytesWritten);
346429
347430 if (_size && _size != bytesWritten) {
348- DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] bytesWritten %d and size %d missmatch !.\n " , bytesWritten, _size);
431+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch !.\n " , bytesWritten, _size);
349432 }
350433
351434 end ();
@@ -362,7 +445,7 @@ String httpClient::getString(void) {
362445 if (_size) {
363446 // try to reserve needed memmory
364447 if (!sstring.reserve ((_size + 1 ))) {
365- DEBUG_HTTPCLIENT (" [HTTP-Client][getString] too less memory to resive as string! need: %d\n " , (_size + 1 ));
448+ DEBUG_HTTPCLIENT (" [HTTP-Client][getString] too less memory to reserve as string! need: %d\n " , (_size + 1 ));
366449 return String (" --too less memory--" );
367450 }
368451 }
@@ -371,7 +454,6 @@ String httpClient::getString(void) {
371454 return sstring;
372455}
373456
374-
375457/* *
376458 * adds Header to the request
377459 * @param name
@@ -380,16 +462,20 @@ String httpClient::getString(void) {
380462 */
381463void httpClient::addHeader (const String& name, const String& value, bool first) {
382464
383- String headerLine = name;
384- headerLine += " : " ;
385- headerLine += value;
386- headerLine += " \r\n " ;
465+ // not allow set of Header handled by code
466+ if (!name.equalsIgnoreCase (" Connection" ) && !name.equalsIgnoreCase (" User-Agent" ) && !name.equalsIgnoreCase (" Host" )) {
467+ String headerLine = name;
468+ headerLine += " : " ;
469+ headerLine += value;
470+ headerLine += " \r\n " ;
387471
388- if (first) {
389- _Headers = headerLine + _Headers;
390- } else {
391- _Headers += headerLine;
472+ if (first) {
473+ _Headers = headerLine + _Headers;
474+ } else {
475+ _Headers += headerLine;
476+ }
392477 }
478+
393479}
394480
395481void httpClient::collectHeaders (const char * headerKeys[], const size_t headerKeysCount) {
@@ -445,7 +531,6 @@ bool httpClient::connect(void) {
445531 return true ;
446532 }
447533
448-
449534 if (_https) {
450535 DEBUG_HTTPCLIENT (" [HTTP-Client] connect https...\n " );
451536 if (_tcps) {
@@ -499,9 +584,10 @@ bool httpClient::sendHeader(const char * type) {
499584 if (!connected ()) {
500585 return false ;
501586 }
587+
502588 String header = String (type) + " " + _url + " HTTP/1.1\r\n "
503589 " Host: " + _host + " \r\n "
504- " User-Agent: ESP8266httpClient \r\n "
590+ " User-Agent: " + _userAgent + " \r\n "
505591 " Connection: " ;
506592
507593 if (_reuse) {
@@ -511,7 +597,7 @@ bool httpClient::sendHeader(const char * type) {
511597 }
512598 header += " \r\n " + _Headers + " \r\n " ;
513599
514- return _tcp->write (header.c_str (), header.length ());
600+ return ( _tcp->write (header.c_str (), header. length ()) == header.length ());
515601}
516602
517603/* *
0 commit comments