@@ -32,11 +32,15 @@ httpClient::httpClient() {
32
32
_tcp = NULL ;
33
33
_tcps = NULL ;
34
34
35
+ _reuse = false ;
36
+
35
37
_headerKeysCount = 0 ;
36
38
_currentHeaders = NULL ;
37
39
38
40
_returnCode = 0 ;
39
41
_size = -1 ;
42
+ _canReuse = false ;
43
+
40
44
}
41
45
42
46
httpClient::~httpClient () {
@@ -52,6 +56,8 @@ httpClient::~httpClient() {
52
56
53
57
void httpClient::begin (const char *host, uint16_t port, const char * url, bool https, const char * httpsFingerprint) {
54
58
59
+ DEBUG_HTTPCLIENT (" [HTTP-Client][begin] host: %s port:%d url: %s https: %d httpsFingerprint: %s\n " , host, port, url, https, httpsFingerprint);
60
+
55
61
_host = host;
56
62
_port = port;
57
63
_url = url;
@@ -69,6 +75,19 @@ void httpClient::begin(String host, uint16_t port, String url, bool https, Strin
69
75
begin (host.c_str (), port, url.c_str (), https, httpsFingerprint.c_str ());
70
76
}
71
77
78
+ /* *
79
+ * end
80
+ * called after the payload is handeld
81
+ */
82
+ void httpClient::end (void ) {
83
+ if ((!_reuse || !_canReuse) && connected ()) {
84
+ DEBUG_HTTPCLIENT (" [HTTP-Client][end] tcp stop \n " );
85
+ _tcp->stop ();
86
+ } else {
87
+ DEBUG_HTTPCLIENT (" [HTTP-Client][end] tcp keep open for reuse\n " );
88
+ }
89
+ }
90
+
72
91
/* *
73
92
* connected
74
93
* @return connected status
@@ -80,6 +99,16 @@ bool httpClient::connected() {
80
99
return false ;
81
100
}
82
101
102
+
103
+ /* *
104
+ * try to reuse the connection to the server
105
+ * keep-alive
106
+ * @param reuse bool
107
+ */
108
+ void httpClient::setReuse (bool reuse) {
109
+ _reuse = reuse;
110
+ }
111
+
83
112
/* *
84
113
* send a GET request
85
114
* @return http code
@@ -157,6 +186,53 @@ WiFiClient & httpClient::getStream(void) {
157
186
// todo return error?
158
187
}
159
188
189
+ /* *
190
+ * write all message body / payload to Stream
191
+ * @param stream Stream *
192
+ * @return bytes written
193
+ */
194
+ int httpClient::writeToStream (Stream * stream) {
195
+
196
+ if (!stream) {
197
+ return -1 ;
198
+ }
199
+
200
+ // get lenght of document (is -1 when Server sends no Content-Length header)
201
+ int len = _size;
202
+ int bytesWritten = 0 ;
203
+
204
+ // create buffer for read
205
+ uint8_t buff[1460 ] = { 0 };
206
+
207
+ // read all data from server
208
+ while (connected () && (len > 0 || len == -1 )) {
209
+
210
+ // get available data size
211
+ size_t size = _tcp->available ();
212
+
213
+ if (size) {
214
+ int c = _tcp->readBytes (buff, ((size > sizeof (buff)) ? sizeof (buff) : size));
215
+
216
+ // write it to Stream
217
+ bytesWritten += stream->write (buff, c);
218
+
219
+ if (len > 0 ) {
220
+ len -= c;
221
+ }
222
+ }
223
+ delay (1 );
224
+ }
225
+
226
+ DEBUG_HTTPCLIENT (" [HTTP-Client] connection closed or file end.\n " );
227
+
228
+ if (_size && _size != bytesWritten) {
229
+ DEBUG_HTTPCLIENT (" [HTTP-Client] bytesWritten %d and size %d missmatch!.\n " , bytesWritten, _size);
230
+ }
231
+
232
+ end ();
233
+ return bytesWritten;
234
+ }
235
+
160
236
/* *
161
237
* adds Headder to the request
162
238
* @param name
@@ -226,7 +302,7 @@ bool httpClient::hasHeader(const char* name) {
226
302
bool httpClient::connect (void ) {
227
303
228
304
if (connected ()) {
229
- DEBUG_HTTPCLIENT (" [HTTP-Client] connect. already connected, reuse!\n " );
305
+ DEBUG_HTTPCLIENT (" [HTTP-Client] connect. already connected, try reuse!\n " );
230
306
return true ;
231
307
}
232
308
@@ -235,7 +311,7 @@ bool httpClient::connect(void) {
235
311
_tcps = new WiFiClientSecure ();
236
312
_tcp = _tcps;
237
313
} else {
238
- DEBUG_HTTPCLIENT (" [HTTP-Client] connect...\n " );
314
+ DEBUG_HTTPCLIENT (" [HTTP-Client] connect http ...\n " );
239
315
_tcp = new WiFiClient ();
240
316
}
241
317
@@ -277,7 +353,14 @@ bool httpClient::sendHeader(const char * type) {
277
353
String header = String (type) + " " + _url + " HTTP/1.1\r\n "
278
354
" Host: " + _host + " \r\n "
279
355
" User-Agent: ESP8266httpClient\r\n "
280
- " Connection: close\r\n " + _Headers + " \r\n " ;
356
+ " Connection: " ;
357
+
358
+ if (_reuse) {
359
+ header += " keep-alive" ;
360
+ } else {
361
+ header += " close" ;
362
+ }
363
+ header += " \r\n " + _Headers + " \r\n " ;
281
364
282
365
return _tcp->write (header.c_str (), header.length ());
283
366
}
@@ -310,19 +393,22 @@ int httpClient::handleHeaderResponse() {
310
393
_size = headerValue.toInt ();
311
394
}
312
395
396
+ if (headerName.equalsIgnoreCase (" Connection" )) {
397
+ _canReuse = headerValue.equalsIgnoreCase (" keep-alive" );
398
+ }
399
+
313
400
for (size_t i = 0 ; i < _headerKeysCount; i++) {
314
- if (_currentHeaders[i].key == headerName) {
401
+ if (_currentHeaders[i].key . equalsIgnoreCase ( headerName) ) {
315
402
_currentHeaders[i].value = headerValue;
316
403
break ;
317
404
}
318
405
}
319
-
320
406
}
321
407
322
408
if (headerLine == " " ) {
323
- DEBUG_HTTPCLIENT (" [HTTP-Client][handleHeaderResponse] code: '%s' \n " , String ( _returnCode). c_str () );
409
+ DEBUG_HTTPCLIENT (" [HTTP-Client][handleHeaderResponse] code: %d \n " , _returnCode);
324
410
if (_size) {
325
- DEBUG_HTTPCLIENT (" [HTTP-Client][handleHeaderResponse] size: '%s' \n " , String ( _size). c_str () );
411
+ DEBUG_HTTPCLIENT (" [HTTP-Client][handleHeaderResponse] size: %d \n " , _size);
326
412
}
327
413
return _returnCode;
328
414
}
0 commit comments