1#ifndef REQUESTHANDLERSIMPL_H
2#define REQUESTHANDLERSIMPL_H
4#include "../../Defines.h"
6#ifdef USE_LIB_WEBSERVER
8class FunctionRequestHandler :
public RequestHandler {
10 FunctionRequestHandler(WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn,
const String &uri, HTTPMethod method)
18 bool canHandle(HTTPMethod requestMethod, String requestUri)
override {
19 if (_method != HTTP_ANY && _method != requestMethod)
22 if (requestUri != _uri)
28 bool canUpload(String requestUri)
override {
29 if (!_ufn || !canHandle(HTTP_POST, requestUri))
35 bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri)
override {
37 if (!canHandle(requestMethod, requestUri))
44 void upload(WebServer& server, String requestUri, HTTPUpload& upload)
override {
47 if (canUpload(requestUri))
52 WebServer::THandlerFunction _fn;
53 WebServer::THandlerFunction _ufn;
58class StaticRequestHandler :
public RequestHandler {
60 StaticRequestHandler(FS& fs,
const char* path,
const char* uri,
const char* cache_header)
64 , _cache_header(cache_header)
66 _isFile = fs.exists(path);
68 _baseUriLength = _uri.length();
71 bool canHandle(HTTPMethod requestMethod, String requestUri)
override {
72 if (requestMethod != HTTP_GET)
75 if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri))
81 bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri)
override {
82 if (!canHandle(requestMethod, requestUri))
92 if (requestUri.endsWith(
"/")) requestUri +=
"index.htm";
95 path += requestUri.substring(_baseUriLength);
99 String contentType = getContentType(path);
103 if (!path.endsWith(
".gz") && !_fs.exists(path)) {
104 String pathWithGz = path +
".gz";
105 if(_fs.exists(pathWithGz))
109 File f = _fs.open(path,
"r");
113 if (_cache_header.length() != 0)
114 server.sendHeader(
"Cache-Control", _cache_header);
116 server.streamFile(f, contentType);
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";
149 String _cache_header;
151 size_t _baseUriLength;