@@ -34,6 +34,7 @@ enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
34
34
UPLOAD_FILE_ABORTED };
35
35
enum HTTPClientStatus { HC_NONE, HC_WAIT_READ, HC_WAIT_CLOSE };
36
36
enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
37
+ enum ClientFuture { CLIENT_REQUEST_CAN_CONTINUE, CLIENT_REQUEST_IS_HANDLED, CLIENT_MUST_STOP, CLIENT_IS_GIVEN };
37
38
38
39
#define HTTP_DOWNLOAD_UNIT_SIZE 1436
39
40
@@ -49,6 +50,8 @@ enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
49
50
#define CONTENT_LENGTH_UNKNOWN ((size_t ) -1 )
50
51
#define CONTENT_LENGTH_NOT_SET ((size_t ) -2 )
51
52
53
+ #define WEBSERVER_HAS_HOOK 1
54
+
52
55
class WebServer ;
53
56
54
57
typedef struct {
@@ -73,7 +76,8 @@ class WebServer
73
76
WebServer (IPAddress addr, int port = 80 );
74
77
WebServer (int port = 80 );
75
78
virtual ~WebServer ();
76
-
79
+ typedef String (*ContentTypeFunction) (const String&);
80
+ using HookFunction = std::function<ClientFuture(const String& method, const String& url, WiFiClient* client, ContentTypeFunction contentType)>;
77
81
virtual void begin ();
78
82
virtual void begin (uint16_t port);
79
83
virtual void handleClient ();
@@ -141,14 +145,26 @@ class WebServer
141
145
_streamFileCore (file.size (), file.name (), contentType);
142
146
return _currentClient.write (file);
143
147
}
144
-
148
+ void addHook (HookFunction hook) {
149
+ if (_hook) {
150
+ auto previousHook = _hook;
151
+ _hook = [previousHook, hook](const String& method, const String& url, WiFiClient* client, ContentTypeFunction contentType) {
152
+ auto whatNow = previousHook (method, url, client, contentType);
153
+ if (whatNow == CLIENT_REQUEST_CAN_CONTINUE)
154
+ return hook (method, url, client, contentType);
155
+ return whatNow;
156
+ };
157
+ } else {
158
+ _hook = hook;
159
+ }
160
+ }
145
161
protected:
146
162
virtual size_t _currentClientWrite (const char * b, size_t l) { return _currentClient.write ( b, l ); }
147
163
virtual size_t _currentClientWrite_P (PGM_P b, size_t l) { return _currentClient.write_P ( b, l ); }
148
164
void _addRequestHandler (RequestHandler* handler);
149
165
void _handleRequest ();
150
166
void _finalizeResponse ();
151
- bool _parseRequest (WiFiClient& client);
167
+ ClientFuture _parseRequest (WiFiClient& client);
152
168
void _parseArguments (String data);
153
169
static String _responseCodeToString (int code);
154
170
bool _parseForm (WiFiClient& client, String boundary, uint32_t len);
@@ -159,7 +175,7 @@ class WebServer
159
175
bool _collectHeader (const char * headerName, const char * headerValue);
160
176
161
177
void _streamFileCore (const size_t fileSize, const String & fileName, const String & contentType);
162
-
178
+
163
179
String _getRandomHexString ();
164
180
// for extracting Auth parameters
165
181
String _extractParam (String& authReq,const String& param,const char delimit = ' "' );
@@ -204,7 +220,7 @@ class WebServer
204
220
String _snonce; // Store noance and opaque for future comparison
205
221
String _sopaque;
206
222
String _srealm; // Store the Auth realm between Calls
207
-
223
+ HookFunction _hook;
208
224
};
209
225
210
226
0 commit comments