Compare commits

..
3 Commits
Author SHA1 Message Date
Henrique Dias f6a0707fe6 refactor: shorten response writer code 2024-07-22 22:28:56 +02:00
Henrique Dias 947b163ea7 fix: rules parsing 2024-07-22 22:25:50 +02:00
Henrique Dias 732cf5eff5 docs: fix readme highlighting 2024-07-22 19:22:25 +02:00
5 changed files with 40 additions and 31 deletions
+1 -1
View File
@@ -157,7 +157,7 @@ location / {
Example configuration of a [`systemd`](https://en.wikipedia.org/wiki/Systemd) service:
```toml
```conf
[Unit]
Description=WebDAV
After=network.target
+28 -1
View File
@@ -133,7 +133,7 @@ username = "basic"
password = "basic"
scope = "/basic"
modify = false
rules = [ ]
rules = []
`
cfg := writeAndParseConfig(t, content, ".toml")
@@ -168,3 +168,30 @@ cors:
require.EqualValues(t, []string{"http://localhost:8080"}, cfg.CORS.AllowedHosts)
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
View File
@@ -106,7 +106,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if r.Method == "HEAD" {
w = newResponseWriterNoBody(w)
w = responseWriterNoBody{w}
}
// Excerpt from RFC4918, section 9.4:
@@ -130,3 +130,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Runs the WebDAV.
user.ServeHTTP(w, r)
}
type responseWriterNoBody struct {
http.ResponseWriter
}
func (w responseWriterNoBody) Write(data []byte) (int, error) {
return 0, nil
}
+2 -1
View File
@@ -31,6 +31,7 @@ func (r *Rule) Validate() error {
}
r.Regexp = rp
r.Path = ""
r.Regex = false
}
return nil
@@ -38,7 +39,7 @@ func (r *Rule) Validate() error {
// Matches checks if [Rule] matches the given path.
func (r *Rule) Matches(path string) bool {
if r.Regex {
if r.Regexp != nil {
return r.Regexp.MatchString(path)
}
-27
View File
@@ -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)
}