Skip to content

Commit 505dd58

Browse files
authored
Merge pull request esp8266#8 from esp8266/master
Update
2 parents 82ae8d1 + 0937b07 commit 505dd58

File tree

7 files changed

+53
-26
lines changed

7 files changed

+53
-26
lines changed

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+34-7
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
225225
return false;
226226
}
227227
_transportTraits = TransportTraitsPtr(new TLSTraits(httpsFingerprint));
228+
if(!_transportTraits) {
229+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] could not create transport traits\n");
230+
return false;
231+
}
232+
228233
DEBUG_HTTPCLIENT("[HTTP-Client][begin] httpsFingerprint: %s\n", httpsFingerprint.c_str());
229234
return true;
230235
}
@@ -242,6 +247,11 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
242247
return false;
243248
}
244249
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
250+
if(!_transportTraits) {
251+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] could not create transport traits\n");
252+
return false;
253+
}
254+
245255
DEBUG_HTTPCLIENT("[HTTP-Client][begin] BearSSL-httpsFingerprint:");
246256
for (size_t i=0; i < 20; i++) {
247257
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
@@ -409,7 +419,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
409419
*/
410420
void HTTPClient::end(void)
411421
{
412-
disconnect();
422+
disconnect(false);
413423
clear();
414424
_redirectCount = 0;
415425
}
@@ -563,6 +573,7 @@ void HTTPClient::setRedirectLimit(uint16_t limit)
563573
void HTTPClient::useHTTP10(bool useHTTP10)
564574
{
565575
_useHTTP10 = useHTTP10;
576+
_reuse = !useHTTP10;
566577
}
567578

568579
/**
@@ -980,7 +991,7 @@ int HTTPClient::writeToStream(Stream * stream)
980991
return returnError(HTTPC_ERROR_ENCODING);
981992
}
982993

983-
disconnect();
994+
disconnect(true);
984995
return ret;
985996
}
986997

@@ -1139,7 +1150,11 @@ bool HTTPClient::hasHeader(const char* name)
11391150
bool HTTPClient::connect(void)
11401151
{
11411152
if(connected()) {
1142-
DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, try reuse!\n");
1153+
if(_reuse) {
1154+
DEBUG_HTTPCLIENT("[HTTP-Client] connect: already connected, reusing connection\n");
1155+
} else {
1156+
DEBUG_HTTPCLIENT("[HTTP-Client] connect: already connected, try reuse!\n");
1157+
}
11431158
while(_client->available() > 0) {
11441159
_client->read();
11451160
}
@@ -1149,6 +1164,10 @@ bool HTTPClient::connect(void)
11491164
#if HTTPCLIENT_1_1_COMPATIBLE
11501165
if(!_client && _transportTraits) {
11511166
_tcpDeprecated = _transportTraits->create();
1167+
if(!_tcpDeprecated) {
1168+
DEBUG_HTTPCLIENT("[HTTP-Client] connect: could not create tcp\n");
1169+
return false;
1170+
}
11521171
_client = _tcpDeprecated.get();
11531172
}
11541173
#endif
@@ -1246,9 +1265,12 @@ int HTTPClient::handleHeaderResponse()
12461265
return HTTPC_ERROR_NOT_CONNECTED;
12471266
}
12481267

1268+
clear();
1269+
1270+
_canReuse = _reuse;
1271+
12491272
String transferEncoding;
1250-
_returnCode = -1;
1251-
_size = -1;
1273+
12521274
_transferEncoding = HTTPC_TE_IDENTITY;
12531275
unsigned long lastDataTime = millis();
12541276

@@ -1263,6 +1285,9 @@ int HTTPClient::handleHeaderResponse()
12631285
DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str());
12641286

12651287
if(headerLine.startsWith("HTTP/1.")) {
1288+
if(_canReuse) {
1289+
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
1290+
}
12661291
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
12671292
} else if(headerLine.indexOf(':')) {
12681293
String headerName = headerLine.substring(0, headerLine.indexOf(':'));
@@ -1273,8 +1298,10 @@ int HTTPClient::handleHeaderResponse()
12731298
_size = headerValue.toInt();
12741299
}
12751300

1276-
if(headerName.equalsIgnoreCase("Connection")) {
1277-
_canReuse = headerValue.equalsIgnoreCase("keep-alive");
1301+
if(_canReuse && headerName.equalsIgnoreCase("Connection")) {
1302+
if(headerValue.indexOf("close") >= 0 && headerValue.indexOf("keep-alive") < 0) {
1303+
_canReuse = false;
1304+
}
12781305
}
12791306

12801307
if(headerName.equalsIgnoreCase("Transfer-Encoding")) {

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class HTTPClient
235235
/// request handling
236236
String _host;
237237
uint16_t _port = 0;
238-
bool _reuse = false;
238+
bool _reuse = true;
239239
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
240240
bool _useHTTP10 = false;
241241

libraries/ESP8266WiFi/src/include/ClientContext.h

+18-18
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ class ClientContext
128128
if (err != ERR_OK) {
129129
return 0;
130130
}
131-
_connect_pending = 1;
131+
_delaying = true;
132132
_op_start_time = millis();
133133
// Following delay will be interrupted by connect callback
134-
for (decltype(_timeout_ms) i = 0; _connect_pending && i < _timeout_ms; i++) {
134+
for (decltype(_timeout_ms) i = 0; _delaying && i < _timeout_ms; i++) {
135135
// Give scheduled functions a chance to run (e.g. Ethernet uses recurrent)
136136
delay(1);
137-
}
138-
_connect_pending = 0;
137+
}
138+
_delaying = false;
139139
if (!_pcb) {
140140
DEBUGV(":cabrt\r\n");
141141
return 0;
@@ -432,15 +432,16 @@ class ClientContext
432432

433433
void _notify_error()
434434
{
435-
if (_connect_pending || _send_waiting) {
436-
esp_schedule();
435+
if (_delaying) {
436+
_delaying = false;
437+
esp_schedule(); // break current delay()
437438
}
438439
}
439440

440441
size_t _write_from_source(DataSource* ds)
441442
{
442443
assert(_datasource == nullptr);
443-
assert(!_send_waiting);
444+
assert(!_delaying);
444445
_datasource = ds;
445446
_written = 0;
446447
_op_start_time = millis();
@@ -458,14 +459,14 @@ class ClientContext
458459
break;
459460
}
460461

461-
_send_waiting = true;
462+
_delaying = true;
462463
// Following delay will be interrupted by on next received ack
463-
for (decltype(_timeout_ms) i = 0; _send_waiting && i < _timeout_ms; i++) {
464+
for (decltype(_timeout_ms) i = 0; _delaying && i < _timeout_ms; i++) {
464465
// Give scheduled functions a chance to run (e.g. Ethernet uses recurrent)
465466
delay(1);
466467
}
468+
_delaying = false;
467469
} while(true);
468-
_send_waiting = false;
469470

470471
if (_sync)
471472
wait_until_sent();
@@ -532,9 +533,9 @@ class ClientContext
532533

533534
void _write_some_from_cb()
534535
{
535-
if (_send_waiting) {
536-
_send_waiting = false;
537-
esp_schedule();
536+
if (_delaying) {
537+
_delaying = false;
538+
esp_schedule(); // break current delay()
538539
}
539540
}
540541

@@ -608,9 +609,9 @@ class ClientContext
608609
(void) err;
609610
(void) pcb;
610611
assert(pcb == _pcb);
611-
assert(_connect_pending);
612-
_connect_pending = 0;
613-
esp_schedule();
612+
assert(_delaying);
613+
_delaying = false;
614+
esp_schedule(); // break current delay()
614615
return ERR_OK;
615616
}
616617

@@ -658,8 +659,7 @@ class ClientContext
658659
size_t _written = 0;
659660
uint32_t _timeout_ms = 5000;
660661
uint32_t _op_start_time = 0;
661-
bool _send_waiting = false;
662-
uint8_t _connect_pending = 0;
662+
bool _delaying = false;
663663

664664
int8_t _refcnt;
665665
ClientContext* _next;

tools/sdk/lib/NONOSDK221/libdriver.a

-65.7 KB
Binary file not shown.

tools/sdk/lib/NONOSDK22x/libdriver.a

-66.1 KB
Binary file not shown.

tools/sdk/lib/NONOSDK22y/libdriver.a

-66.1 KB
Binary file not shown.

tools/sdk/lib/NONOSDK3V0/libdriver.a

-65.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)