Skip to content

Feature/path arguments #5214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char *ssid = "........";
const char *password = "........";

ESP8266WebServer server(80);

void setup(void) {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}

server.on("/", []() {
server.send(200, "text/plain", "hello from esp8266!");
});

server.on("/users/{}", []() {
String user = server.pathArg(0);
server.send(200, "text/plain", "User: '" + user + "'");
});

server.on("^\\/users\\/([0-9]+)\\/devices\\/([0-9]+)$", []() {
String user = server.pathArg(0);
String device = server.pathArg(1);
server.send(200, "text/plain", "User: '" + user + "' and Device: '" + device + "'");
});

server.begin();
Serial.println("HTTP server started");
}

void loop(void) {
server.handleClient();
}
6 changes: 6 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,12 @@ void ESP8266WebServerTemplate<ServerType>::_streamFileCore(const size_t fileSize
send(200, contentType, emptyString);
}

template <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::pathArg(unsigned int i) const {
if (_currentHandler != nullptr)
return _currentHandler->pathArg(i);
return emptyString;
}

template <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::arg(const String& name) const {
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class ESP8266WebServerTemplate
// Allows setting server options (i.e. SSL keys) by the instantiator
ServerType &getServer() { return _server; }

const String& pathArg(unsigned int i) const; // get request path argument by number
const String& arg(const String& name) const; // get request argument value by name
const String& arg(int i) const; // get request argument value by number
const String& argName(int i) const; // get request argument name by number
Expand Down
11 changes: 11 additions & 0 deletions libraries/ESP8266WebServer/src/detail/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define REQUESTHANDLER_H

#include <ESP8266WebServer.h>
#include <vector>
#include <assert.h>

template<typename ServerType>
class RequestHandler {
Expand All @@ -18,6 +20,15 @@ class RequestHandler {

private:
RequestHandler<ServerType>* _next = nullptr;

protected:
std::vector<String> pathArgs;

public:
const String& pathArg(unsigned int i) {
assert(i < pathArgs.size());
return pathArgs[i];
}
};

#endif //REQUESTHANDLER_H
34 changes: 31 additions & 3 deletions libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define REQUESTHANDLERSIMPL_H

#include <ESP8266WebServer.h>
#include <string>
#include <regex>
#include "RequestHandler.h"
#include "mimetable.h"
#include "WString.h"
Expand All @@ -17,17 +19,42 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
, _ufn(ufn)
, _uri(uri)
, _method(method)
, _isRegex(false)
{
_isRegex = uri.startsWith("^") && uri.endsWith("$");
if (_isRegex) {
std::regex rgx((_uri + "|").c_str());
std::smatch matches;
std::string s{""};
std::regex_search(s, matches, rgx);
RequestHandler<ServerType>::pathArgs.resize(matches.size() - 1);
} else {
RequestHandler<ServerType>::pathArgs.resize(0);
}
}

bool canHandle(HTTPMethod requestMethod, String requestUri) override {
if (_method != HTTP_ANY && _method != requestMethod)
return false;

if (requestUri != _uri)
return false;
if (_uri == requestUri)
return true;

if (_isRegex) {
unsigned int pathArgIndex = 0;
std::regex rgx(_uri.c_str());
std::smatch matches;
std::string s(requestUri.c_str());
if (std::regex_search(s, matches, rgx)) {
for (size_t i = 1; i < matches.size(); ++i) { // skip first
RequestHandler<ServerType>::pathArgs[pathArgIndex] = String(matches[i].str().c_str());
pathArgIndex++;
}
return true;
}
}

return true;
return false;
}

bool canUpload(String requestUri) override {
Expand Down Expand Up @@ -58,6 +85,7 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
typename WebServerType::THandlerFunction _ufn;
String _uri;
HTTPMethod _method;
bool _isRegex;
};

template<typename ServerType>
Expand Down