mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-23 19:51:53 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f61b7cab3b | ||
|
|
3cfaa0da3e | ||
|
|
f7b78cd834 | ||
|
|
4b4e555ed5 |
+7
-3
@@ -146,8 +146,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// "index.html" resource, a human-readable view of the contents of
|
// "index.html" resource, a human-readable view of the contents of
|
||||||
// the collection, or something else altogether.
|
// the collection, or something else altogether.
|
||||||
//
|
//
|
||||||
// Get, when applied to collection, will return the same as PROPFIND method.
|
// Similarly, since the definition of HEAD is a GET without a response
|
||||||
if r.Method == "GET" && strings.HasPrefix(r.URL.Path, user.Prefix) {
|
// message body, the semantics of HEAD are unmodified when applied to
|
||||||
|
// collection resources.
|
||||||
|
//
|
||||||
|
// GET (or HEAD), when applied to collection, will return the same as PROPFIND method.
|
||||||
|
if (r.Method == "GET" || r.Method == "HEAD") && strings.HasPrefix(r.URL.Path, user.Prefix) {
|
||||||
info, err := user.FileSystem.Stat(r.Context(), strings.TrimPrefix(r.URL.Path, user.Prefix))
|
info, err := user.FileSystem.Stat(r.Context(), strings.TrimPrefix(r.URL.Path, user.Prefix))
|
||||||
if err == nil && info.IsDir() {
|
if err == nil && info.IsDir() {
|
||||||
r.Method = "PROPFIND"
|
r.Method = "PROPFIND"
|
||||||
@@ -177,5 +181,5 @@ type responseWriterNoBody struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w responseWriterNoBody) Write(data []byte) (int, error) {
|
func (w responseWriterNoBody) Write(data []byte) (int, error) {
|
||||||
return 0, nil
|
return len(data), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -292,6 +292,82 @@ users:
|
|||||||
require.ErrorContains(t, err, "403")
|
require.ErrorContains(t, err, "403")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServerRulesPrefix(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
dir := makeTestDirectory(t, map[string][]byte{
|
||||||
|
"foo.txt": []byte("foo"),
|
||||||
|
"bar.js": []byte("foo js"),
|
||||||
|
"a/foo.js": []byte("foo js"),
|
||||||
|
"a/foo.txt": []byte("foo txt"),
|
||||||
|
"b/foo.txt": []byte("foo b"),
|
||||||
|
"c/a.txt": []byte("b"),
|
||||||
|
"c/b.txt": []byte("b"),
|
||||||
|
"c/c.txt": []byte("b"),
|
||||||
|
})
|
||||||
|
|
||||||
|
srv := makeTestServer(t, fmt.Sprintf(`
|
||||||
|
directory: %s
|
||||||
|
permissions: CRUD
|
||||||
|
prefix: /prefix
|
||||||
|
|
||||||
|
users:
|
||||||
|
- username: basic
|
||||||
|
password: basic
|
||||||
|
rules:
|
||||||
|
- regex: "^.+.js$"
|
||||||
|
permissions: R
|
||||||
|
- path: "/b/"
|
||||||
|
permissions: R
|
||||||
|
- path: "/a/foo.txt"
|
||||||
|
permissions: none
|
||||||
|
- path: "/c/"
|
||||||
|
permissions: none
|
||||||
|
`, dir))
|
||||||
|
|
||||||
|
client := gowebdav.NewClient(srv.URL, "basic", "basic")
|
||||||
|
|
||||||
|
files, err := client.ReadDir("/prefix")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, files, 5)
|
||||||
|
|
||||||
|
err = client.Write("/prefix/foo.txt", []byte("new"), 0666)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = client.Write("/prefix/new.txt", []byte("new"), 0666)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = client.Copy("/prefix/bar.js", "/prefix/b/bar.js", false)
|
||||||
|
require.ErrorContains(t, err, "403")
|
||||||
|
|
||||||
|
err = client.Copy("/prefix/bar.js", "/prefix/bar.jsx", false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = client.Copy("/prefix/b/foo.txt", "/prefix/foo1.txt", false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = client.Rename("/prefix/b/foo.txt", "/prefix/foo2.txt", false)
|
||||||
|
require.ErrorContains(t, err, "403")
|
||||||
|
|
||||||
|
_, err = client.Read("/prefix/a/foo.txt")
|
||||||
|
require.ErrorContains(t, err, "403")
|
||||||
|
|
||||||
|
err = client.Write("/prefix/a/foo.js", []byte("new"), 0666)
|
||||||
|
require.ErrorContains(t, err, "403")
|
||||||
|
|
||||||
|
err = client.Write("/prefix/b/foo.txt", []byte("new"), 0666)
|
||||||
|
require.ErrorContains(t, err, "403")
|
||||||
|
|
||||||
|
_, err = client.ReadDir("/prefix/c")
|
||||||
|
require.ErrorContains(t, err, "403")
|
||||||
|
|
||||||
|
_, err = client.Read("/prefix/c/a.txt")
|
||||||
|
require.ErrorContains(t, err, "403")
|
||||||
|
|
||||||
|
err = client.Write("/prefix/c/b.txt", []byte("new"), 0666)
|
||||||
|
require.ErrorContains(t, err, "403")
|
||||||
|
}
|
||||||
|
|
||||||
func TestServerPermissions(t *testing.T) {
|
func TestServerPermissions(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user