mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
Improve OPTIONS handling
This commit is contained in:
parent
f4c21ca352
commit
e2da5769f5
@ -1,10 +1,11 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime"
|
"mime"
|
||||||
"encoding/xml"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPError struct {
|
type HTTPError struct {
|
||||||
@ -26,6 +27,7 @@ func (err *HTTPError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Backend interface {
|
type Backend interface {
|
||||||
|
Options(r *http.Request) ([]string, error)
|
||||||
HeadGet(w http.ResponseWriter, r *http.Request) error
|
HeadGet(w http.ResponseWriter, r *http.Request) error
|
||||||
Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error)
|
Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error)
|
||||||
}
|
}
|
||||||
@ -61,7 +63,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) error {
|
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) error {
|
||||||
w.Header().Add("Allow", "OPTIONS, GET, HEAD, PROPFIND")
|
methods, err := h.Backend.Options(r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Add("Allow", strings.Join(methods, ", "))
|
||||||
w.Header().Add("DAV", "1, 3")
|
w.Header().Add("DAV", "1, 3")
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return nil
|
return nil
|
||||||
|
21
server.go
21
server.go
@ -37,6 +37,27 @@ type backend struct {
|
|||||||
FileSystem FileSystem
|
FileSystem FileSystem
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *backend) Options(r *http.Request) ([]string, error) {
|
||||||
|
f, err := b.FileSystem.Open(r.URL.Path)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return []string{http.MethodOptions}, nil
|
||||||
|
} else if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
fi, err := f.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.IsDir() {
|
||||||
|
return []string{http.MethodOptions, "PROPFIND"}, nil
|
||||||
|
} else {
|
||||||
|
return []string{http.MethodOptions, http.MethodHead, http.MethodGet, "PROPFIND"}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
|
func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
|
||||||
f, err := b.FileSystem.Open(r.URL.Path)
|
f, err := b.FileSystem.Open(r.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user