2019-05-20 22:21:33 -05:00
|
|
|
package fileserver
|
2019-05-20 11:59:20 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"bitbucket.org/lightcodelabs/caddy2"
|
|
|
|
"bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddy2.RegisterModule(caddy2.Module{
|
|
|
|
Name: "http.matchers.file",
|
|
|
|
New: func() (interface{}, error) { return new(FileMatcher), nil },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:46:47 -05:00
|
|
|
// FileMatcher is a matcher that can match requests
|
|
|
|
// based on the local file system.
|
2019-05-20 11:59:20 -05:00
|
|
|
// TODO: Not sure how to do this well; we'd need the ability to
|
|
|
|
// hide files, etc...
|
|
|
|
// TODO: Also consider a feature to match directory that
|
|
|
|
// contains a certain filename (use filepath.Glob), useful
|
|
|
|
// if wanting to map directory-URI requests where the dir
|
|
|
|
// has index.php to PHP backends, for example (although this
|
|
|
|
// can effectively be done with rehandling already)
|
|
|
|
type FileMatcher struct {
|
|
|
|
Root string `json:"root"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Flags []string `json:"flags"`
|
|
|
|
}
|
|
|
|
|
2019-05-20 16:46:47 -05:00
|
|
|
// Match matches the request r against m.
|
2019-05-20 11:59:20 -05:00
|
|
|
func (m FileMatcher) Match(r *http.Request) bool {
|
2019-05-21 14:03:52 -05:00
|
|
|
fullPath := sanitizedPathJoin(m.Root, m.Path)
|
2019-05-20 11:59:20 -05:00
|
|
|
var match bool
|
|
|
|
if len(m.Flags) > 0 {
|
|
|
|
match = true
|
|
|
|
fi, err := os.Stat(fullPath)
|
|
|
|
for _, f := range m.Flags {
|
|
|
|
switch f {
|
|
|
|
case "EXIST":
|
|
|
|
match = match && os.IsNotExist(err)
|
|
|
|
case "DIR":
|
|
|
|
match = match && err == nil && fi.IsDir()
|
|
|
|
default:
|
|
|
|
match = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return match
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface guard
|
|
|
|
var _ caddyhttp.RequestMatcher = (*FileMatcher)(nil)
|