Read to do some little change of webdav in golang.org/x/net

This commit is contained in:
zicla
2019-04-14 01:55:15 +08:00
parent e9efc70f7c
commit ec60a96172
23 changed files with 8006 additions and 166 deletions

View File

@ -1,10 +1,5 @@
package rest
import (
"net/http"
"os"
"tank/rest/dav"
)
//@Service
type DavService struct {
@ -22,97 +17,3 @@ func (this *DavService) Init() {
this.matterDao = b
}
}
const (
infiniteDepth = -1
invalidDepth = -2
)
// parseDepth maps the strings "0", "1" and "infinity" to 0, 1 and
// infiniteDepth. Parsing any other string returns invalidDepth.
//
// Different WebDAV methods have further constraints on valid depths:
// - PROPFIND has no further restrictions, as per section 9.1.
// - COPY accepts only "0" or "infinity", as per section 9.8.3.
// - MOVE accepts only "infinity", as per section 9.9.2.
// - LOCK accepts only "0" or "infinity", as per section 9.10.3.
// These constraints are enforced by the handleXxx methods.
func parseDepth(s string) int {
switch s {
case "0":
return 0
case "1":
return 1
case "infinity":
return infiniteDepth
}
return invalidDepth
}
//处理 方法
func (this *DavService) HandlePropfind(w http.ResponseWriter, r *http.Request) {
basePath := "/Users/fusu/d/group/golang/src/tank/tmp/dav"
fileSystem := dav.Dir("/Users/fusu/d/group/golang/src/tank/tmp/dav")
lockSystem := dav.NewMemLS()
reqPath := r.URL.Path
ctx := r.Context()
fi, err := os.Stat(basePath + reqPath)
if err != nil {
this.PanicError(err)
}
depth := infiniteDepth
if hdr := r.Header.Get("Depth"); hdr != "" {
depth = parseDepth(hdr)
if depth == invalidDepth {
this.PanicBadRequest("Depth指定错误")
}
}
pf, _, err := dav.ReadPropfind(r.Body)
this.PanicError(err)
mw := dav.MultiStatusWriter{Writer: w}
walkFn := func(reqPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
var pstats []dav.Propstat
if pf.Propname != nil {
pnames, err := dav.Propnames(ctx, fileSystem, lockSystem, reqPath)
if err != nil {
return err
}
pstat := dav.Propstat{Status: http.StatusOK}
for _, xmlname := range pnames {
pstat.Props = append(pstat.Props, dav.Property{XMLName: xmlname})
}
pstats = append(pstats, pstat)
} else if pf.Allprop != nil {
pstats, err = dav.Allprop(ctx, fileSystem, lockSystem, reqPath, pf.Prop)
} else {
pstats, err = dav.Props(ctx, fileSystem, lockSystem, reqPath, pf.Prop)
}
if err != nil {
return err
}
href := reqPath
if info.IsDir() {
href += "/"
}
return mw.Write(dav.MakePropstatResponse(href, pstats))
}
walkErr := dav.WalkFS(ctx, fileSystem, depth, reqPath, fi, walkFn)
closeErr := mw.Close()
this.PanicError(walkErr)
this.PanicError(closeErr)
}