Move dav to tool dir.

This commit is contained in:
zicla 2019-04-26 03:11:15 +08:00
parent c5de9ce9eb
commit 16749d1fa1
8 changed files with 42 additions and 42 deletions

View File

@ -5,8 +5,8 @@ import (
"net/http" "net/http"
"path" "path"
"strconv" "strconv"
"tank/code/dav" dav2 "tank/code/tool/dav"
"tank/code/dav/xml" xml2 "tank/code/tool/dav/xml"
) )
//访问前缀,这个是特殊入口 //访问前缀,这个是特殊入口
@ -19,7 +19,7 @@ type LiveProp struct {
} }
//所有的动态属性定义及其值的获取方式 //所有的动态属性定义及其值的获取方式
var LivePropMap = map[xml.Name]LiveProp{ var LivePropMap = map[xml2.Name]LiveProp{
{Space: "DAV:", Local: "resourcetype"}: { {Space: "DAV:", Local: "resourcetype"}: {
findFn: func(user *User, matter *Matter) string { findFn: func(user *User, matter *Matter) string {
if matter.Dir { if matter.Dir {
@ -35,7 +35,7 @@ var LivePropMap = map[xml.Name]LiveProp{
if path.Clean("/"+matter.Name) == "/" { if path.Clean("/"+matter.Name) == "/" {
return "" return ""
} else { } else {
return dav.EscapeXML(matter.Name) return dav2.EscapeXML(matter.Name)
} }
}, },
dir: true, dir: true,
@ -72,7 +72,7 @@ var LivePropMap = map[xml.Name]LiveProp{
if matter.Dir { if matter.Dir {
return "" return ""
} else { } else {
return dav.EscapeXML(matter.Name) return dav2.EscapeXML(matter.Name)
} }
}, },
dir: false, dir: false,

View File

@ -7,9 +7,9 @@ import (
"path" "path"
"regexp" "regexp"
"strings" "strings"
"tank/code/dav"
"tank/code/dav/xml"
"tank/code/result" "tank/code/result"
dav2 "tank/code/tool/dav"
xml2 "tank/code/tool/dav/xml"
"tank/code/tool/util" "tank/code/tool/util"
) )
@ -63,18 +63,18 @@ func (this *DavService) ParseDepth(request *http.Request) int {
return depth return depth
} }
func (this *DavService) makePropstatResponse(href string, pstats []dav.Propstat) *dav.Response { func (this *DavService) makePropstatResponse(href string, pstats []dav2.Propstat) *dav2.Response {
resp := dav.Response{ resp := dav2.Response{
Href: []string{(&url.URL{Path: href}).EscapedPath()}, Href: []string{(&url.URL{Path: href}).EscapedPath()},
Propstat: make([]dav.SubPropstat, 0, len(pstats)), Propstat: make([]dav2.SubPropstat, 0, len(pstats)),
} }
for _, p := range pstats { for _, p := range pstats {
var xmlErr *dav.XmlError var xmlErr *dav2.XmlError
if p.XMLError != "" { if p.XMLError != "" {
xmlErr = &dav.XmlError{InnerXML: []byte(p.XMLError)} xmlErr = &dav2.XmlError{InnerXML: []byte(p.XMLError)}
} }
resp.Propstat = append(resp.Propstat, dav.SubPropstat{ resp.Propstat = append(resp.Propstat, dav2.SubPropstat{
Status: fmt.Sprintf("HTTP/1.1 %d %s", p.Status, dav.StatusText(p.Status)), Status: fmt.Sprintf("HTTP/1.1 %d %s", p.Status, dav2.StatusText(p.Status)),
Prop: p.Props, Prop: p.Props,
ResponseDescription: p.ResponseDescription, ResponseDescription: p.ResponseDescription,
Error: xmlErr, Error: xmlErr,
@ -84,11 +84,11 @@ func (this *DavService) makePropstatResponse(href string, pstats []dav.Propstat)
} }
//从一个matter中获取其 []dav.Propstat //从一个matter中获取其 []dav.Propstat
func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNames []xml.Name) []dav.Propstat { func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNames []xml2.Name) []dav2.Propstat {
propstats := make([]dav.Propstat, 0) propstats := make([]dav2.Propstat, 0)
var properties []dav.Property var properties []dav2.Property
for _, xmlName := range xmlNames { for _, xmlName := range xmlNames {
//TODO: deadprops尚未考虑 //TODO: deadprops尚未考虑
@ -97,7 +97,7 @@ func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNam
if liveProp := LivePropMap[xmlName]; liveProp.findFn != nil && (liveProp.dir || !matter.Dir) { if liveProp := LivePropMap[xmlName]; liveProp.findFn != nil && (liveProp.dir || !matter.Dir) {
innerXML := liveProp.findFn(user, matter) innerXML := liveProp.findFn(user, matter)
properties = append(properties, dav.Property{ properties = append(properties, dav2.Property{
XMLName: xmlName, XMLName: xmlName,
InnerXML: []byte(innerXML), InnerXML: []byte(innerXML),
}) })
@ -110,7 +110,7 @@ func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNam
panic(result.BadRequest("请求的属性项无法解析!")) panic(result.BadRequest("请求的属性项无法解析!"))
} }
okPropstat := dav.Propstat{Status: http.StatusOK, Props: properties} okPropstat := dav2.Propstat{Status: http.StatusOK, Props: properties}
propstats = append(propstats, okPropstat) propstats = append(propstats, okPropstat)
@ -119,9 +119,9 @@ func (this *DavService) PropstatsFromXmlNames(user *User, matter *Matter, xmlNam
} }
//从一个matter中获取所有的propsNames //从一个matter中获取所有的propsNames
func (this *DavService) AllPropXmlNames(matter *Matter) []xml.Name { func (this *DavService) AllPropXmlNames(matter *Matter) []xml2.Name {
pnames := make([]xml.Name, 0) pnames := make([]xml2.Name, 0)
for pn, prop := range LivePropMap { for pn, prop := range LivePropMap {
if prop.findFn != nil && (prop.dir || !matter.Dir) { if prop.findFn != nil && (prop.dir || !matter.Dir) {
pnames = append(pnames, pn) pnames = append(pnames, pn)
@ -132,9 +132,9 @@ func (this *DavService) AllPropXmlNames(matter *Matter) []xml.Name {
} }
//从一个matter中获取其 []dav.Propstat //从一个matter中获取其 []dav.Propstat
func (this *DavService) Propstats(user *User, matter *Matter, propfind *dav.Propfind) []dav.Propstat { func (this *DavService) Propstats(user *User, matter *Matter, propfind *dav2.Propfind) []dav2.Propstat {
propstats := make([]dav.Propstat, 0) propstats := make([]dav2.Propstat, 0)
if propfind.Propname != nil { if propfind.Propname != nil {
panic(result.BadRequest("propfind.Propname != nil 尚未处理")) panic(result.BadRequest("propfind.Propname != nil 尚未处理"))
} else if propfind.Allprop != nil { } else if propfind.Allprop != nil {
@ -161,7 +161,7 @@ func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http
depth := this.ParseDepth(request) depth := this.ParseDepth(request)
//读取请求参数。按照用户的参数请求返回内容。 //读取请求参数。按照用户的参数请求返回内容。
propfind := dav.ReadPropfind(request.Body) propfind := dav2.ReadPropfind(request.Body)
//寻找符合条件的matter. //寻找符合条件的matter.
//如果是空或者/就是请求根目录 //如果是空或者/就是请求根目录
@ -179,7 +179,7 @@ func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http
} }
//准备一个输出结果的Writer //准备一个输出结果的Writer
multiStatusWriter := &dav.MultiStatusWriter{Writer: writer} multiStatusWriter := &dav2.MultiStatusWriter{Writer: writer}
for _, matter := range matters { for _, matter := range matters {

View File

@ -2,15 +2,15 @@ package test
import ( import (
"bytes" "bytes"
"tank/code/dav" dav2 "tank/code/tool/dav"
"tank/code/dav/xml" xml2 "tank/code/tool/dav/xml"
"testing" "testing"
"time" "time"
) )
func TestXmlDecoder(t *testing.T) { func TestXmlDecoder(t *testing.T) {
propfind := &dav.Propfind{} propfind := &dav2.Propfind{}
str := ` str := `
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
@ -26,7 +26,7 @@ func TestXmlDecoder(t *testing.T) {
reader := bytes.NewReader([]byte(str)) reader := bytes.NewReader([]byte(str))
err := xml.NewDecoder(reader).Decode(propfind) err := xml2.NewDecoder(reader).Decode(propfind)
if err != nil { if err != nil {
t.Error(err.Error()) t.Error(err.Error())
} }
@ -58,18 +58,18 @@ func TestXmlEncoder(t *testing.T) {
writer := &bytes.Buffer{} writer := &bytes.Buffer{}
response := &dav.Response{ response := &dav2.Response{
XMLName: xml.Name{Space: "DAV:", Local: "response"}, XMLName: xml2.Name{Space: "DAV:", Local: "response"},
Href: []string{"/api/dav"}, Href: []string{"/api/dav"},
Propstat: []dav.SubPropstat{ Propstat: []dav2.SubPropstat{
{ {
Prop: []dav.Property{ Prop: []dav2.Property{
{ {
XMLName: xml.Name{Space: "DAV:", Local: "resourcetype"}, XMLName: xml2.Name{Space: "DAV:", Local: "resourcetype"},
InnerXML: []byte(`<D:collection xmlns:D="DAV:"/>`), InnerXML: []byte(`<D:collection xmlns:D="DAV:"/>`),
}, },
{ {
XMLName: xml.Name{Space: "DAV:", Local: "getlastmodified"}, XMLName: xml2.Name{Space: "DAV:", Local: "getlastmodified"},
InnerXML: []byte(`Mon, 22 Apr 2019 06:38:36 GMT`), InnerXML: []byte(`Mon, 22 Apr 2019 06:38:36 GMT`),
}, },
}, },
@ -78,7 +78,7 @@ func TestXmlEncoder(t *testing.T) {
}, },
} }
err := xml.NewEncoder(writer).Encode(response) err := xml2.NewEncoder(writer).Encode(response)
if err != nil { if err != nil {
t.Error(err.Error()) t.Error(err.Error())

View File

@ -10,8 +10,8 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"tank/code/dav/xml"
"tank/code/result" "tank/code/result"
"tank/code/tool/dav/xml"
) )
// Proppatch describes a property update instruction as defined in RFC 4918. // Proppatch describes a property update instruction as defined in RFC 4918.
@ -135,7 +135,7 @@ var (
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_lockinfo // http://www.webdav.org/specs/rfc4918.html#ELEMENT_lockinfo
type LockInfo struct { type LockInfo struct {
XMLName xml.Name `xml:"lockinfo"` XMLName xml.Name `xml:"lockinfo"`
Exclusive *struct{} `xml:"lockscope>exclusive"` Exclusive *struct{} `xml:"lockscope>exclusive"`
Shared *struct{} `xml:"lockscope>shared"` Shared *struct{} `xml:"lockscope>shared"`
Write *struct{} `xml:"locktype>write"` Write *struct{} `xml:"locktype>write"`
@ -217,7 +217,7 @@ func (pn *PropfindProps) UnmarshalXML(d *xml.Decoder, start xml.StartElement) er
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_propfind // http://www.webdav.org/specs/rfc4918.html#ELEMENT_propfind
// <!ELEMENT propfind ( propname | (allprop, include?) | prop ) > // <!ELEMENT propfind ( propname | (allprop, include?) | prop ) >
type Propfind struct { type Propfind struct {
XMLName xml.Name `xml:"DAV: propfind"` XMLName xml.Name `xml:"DAV: propfind"`
Allprop *struct{} `xml:"DAV: allprop"` Allprop *struct{} `xml:"DAV: allprop"`
Propname *struct{} `xml:"DAV: propname"` Propname *struct{} `xml:"DAV: propname"`
Prop PropfindProps `xml:"DAV: prop"` Prop PropfindProps `xml:"DAV: prop"`
@ -290,7 +290,7 @@ type IxmlProperty struct {
// See MultiStatusWriter for the "D:" namespace prefix. // See MultiStatusWriter for the "D:" namespace prefix.
type XmlError struct { type XmlError struct {
XMLName xml.Name `xml:"D:error"` XMLName xml.Name `xml:"D:error"`
InnerXML []byte `xml:",innerxml"` InnerXML []byte `xml:",innerxml"`
} }
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_propstat // http://www.webdav.org/specs/rfc4918.html#ELEMENT_propstat
@ -343,7 +343,7 @@ func (ps SubPropstat) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_response // http://www.webdav.org/specs/rfc4918.html#ELEMENT_response
// See MultiStatusWriter for the "D:" namespace prefix. // See MultiStatusWriter for the "D:" namespace prefix.
type Response struct { type Response struct {
XMLName xml.Name `xml:"D:response"` XMLName xml.Name `xml:"D:response"`
Href []string `xml:"D:href"` Href []string `xml:"D:href"`
Propstat []SubPropstat `xml:"D:propstat"` Propstat []SubPropstat `xml:"D:propstat"`
Status string `xml:"D:status,omitempty"` Status string `xml:"D:status,omitempty"`
@ -542,7 +542,7 @@ type SetRemove struct {
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_propertyupdate // http://www.webdav.org/specs/rfc4918.html#ELEMENT_propertyupdate
type PropertyUpdate struct { type PropertyUpdate struct {
XMLName xml.Name `xml:"DAV: propertyupdate"` XMLName xml.Name `xml:"DAV: propertyupdate"`
Lang string `xml:"xml:lang,attr,omitempty"` Lang string `xml:"xml:lang,attr,omitempty"`
SetRemove []SetRemove `xml:",any"` SetRemove []SetRemove `xml:",any"`
} }