@@ -36,7 +36,12 @@ httpClient::httpClient() {
36
36
_tcp = NULL ;
37
37
_tcps = NULL ;
38
38
39
+ _port = 0 ;
40
+
39
41
_reuse = false ;
42
+ _https = false ;
43
+
44
+ _userAgent = " ESP8266httpClient" ;
40
45
41
46
_headerKeysCount = 0 ;
42
47
_currentHeaders = NULL ;
@@ -74,7 +79,7 @@ httpClient::~httpClient() {
74
79
* @param httpsFingerprint const char *
75
80
*/
76
81
void httpClient::begin (const char *url, const char * httpsFingerprint) {
77
- begin (String (url), String (httpsFingerprint));
82
+ begin (String (url), String (httpsFingerprint));
78
83
}
79
84
80
85
/* *
@@ -108,7 +113,7 @@ void httpClient::begin(String url, String httpsFingerprint) {
108
113
_host = url.substring (0 , index); // hostname
109
114
url.remove (0 , (index + 1 )); // remove hostname + :
110
115
111
- index = url.indexOf (' /' );
116
+ index = url.indexOf (' /' );
112
117
_port = url.substring (0 , index).toInt (); // get port
113
118
url.remove (0 , index); // remove port
114
119
hasPort = true ;
@@ -197,7 +202,6 @@ bool httpClient::connected() {
197
202
return false ;
198
203
}
199
204
200
-
201
205
/* *
202
206
* try to reuse the connection to the server
203
207
* keep-alive
@@ -207,6 +211,14 @@ void httpClient::setReuse(bool reuse) {
207
211
_reuse = reuse;
208
212
}
209
213
214
+ /* *
215
+ * set User Agent
216
+ * @param userAgent const char *
217
+ */
218
+ void httpClient::setUserAgent (const char * userAgent) {
219
+ _userAgent = userAgent;
220
+ }
221
+
210
222
/* *
211
223
* send a GET request
212
224
* @return http code
@@ -237,7 +249,7 @@ int httpClient::POST(String payload) {
237
249
* @return -1 if no info or > 0 when Content-Length is set by server
238
250
*/
239
251
int httpClient::sendRequest (const char * type, uint8_t * payload, size_t size) {
240
- // connect ro server
252
+ // connect to server
241
253
if (!connect ()) {
242
254
return HTTPC_ERROR_CONNECTION_REFUSED;
243
255
}
@@ -262,6 +274,77 @@ int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
262
274
return handleHeaderResponse ();
263
275
}
264
276
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
+
265
348
/* *
266
349
* size of message body / payload
267
350
* @return -1 if no info or > 0 when Content-Length is set by server
@@ -345,7 +428,7 @@ int httpClient::writeToStream(Stream * stream) {
345
428
DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] connection closed or file end (written: %d).\n " , bytesWritten);
346
429
347
430
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);
349
432
}
350
433
351
434
end ();
@@ -362,7 +445,7 @@ String httpClient::getString(void) {
362
445
if (_size) {
363
446
// try to reserve needed memmory
364
447
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 ));
366
449
return String (" --too less memory--" );
367
450
}
368
451
}
@@ -371,7 +454,6 @@ String httpClient::getString(void) {
371
454
return sstring;
372
455
}
373
456
374
-
375
457
/* *
376
458
* adds Header to the request
377
459
* @param name
@@ -380,16 +462,20 @@ String httpClient::getString(void) {
380
462
*/
381
463
void httpClient::addHeader (const String& name, const String& value, bool first) {
382
464
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 " ;
387
471
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
+ }
392
477
}
478
+
393
479
}
394
480
395
481
void httpClient::collectHeaders (const char * headerKeys[], const size_t headerKeysCount) {
@@ -445,7 +531,6 @@ bool httpClient::connect(void) {
445
531
return true ;
446
532
}
447
533
448
-
449
534
if (_https) {
450
535
DEBUG_HTTPCLIENT (" [HTTP-Client] connect https...\n " );
451
536
if (_tcps) {
@@ -499,9 +584,10 @@ bool httpClient::sendHeader(const char * type) {
499
584
if (!connected ()) {
500
585
return false ;
501
586
}
587
+
502
588
String header = String (type) + " " + _url + " HTTP/1.1\r\n "
503
589
" Host: " + _host + " \r\n "
504
- " User-Agent: ESP8266httpClient \r\n "
590
+ " User-Agent: " + _userAgent + " \r\n "
505
591
" Connection: " ;
506
592
507
593
if (_reuse) {
@@ -511,7 +597,7 @@ bool httpClient::sendHeader(const char * type) {
511
597
}
512
598
header += " \r\n " + _Headers + " \r\n " ;
513
599
514
- return _tcp->write (header.c_str (), header.length ());
600
+ return ( _tcp->write (header.c_str (), header. length ()) == header.length ());
515
601
}
516
602
517
603
/* *
0 commit comments