Skip to content

Commit 53b1852

Browse files
committed
WebServer: Use the request's HTTP version and Connection header to set the default keep alive value
1 parent 747b636 commit 53b1852

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h

-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ void ESP8266WebServerTemplate<ServerType>::handleClient() {
320320
_currentClient = client;
321321
_currentStatus = HC_WAIT_READ;
322322
_statusChange = millis();
323-
_keepAlive = true;
324323
}
325324

326325
bool keepCurrentClient = false;

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ class ESP8266WebServerTemplate
170170

171171
// Whether other requests should be accepted from the client on the
172172
// same socket after a response is sent.
173-
// This will automatically configure the "Connection" header.
174-
// Defaults to false.
173+
// This will automatically configure the "Connection" header of the response.
174+
// Defaults to true when the client's HTTP version is 1.1 or above, otherwise it defaults to false.
175+
// If the client sends the "Connection" header, the value given by the header is used.
175176
void keepAlive(bool keepAlive) { _keepAlive = keepAlive; }
176177
bool keepAlive() { return _keepAlive; }
177178

libraries/ESP8266WebServer/src/Parsing-impl.h

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ bool ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
114114
}
115115
_currentMethod = method;
116116

117+
_keepAlive = _currentVersion > 0; // Keep the connection alive by default
118+
// if the protocol version is greater than HTTP 1.0
119+
117120
#ifdef DEBUG_ESP_HTTP_SERVER
118121
DEBUG_OUTPUT.print("method: ");
119122
DEBUG_OUTPUT.print(methodStr);
@@ -177,6 +180,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
177180
contentLength = headerValue.toInt();
178181
} else if (headerName.equalsIgnoreCase(F("Host"))){
179182
_hostHeader = headerValue;
183+
} else if (headerName.equalsIgnoreCase(F("Connection"))){
184+
_keepAlive = headerValue.equalsIgnoreCase(F("keep-alive"));
180185
}
181186
}
182187

@@ -240,6 +245,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
240245

241246
if (headerName.equalsIgnoreCase(F("Host"))){
242247
_hostHeader = headerValue;
248+
} else if (headerName.equalsIgnoreCase(F("Connection"))){
249+
_keepAlive = headerValue.equalsIgnoreCase(F("keep-alive"));
243250
}
244251
}
245252
_parseArguments(searchStr);

0 commit comments

Comments
 (0)