Skip to content

Commit 4eca62c

Browse files
Bmooijd-a-vearlephilhower
authored
Add Uri with support for regexUri and globUri (#6696)
* Add path args * Add example * Update code format * Add missing include * Fix codestyle and unsigned int * fix unsigned int * Remove tabs * use vector<>.resize * rename j to requestUriIndex * using assert checking the path argument index * Add missing include "assert.h" * The order no longer matters. Path arguments may not contain the value '/' Updated the example * make pathArg return a const * Update PathArgServer.ino fix trailing space * const String& * Add regex support * Fix to match templating * Add Uri with support for staticUri, regexUri and globUri * Update example * Add deconstructor to remove _uri pointer * Add newline to end of files * Suppress gcc warnings (unused params) * Replace regex with regex.h * Use the standard STASSID/PSK settings for example Make the example match the existing examples which allow setting the SSID/PSK in the local platform.txt file. * Use 115.2Kbaud for example, match others Co-authored-by: david gauchard <[email protected]> Co-authored-by: Earle F. Philhower, III <[email protected]>
1 parent a40663b commit 4eca62c

File tree

9 files changed

+251
-13
lines changed

9 files changed

+251
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <ESP8266WiFi.h>
2+
#include <WiFiClient.h>
3+
#include <ESP8266WebServer.h>
4+
#include <ESP8266mDNS.h>
5+
6+
#include <uri/UriBraces.h>
7+
#include <uri/UriRegex.h>
8+
9+
#ifndef STASSID
10+
#define STASSID "your-ssid"
11+
#define STAPSK "your-password"
12+
#endif
13+
14+
const char *ssid = STASSID;
15+
const char *password = STAPSK;
16+
17+
ESP8266WebServer server(80);
18+
19+
void setup(void) {
20+
Serial.begin(115200);
21+
WiFi.mode(WIFI_STA);
22+
WiFi.begin(ssid, password);
23+
Serial.println("");
24+
25+
// Wait for connection
26+
while (WiFi.status() != WL_CONNECTED) {
27+
delay(500);
28+
Serial.print(".");
29+
}
30+
Serial.println("");
31+
Serial.print("Connected to ");
32+
Serial.println(ssid);
33+
Serial.print("IP address: ");
34+
Serial.println(WiFi.localIP());
35+
36+
if (MDNS.begin("esp8266")) {
37+
Serial.println("MDNS responder started");
38+
}
39+
40+
server.on("/", []() {
41+
server.send(200, "text/plain", "hello from esp8266!");
42+
});
43+
44+
server.on(UriBraces("/users/{}"), []() {
45+
String user = server.pathArg(0);
46+
server.send(200, "text/plain", "User: '" + user + "'");
47+
});
48+
49+
server.on(UriRegex("^\\/users\\/([0-9]+)\\/devices\\/([0-9]+)$"), []() {
50+
String user = server.pathArg(0);
51+
String device = server.pathArg(1);
52+
server.send(200, "text/plain", "User: '" + user + "' and Device: '" + device + "'");
53+
});
54+
55+
server.begin();
56+
Serial.println("HTTP server started");
57+
}
58+
59+
void loop(void) {
60+
server.handleClient();
61+
}

libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,17 @@ void ESP8266WebServerTemplate<ServerType>::requestAuthentication(HTTPAuthMethod
263263
}
264264

265265
template <typename ServerType>
266-
void ESP8266WebServerTemplate<ServerType>::on(const String &uri, ESP8266WebServerTemplate<ServerType>::THandlerFunction handler) {
266+
void ESP8266WebServerTemplate<ServerType>::on(const Uri &uri, ESP8266WebServerTemplate<ServerType>::THandlerFunction handler) {
267267
on(uri, HTTP_ANY, handler);
268268
}
269269

270270
template <typename ServerType>
271-
void ESP8266WebServerTemplate<ServerType>::on(const String &uri, HTTPMethod method, ESP8266WebServerTemplate<ServerType>::THandlerFunction fn) {
271+
void ESP8266WebServerTemplate<ServerType>::on(const Uri &uri, HTTPMethod method, ESP8266WebServerTemplate<ServerType>::THandlerFunction fn) {
272272
on(uri, method, fn, _fileUploadHandler);
273273
}
274274

275275
template <typename ServerType>
276-
void ESP8266WebServerTemplate<ServerType>::on(const String &uri, HTTPMethod method, ESP8266WebServerTemplate<ServerType>::THandlerFunction fn, ESP8266WebServerTemplate<ServerType>::THandlerFunction ufn) {
276+
void ESP8266WebServerTemplate<ServerType>::on(const Uri &uri, HTTPMethod method, ESP8266WebServerTemplate<ServerType>::THandlerFunction fn, ESP8266WebServerTemplate<ServerType>::THandlerFunction ufn) {
277277
_addRequestHandler(new FunctionRequestHandler<ServerType>(fn, ufn, uri, method));
278278
}
279279

@@ -544,6 +544,12 @@ void ESP8266WebServerTemplate<ServerType>::_streamFileCore(const size_t fileSize
544544
send(200, contentType, emptyString);
545545
}
546546

547+
template <typename ServerType>
548+
const String& ESP8266WebServerTemplate<ServerType>::pathArg(unsigned int i) const {
549+
if (_currentHandler != nullptr)
550+
return _currentHandler->pathArg(i);
551+
return emptyString;
552+
}
547553

548554
template <typename ServerType>
549555
const String& ESP8266WebServerTemplate<ServerType>::arg(const String& name) const {

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <ESP8266WiFi.h>
3030
#include <FS.h>
3131
#include "detail/mimetable.h"
32+
#include "Uri.h"
3233

3334
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_HEAD, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS };
3435
enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
@@ -91,9 +92,9 @@ class ESP8266WebServerTemplate
9192
void requestAuthentication(HTTPAuthMethod mode = BASIC_AUTH, const char* realm = NULL, const String& authFailMsg = String("") );
9293

9394
typedef std::function<void(void)> THandlerFunction;
94-
void on(const String &uri, THandlerFunction handler);
95-
void on(const String &uri, HTTPMethod method, THandlerFunction fn);
96-
void on(const String &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);
95+
void on(const Uri &uri, THandlerFunction handler);
96+
void on(const Uri &uri, HTTPMethod method, THandlerFunction fn);
97+
void on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);
9798
void addHandler(RequestHandlerType* handler);
9899
void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL );
99100
void onNotFound(THandlerFunction fn); //called when handler is not assigned
@@ -107,6 +108,7 @@ class ESP8266WebServerTemplate
107108
// Allows setting server options (i.e. SSL keys) by the instantiator
108109
ServerType &getServer() { return _server; }
109110

111+
const String& pathArg(unsigned int i) const; // get request path argument by number
110112
const String& arg(const String& name) const; // get request argument value by name
111113
const String& arg(int i) const; // get request argument value by number
112114
const String& argName(int i) const; // get request argument name by number

libraries/ESP8266WebServer/src/Uri.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef URI_H
2+
#define URI_H
3+
4+
#include <Arduino.h>
5+
#include <vector>
6+
7+
class Uri {
8+
9+
protected:
10+
const String _uri;
11+
12+
public:
13+
Uri(const char *uri) : _uri(uri) {}
14+
Uri(const String &uri) : _uri(uri) {}
15+
virtual ~Uri() {}
16+
17+
virtual Uri* clone() const {
18+
return new Uri(_uri);
19+
};
20+
21+
virtual bool canHandle(const String &requestUri, __attribute__((unused)) std::vector<String> &pathArgs) {
22+
return _uri == requestUri;
23+
}
24+
};
25+
26+
#endif

libraries/ESP8266WebServer/src/detail/RequestHandler.h

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define REQUESTHANDLER_H
33

44
#include <ESP8266WebServer.h>
5+
#include <vector>
6+
#include <assert.h>
57

68
template<typename ServerType>
79
class RequestHandler {
@@ -18,6 +20,15 @@ class RequestHandler {
1820

1921
private:
2022
RequestHandler<ServerType>* _next = nullptr;
23+
24+
protected:
25+
std::vector<String> pathArgs;
26+
27+
public:
28+
const String& pathArg(unsigned int i) {
29+
assert(i < pathArgs.size());
30+
return pathArgs[i];
31+
}
2132
};
2233

2334
#endif //REQUESTHANDLER_H

libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,31 @@
55
#include "RequestHandler.h"
66
#include "mimetable.h"
77
#include "WString.h"
8+
#include "Uri.h"
89

910
using namespace mime;
1011

1112
template<typename ServerType>
1213
class FunctionRequestHandler : public RequestHandler<ServerType> {
1314
using WebServerType = ESP8266WebServerTemplate<ServerType>;
1415
public:
15-
FunctionRequestHandler(typename WebServerType::THandlerFunction fn, typename WebServerType::THandlerFunction ufn, const String &uri, HTTPMethod method)
16+
FunctionRequestHandler(typename WebServerType::THandlerFunction fn, typename WebServerType::THandlerFunction ufn, const Uri &uri, HTTPMethod method)
1617
: _fn(fn)
1718
, _ufn(ufn)
18-
, _uri(uri)
19+
, _uri(uri.clone())
1920
, _method(method)
2021
{
2122
}
2223

24+
~FunctionRequestHandler() {
25+
delete _uri;
26+
}
27+
2328
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
2429
if (_method != HTTP_ANY && _method != requestMethod)
2530
return false;
2631

27-
if (requestUri != _uri)
28-
return false;
29-
30-
return true;
32+
return _uri->canHandle(requestUri, RequestHandler<ServerType>::pathArgs);
3133
}
3234

3335
bool canUpload(String requestUri) override {
@@ -56,7 +58,7 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
5658
protected:
5759
typename WebServerType::THandlerFunction _fn;
5860
typename WebServerType::THandlerFunction _ufn;
59-
String _uri;
61+
Uri *_uri;
6062
HTTPMethod _method;
6163
};
6264

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef URI_BRACES_H
2+
#define URI_BRACES_H
3+
4+
#include "Uri.h"
5+
6+
class UriBraces : public Uri {
7+
8+
public:
9+
explicit UriBraces(const char *uri) : Uri(uri) {};
10+
explicit UriBraces(const String &uri) : Uri(uri) {};
11+
12+
Uri* clone() const override final {
13+
return new UriBraces(_uri);
14+
};
15+
16+
bool canHandle(const String &requestUri, std::vector<String> &pathArgs) override final {
17+
if (Uri::canHandle(requestUri, pathArgs))
18+
return true;
19+
20+
pathArgs.clear();
21+
22+
size_t uriLength = _uri.length();
23+
unsigned int requestUriIndex = 0;
24+
for (unsigned int i = 0; i < uriLength; i++, requestUriIndex++) {
25+
char uriChar = _uri[i];
26+
char requestUriChar = requestUri[requestUriIndex];
27+
28+
if (uriChar == requestUriChar)
29+
continue;
30+
if (uriChar != '{')
31+
return false;
32+
33+
i += 2; // index of char after '}'
34+
if (i >= uriLength) {
35+
// there is no char after '}'
36+
pathArgs.push_back(requestUri.substring(requestUriIndex));
37+
return pathArgs.back().indexOf("/") == -1; // path argument may not contain a '/'
38+
}
39+
else
40+
{
41+
char charEnd = _uri[i];
42+
int uriIndex = requestUri.indexOf(charEnd, requestUriIndex);
43+
if (uriIndex < 0)
44+
return false;
45+
pathArgs.push_back(requestUri.substring(requestUriIndex, uriIndex));
46+
requestUriIndex = (unsigned int) uriIndex;
47+
}
48+
}
49+
50+
return requestUriIndex >= requestUri.length();
51+
}
52+
};
53+
54+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef URI_GLOB_H
2+
#define URI_GLOB_H
3+
4+
#include "Uri.h"
5+
#include <fnmatch.h>
6+
7+
class UriGlob : public Uri {
8+
9+
public:
10+
explicit UriGlob(const char *uri) : Uri(uri) {};
11+
explicit UriGlob(const String &uri) : Uri(uri) {};
12+
13+
Uri* clone() const override final {
14+
return new UriGlob(_uri);
15+
};
16+
17+
bool canHandle(const String &requestUri, __attribute__((unused)) std::vector<String> &pathArgs) override final {
18+
return fnmatch(_uri.c_str(), requestUri.c_str(), 0) == 0;
19+
}
20+
};
21+
22+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef URI_REGEX_H
2+
#define URI_REGEX_H
3+
4+
#include "Uri.h"
5+
#include <regex.h>
6+
#include <assert.h>
7+
8+
#ifndef REGEX_MAX_GROUPS
9+
#define REGEX_MAX_GROUPS 10
10+
#endif
11+
12+
class UriRegex : public Uri {
13+
14+
private:
15+
regex_t _regexCompiled;
16+
17+
public:
18+
explicit UriRegex(const char *uri) : Uri(uri) {
19+
assert(regcomp(&_regexCompiled, uri, REG_EXTENDED) == 0);
20+
};
21+
explicit UriRegex(const String &uri) : UriRegex(uri.c_str()) {};
22+
23+
~UriRegex() {
24+
regfree(&_regexCompiled);
25+
}
26+
27+
Uri* clone() const override final {
28+
return new UriRegex(_uri);
29+
};
30+
31+
bool canHandle(const String &requestUri, std::vector<String> &pathArgs) override final {
32+
if (Uri::canHandle(requestUri, pathArgs))
33+
return true;
34+
35+
regmatch_t groupArray[REGEX_MAX_GROUPS];
36+
if (regexec(&_regexCompiled, requestUri.c_str(), REGEX_MAX_GROUPS, groupArray, 0) == 0) {
37+
// matches
38+
pathArgs.clear();
39+
40+
unsigned int g = 1;
41+
for (; g < REGEX_MAX_GROUPS; g++) {
42+
if (groupArray[g].rm_so == (long int)-1)
43+
break; // No more groups
44+
45+
pathArgs.push_back(requestUri.substring(groupArray[g].rm_so, groupArray[g].rm_eo));
46+
}
47+
48+
return true;
49+
}
50+
return false;
51+
}
52+
};
53+
54+
#endif

0 commit comments

Comments
 (0)