www.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package www
  2. import (
  3. "log"
  4. "net/http"
  5. "net/url"
  6. "path/filepath"
  7. "strings"
  8. "imuslab.com/arozos/mod/database"
  9. "imuslab.com/arozos/mod/user"
  10. )
  11. /*
  12. www package
  13. This package is the replacement handler for global homepage function in ArozOS.
  14. This allow users to host and create their website using any folder within the user
  15. access file system.
  16. */
  17. type Options struct {
  18. UserHandler *user.UserHandler
  19. Database *database.Database
  20. }
  21. type Handler struct {
  22. Options Options
  23. }
  24. /*
  25. New WebRoot Handler create a new handler for handling and routing webroots
  26. */
  27. func NewWebRootHandler(options Options) *Handler {
  28. //Create the homepage database table
  29. options.Database.NewTable("www")
  30. //Return the handler object
  31. return &Handler{
  32. Options: options,
  33. }
  34. }
  35. func (h *Handler) RouteRequest(w http.ResponseWriter, r *http.Request) {
  36. //Check if it is reaching www root folder or any files directly under www.
  37. if filepath.ToSlash(filepath.Clean(r.RequestURI)) == "/www" {
  38. //Direct access of the root folder. Serve the homepage description.
  39. http.ServeFile(w, r, "web/SystemAO/www/index.html")
  40. return
  41. } else if filepath.ToSlash(filepath.Dir(r.RequestURI)) == "/www" {
  42. //Reaching file under www root and not root. Redirect to www root
  43. http.Redirect(w, r, "/www/", 307)
  44. return
  45. }
  46. //Escape the URL
  47. decodedValue, err := url.QueryUnescape(r.RequestURI)
  48. if err != nil {
  49. //failed to decode. Just use its raw value
  50. decodedValue = r.RequestURI
  51. }
  52. //Check the user name of the user root
  53. parsedRequestURL := strings.Split(filepath.ToSlash(filepath.Clean(decodedValue)[1:]), "/")
  54. log.Println(parsedRequestURL)
  55. //Malparsed URL. Ignore request
  56. if len(parsedRequestURL) < 2 {
  57. http.NotFound(w, r)
  58. return
  59. }
  60. //Extract user information
  61. username := parsedRequestURL[1]
  62. userinfo, err := h.Options.UserHandler.GetUserInfoFromUsername(username)
  63. if err != nil {
  64. http.NotFound(w, r)
  65. return
  66. }
  67. //Check if this user enabled homepage
  68. enabled := h.CheckUserHomePageEnabled(userinfo.Username)
  69. if !enabled {
  70. w.WriteHeader(http.StatusNotFound)
  71. w.Write([]byte("404 - Page not found"))
  72. return
  73. }
  74. //Check if the user have his webroot correctly configured
  75. webroot, err := h.GetUserWebRoot(userinfo.Username)
  76. if err != nil {
  77. w.WriteHeader(http.StatusInternalServerError)
  78. w.Write([]byte("500 - Internal Server Error"))
  79. return
  80. }
  81. //User webroot real path conversion
  82. webrootRealpath, err := userinfo.VirtualPathToRealPath(webroot)
  83. if err != nil {
  84. w.WriteHeader(http.StatusInternalServerError)
  85. w.Write([]byte("500 - Internal Server Error"))
  86. return
  87. }
  88. //Perform path rewrite
  89. rewrittenPath := strings.Join(parsedRequestURL[2:], "/")
  90. //Actual accessing file path
  91. targetFilePath := filepath.ToSlash(filepath.Join(webrootRealpath, rewrittenPath))
  92. //Check if the file exists
  93. if !fileExists(targetFilePath) {
  94. w.WriteHeader(http.StatusNotFound)
  95. w.Write([]byte("404 - Page not found"))
  96. return
  97. }
  98. //Fix mimetype of js files on Windows 10 bug
  99. if filepath.Ext(targetFilePath) == ".js" {
  100. w.Header().Set("Content-Type", "application/javascript")
  101. }
  102. //Record the client IP for analysis, to be added in the future if needed
  103. //Serve the file
  104. http.ServeFile(w, r, targetFilePath)
  105. }