www.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package www
  2. import (
  3. "io/ioutil"
  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. //Malparsed URL. Ignore request
  55. if len(parsedRequestURL) < 2 {
  56. http.NotFound(w, r)
  57. return
  58. }
  59. //Extract user information
  60. username := parsedRequestURL[1]
  61. userinfo, err := h.Options.UserHandler.GetUserInfoFromUsername(username)
  62. if err != nil {
  63. http.NotFound(w, r)
  64. return
  65. }
  66. //Check if this user enabled homepage
  67. enabled := h.CheckUserHomePageEnabled(userinfo.Username)
  68. if !enabled {
  69. w.WriteHeader(http.StatusNotFound)
  70. w.Write([]byte("404 - Page not found"))
  71. return
  72. }
  73. //Check if the user have his webroot correctly configured
  74. webroot, err := h.GetUserWebRoot(userinfo.Username)
  75. if err != nil {
  76. //User do not have a correctly configured webroot. Serve instruction
  77. w.WriteHeader(http.StatusInternalServerError)
  78. if fileExists("./system/www/nowebroot.html") {
  79. content, err := ioutil.ReadFile("./system/www/nowebroot.html")
  80. if err != nil {
  81. w.Write([]byte("500 - Internal Server Error"))
  82. } else {
  83. w.Write(content)
  84. }
  85. } else {
  86. w.Write([]byte("500 - Internal Server Error"))
  87. }
  88. return
  89. }
  90. //User webroot real path conversion
  91. webrootRealpath, err := userinfo.VirtualPathToRealPath(webroot)
  92. if err != nil {
  93. w.WriteHeader(http.StatusInternalServerError)
  94. w.Write([]byte("500 - Internal Server Error"))
  95. return
  96. }
  97. //Perform path rewrite
  98. rewrittenPath := strings.Join(parsedRequestURL[2:], "/")
  99. //Actual accessing file path
  100. targetFilePath := filepath.ToSlash(filepath.Join(webrootRealpath, rewrittenPath))
  101. //Check if the file exists
  102. if !fileExists(targetFilePath) {
  103. w.WriteHeader(http.StatusNotFound)
  104. w.Write([]byte("404 - Page not found"))
  105. return
  106. }
  107. //Fix mimetype of js files on Windows 10 bug
  108. if filepath.Ext(targetFilePath) == ".js" {
  109. w.Header().Set("Content-Type", "application/javascript")
  110. }
  111. //Record the client IP for analysis, to be added in the future if needed
  112. //Serve the file
  113. http.ServeFile(w, r, targetFilePath)
  114. }