2017-09-23 00:56:58 -05:00
|
|
|
// Copyright 2015 Light Code Labs, LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-06-05 22:51:56 -05:00
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// To attempts rewrite. It attempts to rewrite to first valid path
|
|
|
|
// or the last path if none of the paths are valid.
|
|
|
|
func To(fs http.FileSystem, r *http.Request, to string, replacer httpserver.Replacer) Result {
|
|
|
|
tos := strings.Fields(to)
|
|
|
|
|
|
|
|
// try each rewrite paths
|
|
|
|
t := ""
|
2016-06-15 17:55:02 -05:00
|
|
|
query := ""
|
2016-06-05 22:51:56 -05:00
|
|
|
for _, v := range tos {
|
2016-06-15 17:55:02 -05:00
|
|
|
t = replacer.Replace(v)
|
2016-06-16 08:03:31 -05:00
|
|
|
tparts := strings.SplitN(t, "?", 2)
|
2016-06-15 17:55:02 -05:00
|
|
|
t = path.Clean(tparts[0])
|
|
|
|
|
|
|
|
if len(tparts) > 1 {
|
|
|
|
query = tparts[1]
|
|
|
|
}
|
2016-06-05 22:51:56 -05:00
|
|
|
|
|
|
|
// add trailing slash for directories, if present
|
2016-06-21 23:36:29 -05:00
|
|
|
if strings.HasSuffix(tparts[0], "/") && !strings.HasSuffix(t, "/") {
|
2016-06-05 22:51:56 -05:00
|
|
|
t += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate file
|
2016-06-21 23:36:29 -05:00
|
|
|
if validFile(fs, t) {
|
2016-06-05 22:51:56 -05:00
|
|
|
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 RewriteIgnored
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform rewrite
|
|
|
|
r.URL.Path = u.Path
|
2016-06-15 17:55:02 -05:00
|
|
|
if query != "" {
|
2016-06-05 22:51:56 -05:00
|
|
|
// overwrite query string if present
|
2016-06-15 17:55:02 -05:00
|
|
|
r.URL.RawQuery = query
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
if u.Fragment != "" {
|
|
|
|
// overwrite fragment if present
|
|
|
|
r.URL.Fragment = u.Fragment
|
|
|
|
}
|
|
|
|
|
|
|
|
return RewriteDone
|
|
|
|
}
|
|
|
|
|
2016-06-21 23:36:29 -05:00
|
|
|
// validFile checks if file exists on the filesystem.
|
2016-06-05 22:51:56 -05:00
|
|
|
// if file ends with `/`, it is validated as a directory.
|
2016-06-21 23:36:29 -05:00
|
|
|
func validFile(fs http.FileSystem, file string) bool {
|
2016-06-05 22:51:56 -05:00
|
|
|
if fs == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := fs.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// directory
|
|
|
|
if strings.HasSuffix(file, "/") {
|
|
|
|
return stat.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// file
|
|
|
|
return !stat.IsDir()
|
|
|
|
}
|