router.ino 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Router.ino
  3. This is the main router of the whole web server.
  4. It is like the apache.conf where you handle routing
  5. to different services.
  6. By default, all route will go to the SD card /www/ folder
  7. */
  8. class MainRouter : public AsyncWebHandler {
  9. public:
  10. MainRouter() {}
  11. virtual ~MainRouter() {}
  12. bool canHandle(AsyncWebServerRequest *request) {
  13. String requestURI = request->url().c_str();
  14. if (requestURI.equals("/upload")) {
  15. //File Upload Endpoint
  16. return false;
  17. } else if (requestURI.startsWith("/api/")) {
  18. //API paths
  19. return false;
  20. }else if (requestURI.startsWith("/share/")) {
  21. //Share paths
  22. return false;
  23. }
  24. return true;
  25. }
  26. //Main Routing Logic Here
  27. void handleRequest(AsyncWebServerRequest *request) {
  28. String requestURI = request->url().c_str();
  29. /* Rewrite the request path if URI contains ./ */
  30. if (requestURI.indexOf("./") > 0) {
  31. requestURI.replace("./", "");
  32. AsyncWebServerResponse *response = request->beginResponse(307);
  33. response->addHeader("Cache-Control", "no-cache");
  34. response->addHeader("Location", requestURI);
  35. request->send(response);
  36. return;
  37. }
  38. /* Special Routing Rules */
  39. //Redirect / back to index.html
  40. if (requestURI == "/") {
  41. request->redirect("/index.html");
  42. return;
  43. }
  44. //Special interfaces that require access controls
  45. if (requestURI.startsWith("/store/")) {
  46. //Private file storage. Not allow access
  47. AsyncWebServerResponse *response = request->beginResponse(401, "text/html", "403 - Forbidden");
  48. request->send(response);
  49. return;
  50. }
  51. /* Default Routing Rules */
  52. Serial.println("URI: " + requestURI + " | MIME: " + getMime(requestURI));
  53. //Check if the file exists on the SD card
  54. if (SD.exists("/www" + requestURI)) {
  55. // File exists on SD card web root
  56. if (IsDir("/www" + requestURI)) {
  57. //Requesting a directory
  58. if (!requestURI.endsWith("/")) {
  59. //Missing tailing slash
  60. request->redirect(requestURI + "/");
  61. return;
  62. }
  63. if (SD.exists("/www" + requestURI + "index.html")) {
  64. request->send(SDFS, "/www" + requestURI + "/index.html", "text/html", false);
  65. } else {
  66. HandleDirRender(request, requestURI , "/www" + requestURI);
  67. }
  68. } else {
  69. request->send(SDFS, "/www" + requestURI, getMime(requestURI), false);
  70. }
  71. } else {
  72. // File does not exist in web root
  73. AsyncResponseStream *response = request->beginResponseStream("text/html");
  74. Serial.println("NOT FOUND: " + requestURI);
  75. prettyPrintRequest(request);
  76. response->print("<!DOCTYPE html><html><head><title>Not Found</title></head><body>");
  77. response->print("<p>404 - Not Found</p>");
  78. response->printf("<p>Requesting http://%s with URI: %s</p>", request->host().c_str(), request->url().c_str());
  79. response->print("</body></html>");
  80. request->send(response);
  81. }
  82. }
  83. };