error.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.Write([]byte(notFoundTemplate))
  25. }
  26. } else {
  27. http.NotFound(w, r)
  28. }
  29. }
  30. func errorHandleInternalServerError(w http.ResponseWriter, r *http.Request) {
  31. internalServerErrPage := "./web/SystemAO/internalServerError.html"
  32. if fs.FileExists(internalServerErrPage) {
  33. templateBytes, err := os.ReadFile(internalServerErrPage)
  34. template := string(templateBytes)
  35. if err != nil {
  36. http.NotFound(w, r)
  37. } else {
  38. //Replace the request URL inside the page
  39. template = strings.ReplaceAll(template, "{{request_url}}", r.RequestURI)
  40. rel := getRootEscapeFromCurrentPath(r.RequestURI)
  41. template = strings.ReplaceAll(template, "{{root_escape}}", rel)
  42. w.WriteHeader(http.StatusInternalServerError)
  43. w.Write([]byte(template))
  44. }
  45. } else {
  46. w.WriteHeader(http.StatusInternalServerError)
  47. w.Write([]byte("500 - Internal Server Error"))
  48. }
  49. }
  50. func errorHandlePermissionDenied(w http.ResponseWriter, r *http.Request) {
  51. unauthorizedPage := "./web/SystemAO/unauthorized.html"
  52. if fs.FileExists(unauthorizedPage) {
  53. notFoundTemplateBytes, err := os.ReadFile(unauthorizedPage)
  54. notFoundTemplate := string(notFoundTemplateBytes)
  55. if err != nil {
  56. http.NotFound(w, r)
  57. } else {
  58. //Replace the request URL inside the page
  59. notFoundTemplate = strings.ReplaceAll(notFoundTemplate, "{{request_url}}", r.RequestURI)
  60. rel := getRootEscapeFromCurrentPath(r.RequestURI)
  61. notFoundTemplate = strings.ReplaceAll(notFoundTemplate, "{{root_escape}}", rel)
  62. w.WriteHeader(http.StatusUnauthorized)
  63. w.Write([]byte(notFoundTemplate))
  64. }
  65. } else {
  66. http.Error(w, "Not authorized", http.StatusUnauthorized)
  67. }
  68. }
  69. // Get escape root path, example /asd/asd => ../../
  70. func getRootEscapeFromCurrentPath(requestURL string) string {
  71. rel := ""
  72. if !strings.Contains(requestURL, "/") {
  73. return ""
  74. }
  75. splitter := requestURL
  76. if splitter[len(splitter)-1:] != "/" {
  77. splitter = splitter + "/"
  78. }
  79. for i := 0; i < len(strings.Split(splitter, "/"))-2; i++ {
  80. rel += "../"
  81. }
  82. return rel
  83. }