webserv.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Web Server */
  2. const String webroot = "/web/"; //Require tailing slash
  3. // Function to serve files
  4. void handleFileServe(AsyncWebServerRequest *request) {
  5. String path = request->url();
  6. String filepath = path;
  7. filepath.remove(0, 1); // Trim the prefix slash in uri
  8. filepath = webroot + filepath;
  9. Serial.println(filepath);
  10. if (SD.exists(filepath)) {
  11. request->send(SD, filepath, getMime(filepath));
  12. } else {
  13. handleRootRedirect(request);
  14. }
  15. }
  16. void sendNotFound(AsyncWebServerRequest *request) {
  17. request->send(404, "text/plain", "404 - Not found");
  18. }
  19. // Redirect request to index.html
  20. void handleRootRedirect(AsyncWebServerRequest *request) {
  21. request->redirect("/index.html");
  22. }
  23. //Register all the required API endpoint for web server
  24. void registerAPIEndpoints() {
  25. /* Basic handlers */
  26. server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
  27. handleRootRedirect(request);
  28. });
  29. server.onNotFound([](AsyncWebServerRequest * request) {
  30. handleFileServe(request);
  31. });
  32. server.on("/upload", HTTP_POST, [](AsyncWebServerRequest * request) {
  33. request->send(200); // Send empty response for the upload URL
  34. }, handleFileUpload);
  35. /* Application APIs */
  36. server.on("/api/ctr/emoji", HTTP_GET, handleRenderEmoji);
  37. server.on("/api/ctr/move", HTTP_GET, handleMovements);
  38. server.on("/api/ctr/cover", HTTP_GET, handleCover);
  39. server.on("/api/ctr/pusher", HTTP_GET, handlePusher);
  40. server.on("/api/fs/listDir", HTTP_GET, handleListDir);
  41. server.on("/api/fs/download", HTTP_GET, handleFileDownload);
  42. server.on("/api/fs/delete", HTTP_GET, handleFileDelete);
  43. server.on("/api/ipaddr", HTTP_GET, handleGetIPAddress);
  44. }
  45. //ip addr
  46. void handleGetIPAddress(AsyncWebServerRequest *request) {
  47. String ipAddress;
  48. if (!ENABLE_WIFI_DEBUG) {
  49. ipAddress = WiFi.softAPIP().toString();
  50. } else {
  51. ipAddress = WiFi.localIP().toString();
  52. }
  53. request->send(200, "application/json", "{\"ip\":\"" + ipAddress + "\"}");
  54. }
  55. /* Utilities */
  56. String GetPara(AsyncWebServerRequest *request, String key) {
  57. if (request->hasParam(key)) {
  58. return request->getParam(key)->value();
  59. }
  60. return "";
  61. }
  62. //Get the filename from filepath
  63. String basename(const String& filePath) {
  64. int lastSlashIndex = filePath.lastIndexOf('/');
  65. // If no slash is found, return the original path
  66. if (lastSlashIndex == -1) {
  67. return filePath;
  68. }
  69. // Return the substring after the last slash
  70. return filePath.substring(lastSlashIndex + 1);
  71. }
  72. String getMime(const String& path) {
  73. String _contentType = "text/plain";
  74. if (path.endsWith(".html")) _contentType = "text/html";
  75. else if (path.endsWith(".htm")) _contentType = "text/html";
  76. else if (path.endsWith(".css")) _contentType = "text/css";
  77. else if (path.endsWith(".json")) _contentType = "text/json";
  78. else if (path.endsWith(".js")) _contentType = "application/javascript";
  79. else if (path.endsWith(".png")) _contentType = "image/png";
  80. else if (path.endsWith(".gif")) _contentType = "image/gif";
  81. else if (path.endsWith(".jpg")) _contentType = "image/jpeg";
  82. else if (path.endsWith(".ico")) _contentType = "image/x-icon";
  83. else if (path.endsWith(".svg")) _contentType = "image/svg+xml";
  84. else if (path.endsWith(".eot")) _contentType = "font/eot";
  85. else if (path.endsWith(".woff")) _contentType = "font/woff";
  86. else if (path.endsWith(".woff2")) _contentType = "font/woff2";
  87. else if (path.endsWith(".ttf")) _contentType = "font/ttf";
  88. else if (path.endsWith(".xml")) _contentType = "text/xml";
  89. else if (path.endsWith(".pdf")) _contentType = "application/pdf";
  90. else if (path.endsWith(".zip")) _contentType = "application/zip";
  91. else if (path.endsWith(".gz")) _contentType = "application/x-gzip";
  92. else if (path.endsWith(".mp3")) _contentType = "audio/mpeg";
  93. else if (path.endsWith(".mp4")) _contentType = "video/mp4";
  94. else if (path.endsWith(".aac")) _contentType = "audio/aac";
  95. else if (path.endsWith(".ogg")) _contentType = "audio/ogg";
  96. else if (path.endsWith(".wav")) _contentType = "audio/wav";
  97. else if (path.endsWith(".m4v")) _contentType = "video/x-m4v";
  98. else if (path.endsWith(".webm")) _contentType = "video/webm";
  99. else _contentType = "text/plain";
  100. return _contentType;
  101. }