xml.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package webdav
  5. // The XML encoding is covered by Section 14.
  6. // http://www.webdav.org/specs/rfc4918.html#xml.element.definitions
  7. import (
  8. "bytes"
  9. "encoding/xml"
  10. "fmt"
  11. "io"
  12. "net/http"
  13. "time"
  14. )
  15. // http://www.webdav.org/specs/rfc4918.html#ELEMENT_lockinfo
  16. type lockInfo struct {
  17. XMLName xml.Name `xml:"lockinfo"`
  18. Exclusive *struct{} `xml:"lockscope>exclusive"`
  19. Shared *struct{} `xml:"lockscope>shared"`
  20. Write *struct{} `xml:"locktype>write"`
  21. Owner owner `xml:"owner"`
  22. }
  23. // http://www.webdav.org/specs/rfc4918.html#ELEMENT_owner
  24. type owner struct {
  25. InnerXML string `xml:",innerxml"`
  26. }
  27. func readLockInfo(r io.Reader) (li lockInfo, status int, err error) {
  28. c := &countingReader{r: r}
  29. if err = xml.NewDecoder(c).Decode(&li); err != nil {
  30. if err == io.EOF {
  31. if c.n == 0 {
  32. // An empty body means to refresh the lock.
  33. // http://www.webdav.org/specs/rfc4918.html#refreshing-locks
  34. return lockInfo{}, 0, nil
  35. }
  36. err = errInvalidLockInfo
  37. }
  38. return lockInfo{}, http.StatusBadRequest, err
  39. }
  40. // We only support exclusive (non-shared) write locks. In practice, these are
  41. // the only types of locks that seem to matter.
  42. if li.Exclusive == nil || li.Shared != nil || li.Write == nil {
  43. return lockInfo{}, http.StatusNotImplemented, errUnsupportedLockInfo
  44. }
  45. return li, 0, nil
  46. }
  47. type countingReader struct {
  48. n int
  49. r io.Reader
  50. }
  51. func (c *countingReader) Read(p []byte) (int, error) {
  52. n, err := c.r.Read(p)
  53. c.n += n
  54. return n, err
  55. }
  56. func writeLockInfo(w io.Writer, token string, ld LockDetails) (int, error) {
  57. depth := "infinity"
  58. if ld.ZeroDepth {
  59. depth = "0"
  60. }
  61. timeout := ld.Duration / time.Second
  62. return fmt.Fprintf(w, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
  63. "<D:prop xmlns:D=\"DAV:\"><D:lockdiscovery><D:activelock>\n"+
  64. " <D:locktype><D:write/></D:locktype>\n"+
  65. " <D:lockscope><D:exclusive/></D:lockscope>\n"+
  66. " <D:depth>%s</D:depth>\n"+
  67. " <D:owner>%s</D:owner>\n"+
  68. " <D:timeout>Second-%d</D:timeout>\n"+
  69. " <D:locktoken><D:href>%s</D:href></D:locktoken>\n"+
  70. " <D:lockroot><D:href>%s</D:href></D:lockroot>\n"+
  71. "</D:activelock></D:lockdiscovery></D:prop>",
  72. depth, ld.OwnerXML, timeout, escape(token), escape(ld.Root),
  73. )
  74. }
  75. func escape(s string) string {
  76. for i := 0; i < len(s); i++ {
  77. switch s[i] {
  78. case '"', '&', '\'', '<', '>':
  79. b := bytes.NewBuffer(nil)
  80. xml.EscapeText(b, []byte(s))
  81. return b.String()
  82. }
  83. }
  84. return s
  85. }
  86. // Next returns the next token, if any, in the XML stream of d.
  87. // RFC 4918 requires to ignore comments, processing instructions
  88. // and directives.
  89. // http://www.webdav.org/specs/rfc4918.html#property_values
  90. // http://www.webdav.org/specs/rfc4918.html#xml-extensibility
  91. func next(d *xml.Decoder) (xml.Token, error) {
  92. for {
  93. t, err := d.Token()
  94. if err != nil {
  95. return t, err
  96. }
  97. switch t.(type) {
  98. case xml.Comment, xml.Directive, xml.ProcInst:
  99. continue
  100. default:
  101. return t, nil
  102. }
  103. }
  104. }
  105. type propnames []xml.Name
  106. // UnmarshalXML appends the property names enclosed within start to pn.
  107. //
  108. // It returns an error if start does not contain any properties or if
  109. // properties contain values. Character data between properties is ignored.
  110. func (pn *propnames) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  111. for {
  112. t, err := next(d)
  113. if err != nil {
  114. return err
  115. }
  116. switch t.(type) {
  117. case xml.EndElement:
  118. if len(*pn) == 0 {
  119. return fmt.Errorf("%s must not be empty", start.Name.Local)
  120. }
  121. return nil
  122. case xml.StartElement:
  123. name := t.(xml.StartElement).Name
  124. t, err = next(d)
  125. if err != nil {
  126. return err
  127. }
  128. if _, ok := t.(xml.EndElement); !ok {
  129. return fmt.Errorf("unexpected token %T", t)
  130. }
  131. *pn = append(*pn, name)
  132. }
  133. }
  134. }
  135. // http://www.webdav.org/specs/rfc4918.html#ELEMENT_propfind
  136. type propfind struct {
  137. XMLName xml.Name `xml:"DAV: propfind"`
  138. Allprop *struct{} `xml:"DAV: allprop"`
  139. Propname *struct{} `xml:"DAV: propname"`
  140. Prop propnames `xml:"DAV: prop"`
  141. Include propnames `xml:"DAV: include"`
  142. }
  143. func readPropfind(r io.Reader) (pf propfind, status int, err error) {
  144. c := countingReader{r: r}
  145. if err = xml.NewDecoder(&c).Decode(&pf); err != nil {
  146. if err == io.EOF {
  147. if c.n == 0 {
  148. // An empty body means to propfind allprop.
  149. // http://www.webdav.org/specs/rfc4918.html#METHOD_PROPFIND
  150. return propfind{Allprop: new(struct{})}, 0, nil
  151. }
  152. err = errInvalidPropfind
  153. }
  154. return propfind{}, http.StatusBadRequest, err
  155. }
  156. if pf.Allprop == nil && pf.Include != nil {
  157. return propfind{}, http.StatusBadRequest, errInvalidPropfind
  158. }
  159. if pf.Allprop != nil && (pf.Prop != nil || pf.Propname != nil) {
  160. return propfind{}, http.StatusBadRequest, errInvalidPropfind
  161. }
  162. if pf.Prop != nil && pf.Propname != nil {
  163. return propfind{}, http.StatusBadRequest, errInvalidPropfind
  164. }
  165. if pf.Propname == nil && pf.Allprop == nil && pf.Prop == nil {
  166. return propfind{}, http.StatusBadRequest, errInvalidPropfind
  167. }
  168. return pf, 0, nil
  169. }