internal: add Handler

This commit is contained in:
Simon Ser 2020-01-17 11:30:42 +01:00
parent 3beeb23f7c
commit 326c4b9b6f
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 138 additions and 100 deletions

View File

@ -6,17 +6,19 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/emersion/go-webdav/internal"
) )
type LocalFileSystem string type LocalFileSystem string
func (fs LocalFileSystem) path(name string) (string, error) { func (fs LocalFileSystem) path(name string) (string, error) {
if (filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0) || strings.Contains(name, "\x00") { if (filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0) || strings.Contains(name, "\x00") {
return "", HTTPErrorf(http.StatusBadRequest, "webdav: invalid character in path") return "", internal.HTTPErrorf(http.StatusBadRequest, "webdav: invalid character in path")
} }
name = path.Clean(name) name = path.Clean(name)
if !path.IsAbs(name) { if !path.IsAbs(name) {
return "", HTTPErrorf(http.StatusBadRequest, "webdav: expected absolute path") return "", internal.HTTPErrorf(http.StatusBadRequest, "webdav: expected absolute path")
} }
return filepath.Join(string(fs), filepath.FromSlash(name)), nil return filepath.Join(string(fs), filepath.FromSlash(name)), nil
} }

99
internal/server.go Normal file
View File

@ -0,0 +1,99 @@
package internal
import (
"net/http"
"fmt"
"mime"
"encoding/xml"
)
type HTTPError struct {
Code int
Err error
}
func HTTPErrorf(code int, format string, a ...interface{}) *HTTPError {
return &HTTPError{code, fmt.Errorf(format, a...)}
}
func (err *HTTPError) Error() string {
s := fmt.Sprintf("%v %v", err.Code, http.StatusText(err.Code))
if err.Err != nil {
return fmt.Sprintf("%v: %v", s, err.Err)
} else {
return s
}
}
type Backend interface {
HeadGet(w http.ResponseWriter, r *http.Request) error
Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error)
}
type Handler struct {
Backend Backend
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
if h.Backend == nil {
err = fmt.Errorf("webdav: no backend available")
} else {
switch r.Method {
case http.MethodOptions:
err = h.handleOptions(w, r)
case http.MethodGet, http.MethodHead:
err = h.Backend.HeadGet(w, r)
case "PROPFIND":
err = h.handlePropfind(w, r)
default:
err = HTTPErrorf(http.StatusMethodNotAllowed, "webdav: unsupported method")
}
}
if err != nil {
code := http.StatusInternalServerError
if httpErr, ok := err.(*HTTPError); ok {
code = httpErr.Code
}
http.Error(w, err.Error(), code)
}
}
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) error {
w.Header().Add("Allow", "OPTIONS, GET, HEAD, PROPFIND")
w.Header().Add("DAV", "1, 3")
w.WriteHeader(http.StatusNoContent)
return nil
}
func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) error {
t, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if t != "application/xml" && t != "text/xml" {
return HTTPErrorf(http.StatusBadRequest, "webdav: expected application/xml PROPFIND request")
}
var propfind Propfind
if err := xml.NewDecoder(r.Body).Decode(&propfind); err != nil {
return &HTTPError{http.StatusBadRequest, err}
}
depth := DepthInfinity
if s := r.Header.Get("Depth"); s != "" {
var err error
depth, err = ParseDepth(s)
if err != nil {
return &HTTPError{http.StatusBadRequest, err}
}
}
ms, err := h.Backend.Propfind(r, &propfind, depth)
if err != nil {
return err
}
w.Header().Add("Content-Type", "text/xml; charset=\"utf-8\"")
w.WriteHeader(http.StatusMultiStatus)
w.Write([]byte(xml.Header))
return xml.NewEncoder(w).Encode(&ms)
}

133
server.go
View File

