Skip to content
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

bugfix2/ESP8266HTTPClient #6476

Merged
merged 13 commits into from
May 15, 2020
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
reuseConnectionV2.ino

Created on: 22.11.2015

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


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

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif

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(STASSID, STAPSK);

// 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++) {
earlephilhower marked this conversation as resolved.
Show resolved Hide resolved
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 (;;) {
earlephilhower marked this conversation as resolved.
Show resolved Hide resolved
delay(100); // Wait forever
}
}
30 changes: 18 additions & 12 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,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 @@ -970,7 +974,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 @@ -979,11 +985,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 @@ -1334,22 +1342,20 @@ int HTTPClient::handleHeaderResponse()
while(connected()) {
size_t len = _client->available();
if(len > 0) {
int headerSeparator = -1;
String headerLine = _client->readStringUntil('\n');

lastDataTime = millis();

DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str());

if (headerLine.startsWith(F("HTTP/1."))) {
if (_canReuse) {
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
}

_canReuse = _canReuse && (headerLine[sizeof "HTTP/1." - 1] != '0');
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
continue;
}
_canReuse = _canReuse && (_returnCode != '0');
d-a-v marked this conversation as resolved.
Show resolved Hide resolved

int headerSeparator = headerLine.indexOf(':');
if (headerSeparator > 0) {
} else if ((headerSeparator = headerLine.indexOf(':')) > 0) {
String headerName = headerLine.substring(0, headerSeparator);
String headerValue = headerLine.substring(headerSeparator + 1);
headerValue.trim();
Expand Down