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