error.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package main
  2. /*
  3. This page mainly used to handle error interfaces
  4. */
  5. import (
  6. "net/http"
  7. "os"
  8. "strings"
  9. fs "imuslab.com/arozos/mod/filesystem"
  10. )
  11. func errorHandleNotFound(w http.ResponseWriter, r *http.Request) {
  12. notFoundPage := "./web/SystemAO/notfound.html"
  13. if fs.FileExists(notFoundPage) {
  14. notFoundTemplateBytes, err := os.ReadFile(notFoundPage)
  15. notFoundTemplate := string(notFoundTemplateBytes)
  16. if err != nil {
  17. http.NotFound(w, r)
  18. } else {
  19. //Replace the request URL inside the page
  20. notFoundTemplate = strings.ReplaceAll(notFoundTemplate, "{{request_url}}", r.RequestURI)
  21. rel := getRootEscapeFromCurrentPath(r.RequestURI)
  22. notFoundTemplate = strings.ReplaceAll(notFoundTemplate, "{{root_escape}}", rel)
  23. w.WriteHeader(http.StatusNotFound)
  24. w.Header().Set("Content-Type", "text/html")
  25. w.Write([]byte(notFoundTemplate))
  26. }
  27. } else {
  28. http.NotFound(w, r)
  29. }
  30. }
  31. func errorHandleInternalServerError(w http.ResponseWriter, r *http.Request) {
  32. internalServerErrPage := "./web/SystemAO/internalServerError.html"
  33. if fs.FileExists(internalServerErrPage) {
  34. templateBytes, err := os.ReadFile(internalServerErrPage)
  35. template := string(templateBytes)
  36. if err != nil {
  37. http.NotFound(w, r)
  38. } else {
  39. //Replace the request URL inside the page
  40. template = strings.ReplaceAll(template, "{{request_url}}", r.RequestURI)
  41. rel := getRootEscapeFromCurrentPath(r.RequestURI)
  42. template = strings.ReplaceAll(template, "{{root_escape}}", rel)
  43. w.WriteHeader(http.StatusInternalServerError)
  44. w.Write([]byte(template))
  45. }
  46. } else {
  47. w.WriteHeader(http.StatusInternalServerError)
  48. w.Write([]byte("500 - Internal Server Error"))
  49. }
  50. }
  51. func errorHandlePermissionDenied(w http.ResponseWriter, r *http.Request) {
  52. unauthorizedPage := "./web/SystemAO/unauthorized.html"
  53. if fs.FileExists(unauthorizedPage) {
  54. notFoundTemplateBytes, err := os.ReadFile(unauthorizedPage)
  55. notFoundTemplate := string(notFoundTemplateBytes)
  56. if err != nil {
  57. http.NotFound(w, r)
  58. } else {
  59. //Replace the request URL inside the page
  60. notFoundTemplate = strings.ReplaceAll(notFoundTemplate, "{{request_url}}", r.RequestURI)
  61. rel := getRootEscapeFromCurrentPath(r.RequestURI)
  62. notFoundTemplate = strings.ReplaceAll(notFoundTemplate, "{{root_escape}}", rel)
  63. w.WriteHeader(http.StatusUnauthorized)
  64. w.Write([]byte(notFoundTemplate))
  65. }
  66. } else {
  67. http.Error(w, "Not authorized", http.StatusUnauthorized)
  68. }
  69. }
  70. // Get escape root path, example /asd/asd => ../../
  71. func getRootEscapeFromCurrentPath(requestURL string) string {
  72. rel := ""
  73. if !strings.Contains(requestURL, "/") {
  74. return ""
  75. }
  76. splitter := requestURL
  77. if splitter[len(splitter)-1:] != "/" {
  78. splitter = splitter + "/"
  79. }
  80. for i := 0; i < len(strings.Split(splitter, "/"))-2; i++ {
  81. rel += "../"
  82. }
  83. return rel
  84. }