8000 Merge branch 'master' into poc-cache-config · esp8266/Arduino@d9ab27e · GitHub
[go: up one dir, main page]

Skip to content

Commit d9ab27e

Browse files
authored
Merge branch 'master' into poc-cache-config
2 parents 7b4a8d4 + 4519db8 commit d9ab27e

File tree

6 files changed

+409
-47
lines changed

6 files changed

+409
-47
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Arduino core for ESP8266 WiFi chip
33

44
# Quick links
55

6-
- [Latest release documentation](https://arduino-esp8266.readthedocs.io/en/2.7.0/)
6+
- [Latest release documentation](https://arduino-esp8266.readthedocs.io/en/2.7.1/)
77
- [Current "git version" documentation](https://arduino-esp8266.readthedocs.io/en/latest/)
88
- [Install git version](https://arduino-esp8266.readthedocs.io/en/latest/installing.html#using-git-version) ([sources](doc/installing.rst#using-git-version))
99

@@ -28,15 +28,15 @@ ESP8266 Arduino core comes with libraries to communicate over WiFi using TCP and
2828

2929
Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit).
3030

31-
- Install the current upstream Arduino IDE at the 1.8.7 level or later. The current version is on the [Arduino website](https://www.arduino.cc/en/main/software).
31+
- Install the current upstream Arduino IDE at the 1.8.9 level or later. The current version is on the [Arduino website](https://www.arduino.cc/en/main/software).
3232
- Start Arduino and open the Preferences window.
3333
- Enter ```https://arduino.esp8266.com/stable/package_esp8266com_index.json``` into the *Additional Board Manager URLs* field. You can add multiple URLs, separating them with commas.
3434
- Open Boards Manager from Tools > Board menu and install *esp8266* platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
3535

3636
#### Latest release [![Latest release](https://img.shields.io/github/release/esp8266/Arduino.svg)](https://github.com/esp8266/Arduino/releases/latest/)
3737
Boards manager link: `https://arduino.esp8266.com/stable/package_esp8266com_index.json`
3838

39-
Documentation: [https://arduino-esp8266.readthedocs.io/en/2.7.0/](https://arduino-esp8266.readthedocs.io/en/2.7.0/)
39+
Documentation: [https://arduino-esp8266.readthedocs.io/en/2.7.1/](https://arduino-esp8266.readthedocs.io/en/2.7.1/)
4040

4141
### Using git version
4242
[![Linux build status](https://travis-ci.org/esp8266/Arduino.svg)](https://travis-ci.org/esp8266/Arduino)
@@ -73,7 +73,7 @@ Documentation for latest development version: https://arduino-esp8266.readthedoc
7373

7474
### Issues and support ###
7575

76-
[ESP8266 Community Forum](https://www.esp8266.com/u/arduinoanswers) is a well-established community for questions and answers about Arduino for ESP8266. If you need help, have a "How do I..." type question, have a problem with a 3rd party library not hosted in this repo, or just want to discuss how to approach a problem, please ask there.
76+
[ESP8266 Community Forum](https://www.esp8266.com/u/arduinoanswers) is a well-established community for questions and answers about Arduino for ESP8266. Stackoverflow is also an alternative. If you need help, have a "How do I..." type question, have a problem with a 3rd party library not hosted in this repo, or just want to discuss how to approach a problem, please ask there.
7777

7878
If you find the forum useful, please consider supporting it with a donation. <br />
7979
[![Donate](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.com/webscr?cmd=_s-xclick&hosted_button_id=4M56YCWV6PX66)
@@ -96,7 +96,7 @@ For minor fixes of code and documentation, please go ahead and submit a pull req
9696

9797
Check out the list of issues that are easy to fix — [easy issues pending](https://github.com/esp8266/Arduino/issues?q=is%3Aopen+is%3Aissue+label%3A%22level%3A+easy%22). Working on them is a great way to move the project forward.
9898

99-
Larger changes (rewriting parts of existing code from scratch, adding new functions to the core, adding new libraries) should generally be discussed by opening an issue first.
99+
Larger changes (rewriting parts of existing code from scratch, adding new functions to the core, adding new libraries) should generally be discussed by opening an issue first. PRs with such changes require testing and approval.
100100

101101
Feature branches with lots of small commits (especially titled "oops", "fix typo", "forgot to add file", etc.) should be squashed before opening a pull request. At the same time, please refrain from putting multiple unrelated changes into a single pull request.
102102

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
reuseConnectionV2.ino
3+
4+
Created on: 22.11.2015
5+
6+
This example reuses the http connection and also restores the connection if the connection is lost
7+
*/
8+
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
#include <ESP8266HTTPClient.h>
13+
14+
#ifndef STASSID
15+
#define STASSID "your-ssid"
16+
#define STAPSK "your-password"
17+
#endif
18+
19+
ESP8266WiFiMulti WiFiMulti;
20+
21+
HTTPClient http;
22+
WiFiClient client;
23+
24+
void setup() {
25+
26+
Serial.begin(115200);
27+
// Serial.setDebugOutput(true);
28+
29+
Serial.println();
30+
Serial.println();
31+
Serial.println("Connecting to WiFi...");
32+
33+
WiFi.mode(WIFI_STA);
34+
WiFiMulti.addAP(STASSID, STAPSK);
35+
36+
// wait for WiFi connection
37+
while ((WiFiMulti.run() != WL_CONNECTED)) {
38+
Serial.write('.');
39+
delay(500);
40+
}
41+
Serial.println(" connected to WiFi");
42+
43+
// allow reuse (if server supports it)
44+
http.setReuse(true);
45+
46+
47+
http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
48+
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
49+
}
50+
51+
int pass = 0;
52+
53+
void loop() {
54+
// First 10 loop()s, retrieve the URL
55+
if (pass < 10) {
56+
pass++;
57+
Serial.printf("Reuse connection example, GET url for the %d time\n", pass);
58+
int httpCode = http.GET();
59+
if (httpCode > 0) {
60+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
61+
62+
// file found at server
63+
if (httpCode == HTTP_CODE_OK) {
64+
http.writeToStream(&Serial);
65+
}
66+
} else {
67+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
68+
// Something went wrong with the connection, try to reconnect
69+
http.end();
70+
http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
71+
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
72+
}
73+
74+
if (pass == 10) {
75+
http.end();
76+
Serial.println("Done testing");
77+
} else {
78+
Serial.println("\n\n\nWait 5 second...\n");
79+
delay(5000);
80+
}
81+
}
82+
}

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ void HTTPClient::disconnect(bool preserveClient)
457457
#endif
458458
}
459459
} else {
460+
if (!preserveClient && _client) { // Also destroy _client if not connected()
461+
_client = nullptr;
462+
}
463+
460464
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n");
461465
}
462466
}
@@ -970,7 +974,9 @@ int HTTPClient::writeToStream(Stream * stream)
970974
return returnError(HTTPC_ERROR_NO_STREAM);
971975
}
972976

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

@@ -979,11 +985,13 @@ int HTTPClient::writeToStream(Stream * stream)
979985
int ret = 0;
980986

981987
if(_transferEncoding == HTTPC_TE_IDENTITY) {
982-
ret = writeToStreamDataBlock(stream, len);
988+
if(len > 0) {
989+
ret = writeToStreamDataBlock(stream, len);
983990

984-
// have we an error?
985-
if(ret < 0) {
986-
return returnError(ret);
991+
// have we an error?
992+
if(ret < 0) {
993+
return returnError(ret);
994+
}
987995
}
988996
} else if(_transferEncoding == HTTPC_TE_CHUNKED) {
989997
int size = 0;
@@ -1198,12 +1206,8 @@ bool HTTPClient::hasHeader(const char* name)
11981206
*/
11991207
bool HTTPClient::connect(void)
12001208
{
1201-
if(connected()) {
1202-
if(_reuse) {
1203-
DEBUG_HTTPCLIENT("[HTTP-Client] connect: already connected, reusing connection\n");
1204-
} else {
1205-
DEBUG_HTTPCLIENT("[HTTP-Client] connect: already connected, try reuse!\n");
1206-
}
1209+
if(_reuse && _canReuse && connected()) {
1210+
DEBUG_HTTPCLIENT("[HTTP-Client] connect: already connected, reusing connection\n");
12071211
while(_client->available() > 0) {
12081212
_client->read();
12091213
}
@@ -1334,22 +1338,21 @@ int HTTPClient::handleHeaderResponse()
13341338
while(connected()) {
13351339
size_t len = _client->available();
13361340
if(len > 0) {
1341+
int headerSeparator = -1;
13371342
String headerLine = _client->readStringUntil('\n');
13381343

13391344
lastDataTime = millis();
13401345

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

13431348
if (headerLine.startsWith(F("HTTP/1."))) {
1344-
if (_canReuse) {
1345-
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
1346-
}
1347-
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
1348-
continue;
1349-
}
13501349

1351-
int headerSeparator = headerLine.indexOf(':');
1352-
if (headerSeparator > 0) {
1350+
constexpr auto httpVersionIdx = sizeof "HTTP/1." - 1;
1351+
_canReuse = _canReuse && (headerLine[httpVersionIdx] != '0');
1352+
_returnCode = headerLine.substring(httpVersionIdx + 2, headerLine.indexOf(' ', httpVersionIdx + 2)).toInt();
1353+
_canReuse = _canReuse && (_returnCode > 0) && (_returnCode < 500);
1354+
1355+
} else if ((headerSeparator = headerLine.indexOf(':')) > 0) {
13531356
String headerName = headerLine.substring(0, headerSeparator);
13541357
String headerValue = headerLine.substring(headerSeparator + 1);
13551358
headerValue.trim();

0 commit comments

Comments
 (0)
0