Skip to content

Commit a9f3b6f

Browse files
committed
webhook api
1 parent 1134194 commit a9f3b6f

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,14 @@ void ESP8266WebServerTemplate<ServerType>::handleClient() {
336336
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT);
337337
_contentLength = CONTENT_LENGTH_NOT_SET;
338338
_handleRequest();
339-
340-
if (_currentClient.connected()) {
341-
_currentStatus = HC_WAIT_CLOSE;
342-
_statusChange = millis();
343-
keepCurrentClient = true;
344-
}
339+
} else {
340+
keepCurrentClient = false;
341+
_currentClient.stop();
342+
}
343+
if (_currentClient.connected()) {
344+
_currentStatus = HC_WAIT_CLOSE;
345+
_statusChange = millis();
346+
keepCurrentClient = true;
345347
}
346348
} else { // !_currentClient.available()
347349
if (millis() - _statusChange <= HTTP_MAX_DATA_WAIT) {

libraries/ESP8266WebServer/src/ESP8266WebServer.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <functional>
2828
#include <memory>
29+
#include <functional>
2930
#include <ESP8266WiFi.h>
3031
#include <FS.h>
3132
#include "detail/mimetable.h"
@@ -80,6 +81,7 @@ class ESP8266WebServerTemplate
8081
using ClientType = typename ServerType::ClientType;
8182
using RequestHandlerType = RequestHandler<ServerType>;
8283
using WebServerType = ESP8266WebServerTemplate<ServerType>;
84+
using hook_f = std::function<bool(const String& method, const String& url, ClientType& client)>;
8385

8486
void begin();
8587
void begin(uint16_t port);
@@ -192,6 +194,22 @@ class ESP8266WebServerTemplate
192194

193195
static String responseCodeToString(const int code);
194196

197+
void addHook (hook_f hook)
198+
{
199+
if (_hook)
200+
{
201+
auto old = _hook;
202+
_hook = [old, hook](const String& method, const String& url, ClientType& client)
203+
{
204+
if (first(method, url, client))
205+
return true;
206+
return hook(method, url, client);
207+
};
208+
}
209+
else
210+
_hook = hook;
211+
}
212+
195213
protected:
196214
void _addRequestHandler(RequestHandlerType* handler);
197215
void _handleRequest();
@@ -251,7 +269,7 @@ class ESP8266WebServerTemplate
251269
String _sopaque;
252270
String _srealm; // Store the Auth realm between Calls
253271

254-
272+
hook_f _hook;
255273

256274
};
257275

libraries/ESP8266WebServer/src/Parsing-impl.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ bool ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
7171
client.readStringUntil('\n');
7272
//reset header value
7373
for (int i = 0; i < _headerKeysCount; ++i) {
74-
_currentHeaders[i].value =String();
74+
_currentHeaders[i].value.clear();
7575
}
7676

7777
// First line of HTTP request looks like "GET /path HTTP/1.1"
@@ -98,6 +98,15 @@ bool ESP8266WebServerTemplate<ServerType>::_parseRequest(ClientType& client) {
9898
_currentUri = url;
9999
_chunked = false;
100100

101+
if (_hook && _hook(methodStr, url, client))
102+
{
103+
// _hook() must return true when method is recognized
104+
// (even when request goes wrong)
105+
// in any case, request header and content must be read
106+
// returning false because request is now already handled
107+
return false;
108+
}
109+
101110
HTTPMethod method = HTTP_GET;
102111
if (methodStr == F("HEAD")) {
103112
method = HTTP_HEAD;

0 commit comments

Comments
 (0)