mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
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
This commit is contained in:
parent
fbec557ddb
commit
81af3a061e
@ -221,6 +221,32 @@ String ESP8266WebServer::arg(const char* name)
|
|||||||
return String();
|
return String();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String ESP8266WebServer::arg(int i)
|
||||||
|
{
|
||||||
|
if (i < _currentArgCount)
|
||||||
|
return _currentArgs[i].value;
|
||||||
|
return String();
|
||||||
|
}
|
||||||
|
|
||||||
|
String ESP8266WebServer::argName(int i)
|
||||||
|
{
|
||||||
|
if (i < _currentArgCount)
|
||||||
|
return _currentArgs[i].key;
|
||||||
|
return String();
|
||||||
|
}
|
||||||
|
|
||||||
|
int ESP8266WebServer::args(){
|
||||||
|
return _currentArgCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ESP8266WebServer::hasArg(const char* name)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _currentArgCount; ++i) {
|
||||||
|
if (_currentArgs[i].key == name)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void ESP8266WebServer::_parseArguments(String data)
|
void ESP8266WebServer::_parseArguments(String data)
|
||||||
{
|
{
|
||||||
@ -293,6 +319,9 @@ void ESP8266WebServer::_parseArguments(String data)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESP8266WebServer::onNotFound(TNotFoundHandlerFunction fn){
|
||||||
|
_notFoundHandler = fn;
|
||||||
|
}
|
||||||
|
|
||||||
void ESP8266WebServer::_handleRequest(WiFiClient& client, String uri, HTTPMethod method) {
|
void ESP8266WebServer::_handleRequest(WiFiClient& client, String uri, HTTPMethod method) {
|
||||||
_currentClient = client;
|
_currentClient = client;
|
||||||
@ -312,12 +341,12 @@ void ESP8266WebServer::_handleRequest(WiFiClient& client, String uri, HTTPMethod
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!handler)
|
if (!handler){
|
||||||
{
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("request handler not found");
|
Serial.println("request handler not found");
|
||||||
#endif
|
#endif
|
||||||
send(404, "text/plain", String("Not found: ") + uri);
|
if(!_notFoundHandler || !_notFoundHandler())
|
||||||
|
send(404, "text/plain", String("Not found: ") + uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
_currentClient = WiFiClient();
|
_currentClient = WiFiClient();
|
||||||
|
@ -39,22 +39,27 @@ public:
|
|||||||
void handleClient();
|
void handleClient();
|
||||||
|
|
||||||
typedef std::function<void(void)> THandlerFunction;
|
typedef std::function<void(void)> THandlerFunction;
|
||||||
|
typedef std::function<bool(void)> TNotFoundHandlerFunction;
|
||||||
void on(const char* uri, THandlerFunction handler);
|
void on(const char* uri, THandlerFunction handler);
|
||||||
void on(const char* uri, HTTPMethod method, THandlerFunction fn);
|
void on(const char* uri, HTTPMethod method, THandlerFunction fn);
|
||||||
|
void onNotFound(TNotFoundHandlerFunction fn);//called when handler is not assigned
|
||||||
|
|
||||||
String uri() { return _currentUri; }
|
String uri() { return _currentUri; }
|
||||||
HTTPMethod method() { return _currentMethod; }
|
HTTPMethod method() { return _currentMethod; }
|
||||||
WiFiClient client() { return _currentClient; }
|
WiFiClient client() { return _currentClient; }
|
||||||
|
|
||||||
|
String arg(const char* name);// get request argument value
|
||||||
|
String arg(int i);// get request argument value buy number
|
||||||
|
String argName(int i);// get request argument name buy number
|
||||||
|
int args();//get arguments count
|
||||||
|
bool hasArg(const char* name);//check if argument exists
|
||||||
|
|
||||||
// send response to the client
|
// send response to the client
|
||||||
// code - HTTP response code, can be 200 or 404
|
// code - HTTP response code, can be 200 or 404
|
||||||
// content_type - HTTP content type, like "text/plain" or "image/png"
|
// content_type - HTTP content type, like "text/plain" or "image/png"
|
||||||
// content - actual content body
|
// content - actual content body
|
||||||
void send(int code, const char* content_type = NULL, String content = String(""));
|
void send(int code, const char* content_type = NULL, String content = String(""));
|
||||||
|
|
||||||
// get request argument value
|
|
||||||
String arg(const char* name);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void _handleRequest(WiFiClient& client, String uri, HTTPMethod method);
|
void _handleRequest(WiFiClient& client, String uri, HTTPMethod method);
|
||||||
void _parseArguments(String data);
|
void _parseArguments(String data);
|
||||||
@ -76,6 +81,7 @@ protected:
|
|||||||
|
|
||||||
RequestHandler* _firstHandler;
|
RequestHandler* _firstHandler;
|
||||||
RequestHandler* _lastHandler;
|
RequestHandler* _lastHandler;
|
||||||
|
TNotFoundHandlerFunction _notFoundHandler;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user