@ -2,7 +2,6 @@ package webdav
import ( import (
"encoding/xml" "encoding/xml"
"fmt"
"mime" "mime"
"net/http" "net/http"
"os" "os"
@ -11,24 +10,6 @@ import (
"github.com/emersion/go-webdav/internal" "github.com/emersion/go-webdav/internal"
) )
type HTTPError struct {
Code int
Err error
}
func HTTPErrorf(code int, format string, a ...interface{}) *HTTPError {
return &HTTPError{code, fmt.Errorf(format, a...)}
}
func (err *HTTPError) Error() string {
s := fmt.Sprintf("%v %v", err.Code, http.StatusText(err.Code))
if err.Err != nil {
return fmt.Sprintf("%v: %v", s, err.Err)
} else {
return s
}
}
type File interface { type File interface {
http.File http.File
} }
@ -42,40 +23,22 @@ type Handler struct {
} }
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
if h.FileSystem == nil { if h.FileSystem == nil {
err = HTTPErrorf(http.StatusInternalServerError, "webdav: no filesystem available") http.Error(w, "webdav: no filesystem available", http.StatusInternalServerError)
} else { return
switch r.Method {
case http.MethodOptions:
err = h.handleOptions(w, r)
case http.MethodGet, http.MethodHead:
err = h.handleGetHead(w, r)
case "PROPFIND":
err = h.handlePropfind(w, r)
default:
err = HTTPErrorf(http.StatusMethodNotAllowed, "webdav: unsupported method")
}
} }
if err != nil { b := backend{h.FileSystem}
code := http.StatusInternalServerError hh := internal.Handler{&b}
if httpErr, ok := err.(*HTTPError); ok { hh.ServeHTTP(w, r)
code = httpErr.Code
}
http.Error(w, err.Error(), code)
}
} }
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) error { type backend struct {
w.Header().Add("Allow", "OPTIONS, GET, HEAD, PROPFIND") FileSystem FileSystem
w.Header().Add("DAV", "1, 3")
w.WriteHeader(http.StatusNoContent)
return nil
} }
func (h *Handler) handleGetHead(w http.ResponseWriter, r *http.Request) error { func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
f, err := h.FileSystem.Open(r.URL.Path) f, err := b.FileSystem.Open(r.URL.Path)
if err != nil { if err != nil {
return err return err
} }
@ -90,56 +53,30 @@ func (h *Handler) handleGetHead(w http.ResponseWriter, r *http.Request) error {
return nil return nil
} }
func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) error { func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) {
t, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) f, err := b.FileSystem.Open(r.URL.Path)
if t != "application/xml" && t != "text/xml" {
return HTTPErrorf(http.StatusBadRequest, "webdav: expected application/xml PROPFIND request")
}
var propfind internal.Propfind
if err := xml.NewDecoder(r.Body).Decode(&propfind); err != nil {
return &HTTPError{http.StatusBadRequest, err}
}
depth := internal.DepthInfinity
if s := r.Header.Get("Depth"); s != "" {
var err error
depth, err = internal.ParseDepth(s)
if err != nil { if err != nil {
return &HTTPError{http.StatusBadRequest, err} return nil, err
}
}
// TODO: refuse DepthInfinity, can cause infinite loops with symlinks
f, err := h.FileSystem.Open(r.URL.Path)
if err != nil {
return err
} }
defer f.Close() defer f.Close()
fi, err := f.Stat() fi, err := f.Stat()
if err != nil { if err != nil {
return err return nil, err
} }
var resps []internal.Response var resps []internal.Response
if err := h.propfind(&propfind, r.URL.Path, fi, depth, &resps); err != nil { if err := b.propfind(propfind, r.URL.Path, fi, depth, &resps); err != nil {
return err return nil, err
} }
ms := internal.NewMultistatus(resps...) return internal.NewMultistatus(resps...), nil
w.Header().Add("Content-Type", "text/xml; charset=\"utf-8\"")
w.WriteHeader(http.StatusMultiStatus)
w.Write([]byte(xml.Header))
return xml.NewEncoder(w).Encode(&ms)
} }
func (h *Handler) propfind(propfind *internal.Propfind, name string, fi os.FileInfo, depth internal.Depth, resps *[]internal.Response) error { func (b *backend) propfind(propfind *internal.Propfind, name string, fi os.FileInfo, depth internal.Depth, resps *[]internal.Response) error {
// TODO: use partial error Response on error // TODO: use partial error Response on error
resp, err := h.propfindFile(propfind, name, fi) resp, err := b.propfindFile(propfind, name, fi)
if err != nil { if err != nil {
return err return err
} }
@ -151,7 +88,7 @@ func (h *Handler) propfind(propfind *internal.Propfind, name string, fi os.FileI
childDepth = internal.DepthZero childDepth = internal.DepthZero
} }
f, err := h.FileSystem.Open(name) f, err := b.FileSystem.Open(name)
if err != nil { if err != nil {
return err return err
} }
@ -163,7 +100,7 @@ func (h *Handler) propfind(propfind *internal.Propfind, name string, fi os.FileI
} }
for _, child := range children { for _, child := range children {
if err := h.propfind(propfind, path.Join(name, child.Name()), child, childDepth, resps); err != nil { if err := b.propfind(propfind, path.Join(name, child.Name()), child, childDepth, resps); err != nil {
return err return err
} }
} }
@ -172,14 +109,14 @@ func (h *Handler) propfind(propfind *internal.Propfind, name string, fi os.FileI
return nil return nil
} }
func (h *Handler) propfindFile(propfind *internal.Propfind, name string, fi os.FileInfo) (*internal.Response, error) { func (b *backend) propfindFile(propfind *internal.Propfind, name string, fi os.FileInfo) (*internal.Response, error) {
resp := internal.NewOKResponse(name) resp := internal.NewOKResponse(name)
if propfind.PropName != nil { if propfind.PropName != nil {
for xmlName, f := range liveProps { for xmlName, f := range liveProps {
emptyVal := internal.NewRawXMLElement(xmlName, nil, nil) emptyVal := internal.NewRawXMLElement(xmlName, nil, nil)
_, err := f(h, name, fi) _, err := f(b, name, fi)
if err != nil { if err != nil {
continue continue
} }
@ -191,7 +128,7 @@ func (h *Handler) propfindFile(propfind *internal.Propfind, name string, fi os.F
} else if propfind.AllProp != nil { } else if propfind.AllProp != nil {
// TODO: add support for propfind.Include // TODO: add support for propfind.Include
for _, f := range liveProps { for _, f := range liveProps {
val, err := f(h, name, fi) val, err := f(b, name, fi)
if err != nil { if err != nil {
continue continue
} }
@ -208,9 +145,9 @@ func (h *Handler) propfindFile(propfind *internal.Propfind, name string, fi os.F
var val interface{} = emptyVal var val interface{} = emptyVal
f, ok := liveProps[xmlName] f, ok := liveProps[xmlName]
if ok { if ok {
if v, err := f(h, name, fi); err != nil { if v, err := f(b, name, fi); err != nil {
// TODO: don't throw away error message here // TODO: don't throw away error message here
if httpErr, ok := err.(*HTTPError); ok { if httpErr, ok := err.(*internal.HTTPError); ok {
code = httpErr.Code code = httpErr.Code
} else { } else {
code = http.StatusInternalServerError code = http.StatusInternalServerError
@ -228,42 +165,42 @@ func (h *Handler) propfindFile(propfind *internal.Propfind, name string, fi os.F
} }
} }
} else { } else {
return nil, HTTPErrorf(http.StatusBadRequest, "webdav: propfind request missing propname, allprop or prop element") return nil, internal.HTTPErrorf(http.StatusBadRequest, "webdav: propfind request missing propname, allprop or prop element")
} }
return resp, nil return resp, nil
} }
type PropfindFunc func(h *Handler, name string, fi os.FileInfo) (interface{}, error) type PropfindFunc func(b *backend, name string, fi os.FileInfo) (interface{}, error)
var liveProps = map[xml.Name]PropfindFunc{ var liveProps = map[xml.Name]PropfindFunc{
{"DAV:", "resourcetype"}: func(h *Handler, name string, fi os.FileInfo) (interface{}, error) { {"DAV:", "resourcetype"}: func(b *backend, name string, fi os.FileInfo) (interface{}, error) {
var types []xml.Name var types []xml.Name
if fi.IsDir() { if fi.IsDir() {
types = append(types, internal.CollectionName) types = append(types, internal.CollectionName)
} }
return internal.NewResourceType(types...), nil return internal.NewResourceType(types...), nil
}, },
{"DAV:", "getcontentlength"}: func(h *Handler, name string, fi os.FileInfo) (interface{}, error) { {"DAV:", "getcontentlength"}: func(b *backend, name string, fi os.FileInfo) (interface{}, error) {
if fi.IsDir() { if fi.IsDir() {
return nil, &HTTPError{Code: http.StatusNotFound} return nil, &internal.HTTPError{Code: http.StatusNotFound}
} }
return &internal.GetContentLength{Length: fi.Size()}, nil return &internal.GetContentLength{Length: fi.Size()}, nil
}, },
{"DAV:", "getcontenttype"}: func(h *Handler, name string, fi os.FileInfo) (interface{}, error) { {"DAV:", "getcontenttype"}: func(b *backend, name string, fi os.FileInfo) (interface{}, error) {
if fi.IsDir() { if fi.IsDir() {
return nil, &HTTPError{Code: http.StatusNotFound} return nil, &internal.HTTPError{Code: http.StatusNotFound}
} }
t := mime.TypeByExtension(path.Ext(name)) t := mime.TypeByExtension(path.Ext(name))
if t == "" { if t == "" {
// TODO: use http.DetectContentType // TODO: use http.DetectContentType
return nil, &HTTPError{Code: http.StatusNotFound} return nil, &internal.HTTPError{Code: http.StatusNotFound}
} }
return &internal.GetContentType{Type: t}, nil return &internal.GetContentType{Type: t}, nil
}, },
{"DAV:", "getlastmodified"}: func(h *Handler, name string, fi os.FileInfo) (interface{}, error) { {"DAV:", "getlastmodified"}: func(b *backend, name string, fi os.FileInfo) (interface{}, error) {
if fi.IsDir() { if fi.IsDir() {
return nil, &HTTPError{Code: http.StatusNotFound} return nil, &internal.HTTPError{Code: http.StatusNotFound}
} }
return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime())}, nil return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime())}, nil
}, },