Try to fix the xml things.
This commit is contained in:
parent
522a4f903e
commit
ba7b632046
129
main_test.go
Normal file
129
main_test.go
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"tank/rest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestXmlMarshal(t *testing.T) {
|
||||||
|
fmt.Println("Hello World!")
|
||||||
|
|
||||||
|
type Address struct {
|
||||||
|
City, State string
|
||||||
|
}
|
||||||
|
type Person struct {
|
||||||
|
XMLName xml.Name `xml:"person"`
|
||||||
|
Id int `xml:"id,attr"`
|
||||||
|
FirstName string `xml:"name>first"`
|
||||||
|
LastName string `xml:"name>last"`
|
||||||
|
Age int `xml:"age"`
|
||||||
|
Height float32 `xml:"height,omitempty"`
|
||||||
|
Married bool
|
||||||
|
Address
|
||||||
|
Comment string `xml:",comment"`
|
||||||
|
}
|
||||||
|
|
||||||
|
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
|
||||||
|
v.Comment = " Need more details. "
|
||||||
|
v.Address = Address{"Hanga Roa", "Easter Island"}
|
||||||
|
|
||||||
|
output, err := xml.MarshalIndent(v, " ", " ")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(string(output))
|
||||||
|
// Output:
|
||||||
|
// <person id="13">
|
||||||
|
// <name>
|
||||||
|
// <first>John</first>
|
||||||
|
// <last>Doe</last>
|
||||||
|
// </name>
|
||||||
|
// <age>42</age>
|
||||||
|
// <Married>false</Married>
|
||||||
|
// <City>Hanga Roa</City>
|
||||||
|
// <State>Easter Island</State>
|
||||||
|
// <!-- Need more details. -->
|
||||||
|
// </person>
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestXmlUnMarshal(t *testing.T) {
|
||||||
|
type Email struct {
|
||||||
|
Where string `xml:"where,attr"`
|
||||||
|
Addr string
|
||||||
|
}
|
||||||
|
type Address struct {
|
||||||
|
City, State string
|
||||||
|
}
|
||||||
|
type Result struct {
|
||||||
|
XMLName xml.Name `xml:"Person"`
|
||||||
|
Name string `xml:"FullName"`
|
||||||
|
Phone string
|
||||||
|
Email []Email
|
||||||
|
Groups []string `xml:"Group>Value"`
|
||||||
|
Address
|
||||||
|
}
|
||||||
|
v := Result{Name: "none", Phone: "none"}
|
||||||
|
|
||||||
|
data := `
|
||||||
|
<Person>
|
||||||
|
<FullName>Grace R. Emlin</FullName>
|
||||||
|
<Company>Example Inc.</Company>
|
||||||
|
<Email where="home">
|
||||||
|
<Addr>gre@example.com</Addr>
|
||||||
|
</Email>
|
||||||
|
<Email where='work'>
|
||||||
|
<Addr>gre@work.com</Addr>
|
||||||
|
</Email>
|
||||||
|
<Group>
|
||||||
|
<Value>Friends</Value>
|
||||||
|
<Value>Squash</Value>
|
||||||
|
</Group>
|
||||||
|
<City>Hanga Roa</City>
|
||||||
|
<State>Easter Island</State>
|
||||||
|
</Person>
|
||||||
|
`
|
||||||
|
err := xml.Unmarshal([]byte(data), &v)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("XMLName: %#v\n", v.XMLName)
|
||||||
|
fmt.Printf("Name: %q\n", v.Name)
|
||||||
|
fmt.Printf("Phone: %q\n", v.Phone)
|
||||||
|
fmt.Printf("Email: %v\n", v.Email)
|
||||||
|
fmt.Printf("Groups: %v\n", v.Groups)
|
||||||
|
fmt.Printf("Address: %v\n", v.Address)
|
||||||
|
// Output:
|
||||||
|
// XMLName: xml.Name{Space:"", Local:"Person"}
|
||||||
|
// Name: "Grace R. Emlin"
|
||||||
|
// Phone: "none"
|
||||||
|
// Email: [{home gre@example.com} {work gre@work.com}]
|
||||||
|
// Groups: [Friends Squash]
|
||||||
|
// Address: {Hanga Roa Easter Island}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPropfind(t *testing.T) {
|
||||||
|
data := `
|
||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<D:propfind xmlns:D="DAV:">
|
||||||
|
<D:prop>
|
||||||
|
<D:resourcetype />
|
||||||
|
<D:getcontentlength />
|
||||||
|
<D:creationdate />
|
||||||
|
<D:getlastmodified />
|
||||||
|
</D:prop>
|
||||||
|
</D:propfind>
|
||||||
|
`
|
||||||
|
propfind := &rest.Propfind{XmlNS: "DAV:"}
|
||||||
|
err := xml.Unmarshal([]byte(data), &propfind)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, err := xml.MarshalIndent(propfind, " ", " ")
|
||||||
|
fmt.Println(string(output))
|
||||||
|
}
|
@ -564,7 +564,9 @@ func (this *Handler) handlePropfind(writer http.ResponseWriter, request *http.Re
|
|||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
href += "/"
|
href += "/"
|
||||||
}
|
}
|
||||||
return multiStatusWriter.write(makePropstatResponse(href, propstats))
|
|
||||||
|
propstatResponse := makePropstatResponse(href, propstats)
|
||||||
|
return multiStatusWriter.write(propstatResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
walkErr := walkFS(ctx, this.FileSystem, depth, reqPath, fileInfo, walkFn)
|
walkErr := walkFS(ctx, this.FileSystem, depth, reqPath, fileInfo, walkFn)
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
ixml "golang.org/x/net/webdav/internal/xml"
|
ixml "tank/rest/dav/internal/xml"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReadLockInfo(t *testing.T) {
|
func TestReadLockInfo(t *testing.T) {
|
||||||
|
@ -10,8 +10,10 @@ import (
|
|||||||
*
|
*
|
||||||
* WebDav协议文档
|
* WebDav协议文档
|
||||||
* https://tools.ietf.org/html/rfc4918
|
* https://tools.ietf.org/html/rfc4918
|
||||||
|
* http://www.webdav.org/specs/rfc4918.html
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type DavController struct {
|
type DavController struct {
|
||||||
BaseController
|
BaseController
|
||||||
uploadTokenDao *UploadTokenDao
|
uploadTokenDao *UploadTokenDao
|
||||||
@ -90,17 +92,24 @@ func (this *DavController) HandleRoutes(writer http.ResponseWriter, request *htt
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//完成系统安装
|
//完成系统安装
|
||||||
func (this *DavController) Index(writer http.ResponseWriter, request *http.Request, subPath string) {
|
func (this *DavController) Index(writer http.ResponseWriter, request *http.Request, subPath string) {
|
||||||
|
|
||||||
this.logger.Info("请求访问来了:%s %s", request.RequestURI, subPath)
|
this.logger.Info("请求访问来了:%s %s", request.RequestURI, subPath)
|
||||||
|
|
||||||
handler := &dav.Handler{
|
method := request.Method
|
||||||
FileSystem: dav.Dir("D:/Group/Golang/src/webdav/tmp"),
|
if method == "PROPFIND1" {
|
||||||
LockSystem: dav.NewMemLS(),
|
|
||||||
|
this.davService.HandlePropfind(writer, request, subPath)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
handler := &dav.Handler{
|
||||||
|
FileSystem: dav.Dir("D:/Group/Golang/src/webdav/tmp"),
|
||||||
|
LockSystem: dav.NewMemLS(),
|
||||||
|
}
|
||||||
|
|
||||||
|
handler.ServeHTTP(writer, request)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler.ServeHTTP(writer, request)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
33
rest/dav_model.go
Normal file
33
rest/dav_model.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package rest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* WebDav协议文档
|
||||||
|
* https://tools.ietf.org/html/rfc4918
|
||||||
|
* http://www.webdav.org/specs/rfc4918.html
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
const (
|
||||||
|
//有多少层展示多少层
|
||||||
|
INFINITE_DEPTH = -1
|
||||||
|
)
|
||||||
|
|
||||||
|
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_propfind
|
||||||
|
//PROPFIND方法请求时POST BODY入参
|
||||||
|
type Propfind struct {
|
||||||
|
XMLName xml.Name `xml:"D:propfind"`
|
||||||
|
XmlNS string `xml:"xmlns:D,attr"`
|
||||||
|
|
||||||
|
Allprop *struct{} `xml:"D:allprop"`
|
||||||
|
Propname *struct{} `xml:"D:propname"`
|
||||||
|
Prop PropfindProps `xml:"D:prop"`
|
||||||
|
Include PropfindProps `xml:"D:include"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://www.webdav.org/specs/rfc4918.html#ELEMENT_prop (for propfind)
|
||||||
|
type PropfindProps []xml.Name
|
@ -1,6 +1,18 @@
|
|||||||
package rest
|
package rest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* WebDav协议文档
|
||||||
|
* https://tools.ietf.org/html/rfc4918
|
||||||
|
*
|
||||||
|
*/
|
||||||
//@Service
|
//@Service
|
||||||
type DavService struct {
|
type DavService struct {
|
||||||
Bean
|
Bean
|
||||||
@ -17,3 +29,53 @@ func (this *DavService) Init() {
|
|||||||
this.matterDao = b
|
this.matterDao = b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//从request中读取深度
|
||||||
|
func (this *DavService) readDepth(request *http.Request) int {
|
||||||
|
|
||||||
|
depth := INFINITE_DEPTH
|
||||||
|
if hdr := request.Header.Get("Depth"); hdr != "" {
|
||||||
|
if hdr == "0" {
|
||||||
|
depth = 0
|
||||||
|
} else if hdr == "1" {
|
||||||
|
depth = 1
|
||||||
|
} else if hdr == "infinity" {
|
||||||
|
depth = INFINITE_DEPTH
|
||||||
|
} else {
|
||||||
|
panic("Depth格式错误!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return depth
|
||||||
|
}
|
||||||
|
|
||||||
|
//处理 方法
|
||||||
|
func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http.Request, subPath string) {
|
||||||
|
|
||||||
|
//获取请求者
|
||||||
|
user := this.checkUser(writer, request)
|
||||||
|
|
||||||
|
//读取希望访问的深度。
|
||||||
|
depth := this.readDepth(request)
|
||||||
|
|
||||||
|
//找寻请求的目录
|
||||||
|
matter := this.matterDao.checkByUserUuidAndPath(user.Uuid, subPath)
|
||||||
|
|
||||||
|
//TODO: 读取请求参数。按照用户的参数请求返回内容。
|
||||||
|
propfind := &Propfind{}
|
||||||
|
body, err := ioutil.ReadAll(request.Body)
|
||||||
|
this.PanicError(err)
|
||||||
|
|
||||||
|
//从xml中解析内容到struct
|
||||||
|
err = xml.Unmarshal(body, &propfind)
|
||||||
|
this.PanicError(err)
|
||||||
|
|
||||||
|
//从struct还原到xml
|
||||||
|
output, err := xml.MarshalIndent(propfind, " ", " ")
|
||||||
|
this.PanicError(err)
|
||||||
|
fmt.Println(string(output))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fmt.Printf("%v %v \n", depth, matter.Name)
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -283,6 +283,26 @@ func (this *MatterDao) SizeBetweenTime(startTime time.Time, endTime time.Time) i
|
|||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//根据userUuid和path来查找
|
||||||
|
func (this *MatterDao) checkByUserUuidAndPath(userUuid string, path string) *Matter {
|
||||||
|
|
||||||
|
var wp = &WherePair{Query: "user_uuid = ? AND path = ?", Args: []interface{}{userUuid, path}}
|
||||||
|
|
||||||
|
var matter = &Matter{}
|
||||||
|
db := CONTEXT.DB.Model(&Matter{}).Where(wp.Query, wp.Args...).First(matter)
|
||||||
|
|
||||||
|
|
||||||
|
if db.Error != nil {
|
||||||
|
if db.Error.Error() == DB_ERROR_NOT_FOUND {
|
||||||
|
this.PanicNotFound("%s 不存在", path)
|
||||||
|
} else {
|
||||||
|
this.PanicError(db.Error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matter
|
||||||
|
}
|
||||||
|
|
||||||
//执行清理操作
|
//执行清理操作
|
||||||
func (this *MatterDao) Cleanup() {
|
func (this *MatterDao) Cleanup() {
|
||||||
this.logger.Info("[MatterDao]执行清理:清除数据库中所有Matter记录。删除磁盘中所有Matter文件。")
|
this.logger.Info("[MatterDao]执行清理:清除数据库中所有Matter记录。删除磁盘中所有Matter文件。")
|
||||||
|
Loading…
Reference in New Issue
Block a user