mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
WebServer: Allow client to send many requests on the same connection (#7414)
* WebServer: Allow client to send many requests on the same connection * WebServer: Keep the connection alive with a client by default * WebServer: Use the request's HTTP version and Connection header to set the default keep alive value * Fix a typo in a comment
This commit is contained in:
parent
709ba7981e
commit
3c1bd65a76
@ -49,6 +49,7 @@ ESP8266WebServerTemplate<ServerType>::ESP8266WebServerTemplate(IPAddress addr, i
|
|||||||
, _currentVersion(0)
|
, _currentVersion(0)
|
||||||
, _currentStatus(HC_NONE)
|
, _currentStatus(HC_NONE)
|
||||||
, _statusChange(0)
|
, _statusChange(0)
|
||||||
|
, _keepAlive(false)
|
||||||
, _currentHandler(nullptr)
|
, _currentHandler(nullptr)
|
||||||
, _firstHandler(nullptr)
|
, _firstHandler(nullptr)
|
||||||
, _lastHandler(nullptr)
|
, _lastHandler(nullptr)
|
||||||
@ -327,6 +328,10 @@ void ESP8266WebServerTemplate<ServerType>::handleClient() {
|
|||||||
bool callYield = false;
|
bool callYield = false;
|
||||||
|
|
||||||
if (_currentClient.connected() || _currentClient.available()) {
|
if (_currentClient.connected() || _currentClient.available()) {
|
||||||
|
if (_currentClient.available() && _keepAlive) {
|
||||||
|
_currentStatus = HC_WAIT_READ;
|
||||||
|
}
|
||||||
|
|
||||||
switch (_currentStatus) {
|
switch (_currentStatus) {
|
||||||
case HC_NONE:
|
case HC_NONE:
|
||||||
// No-op to avoid C++ compiler warning
|
// No-op to avoid C++ compiler warning
|
||||||
@ -431,7 +436,14 @@ void ESP8266WebServerTemplate<ServerType>::_prepareHeader(String& response, int
|
|||||||
if (_corsEnabled) {
|
if (_corsEnabled) {
|
||||||
sendHeader(String(F("Access-Control-Allow-Origin")), String("*"));
|
sendHeader(String(F("Access-Control-Allow-Origin")), String("*"));
|
||||||
}
|
}
|
||||||
sendHeader(String(F("Connection")), String(F("close")));
|
|
||||||
|
if (_keepAlive && _server.hasClient()) { // Disable keep alive if another client is waiting.
|
||||||
|
_keepAlive = false;
|
||||||
|
}
|
||||||
|
sendHeader(String(F("Connection")), String(_keepAlive ? F("keep-alive") : F("close")));
|
||||||
|
if (_keepAlive) {
|
||||||
|
sendHeader(String(F("Keep-Alive")), String(F("timeout=")) + HTTP_MAX_CLOSE_WAIT);
|
||||||
|
}
|
||||||
|
|
||||||
response += _responseHeaders;
|
response += _responseHeaders;
|
||||||
response += "\r\n";
|
response += "\r\n";
|
||||||
|
@ -168,6 +168,14 @@ public:
|
|||||||
sendContent(emptyString);
|
sendContent(emptyString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Whether other requests should be accepted from the client on the
|
||||||
|
// same socket after a response is sent.
|
||||||
|
// This will automatically configure the "Connection" header of the response.
|
||||||
|
// Defaults to true when the client's HTTP version is 1.1 or above, otherwise it defaults to false.
|
||||||
|
// If the client sends the "Connection" header, the value given by the header is used.
|
||||||
|
void keepAlive(bool keepAlive) { _keepAlive = keepAlive; }
|
||||||
|
bool keepAlive() { return _keepAlive; }
|
||||||
|
|
||||||
static String credentialHash(const String& username, const String& realm, const String& password);
|
static String credentialHash(const String& username, const String& realm, const String& password);
|
||||||
|
|
||||||
static String urlDecode(const String& text);
|
static String urlDecode(const String& text);
|
||||||
@ -224,6 +232,7 @@ protected:
|
|||||||
uint8_t _currentVersion;
|
uint8_t _currentVersion;
|
||||||
HTTPClientStatus _currentStatus;
|
HTTPClientStatus _currentStatus;
|
||||||
unsigned long _statusChange;
|
unsigned long _statusChange;
|
||||||
|
bool _keepAlive;
|
||||||
|
|
||||||
RequestHandlerType* _currentHandler;
|
RequestHandlerType* _currentHandler;
|
||||||
RequestHandlerType* _firstHandler;
|
RequestHandlerType* _firstHandler;
|
||||||
|
@ -114,6 +114,9 @@ bool ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
|
|||||||
}
|
}
|
||||||
_currentMethod = method;
|
_currentMethod = method;
|
||||||
|
|
||||||
|
_keepAlive = _currentVersion > 0; // Keep the connection alive by default
|
||||||
|
// if the protocol version is greater than HTTP 1.0
|
||||||
|
|
||||||
#ifdef DEBUG_ESP_HTTP_SERVER
|
#ifdef DEBUG_ESP_HTTP_SERVER
|
||||||
DEBUG_OUTPUT.print("method: ");
|
DEBUG_OUTPUT.print("method: ");
|
||||||
DEBUG_OUTPUT.print(methodStr);
|
DEBUG_OUTPUT.print(methodStr);
|
||||||
@ -144,7 +147,7 @@ bool ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
|
|||||||
while(1){
|
while(1){
|
||||||
req = client.readStringUntil('\r');
|
req = client.readStringUntil('\r');
|
||||||
client.readStringUntil('\n');
|
client.readStringUntil('\n');
|
||||||
if (req.isEmpty()) break;//no moar headers
|
if (req.isEmpty()) break; //no more headers
|
||||||
int headerDiv = req.indexOf(':');
|
int headerDiv = req.indexOf(':');
|
||||||
if (headerDiv == -1){
|
if (headerDiv == -1){
|
||||||
break;
|
break;
|
||||||
@ -177,6 +180,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
|
|||||||
contentLength = headerValue.toInt();
|
contentLength = headerValue.toInt();
|
||||||
} else if (headerName.equalsIgnoreCase(F("Host"))){
|
} else if (headerName.equalsIgnoreCase(F("Host"))){
|
||||||
_hostHeader = headerValue;
|
_hostHeader = headerValue;
|
||||||
|
} else if (headerName.equalsIgnoreCase(F("Connection"))){
|
||||||
|
_keepAlive = headerValue.equalsIgnoreCase(F("keep-alive"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,6 +246,8 @@ bool ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
|
|||||||
|
|
||||||
if (headerName.equalsIgnoreCase(F("Host"))){
|
if (headerName.equalsIgnoreCase(F("Host"))){
|
||||||
_hostHeader = headerValue;
|
_hostHeader = headerValue;
|
||||||
|
} else if (headerName.equalsIgnoreCase(F("Connection"))){
|
||||||
|
_keepAlive = headerValue.equalsIgnoreCase(F("keep-alive"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_parseArguments(searchStr);
|
_parseArguments(searchStr);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user