fix /~user to /~user/ redir

This commit is contained in:
Hedy Li 2021-08-02 13:09:26 +08:00
parent 87487d3e49
commit ea6f966165
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
2 changed files with 12 additions and 10 deletions

View File

@ -2,9 +2,11 @@ package main
import (
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"os"
"strings"
"github.com/BurntSushi/toml"
)
type Config struct {
@ -58,10 +60,13 @@ func LoadConfig(path string) (*Config, error) {
}
}
// Config validation
if conf.DirlistSort != "name" && conf.DirlistSort != "time" && conf.DirlistSort != "size" {
fmt.Println("Warning: DirlistSort config option is not one of name/time/size, defaulting to name.")
conf.DirlistSort = "name"
}
// Strip trailing '/' so /~user to /~user/ redirects can work
conf.UserDir = strings.TrimRight(conf.UserDir, "/")
return &conf, nil
}

View File

@ -154,19 +154,16 @@ func resolvePath(reqPath string, conf *Config, req *Request) (path string) {
if conf.UserDirEnable && strings.HasPrefix(reqPath, "/~") {
bits := strings.Split(reqPath, "/")
username := bits[1][1:]
if len(bits) == 2 && !strings.HasSuffix(reqPath, "/") {
// /~user -> /~user/
// Not going to redirect here because this function (resolvePath) should stay pure
// it should only take a reqPath and return the file path requested.
reqPath += "/"
// This could potentially create a problem with search engines indenxing both /~user and
// /~user/ and have duplicate results, although in that case the search should handle
// omitting duplicates...
}
// /~user to /~user/ is somehow able to be handled together with any other /folder to /foler/ redirects
// So I won't worry about that nor handle it specifically
req.filePath = strings.TrimPrefix(filepath.Clean(strings.TrimPrefix(reqPath, "/~"+username)), "/")
new_prefix := filepath.Join("/home/", username, conf.UserDir)
req.user = username
path = filepath.Clean(strings.Replace(reqPath, bits[1], new_prefix, 1))
if strings.HasSuffix(reqPath, "/") {
path = filepath.Join(path, "index.gmi")
}