@@ -79,7 +79,7 @@ httpClient::~httpClient() {
79
79
* @param httpsFingerprint const char *
80
80
*/
81
81
void httpClient::begin (const char *url, const char * httpsFingerprint) {
82
- begin (String (url), String (httpsFingerprint));
82
+ begin (String (url), String (httpsFingerprint));
83
83
}
84
84
85
85
/* *
@@ -113,7 +113,7 @@ void httpClient::begin(String url, String httpsFingerprint) {
113
113
_host = url.substring (0 , index); // hostname
114
114
url.remove (0 , (index + 1 )); // remove hostname + :
115
115
116
- index = url.indexOf (' /' );
116
+ index = url.indexOf (' /' );
117
117
_port = url.substring (0 , index).toInt (); // get port
118
118
url.remove (0 , index); // remove port
119
119
hasPort = true ;
@@ -202,7 +202,6 @@ bool httpClient::connected() {
202
202
return false ;
203
203
}
204
204
205
-
206
205
/* *
207
206
* try to reuse the connection to the server
208
207
* keep-alive
@@ -220,7 +219,6 @@ void httpClient::setUserAgent(const char * userAgent) {
220
219
_userAgent = userAgent;
221
220
}
222
221
223
-
224
222
/* *
225
223
* send a GET request
226
224
* @return http code
@@ -251,7 +249,7 @@ int httpClient::POST(String payload) {
251
249
* @return -1 if no info or > 0 when Content-Length is set by server
252
250
*/
253
251
int httpClient::sendRequest (const char * type, uint8_t * payload, size_t size) {
254
- // connect ro server
252
+ // connect to server
255
253
if (!connect ()) {
256
254
return HTTPC_ERROR_CONNECTION_REFUSED;
257
255
}
@@ -276,6 +274,77 @@ int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
276
274
return handleHeaderResponse ();
277
275
}
278
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
+
279
348
/* *
280
349
* size of message body / payload
281
350
* @return -1 if no info or > 0 when Content-Length is set by server
@@ -385,7 +454,6 @@ String httpClient::getString(void) {
385
454
return sstring;
386
455
}
387
456
388
-
389
457
/* *
390
458
* adds Header to the request
391
459
* @param name
@@ -394,16 +462,20 @@ String httpClient::getString(void) {
394
462
*/
395
463
void httpClient::addHeader (const String& name, const String& value, bool first) {
396
464
397
- String headerLine = name;
398
- headerLine += " : " ;
399
- headerLine += value;
400
- 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 " ;
401
471
402
- if (first) {
403
- _Headers = headerLine + _Headers;
404
- } else {
405
- _Headers += headerLine;
472
+ if (first) {
473
+ _Headers = headerLine + _Headers;
474
+ } else {
475
+ _Headers += headerLine;
476
+ }
406
477
}
478
+
407
479
}
408
480
409
481
void httpClient::collectHeaders (const char * headerKeys[], const size_t headerKeysCount) {
@@ -459,7 +531,6 @@ bool httpClient::connect(void) {
459
531
return true ;
460
532
}
461
533
462
-
463
534
if (_https) {
464
535
DEBUG_HTTPCLIENT (" [HTTP-Client] connect https...\n " );
465
536
if (_tcps) {
0 commit comments