summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/staticfiles/matcher.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-05-20 10:59:20 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2019-05-20 10:59:20 -0600
commitfec7fa8bfda713e8042b9bbf9a480c7792b78c41 (patch)
tree53d86ab50ef7d15e9688c81b6618024c4243c98d /modules/caddyhttp/staticfiles/matcher.go
parent1a20fe330ecc39e8b98b5669b836f3b1b185f622 (diff)
Implement most of static file server; refactor and improve Replacer
Diffstat (limited to 'modules/caddyhttp/staticfiles/matcher.go')
-rw-r--r--modules/caddyhttp/staticfiles/matcher.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/modules/caddyhttp/staticfiles/matcher.go b/modules/caddyhttp/staticfiles/matcher.go
new file mode 100644
index 0000000..cccf54b
--- /dev/null
+++ b/modules/caddyhttp/staticfiles/matcher.go
@@ -0,0 +1,54 @@
+package staticfiles
+
+import (
+ "net/http"
+ "os"
+ "path/filepath"
+
+ "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 },
+ })
+}
+
+// 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"`
+}
+
+func (m FileMatcher) Match(r *http.Request) bool {
+ // TODO: sanitize path
+ fullPath := filepath.Join(m.Root, m.Path)
+ 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)