webserv.ino 4.1 KB

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