mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-24 04:01:54 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f6a0707fe6 | ||
|
|
947b163ea7 | ||
|
|
732cf5eff5 |
@@ -157,7 +157,7 @@ location / {
|
|||||||
|
|
||||||
Example configuration of a [`systemd`](https://en.wikipedia.org/wiki/Systemd) service:
|
Example configuration of a [`systemd`](https://en.wikipedia.org/wiki/Systemd) service:
|
||||||
|
|
||||||
```toml
|
```conf
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=WebDAV
|
Description=WebDAV
|
||||||
After=network.target
|
After=network.target
|
||||||
|
|||||||
+28
-1
@@ -133,7 +133,7 @@ username = "basic"
|
|||||||
password = "basic"
|
password = "basic"
|
||||||
scope = "/basic"
|
scope = "/basic"
|
||||||
modify = false
|
modify = false
|
||||||
rules = [ ]
|
rules = []
|
||||||
`
|
`
|
||||||
|
|
||||||
cfg := writeAndParseConfig(t, content, ".toml")
|
cfg := writeAndParseConfig(t, content, ".toml")
|
||||||
@@ -168,3 +168,30 @@ cors:
|
|||||||
require.EqualValues(t, []string{"http://localhost:8080"}, cfg.CORS.AllowedHosts)
|
require.EqualValues(t, []string{"http://localhost:8080"}, cfg.CORS.AllowedHosts)
|
||||||
require.EqualValues(t, []string{"GET"}, cfg.CORS.AllowedMethods)
|
require.EqualValues(t, []string{"GET"}, cfg.CORS.AllowedMethods)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigRules(t *testing.T) {
|
||||||
|
content := `
|
||||||
|
auth: false
|
||||||
|
scope: /
|
||||||
|
modify: true
|
||||||
|
rules:
|
||||||
|
- path: '^.+\.js$'
|
||||||
|
regex: true
|
||||||
|
modify: true
|
||||||
|
- path: /public/access/
|
||||||
|
regex: false
|
||||||
|
modify: true`
|
||||||
|
|
||||||
|
cfg := writeAndParseConfig(t, content, ".yaml")
|
||||||
|
require.NoError(t, cfg.Validate())
|
||||||
|
|
||||||
|
require.Len(t, cfg.Rules, 2)
|
||||||
|
|
||||||
|
require.Empty(t, cfg.Rules[0].Path)
|
||||||
|
require.NotNil(t, cfg.Rules[0].Regexp)
|
||||||
|
require.True(t, cfg.Rules[0].Regexp.MatchString("/my/path/to/file.js"))
|
||||||
|
require.False(t, cfg.Rules[0].Regexp.MatchString("/my/path/to/file.ts"))
|
||||||
|
|
||||||
|
require.NotEmpty(t, cfg.Rules[1].Path)
|
||||||
|
require.Nil(t, cfg.Rules[1].Regexp)
|
||||||
|
}
|
||||||
|
|||||||
+9
-1
@@ -106,7 +106,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if r.Method == "HEAD" {
|
if r.Method == "HEAD" {
|
||||||
w = newResponseWriterNoBody(w)
|
w = responseWriterNoBody{w}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Excerpt from RFC4918, section 9.4:
|
// Excerpt from RFC4918, section 9.4:
|
||||||
@@ -130,3 +130,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Runs the WebDAV.
|
// Runs the WebDAV.
|
||||||
user.ServeHTTP(w, r)
|
user.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type responseWriterNoBody struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w responseWriterNoBody) Write(data []byte) (int, error) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|||||||
+2
-1
@@ -31,6 +31,7 @@ func (r *Rule) Validate() error {
|
|||||||
}
|
}
|
||||||
r.Regexp = rp
|
r.Regexp = rp
|
||||||
r.Path = ""
|
r.Path = ""
|
||||||
|
r.Regex = false
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -38,7 +39,7 @@ func (r *Rule) Validate() error {
|
|||||||
|
|
||||||
// Matches checks if [Rule] matches the given path.
|
// Matches checks if [Rule] matches the given path.
|
||||||
func (r *Rule) Matches(path string) bool {
|
func (r *Rule) Matches(path string) bool {
|
||||||
if r.Regex {
|
if r.Regexp != nil {
|
||||||
return r.Regexp.MatchString(path)
|
return r.Regexp.MatchString(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
package lib
|
|
||||||
|
|
||||||
import "net/http"
|
|
||||||
|
|
||||||
var _ http.ResponseWriter = responseWriterNoBody{}
|
|
||||||
|
|
||||||
// responseWriterNoBody is a wrapper used to suppress the body of the response
|
|
||||||
// to a request. Mainly used for HEAD requests.
|
|
||||||
type responseWriterNoBody struct {
|
|
||||||
http.ResponseWriter
|
|
||||||
}
|
|
||||||
|
|
||||||
// newResponseWriterNoBody creates a new responseWriterNoBody.
|
|
||||||
func newResponseWriterNoBody(w http.ResponseWriter) *responseWriterNoBody {
|
|
||||||
return &responseWriterNoBody{w}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write suppress the body.
|
|
||||||
func (w responseWriterNoBody) Write(data []byte) (int, error) {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteHeader writes the header to the http.ResponseWriter.
|
|
||||||
func (w responseWriterNoBody) WriteHeader(statusCode int) {
|
|
||||||
w.Header().Del("Content-Length")
|
|
||||||
w.ResponseWriter.WriteHeader(statusCode)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user