|
| 1 | +/**************************************************************************************************************************** |
| 2 | + ESP_RequestHandlersImpl.h - Dead simple web-server. |
| 3 | + For Ethernet shields on ESP8266 and ESP32 |
| 4 | + For EthernetWebServer library by Khoi Hoang |
| 5 | + |
| 6 | + EthernetWebServer is a library for the Ethernet shields to run WebServer |
| 7 | +
|
| 8 | + Based on and modified from ESP8266 https://github.com./esp8266/Arduino/releases |
| 9 | + Built by Khoi Hoang https://github.com./khoih-prog/EthernetWebServer |
| 10 | + Licensed under MIT license |
| 11 | + Version: 1.0.0 |
| 12 | +
|
| 13 | + Original author: |
| 14 | + @file RequestHandlersImpl.h |
| 15 | + @author Khoi Hoang |
| 16 | +
|
| 17 | + Version Modified By Date Comments |
| 18 | + ------- ----------- ---------- ----------- |
| 19 | + 1.0.0 S Azari 25/01/2021 Initial coding, inheriting EthernetWebServer class and porting ESP8266 code |
| 20 | + *****************************************************************************************************************************/ |
| 21 | + |
| 22 | +#pragma once |
| 23 | + |
| 24 | +#include "RequestHandler.h" |
| 25 | +#include "esp_detail/mimetable.h" |
| 26 | +#include "FS.h" |
| 27 | +#include "WString.h" |
| 28 | +#include <MD5Builder.h> |
| 29 | +#include <base64.h> |
| 30 | + |
| 31 | +class FunctionRequestHandler : public RequestHandler |
| 32 | +{ |
| 33 | + public: |
| 34 | + |
| 35 | + FunctionRequestHandler(EthernetWebServer::THandlerFunction fn, EthernetWebServer::THandlerFunction ufn, const String &uri, HTTPMethod method) |
| 36 | + : _fn(fn) |
| 37 | + , _ufn(ufn) |
| 38 | + , _uri(uri) |
| 39 | + , _method(method) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + bool canHandle(HTTPMethod requestMethod, String requestUri) override |
| 44 | + { |
| 45 | + if (_method != HTTP_ANY && _method != requestMethod) |
| 46 | + return false; |
| 47 | + |
| 48 | + if (requestUri == _uri) |
| 49 | + return true; |
| 50 | + |
| 51 | + if (_uri.endsWith("/*")) |
| 52 | + { |
| 53 | + String _uristart = _uri; |
| 54 | + _uristart.replace("/*", ""); |
| 55 | + |
| 56 | + if (requestUri.startsWith(_uristart)) |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + bool canUpload(String requestUri) override |
| 64 | + { |
| 65 | + if (!_ufn || !canHandle(HTTP_POST, requestUri)) |
| 66 | + return false; |
| 67 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + bool handle(EthernetWebServer& server, HTTPMethod requestMethod, String requestUri) override |
| 72 | + { |
| 73 | + ETW_UNUSED(server); |
| 74 | + |
| 75 | + if (!canHandle(requestMethod, requestUri)) |
| 76 | + return false; |
| 77 | + |
| 78 | + _fn(); |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + void upload(EthernetWebServer& server, String requestUri, HTTPUpload& upload) override |
| 83 | + { |
| 84 | + ETW_UNUSED(server); |
| 85 | + ETW_UNUSED(upload); |
| 86 | + |
| 87 | + if (canUpload(requestUri)) |
| 88 | + _ufn(); |
| 89 | + } |
| 90 | + |
| 91 | + protected: |
| 92 | + EthernetWebServer::THandlerFunction _fn; |
| 93 | + EthernetWebServer::THandlerFunction _ufn; |
| 94 | + String _uri; |
| 95 | + HTTPMethod _method; |
| 96 | +}; |
| 97 | + |
| 98 | +class StaticRequestHandler : public RequestHandler { |
| 99 | + using WebServerType = EthernetWebServer; |
| 100 | +public: |
| 101 | + StaticRequestHandler(FS& fs, const char* path, const char* uri, const char* cache_header) |
| 102 | + : _fs(fs) |
| 103 | + , _uri(uri) |
| 104 | + , _path(path) |
| 105 | + , _cache_header(cache_header) |
| 106 | + { |
| 107 | + //DEBUGV("StaticRequestHandler: path=%s uri=%s, cache_header=%s\r\n", path, uri, cache_header == __null ? "" : cache_header); |
| 108 | + _isFile = fs.exists(path); |
| 109 | + _baseUriLength = _uri.length(); |
| 110 | + } |
| 111 | + |
| 112 | + bool validMethod(HTTPMethod requestMethod){ |
| 113 | + return (requestMethod == HTTP_GET) || (requestMethod == HTTP_HEAD); |
| 114 | + } |
| 115 | + |
| 116 | + /* Deprecated version. Please use mime::getContentType instead */ |
| 117 | + static String getContentType(const String& path) __attribute__((deprecated)) { |
| 118 | + return mime_esp::getContentType(path); |
| 119 | + } |
| 120 | + |
| 121 | +protected: |
| 122 | + FS _fs; |
| 123 | + bool _isFile; |
| 124 | + String _uri; |
| 125 | + String _path; |
| 126 | + String _cache_header; |
| 127 | + size_t _baseUriLength; |
| 128 | +}; |
| 129 | + |
| 130 | + |
| 131 | +class StaticFileRequestHandler |
| 132 | + : |
| 133 | +public StaticRequestHandler { |
| 134 | + |
| 135 | + using SRH = StaticRequestHandler; |
| 136 | + using WebServerType = EthernetWebServer; |
| 137 | + |
| 138 | +public: |
| 139 | + StaticFileRequestHandler(FS& fs, const char* path, const char* uri, const char* cache_header) |
| 140 | + : |
| 141 | + StaticRequestHandler{fs, path, uri, cache_header} |
| 142 | + { |
| 143 | + File f = SRH::_fs.open(path, "r"); |
| 144 | + MD5Builder calcMD5; |
| 145 | + calcMD5.begin(); |
| 146 | + calcMD5.addStream(f, f.size()); |
| 147 | + calcMD5.calculate(); |
| 148 | + calcMD5.getBytes(_ETag_md5); |
| 149 | + f.close(); |
| 150 | + } |
| 151 | + |
| 152 | + bool canHandle(HTTPMethod requestMethod, const String requestUri) override { |
| 153 | + return SRH::validMethod(requestMethod) && requestUri == SRH::_uri; |
| 154 | + } |
| 155 | + |
| 156 | + bool handle(EthernetWebServer& server, HTTPMethod requestMethod, const String requestUri) { |
| 157 | + |
| 158 | + if (!canHandle(requestMethod, requestUri)) |
| 159 | + return false; |
| 160 | + |
| 161 | + |
| 162 | + const String etag = "\"" + base64::encode(_ETag_md5, 16) + "\""; |
| 163 | + |
| 164 | + if(server.header("If-None-Match") == etag){ |
| 165 | + server.send(304); |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + File f = SRH::_fs.open(SRH::_path, "r"); |
| 170 | + |
| 171 | + if (!f) |
| 172 | + return false; |
| 173 | + |
| 174 | + if (!_isFile) { |
| 175 | + f.close(); |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + if (SRH::_cache_header.length() != 0) |
| 180 | + server.sendHeader("Cache-Control", SRH::_cache_header); |
| 181 | + |
| 182 | + server.sendHeader("ETag", etag); |
| 183 | + |
| 184 | + server.streamFile(f, mime_esp::getContentType(SRH::_path), requestMethod); |
| 185 | + return true; |
| 186 | + } |
| 187 | + |
| 188 | +protected: |
| 189 | + uint8_t _ETag_md5[16]; |
| 190 | +}; |
| 191 | + |
0 commit comments