RequestHandlersImpl.h 5.2 KB

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