2015-12-22 17:19:22 -05:00
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2015-12-23 03:02:52 -05:00
|
|
|
"path"
|
2015-12-22 17:19:22 -05:00
|
|
|
"strings"
|
2015-12-30 15:47:37 -05:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-12-22 17:19:22 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// To attempts rewrite. It attempts to rewrite to first valid path
|
|
|
|
// or the last path if none of the paths are valid.
|
|
|
|
// Returns true if rewrite is successful and false otherwise.
|
2015-12-30 15:47:37 -05:00
|
|
|
func To(fs http.FileSystem, r *http.Request, to string, replacer middleware.Replacer) bool {
|
2015-12-22 17:19:22 -05:00
|
|
|
tos := strings.Fields(to)
|
|
|
|
|
|
|
|
// try each rewrite paths
|
|
|
|
t := ""
|
|
|
|
for _, v := range tos {
|
2015-12-23 03:02:52 -05:00
|
|
|
t = path.Clean(replacer.Replace(v))
|
2015-12-23 06:11:11 -05:00
|
|
|
|
|
|
|
// add trailing slash for directories, if present
|
|
|
|
if strings.HasSuffix(v, "/") && !strings.HasSuffix(t, "/") {
|
|
|
|
t += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate file
|
2015-12-22 17:19:22 -05:00
|
|
|
if isValidFile(fs, t) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate resulting path
|
|
|
|
u, err := url.Parse(t)
|
|
|
|
if err != nil {
|
|
|
|
// Let the user know we got here. Rewrite is expected but
|
|
|
|
// the resulting url is invalid.
|
|
|
|
log.Printf("[ERROR] rewrite: resulting path '%v' is invalid. error: %v", t, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// take note of this rewrite for internal use by fastcgi
|
|
|
|
// all we need is the URI, not full URL
|
|
|
|
r.Header.Set(headerFieldName, r.URL.RequestURI())
|
|
|
|
|
|
|
|
// perform rewrite
|
|
|
|
r.URL.Path = u.Path
|
|
|
|
if u.RawQuery != "" {
|
|
|
|
// overwrite query string if present
|
|
|
|
r.URL.RawQuery = u.RawQuery
|
|
|
|
}
|
|
|
|
if u.Fragment != "" {
|
|
|
|
// overwrite fragment if present
|
|
|
|
r.URL.Fragment = u.Fragment
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValidFile checks if file exists on the filesystem.
|
|
|
|
// if file ends with `/`, it is validated as a directory.
|
|
|
|
func isValidFile(fs http.FileSystem, file string) bool {
|
2015-12-23 03:36:00 -05:00
|
|
|
if fs == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:19:22 -05:00
|
|
|
f, err := fs.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-23 06:11:11 -05:00
|
|
|
// directory
|
|
|
|
if strings.HasSuffix(file, "/") {
|
|
|
|
return stat.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// file
|
|
|
|
return !stat.IsDir()
|
2015-12-22 17:19:22 -05:00
|
|
|
}
|