Skip to content

Bugfix/esp8266 http client #6457

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 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
d532476
Merge branch 'master' of https://github.com/esp8266/Arduino
Jun 6, 2019
d45fe30
Merge branch 'master' of https://github.com/esp8266/Arduino
Jun 7, 2019
9fb5c4c
Merge branch 'master' of https://github.com/esp8266/Arduino
Jul 6, 2019
37b62a4
Merge branch 'master' of https://github.com/esp8266/Arduino
Aug 4, 2019
1d13fec
Merge branch 'master' of https://github.com/esp8266/Arduino
Aug 5, 2019
96ecd06
Merge branch 'master' of https://github.com/esp8266/Arduino
Aug 7, 2019
0458500
Merged upstream master
Aug 27, 2019
e33fc7e
Make sure ::getString() will return an empty string if the server res…
Aug 27, 2019
c94374d
Add a reuse connection including restoring the connection if it gets …
Aug 27, 2019
62e6282
Add ::clear() in ::handleHeaderResponse()
Aug 27, 2019
3981b18
Fix Travis formatting issue for example ReuseConnectionV2.ino
Aug 27, 2019
25f3a84
Fix Travis formatting spacing for example ReuseConnectionV2.ino
Aug 27, 2019
85d836b
Merge branch 'master' into bugfix/ESP8266HTTPClient
earlephilhower Aug 28, 2019
dfef90f
pull from upstream master
Aug 30, 2019
895ec4e
Merge branch 'master' into bugfix/ESP8266HTTPClient
Aug 30, 2019
3368043
Merge branch 'bugfix/ESP8266HTTPClient' of https://github.com/Jeroen8…
Aug 30, 2019
a132f82
Try to get rid off mklittlefs
Aug 30, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
reuseConnectionV2.ino

Created on: 22.11.2015

This example reuses the http connection and also restores the connection if the connection is lost
*/


#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

ESP8266WiFiMulti WiFiMulti;

HTTPClient http;
WiFiClient client;

void setup() {

Serial.begin(115200);
// Serial.setDebugOutput(true);

Serial.println();
Serial.println();
Serial.println("Connecting to WiFi...");

WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASSWORD");

// wait for WiFi connection
while ((WiFiMulti.run() != WL_CONNECTED)) {
Serial.write('.');
delay(500);
}
Serial.println(" connected to WiFi");

// allow reuse (if server supports it)
http.setReuse(true);


http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
}

void loop() {
for (int i = 0; i < 10; i++) {
Serial.printf("Reuse connection example, GET url for the %d time\n", i + 1);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);

// file found at server
if (httpCode == HTTP_CODE_OK) {
http.writeToStream(&Serial);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
// Something went wrong with the connection, try to reconnect
http.end();
http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
}

Serial.println("\n\n\nWait 5 second...\n");
delay(5000);
}

http.end();

Serial.println("Done testing, now wait forever");
for (;;) delay(100); // Wait forever
}
19 changes: 14 additions & 5 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ void HTTPClient::disconnect(bool preserveClient)
#endif
}
} else {
if (!preserveClient && _client) { // Also destroy _client if not connected()
_client = nullptr;
}

DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n");
}
}
Expand Down Expand Up @@ -922,7 +926,9 @@ int HTTPClient::writeToStream(Stream * stream)
return returnError(HTTPC_ERROR_NO_STREAM);
}

if(!connected()) {
// Only return error if not connected and no data available, because otherwise ::getString() will return an error instead of an empty
// string when the server returned a http code 204 (no content)
if(!connected() && _transferEncoding != HTTPC_TE_IDENTITY && _size > 0) {
return returnError(HTTPC_ERROR_NOT_CONNECTED);
}

Expand All @@ -931,11 +937,13 @@ int HTTPClient::writeToStream(Stream * stream)
int ret = 0;

if(_transferEncoding == HTTPC_TE_IDENTITY) {
ret = writeToStreamDataBlock(stream, len);
if(len > 0) {
ret = writeToStreamDataBlock(stream, len);

// have we an error?
if(ret < 0) {
return returnError(ret);
// have we an error?
if(ret < 0) {
return returnError(ret);
}
}
} else if(_transferEncoding == HTTPC_TE_CHUNKED) {
int size = 0;
Expand Down Expand Up @@ -1289,6 +1297,7 @@ int HTTPClient::handleHeaderResponse()
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
}
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
_canReuse = (_returnCode != '0');
} else if(headerLine.indexOf(':')) {
String headerName = headerLine.substring(0, headerLine.indexOf(':'));
String headerValue = headerLine.substring(headerLine.indexOf(':') + 1);
Expand Down