Improve OPTIONS handling

This commit is contained in:
Simon Ser 2020-01-17 11:41:44 +01:00
parent f4c21ca352
commit e2da5769f5
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 31 additions and 3 deletions

View File

@ -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

View File

@ -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 {