|
3 | 3 |
|
4 | 4 | class RequestHandler {
|
5 | 5 | public:
|
6 |
| - RequestHandler(const char* uri, HTTPMethod method) |
7 |
| - : _uri(uri) |
8 |
| - , _method(method) |
9 |
| - , next(NULL) |
10 |
| - { |
11 |
| - } |
| 6 | + virtual bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) { return false; } |
12 | 7 |
|
13 |
| - virtual bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) = 0; |
| 8 | + RequestHandler* next() { return _next; } |
| 9 | + void next(RequestHandler* r) { _next = r; } |
14 | 10 |
|
15 |
| - RequestHandler* next; |
16 |
| - |
17 |
| -protected: |
18 |
| - String _uri; |
19 |
| - HTTPMethod _method; |
20 |
| -}; |
21 |
| - |
22 |
| - |
23 |
| -class FunctionRequestHandler : public RequestHandler { |
24 |
| - typedef RequestHandler base; |
25 |
| - |
26 |
| -public: |
27 |
| - FunctionRequestHandler(ESP8266WebServer::THandlerFunction fn, const char* uri, HTTPMethod method) |
28 |
| - : _fn(fn) |
29 |
| - , base(uri, method) |
30 |
| - { |
31 |
| - } |
32 |
| - |
33 |
| - bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override { |
34 |
| - if (_method != HTTP_ANY && _method != requestMethod) |
35 |
| - return false; |
36 |
| - |
37 |
| - if (requestUri != _uri) |
38 |
| - return false; |
39 |
| - |
40 |
| - _fn(); |
41 |
| - return true; |
42 |
| - } |
43 |
| - |
44 |
| -protected: |
45 |
| - ESP8266WebServer::THandlerFunction _fn; |
46 |
| -}; |
47 |
| - |
48 |
| -class StaticRequestHandler : public RequestHandler { |
49 |
| - typedef RequestHandler base; |
50 |
| - |
51 |
| -public: |
52 |
| - StaticRequestHandler(FS& fs, const char* path, const char* uri) |
53 |
| - : _fs(fs) |
54 |
| - , base(uri, HTTP_GET) |
55 |
| - , _path(path) |
56 |
| - { |
57 |
| - _isFile = fs.exists(path); |
58 |
| - DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d\r\n", path, uri, _isFile); |
59 |
| - _baseUriLength = _uri.length(); |
60 |
| - } |
61 |
| - |
62 |
| - bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override { |
63 |
| - if (requestMethod != _method) |
64 |
| - return false; |
65 |
| - DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str()); |
66 |
| - if (!requestUri.startsWith(_uri)) |
67 |
| - return false; |
68 |
| - |
69 |
| - String path(_path); |
70 |
| - if (!_isFile) { |
71 |
| - // Base URI doesn't point to a file. Append whatever follows this |
72 |
| - // URI in request to get the file path. |
73 |
| - path += requestUri.substring(_baseUriLength); |
74 |
| - } |
75 |
| - else if (requestUri != _uri) { |
76 |
| - // Base URI points to a file but request doesn't match this URI exactly |
77 |
| - return false; |
78 |
| - } |
79 |
| - DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile); |
80 |
| - File f = _fs.open(path, "r"); |
81 |
| - if (!f) |
82 |
| - return false; |
83 |
| - |
84 |
| - server.streamFile(f, getContentType(path)); |
85 |
| - return true; |
86 |
| - } |
87 |
| - |
88 |
| - static String getContentType(const String& path) { |
89 |
| - if (path.endsWith(".html")) return "text/html"; |
90 |
| - else if (path.endsWith(".htm")) return "text/html"; |
91 |
| - else if (path.endsWith(".css")) return "text/css"; |
92 |
| - else if (path.endsWith(".txt")) return "text/plain"; |
93 |
| - else if (path.endsWith(".js")) return "application/javascript"; |
94 |
| - else if (path.endsWith(".png")) return "image/png"; |
95 |
| - else if (path.endsWith(".gif")) return "image/gif"; |
96 |
| - else if (path.endsWith(".jpg")) return "image/jpeg"; |
97 |
| - else if (path.endsWith(".ico")) return "image/x-icon"; |
98 |
| - else if (path.endsWith(".xml")) return "text/xml"; |
99 |
| - else if (path.endsWith(".pdf")) return "application/pdf"; |
100 |
| - else if (path.endsWith(".zip")) return "application/zip"; |
101 |
| - return "text/plain"; |
102 |
| - } |
103 |
| - |
104 |
| -protected: |
105 |
| - FS _fs; |
106 |
| - String _path; |
107 |
| - bool _isFile; |
108 |
| - size_t _baseUriLength; |
| 11 | +private: |
| 12 | + RequestHandler* _next = nullptr; |
109 | 13 | };
|
110 | 14 |
|
111 | 15 | #endif //REQUESTHANDLER_H
|
0 commit comments