Skip to content

Commit 81af3a0

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
adding extra finctionaity the the web server
void onNotFound(bool(void)) handler routes and errors not in the defined handlers int args() returns nubmer of currentarguments String arg(int i) returns the "i" argument value String argName(int i) returns the "i" argument name(key) bool hasArg(const char * name) looks up if an argument exist by seraching for it's key
1 parent fbec557 commit 81af3a0

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+32-3
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,32 @@ String ESP8266WebServer::arg(const char* name)
221221
return String();
222222
}
223223

224+
String ESP8266WebServer::arg(int i)
225+
{
226+
if (i < _currentArgCount)
227+
return _currentArgs[i].value;
228+
return String();
229+
}
230+
231+
String ESP8266WebServer::argName(int i)
232+
{
233+
if (i < _currentArgCount)
234+
return _currentArgs[i].key;
235+
return String();
236+
}
237+
238+
int ESP8266WebServer::args(){
239+
return _currentArgCount;
240+
}
241+
242+
bool ESP8266WebServer::hasArg(const char* name)
243+
{
244+
for (int i = 0; i < _currentArgCount; ++i) {
245+
if (_currentArgs[i].key == name)
246+
return true;
247+
}
248+
return false;
249+
}
224250

225251
void ESP8266WebServer::_parseArguments(String data)
226252
{
@@ -293,6 +319,9 @@ void ESP8266WebServer::_parseArguments(String data)
293319

294320
}
295321

322+
void ESP8266WebServer::onNotFound(TNotFoundHandlerFunction fn){
323+
_notFoundHandler = fn;
324+
}
296325

297326
void ESP8266WebServer::_handleRequest(WiFiClient& client, String uri, HTTPMethod method) {
298327
_currentClient = client;
@@ -312,12 +341,12 @@ void ESP8266WebServer::_handleRequest(WiFiClient& client, String uri, HTTPMethod
312341
break;
313342
}
314343

315-
if (!handler)
316-
{
344+
if (!handler){
317345
#ifdef DEBUG
318346
Serial.println("request handler not found");
319347
#endif
320-
send(404, "text/plain", String("Not found: ") + uri);
348+
if(!_notFoundHandler || !_notFoundHandler())
349+
send(404, "text/plain", String("Not found: ") + uri);
321350
}
322351

323352
_currentClient = WiFiClient();

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,27 @@ class ESP8266WebServer
3939
void handleClient();
4040

4141
typedef std::function<void(void)> THandlerFunction;
42+
typedef std::function<bool(void)> TNotFoundHandlerFunction;
4243
void on(const char* uri, THandlerFunction handler);
4344
void on(const char* uri, HTTPMethod method, THandlerFunction fn);
45+
void onNotFound(TNotFoundHandlerFunction fn);//called when handler is not assigned
4446

4547
String uri() { return _currentUri; }
4648
HTTPMethod method() { return _currentMethod; }
4749
WiFiClient client() { return _currentClient; }
48-
50+
51+
String arg(const char* name);// get request argument value
52+
String arg(int i);// get request argument value buy number
53+
String argName(int i);// get request argument name buy number
54+
int args();//get arguments count
55+
bool hasArg(const char* name);//check if argument exists
56+
4957
// send response to the client
5058
// code - HTTP response code, can be 200 or 404
5159
// content_type - HTTP content type, like "text/plain" or "image/png"
5260
// content - actual content body
5361
void send(int code, const char* content_type = NULL, String content = String(""));
5462

55-
// get request argument value
56-
String arg(const char* name);
57-
5863
protected:
5964
void _handleRequest(WiFiClient& client, String uri, HTTPMethod method);
6065
void _parseArguments(String data);
@@ -76,6 +81,7 @@ class ESP8266WebServer
7681

7782
RequestHandler* _firstHandler;
7883
RequestHandler* _lastHandler;
84+
TNotFoundHandlerFunction _notFoundHandler;
7985

8086
};
8187

0 commit comments

Comments
 (0)