server.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Web Server
  3. This is the main entry point of the WebStick bare metal
  4. web server. If you have exception rules that shall not
  5. be handled by the main router, you can do them here.
  6. */
  7. //Check if a user is authenticated / logged in
  8. bool IsUserAuthed(AsyncWebServerRequest *request) {
  9. if (request->hasHeader("Cookie")) {
  10. //User cookie from browser
  11. String authCookie = GetCookieValueByKey(request, "web-auth");
  12. if (authCookie == "") {
  13. return false;
  14. }
  15. //Match it to the server side value in kvdb
  16. //Serial.println(authCookie); //user cookie
  17. //Serial.println(authSession); //server session
  18. if (authSession == "") {
  19. //Server side has no resumable login session
  20. return false;
  21. }
  22. if (authCookie.equals(authSession)) {
  23. return true;
  24. }
  25. return false;
  26. } else {
  27. Serial.println("Cookie Missing");
  28. return false;
  29. }
  30. }
  31. //Reply the request by a directory list
  32. void HandleDirRender(AsyncWebServerRequest *r, String dirName, String dirToList) {
  33. AsyncResponseStream *response = r->beginResponseStream("text/html");
  34. //Serve directory entries
  35. File directory = SD.open(dirToList);
  36. // Check if the directory is open
  37. if (!directory) {
  38. SendErrorResp(r, "unable to open directory");
  39. return;
  40. }
  41. response->print("<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Content of " + dirName + "</title></head><body style=\"margin: 3em;font-family: Arial;\">");
  42. response->print("<h3>Content of " + dirName + "</h3><div style=\"width: 100%;border-bottom: 1px solid #d9d9d9;\"></div><ul>");
  43. // List the contents of the directory
  44. while (true) {
  45. File entry = directory.openNextFile();
  46. if (!entry) {
  47. // No more files
  48. break;
  49. }
  50. // Print the file name
  51. response->print("<li><a href=\"./" + String(entry.name()) + "\">");
  52. response->print(entry.name());
  53. response->print(" (" + humanReadableSize(entry.size()) + ")</a></li>");
  54. Serial.println(entry.name());
  55. entry.close();
  56. }
  57. // Close the directory
  58. directory.close();
  59. response->print("</ul><div style=\"width: 100%;border-bottom: 1px solid #d9d9d9;\"></div><br><a href=\"../\">Back</a>");
  60. response->print("<br><br><body></html>");
  61. r->send(response);
  62. }
  63. void initWebServer() {
  64. /*
  65. Other handles here, like this
  66. server.on("/logout", HTTP_GET, [](AsyncWebServerRequest *request){
  67. request->send(401);
  68. });
  69. */
  70. /*
  71. server.on("/test", HTTP_GET, [](AsyncWebServerRequest * request) {
  72. getSDCardUsedSpace();
  73. request->send(200);
  74. });
  75. */
  76. /* Authentication Functions */
  77. server.on("/api/auth/chk", HTTP_GET, HandleCheckAuth);
  78. server.on("/api/auth/login", HTTP_POST, HandleLogin);
  79. server.on("/api/auth/logout", HTTP_GET, HandleLogout);
  80. /* File System Functions */
  81. server.on("/api/fs/list", HTTP_GET, HandleListDir);
  82. server.on("/api/fs/del", HTTP_POST, HandleFileDel);
  83. server.on("/api/fs/move", HTTP_POST, HandleFileRename);
  84. server.on("/api/fs/download", HTTP_GET, HandleFileDownload);
  85. server.on("/api/fs/newFolder", HTTP_POST, HandleNewFolder);
  86. server.on("/api/fs/disk", HTTP_GET, HandleLoadSpaceInfo);
  87. server.on("/api/fs/properties", HTTP_GET, HandleFileProp);
  88. server.on("/api/fs/search", HTTP_GET, HandleFileSearch);
  89. /* Preference */
  90. server.on("/api/pref/set", HTTP_GET, HandleSetPref);
  91. server.on("/api/pref/get", HTTP_GET, HandleLoadPref);
  92. /* Others */
  93. server.on("/api/info/wifi", HTTP_GET, HandleWiFiInfo);
  94. //File upload handler. see upload.ino
  95. server.onFileUpload(handleFileUpload);
  96. //Not found handler
  97. server.onNotFound([](AsyncWebServerRequest * request) {
  98. //Generally it will not arrive here as NOT FOUND is also handled in the main router.
  99. //See router.ino for implementation details.
  100. prettyPrintRequest(request);
  101. request->send(404, "text/plain", "Not Found");
  102. });
  103. //Main Router, see router.ino
  104. server.addHandler(new MainRouter());
  105. server.begin();
  106. }