Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Bugfix/esp8266 http client #6176

Merged
merged 29 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9600f3e
Got tools/mklittlefs/mklittlefs after submodule update --init
Jun 2, 2019
b42f5a4
Solve HTTP1.1 persistance issue #6152
Jun 2, 2019
2bc8695
Merge branch 'master' into bugfix/ESP8266HTTPClient
Jun 5, 2019
8fc039d
fix checking the header
Jun 6, 2019
d532476
Merge branch 'master' of https://github.com/esp8266/Arduino
Jun 6, 2019
6cc3c60
Merge branch 'master' into bugfix/ESP8266HTTPClient
Jun 6, 2019
f382aa6
Let reuse connection depend on protocol used: HTTP1.0 or HTTP1.1
Jun 6, 2019
ed06407
Merge branch 'master' into bugfix/ESP8266HTTPClient
Jun 6, 2019
d45fe30
Merge branch 'master' of https://github.com/esp8266/Arduino
Jun 7, 2019
cd7d7d5
Merge branch 'master' into bugfix/ESP8266HTTPClient
Jun 7, 2019
9bd99ca
Merge remote-tracking branch 'origin/bugfix/ESP8266HTTPClient' into b…
Jun 7, 2019
51dd084
Removed tools/mklittlefs/mklittlefs
Jun 7, 2019
9ea6c69
Merge branch 'master' into bugfix/ESP8266HTTPClient
Jun 19, 2019
9fb5c4c
Merge branch 'master' of https://github.com/esp8266/Arduino
Jul 6, 2019
952c9b7
Merge branch 'master' into bugfix/ESP8266HTTPClient
Jul 6, 2019
7dba2f4
Fixed reuse, added null ptr checks
Jul 6, 2019
cf4a476
Merge branch 'bugfix/ESP8266HTTPClient' of https://github.com/Jeroen8…
Jul 6, 2019
b3a36b4
Removed space to pass Travis tests
Jul 6, 2019
37b62a4
Merge branch 'master' of https://github.com/esp8266/Arduino
Aug 4, 2019
910f033
Merge branch 'master' into bugfix/ESP8266HTTPClient
Aug 4, 2019
a558240
Fix bug reusing connection. ::handleHeaderResponse() did not clear _p…
Aug 4, 2019
1d13fec
Merge branch 'master' of https://github.com/esp8266/Arduino
Aug 5, 2019
afc51c5
Merge branch 'master' into bugfix/ESP8266HTTPClient
Aug 5, 2019
0e3c92f
Varios changes
Aug 5, 2019
293c68c
Restore ::disconnect() default parameter to false
Aug 6, 2019
96ecd06
Merge branch 'master' of https://github.com/esp8266/Arduino
Aug 7, 2019
84ef3d3
Merge branch 'master' into bugfix/ESP8266HTTPClient
Aug 7, 2019
82affca
Add return false in case of oom
Aug 7, 2019
aeb8f3e
Merge branch 'master' into bugfix/ESP8266HTTPClient
d-a-v Aug 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
return false;
}
_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());
return true;
}
Expand All @@ -242,6 +247,11 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
return false;
}
_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:");
for (size_t i=0; i < 20; i++) {
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
Expand Down Expand Up @@ -409,7 +419,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
*/
void HTTPClient::end(void)
{
disconnect();
disconnect(false);
clear();
_redirectCount = 0;
}
Expand Down Expand Up @@ -563,6 +573,7 @@ void HTTPClient::setRedirectLimit(uint16_t limit)
void HTTPClient::useHTTP10(bool useHTTP10)
{
_useHTTP10 = useHTTP10;
_reuse = !useHTTP10;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this method and ::setReuse() it is possible to set useHTTP10 = true and _reuse=true. That is the user shooting themselves in the foot, which is not a high prio for me.

At some point might want to go back and see if useHTTP10 === !reuse (I think so after reading the code briefly) and then drop one or the other variable if that's true. But for this PR, I'm OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@earlephilhower for HTTP/1.0 the default is not reusing the connection, the default for HTTP/1.1 is reusing. However reusing is allowed also on HTTP/1.0. In this case the client must send a Connection: Keep-Alive request header. The server may respond with a Connection: Keep-Alive in which case the connection can be reused. In HTTP/1.1 the client may send a connection: Keep-Alive header, but this is not obligatory. The connection is supposed to be reused unless the server sends a connection: close response header.
This is exactly what ESP8266HTTPClient does

}

/**
Expand Down Expand Up @@ -980,7 +991,7 @@ int HTTPClient::writeToStream(Stream * stream)
return returnError(HTTPC_ERROR_ENCODING);
}

disconnect();
disconnect(true);
return ret;
}

Expand Down Expand Up @@ -1139,7 +1150,11 @@ bool HTTPClient::hasHeader(const char* name)
bool HTTPClient::connect(void)
{
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) {
_client->read();
}
Expand All @@ -1149,6 +1164,10 @@ bool HTTPClient::connect(void)
#if HTTPCLIENT_1_1_COMPATIBLE
if(!_client && _transportTraits) {
_tcpDeprecated = _transportTraits->create();
if(!_tcpDeprecated) {
DEBUG_HTTPCLIENT("[HTTP-Client] connect: could not create tcp\n");
return false;
}
_client = _tcpDeprecated.get();
}
#endif
Expand Down Expand Up @@ -1246,9 +1265,12 @@ int HTTPClient::handleHeaderResponse()
return HTTPC_ERROR_NOT_CONNECTED;
}

clear();

_canReuse = _reuse;

String transferEncoding;
_returnCode = -1;
_size = -1;

_transferEncoding = HTTPC_TE_IDENTITY;
unsigned long lastDataTime = millis();

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

if(headerLine.startsWith("HTTP/1.")) {
if(_canReuse) {
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
}
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
} else if(headerLine.indexOf(':')) {
String headerName = headerLine.substring(0, headerLine.indexOf(':'));
Expand All @@ -1273,8 +1298,10 @@ int HTTPClient::handleHeaderResponse()
_size = headerValue.toInt();
}

if(headerName.equalsIgnoreCase("Connection")) {
_canReuse = headerValue.equalsIgnoreCase("keep-alive");
if(_canReuse && headerName.equalsIgnoreCase("Connection")) {
if(headerValue.indexOf("close") >= 0 && headerValue.indexOf("keep-alive") < 0) {
_canReuse = false;
}
}

if(headerName.equalsIgnoreCase("Transfer-Encoding")) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class HTTPClient
/// request handling
String _host;
uint16_t _port = 0;
bool _reuse = false;
bool _reuse = true;
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
bool _useHTTP10 = false;

Expand Down