router.ino 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }
  21. return true;
  22. }
  23. //Main Routing Logic Here
  24. void handleRequest(AsyncWebServerRequest *request) {
  25. String requestURI = request->url().c_str();
  26. /* Rewrite the request path if URI contains ./ */
  27. if (requestURI.indexOf("./") > 0) {
  28. requestURI.replace("./", "");
  29. AsyncWebServerResponse *response = request->beginResponse(307);
  30. response->addHeader("Cache-Control", "no-cache");
  31. response->addHeader("Location", requestURI);
  32. request->send(response);
  33. return;
  34. }
  35. /* Special Routing Rules */
  36. //Redirect / back to index.html
  37. if (requestURI == "/") {
  38. request->redirect("/index.html");
  39. return;
  40. }
  41. //Special interfaces that require access controls
  42. if (requestURI.startsWith("/store/")) {
  43. //Private file storage. Not allow access
  44. AsyncWebServerResponse *response = request->beginResponse(401, "text/html", "403 - Forbidden");
  45. request->send(response);
  46. return;
  47. }
  48. /* Default Routing Rules */
  49. Serial.println("URI: " + requestURI + " | MIME: " + getMime(requestURI));
  50. //Check if the file exists on the SD card
  51. if (SD.exists("/www" + requestURI)) {
  52. // File exists on SD card web root
  53. if (IsDir("/www" + requestURI)) {
  54. //Requesting a directory
  55. if (!requestURI.endsWith("/")) {
  56. //Missing tailing slash
  57. request->redirect(requestURI + "/");
  58. return;
  59. }
  60. if (SD.exists("/www" + requestURI + "index.html")) {
  61. request->send(SDFS, "/www" + requestURI + "/index.html", "text/html", false);
  62. } else {
  63. HandleDirRender(request, requestURI , "/www" + requestURI);
  64. }
  65. } else {
  66. request->send(SDFS, "/www" + requestURI, getMime(requestURI), false);
  67. }
  68. } else {
  69. // File does not exist in web root
  70. AsyncResponseStream *response = request->beginResponseStream("text/html");
  71. Serial.println("NOT FOUND: " + requestURI);
  72. prettyPrintRequest(request);
  73. response->print("<!DOCTYPE html><html><head><title>Not Found</title></head><body>");
  74. response->print("<p>404 - Not Found</p>");
  75. response->printf("<p>Requesting http://%s with URI: %s</p>", request->host().c_str(), request->url().c_str());
  76. response->print("</body></html>");
  77. request->send(response);
  78. }
  79. }
  80. };