package www import ( "log" "net/http" "path/filepath" "strings" "imuslab.com/arozos/mod/user" ) /* www package This package is the replacement handler for global homepage function in ArozOS. This allow users to host and create their website using any folder within the user access file system. */ type Options struct { UserHandler *user.UserHandler } type Handler struct { Options Options } /* New WebRoot Handler create a new handler for handling and routing webroots */ func NewWebRootHandler(options Options) *Handler { return &Handler{ Options: options, } } func (h *Handler) RouteRequest(w http.ResponseWriter, r *http.Request) { //Check if it is reaching www root folder or any files directly under www. if filepath.ToSlash(filepath.Clean(r.RequestURI)) == "/www" { //Direct access of the root folder. Serve the homepage description. http.ServeFile(w, r, "web/SystemAO/www/index.html") return } else if filepath.ToSlash(filepath.Dir(r.RequestURI)) == "/www" { //Reaching file under www root and not root. Redirect to www root http.Redirect(w, r, "/www/", 307) return } //Check the user name of the user root parsedRequestURL := strings.Split(filepath.ToSlash(filepath.Clean(r.RequestURI)[1:]), "/") //Malparsed URL. Ignore request if len(parsedRequestURL) < 2 { http.NotFound(w, r) return } //Extract user information username := parsedRequestURL[1] _, err := h.Options.UserHandler.GetUserInfoFromUsername(username) if err != nil { http.NotFound(w, r) return } log.Println("Serving user webroot: ", username) sendOK(w) }