1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

Bugfix/esp8266 http client (#6176)

This commit is contained in:
Jeroen88 2019-08-26 16:12:50 +02:00 committed by david gauchard
parent 55539ae941
commit 60d519e235
2 changed files with 35 additions and 8 deletions

View File

@ -225,6 +225,11 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
return false; return false;
} }
_transportTraits = TransportTraitsPtr(new TLSTraits(httpsFingerprint)); _transportTraits = TransportTraitsPtr(new TLSTraits(httpsFingerprint));
if(!_transportTraits) {
DEBUG_HTTPCLIENT("[HTTP-Client][begin] could not create transport traits\n");
return false;
}
DEBUG_HTTPCLIENT("[HTTP-Client][begin] httpsFingerprint: %s\n", httpsFingerprint.c_str()); DEBUG_HTTPCLIENT("[HTTP-Client][begin] httpsFingerprint: %s\n", httpsFingerprint.c_str());
return true; return true;
} }
@ -242,6 +247,11 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
return false; return false;
} }
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint)); _transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
if(!_transportTraits) {
DEBUG_HTTPCLIENT("[HTTP-Client][begin] could not create transport traits\n");
return false;
}
DEBUG_HTTPCLIENT("[HTTP-Client][begin] BearSSL-httpsFingerprint:"); DEBUG_HTTPCLIENT("[HTTP-Client][begin] BearSSL-httpsFingerprint:");
for (size_t i=0; i < 20; i++) { for (size_t i=0; i < 20; i++) {
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]); DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
@ -409,7 +419,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
*/ */
void HTTPClient::end(void) void HTTPClient::end(void)
{ {
disconnect(); disconnect(false);
clear(); clear();
_redirectCount = 0; _redirectCount = 0;
} }
@ -563,6 +573,7 @@ void HTTPClient::setRedirectLimit(uint16_t limit)
void HTTPClient::useHTTP10(bool useHTTP10) void HTTPClient::useHTTP10(bool useHTTP10)
{ {
_useHTTP10 = useHTTP10; _useHTTP10 = useHTTP10;
_reuse = !useHTTP10;
} }
/** /**
@ -980,7 +991,7 @@ int HTTPClient::writeToStream(Stream * stream)
return returnError(HTTPC_ERROR_ENCODING); return returnError(HTTPC_ERROR_ENCODING);
} }
disconnect(); disconnect(true);
return ret; return ret;
} }
@ -1139,7 +1150,11 @@ bool HTTPClient::hasHeader(const char* name)
bool HTTPClient::connect(void) bool HTTPClient::connect(void)
{ {
if(connected()) { if(connected()) {
DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, try reuse!\n"); if(_reuse) {
DEBUG_HTTPCLIENT("[HTTP-Client] connect: already connected, reusing connection\n");
} else {
DEBUG_HTTPCLIENT("[HTTP-Client] connect: already connected, try reuse!\n");
}
while(_client->available() > 0) { while(_client->available() > 0) {
_client->read(); _client->read();
} }
@ -1149,6 +1164,10 @@ bool HTTPClient::connect(void)
#if HTTPCLIENT_1_1_COMPATIBLE #if HTTPCLIENT_1_1_COMPATIBLE
if(!_client && _transportTraits) { if(!_client && _transportTraits) {
_tcpDeprecated = _transportTraits->create(); _tcpDeprecated = _transportTraits->create();
if(!_tcpDeprecated) {
DEBUG_HTTPCLIENT("[HTTP-Client] connect: could not create tcp\n");
return false;
}
_client = _tcpDeprecated.get(); _client = _tcpDeprecated.get();
} }
#endif #endif
@ -1246,9 +1265,12 @@ int HTTPClient::handleHeaderResponse()
return HTTPC_ERROR_NOT_CONNECTED; return HTTPC_ERROR_NOT_CONNECTED;
} }
clear();
_canReuse = _reuse;
String transferEncoding; String transferEncoding;
_returnCode = -1;
_size = -1;
_transferEncoding = HTTPC_TE_IDENTITY; _transferEncoding = HTTPC_TE_IDENTITY;
unsigned long lastDataTime = millis(); unsigned long lastDataTime = millis();
@ -1263,6 +1285,9 @@ int HTTPClient::handleHeaderResponse()
DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str()); DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str());
if(headerLine.startsWith("HTTP/1.")) { if(headerLine.startsWith("HTTP/1.")) {
if(_canReuse) {
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
}
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt(); _returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
} else if(headerLine.indexOf(':')) { } else if(headerLine.indexOf(':')) {
String headerName = headerLine.substring(0, headerLine.indexOf(':')); String headerName = headerLine.substring(0, headerLine.indexOf(':'));
@ -1273,8 +1298,10 @@ int HTTPClient::handleHeaderResponse()
_size = headerValue.toInt(); _size = headerValue.toInt();
} }
if(headerName.equalsIgnoreCase("Connection")) { if(_canReuse && headerName.equalsIgnoreCase("Connection")) {
_canReuse = headerValue.equalsIgnoreCase("keep-alive"); if(headerValue.indexOf("close") >= 0 && headerValue.indexOf("keep-alive") < 0) {
_canReuse = false;
}
} }
if(headerName.equalsIgnoreCase("Transfer-Encoding")) { if(headerName.equalsIgnoreCase("Transfer-Encoding")) {

View File

@ -235,7 +235,7 @@ protected:
/// request handling /// request handling
String _host; String _host;
uint16_t _port = 0; uint16_t _port = 0;
bool _reuse = false; bool _reuse = true;
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT; uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
bool _useHTTP10 = false; bool _useHTTP10 = false;