ESP_IOT v2.5
IOT ESP Coding
RequestHandlersImpl.h
Go to the documentation of this file.
1#ifndef REQUESTHANDLERSIMPL_H
2#define REQUESTHANDLERSIMPL_H
3
4#include "../../Defines.h"
5#include "RequestHandler.h"
6
8public:
10 : _fn(fn)
11 , _ufn(ufn)
12 , _uri(uri)
13 , _method(method)
14 {
15 }
16
17 bool canHandle(HTTPMethod requestMethod, String requestUri) override {
18 if (_method != HTTP_ANY && _method != requestMethod)
19 return false;
20
21 if (requestUri != _uri)
22 return false;
23
24 return true;
25 }
26
27 bool canUpload(String requestUri) override {
28 if (!_ufn || !canHandle(HTTP_POST, requestUri))
29 return false;
30
31 return true;
32 }
33
34 bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) override {
35 (void) server;
36 if (!canHandle(requestMethod, requestUri))
37 return false;
38
39 _fn();
40 return true;
41 }
42
43 void upload(WebServer& server, String requestUri, HTTPUpload& upload) override {
44 (void) server;
45 (void) upload;
46 if (canUpload(requestUri))
47 _ufn();
48 }
49
50protected:
53 String _uri;
55};
56
58public:
59 StaticRequestHandler(FS& fs, const char* path, const char* uri, const char* cache_header)
60 : _fs(fs)
61 , _uri(uri)
62 , _path(path)
63 , _cache_header(cache_header)
64 {
65 _isFile = fs.exists(path);
66 // DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d, cache_header=%s\r\n", path, uri, _isFile, cache_header);
67 _baseUriLength = _uri.length();
68 }
69
70 bool canHandle(HTTPMethod requestMethod, String requestUri) override {
71 if (requestMethod != HTTP_GET)
72 return false;
73
74 if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri))
75 return false;
76
77 return true;
78 }
79
80 bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) override {
81 if (!canHandle(requestMethod, requestUri))
82 return false;
83
84 // DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());
85
86 String path(_path);
87
88 if (!_isFile) {
89 // Base URI doesn't point to a file.
90 // If a directory is requested, look for index file.
91 if (requestUri.endsWith("/")) requestUri += "index.htm";
92
93 // Append whatever follows this URI in request to get the file path.
94 path += requestUri.substring(_baseUriLength);
95 }
96 // DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);
97
98 String contentType = getContentType(path);
99
100 // look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for
101 // if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
102 if (!path.endsWith(".gz") && !_fs.exists(path)) {
103 String pathWithGz = path + ".gz";
104 if(_fs.exists(pathWithGz))
105 path += ".gz";
106 }
107
108 File f = _fs.open(path, "r");
109 if (!f)
110 return false;
111
112 if (_cache_header.length() != 0)
113 server.sendHeader("Cache-Control", _cache_header);
114
115 server.streamFile(f, contentType);
116 return true;
117 }
118
119 static String getContentType(const String& path) {
120 if (path.endsWith(".html")) return "text/html";
121 else if (path.endsWith(".htm")) return "text/html";
122 else if (path.endsWith(".css")) return "text/css";
123 else if (path.endsWith(".txt")) return "text/plain";
124 else if (path.endsWith(".js")) return "application/javascript";
125 else if (path.endsWith(".png")) return "image/png";
126 else if (path.endsWith(".gif")) return "image/gif";
127 else if (path.endsWith(".jpg")) return "image/jpeg";
128 else if (path.endsWith(".ico")) return "image/x-icon";
129 else if (path.endsWith(".svg")) return "image/svg+xml";
130 else if (path.endsWith(".ttf")) return "application/x-font-ttf";
131 else if (path.endsWith(".otf")) return "application/x-font-opentype";
132 else if (path.endsWith(".woff")) return "application/font-woff";
133 else if (path.endsWith(".woff2")) return "application/font-woff2";
134 else if (path.endsWith(".eot")) return "application/vnd.ms-fontobject";
135 else if (path.endsWith(".sfnt")) return "application/font-sfnt";
136 else if (path.endsWith(".xml")) return "text/xml";
137 else if (path.endsWith(".pdf")) return "application/pdf";
138 else if (path.endsWith(".zip")) return "application/zip";
139 else if(path.endsWith(".gz")) return "application/x-gzip";
140 else if (path.endsWith(".appcache")) return "text/cache-manifest";
141 return "application/octet-stream";
142 }
143
144protected:
145 FS _fs;
146 String _uri;
147 String _path;
151};
152
153
154#endif //REQUESTHANDLERSIMPL_H
HTTPMethod
Definition: WebServer.h:33
@ HTTP_ANY
Definition: WebServer.h:33
@ HTTP_GET
Definition: WebServer.h:33
@ HTTP_POST
Definition: WebServer.h:33
FunctionRequestHandler(WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn, const String &uri, HTTPMethod method)
bool canUpload(String requestUri) override
WebServer::THandlerFunction _fn
bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) override
void upload(WebServer &server, String requestUri, HTTPUpload &upload) override
WebServer::THandlerFunction _ufn
bool canHandle(HTTPMethod requestMethod, String requestUri) override
bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) override
bool canHandle(HTTPMethod requestMethod, String requestUri) override
StaticRequestHandler(FS &fs, const char *path, const char *uri, const char *cache_header)
static String getContentType(const String &path)
size_t streamFile(T &file, const String &contentType)
Definition: WebServer.h:128
void sendHeader(const String &name, const String &value, bool first=false)
Definition: WebServer.cpp:241
std::function< void(void)> THandlerFunction
Definition: WebServer.h:82
Definition: WebServer.h:62