webdav.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 etc etc TODO.
  5. package webdav // import "golang.org/x/net/webdav"
  6. // TODO: ETag, properties.
  7. import (
  8. "encoding/xml"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "log"
  13. "net/http"
  14. "net/url"
  15. "os"
  16. "runtime"
  17. "strings"
  18. "time"
  19. )
  20. // Package webdav's XML output requires the standard library's encoding/xml
  21. // package version 1.5 or greater. Otherwise, it will produce malformed XML.
  22. //
  23. // As of May 2015, the Go stable release is version 1.4, so we print a message
  24. // to let users know that this golang.org/x/etc package won't work yet.
  25. //
  26. // This package also won't work with Go 1.3 and earlier, but making this
  27. // runtime version check catch all the earlier versions too, and not just
  28. // "1.4.x", isn't worth the complexity.
  29. //
  30. // TODO: delete this check at some point after Go 1.5 is released.
  31. var go1Dot4 = strings.HasPrefix(runtime.Version(), "go1.4.")
  32. func init() {
  33. if go1Dot4 {
  34. log.Println("package webdav requires Go version 1.5 or greater")
  35. }
  36. }
  37. type Handler struct {
  38. // FileSystem is the virtual file system.
  39. FileSystem FileSystem
  40. // LockSystem is the lock management system.
  41. LockSystem LockSystem
  42. // PropSystem is the property management system.
  43. PropSystem PropSystem
  44. // Logger is an optional error logger. If non-nil, it will be called
  45. // for all HTTP requests.
  46. Logger func(*http.Request, error)
  47. }
  48. func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  49. status, err := http.StatusBadRequest, errUnsupportedMethod
  50. if h.FileSystem == nil {
  51. status, err = http.StatusInternalServerError, errNoFileSystem
  52. } else if h.LockSystem == nil {
  53. status, err = http.StatusInternalServerError, errNoLockSystem
  54. } else if h.PropSystem == nil {
  55. status, err = http.StatusInternalServerError, errNoPropSystem
  56. } else {
  57. switch r.Method {
  58. case "OPTIONS":
  59. status, err = h.handleOptions(w, r)
  60. case "GET", "HEAD", "POST":
  61. status, err = h.handleGetHeadPost(w, r)
  62. case "DELETE":
  63. status, err = h.handleDelete(w, r)
  64. case "PUT":
  65. status, err = h.handlePut(w, r)
  66. case "MKCOL":
  67. status, err = h.handleMkcol(w, r)
  68. case "COPY", "MOVE":
  69. status, err = h.handleCopyMove(w, r)
  70. case "LOCK":
  71. status, err = h.handleLock(w, r)
  72. case "UNLOCK":
  73. status, err = h.handleUnlock(w, r)
  74. case "PROPFIND":
  75. status, err = h.handlePropfind(w, r)
  76. case "PROPPATCH":
  77. status, err = h.handleProppatch(w, r)
  78. }
  79. }
  80. if status != 0 {
  81. w.WriteHeader(status)
  82. if status != http.StatusNoContent {
  83. w.Write([]byte(StatusText(status)))
  84. }
  85. }
  86. if h.Logger != nil {
  87. h.Logger(r, err)
  88. }
  89. }
  90. func (h *Handler) lock(now time.Time, root string) (token string, status int, err error) {
  91. token, err = h.LockSystem.Create(now, LockDetails{
  92. Root: root,
  93. Duration: infiniteTimeout,
  94. ZeroDepth: true,
  95. })
  96. if err != nil {
  97. if err == ErrLocked {
  98. return "", StatusLocked, err
  99. }
  100. return "", http.StatusInternalServerError, err
  101. }
  102. return token, 0, nil
  103. }
  104. func (h *Handler) confirmLocks(r *http.Request, src, dst string) (release func(), status int, err error) {
  105. hdr := r.Header.Get("If")
  106. if hdr == "" {
  107. // An empty If header means that the client hasn't previously created locks.
  108. // Even if this client doesn't care about locks, we still need to check that
  109. // the resources aren't locked by another client, so we create temporary
  110. // locks that would conflict with another client's locks. These temporary
  111. // locks are unlocked at the end of the HTTP request.
  112. now, srcToken, dstToken := time.Now(), "", ""
  113. if src != "" {
  114. srcToken, status, err = h.lock(now, src)
  115. if err != nil {
  116. return nil, status, err
  117. }
  118. }
  119. if dst != "" {
  120. dstToken, status, err = h.lock(now, dst)
  121. if err != nil {
  122. if srcToken != "" {
  123. h.LockSystem.Unlock(now, srcToken)
  124. }
  125. return nil, status, err
  126. }
  127. }
  128. return func() {
  129. if dstToken != "" {
  130. h.LockSystem.Unlock(now, dstToken)
  131. }
  132. if srcToken != "" {
  133. h.LockSystem.Unlock(now, srcToken)
  134. }
  135. }, 0, nil
  136. }
  137. ih, ok := parseIfHeader(hdr)
  138. if !ok {
  139. return nil, http.StatusBadRequest, errInvalidIfHeader
  140. }
  141. // ih is a disjunction (OR) of ifLists, so any ifList will do.
  142. for _, l := range ih.lists {
  143. lsrc := l.resourceTag
  144. if lsrc == "" {
  145. lsrc = src
  146. } else {
  147. u, err := url.Parse(lsrc)
  148. if err != nil {
  149. continue
  150. }
  151. if u.Host != r.Host {
  152. continue
  153. }
  154. lsrc = u.Path
  155. }
  156. release, err = h.LockSystem.Confirm(time.Now(), lsrc, dst, l.conditions...)
  157. if err == ErrConfirmationFailed {
  158. continue
  159. }
  160. if err != nil {
  161. return nil, http.StatusInternalServerError, err
  162. }
  163. return release, 0, nil
  164. }
  165. // Section 10.4.1 says that "If this header is evaluated and all state lists
  166. // fail, then the request must fail with a 412 (Precondition Failed) status."
  167. // We follow the spec even though the cond_put_corrupt_token test case from
  168. // the litmus test warns on seeing a 412 instead of a 423 (Locked).
  169. return nil, http.StatusPreconditionFailed, ErrLocked
  170. }
  171. func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) (status int, err error) {
  172. allow := "OPTIONS, LOCK, PUT, MKCOL"
  173. if fi, err := h.FileSystem.Stat(r.URL.Path); err == nil {
  174. if fi.IsDir() {
  175. allow = "OPTIONS, LOCK, GET, HEAD, POST, DELETE, PROPPATCH, COPY, MOVE, UNLOCK, PROPFIND"
  176. } else {
  177. allow = "OPTIONS, LOCK, GET, HEAD, POST, DELETE, PROPPATCH, COPY, MOVE, UNLOCK, PROPFIND, PUT"
  178. }
  179. }
  180. w.Header().Set("Allow", allow)
  181. // http://www.webdav.org/specs/rfc4918.html#dav.compliance.classes
  182. w.Header().Set("DAV", "1, 2")
  183. // http://msdn.microsoft.com/en-au/library/cc250217.aspx
  184. w.Header().Set("MS-Author-Via", "DAV")
  185. return 0, nil
  186. }
  187. func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request) (status int, err error) {
  188. // TODO: check locks for read-only access??
  189. f, err := h.FileSystem.OpenFile(r.URL.Path, os.O_RDONLY, 0)
  190. if err != nil {
  191. return http.StatusNotFound, err
  192. }
  193. defer f.Close()
  194. fi, err := f.Stat()
  195. if err != nil {
  196. return http.StatusNotFound, err
  197. }
  198. pstats, err := h.PropSystem.Find(r.URL.Path, []xml.Name{
  199. {Space: "DAV:", Local: "getetag"},
  200. {Space: "DAV:", Local: "getcontenttype"},
  201. })
  202. if err != nil {
  203. return http.StatusInternalServerError, err
  204. }
  205. writeDAVHeaders(w, pstats)
  206. http.ServeContent(w, r, r.URL.Path, fi.ModTime(), f)
  207. return 0, nil
  208. }
  209. func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request) (status int, err error) {
  210. release, status, err := h.confirmLocks(r, r.URL.Path, "")
  211. if err != nil {
  212. return status, err
  213. }
  214. defer release()
  215. // TODO: return MultiStatus where appropriate.
  216. // "godoc os RemoveAll" says that "If the path does not exist, RemoveAll
  217. // returns nil (no error)." WebDAV semantics are that it should return a
  218. // "404 Not Found". We therefore have to Stat before we RemoveAll.
  219. if _, err := h.FileSystem.Stat(r.URL.Path); err != nil {
  220. if os.IsNotExist(err) {
  221. return http.StatusNotFound, err
  222. }
  223. return http.StatusMethodNotAllowed, err
  224. }
  225. if err := h.FileSystem.RemoveAll(r.URL.Path); err != nil {
  226. return http.StatusMethodNotAllowed, err
  227. }
  228. return http.StatusNoContent, nil
  229. }
  230. func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int, err error) {
  231. release, status, err := h.confirmLocks(r, r.URL.Path, "")
  232. if err != nil {
  233. return status, err
  234. }
  235. defer release()
  236. f, err := h.FileSystem.OpenFile(r.URL.Path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
  237. if err != nil {
  238. return http.StatusNotFound, err
  239. }
  240. _, copyErr := io.Copy(f, r.Body)
  241. closeErr := f.Close()
  242. if copyErr != nil {
  243. return http.StatusMethodNotAllowed, copyErr
  244. }
  245. if closeErr != nil {
  246. return http.StatusMethodNotAllowed, closeErr
  247. }
  248. pstats, err := h.PropSystem.Find(r.URL.Path, []xml.Name{
  249. {Space: "DAV:", Local: "getetag"},
  250. })
  251. if err != nil {
  252. return http.StatusInternalServerError, err
  253. }
  254. writeDAVHeaders(w, pstats)
  255. return http.StatusCreated, nil
  256. }
  257. func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request) (status int, err error) {
  258. release, status, err := h.confirmLocks(r, r.URL.Path, "")
  259. if err != nil {
  260. return status, err
  261. }
  262. defer release()
  263. if r.ContentLength > 0 {
  264. return http.StatusUnsupportedMediaType, nil
  265. }
  266. if err := h.FileSystem.Mkdir(r.URL.Path, 0777); err != nil {
  267. if os.IsNotExist(err) {
  268. return http.StatusConflict, err
  269. }
  270. return http.StatusMethodNotAllowed, err
  271. }
  272. return http.StatusCreated, nil
  273. }
  274. func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request) (status int, err error) {
  275. // TODO: COPY/MOVE for Properties, as per sections 9.8.2 and 9.9.1.
  276. hdr := r.Header.Get("Destination")
  277. if hdr == "" {
  278. return http.StatusBadRequest, errInvalidDestination
  279. }
  280. u, err := url.Parse(hdr)
  281. if err != nil {
  282. return http.StatusBadRequest, errInvalidDestination
  283. }
  284. if u.Host != r.Host {
  285. return http.StatusBadGateway, errInvalidDestination
  286. }
  287. // TODO: do we need a webdav.StripPrefix HTTP handler that's like the
  288. // standard library's http.StripPrefix handler, but also strips the
  289. // prefix in the Destination header?
  290. dst, src := u.Path, r.URL.Path
  291. if dst == "" {
  292. return http.StatusBadGateway, errInvalidDestination
  293. }
  294. if dst == src {
  295. return http.StatusForbidden, errDestinationEqualsSource
  296. }
  297. if r.Method == "COPY" {
  298. // Section 7.5.1 says that a COPY only needs to lock the destination,
  299. // not both destination and source. Strictly speaking, this is racy,
  300. // even though a COPY doesn't modify the source, if a concurrent
  301. // operation modifies the source. However, the litmus test explicitly
  302. // checks that COPYing a locked-by-another source is OK.
  303. release, status, err := h.confirmLocks(r, "", dst)
  304. if err != nil {
  305. return status, err
  306. }
  307. defer release()
  308. // Section 9.8.3 says that "The COPY method on a collection without a Depth
  309. // header must act as if a Depth header with value "infinity" was included".
  310. depth := infiniteDepth
  311. if hdr := r.Header.Get("Depth"); hdr != "" {
  312. depth = parseDepth(hdr)
  313. if depth != 0 && depth != infiniteDepth {
  314. // Section 9.8.3 says that "A client may submit a Depth header on a
  315. // COPY on a collection with a value of "0" or "infinity"."
  316. return http.StatusBadRequest, errInvalidDepth
  317. }
  318. }
  319. return copyFiles(h.FileSystem, src, dst, r.Header.Get("Overwrite") != "F", depth, 0)
  320. }
  321. release, status, err := h.confirmLocks(r, src, dst)
  322. if err != nil {
  323. return status, err
  324. }
  325. defer release()
  326. // Section 9.9.2 says that "The MOVE method on a collection must act as if
  327. // a "Depth: infinity" header was used on it. A client must not submit a
  328. // Depth header on a MOVE on a collection with any value but "infinity"."
  329. if hdr := r.Header.Get("Depth"); hdr != "" {
  330. if parseDepth(hdr) != infiniteDepth {
  331. return http.StatusBadRequest, errInvalidDepth
  332. }
  333. }
  334. return moveFiles(h.FileSystem, src, dst, r.Header.Get("Overwrite") == "T")
  335. }
  336. func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request) (retStatus int, retErr error) {
  337. duration, err := parseTimeout(r.Header.Get("Timeout"))
  338. if err != nil {
  339. return http.StatusBadRequest, err
  340. }
  341. li, status, err := readLockInfo(r.Body)
  342. if err != nil {
  343. return status, err
  344. }
  345. token, ld, now, created := "", LockDetails{}, time.Now(), false
  346. if li == (lockInfo{}) {
  347. // An empty lockInfo means to refresh the lock.
  348. ih, ok := parseIfHeader(r.Header.Get("If"))
  349. if !ok {
  350. return http.StatusBadRequest, errInvalidIfHeader
  351. }
  352. if len(ih.lists) == 1 && len(ih.lists[0].conditions) == 1 {
  353. token = ih.lists[0].conditions[0].Token
  354. }
  355. if token == "" {
  356. return http.StatusBadRequest, errInvalidLockToken
  357. }
  358. ld, err = h.LockSystem.Refresh(now, token, duration)
  359. if err != nil {
  360. if err == ErrNoSuchLock {
  361. return http.StatusPreconditionFailed, err
  362. }
  363. return http.StatusInternalServerError, err
  364. }
  365. } else {
  366. // Section 9.10.3 says that "If no Depth header is submitted on a LOCK request,
  367. // then the request MUST act as if a "Depth:infinity" had been submitted."
  368. depth := infiniteDepth
  369. if hdr := r.Header.Get("Depth"); hdr != "" {
  370. depth = parseDepth(hdr)
  371. if depth != 0 && depth != infiniteDepth {
  372. // Section 9.10.3 says that "Values other than 0 or infinity must not be
  373. // used with the Depth header on a LOCK method".
  374. return http.StatusBadRequest, errInvalidDepth
  375. }
  376. }
  377. ld = LockDetails{
  378. Root: r.URL.Path,
  379. Duration: duration,
  380. OwnerXML: li.Owner.InnerXML,
  381. ZeroDepth: depth == 0,
  382. }
  383. token, err = h.LockSystem.Create(now, ld)
  384. if err != nil {
  385. if err == ErrLocked {
  386. return StatusLocked, err
  387. }
  388. return http.StatusInternalServerError, err
  389. }
  390. defer func() {
  391. if retErr != nil {
  392. h.LockSystem.Unlock(now, token)
  393. }
  394. }()
  395. // Create the resource if it didn't previously exist.
  396. if _, err := h.FileSystem.Stat(r.URL.Path); err != nil {
  397. f, err := h.FileSystem.OpenFile(r.URL.Path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
  398. if err != nil {
  399. // TODO: detect missing intermediate dirs and return http.StatusConflict?
  400. return http.StatusInternalServerError, err
  401. }
  402. f.Close()
  403. created = true
  404. }
  405. // http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
  406. // Lock-Token value is a Coded-URL. We add angle brackets.
  407. w.Header().Set("Lock-Token", "<"+token+">")
  408. }
  409. w.Header().Set("Content-Type", "application/xml; charset=utf-8")
  410. if created {
  411. // This is "w.WriteHeader(http.StatusCreated)" and not "return
  412. // http.StatusCreated, nil" because we write our own (XML) response to w
  413. // and Handler.ServeHTTP would otherwise write "Created".
  414. w.WriteHeader(http.StatusCreated)
  415. }
  416. writeLockInfo(w, token, ld)
  417. return 0, nil
  418. }
  419. func (h *Handler) handleUnlock(w http.ResponseWriter, r *http.Request) (status int, err error) {
  420. // http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
  421. // Lock-Token value is a Coded-URL. We strip its angle brackets.
  422. t := r.Header.Get("Lock-Token")
  423. if len(t) < 2 || t[0] != '<' || t[len(t)-1] != '>' {
  424. return http.StatusBadRequest, errInvalidLockToken
  425. }
  426. t = t[1 : len(t)-1]
  427. switch err = h.LockSystem.Unlock(time.Now(), t); err {
  428. case nil:
  429. return http.StatusNoContent, err
  430. case ErrForbidden:
  431. return http.StatusForbidden, err
  432. case ErrLocked:
  433. return StatusLocked, err
  434. case ErrNoSuchLock:
  435. return http.StatusConflict, err
  436. default:
  437. return http.StatusInternalServerError, err
  438. }
  439. }
  440. func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) (status int, err error) {
  441. fi, err := h.FileSystem.Stat(r.URL.Path)
  442. if err != nil {
  443. if err == os.ErrNotExist {
  444. return http.StatusNotFound, err
  445. }
  446. return http.StatusMethodNotAllowed, err
  447. }
  448. depth := infiniteDepth
  449. if hdr := r.Header.Get("Depth"); hdr != "" {
  450. depth = parseDepth(hdr)
  451. if depth == invalidDepth {
  452. return http.StatusBadRequest, errInvalidDepth
  453. }
  454. }
  455. pf, status, err := readPropfind(r.Body)
  456. if err != nil {
  457. return status, err
  458. }
  459. mw := multistatusWriter{w: w}
  460. walkFn := func(path string, info os.FileInfo, err error) error {
  461. if err != nil {
  462. return err
  463. }
  464. var pstats []Propstat
  465. if pf.Propname != nil {
  466. propnames, err := h.PropSystem.Propnames(path)
  467. if err != nil {
  468. return err
  469. }
  470. pstat := Propstat{Status: http.StatusOK}
  471. for _, xmlname := range propnames {
  472. pstat.Props = append(pstat.Props, Property{XMLName: xmlname})
  473. }
  474. pstats = append(pstats, pstat)
  475. } else if pf.Allprop != nil {
  476. pstats, err = h.PropSystem.Allprop(path, pf.Prop)
  477. } else {
  478. pstats, err = h.PropSystem.Find(path, pf.Prop)
  479. }
  480. if err != nil {
  481. return err
  482. }
  483. return mw.write(makePropstatResponse(path, pstats))
  484. }
  485. walkErr := walkFS(h.FileSystem, depth, r.URL.Path, fi, walkFn)
  486. closeErr := mw.close()
  487. if walkErr != nil {
  488. return http.StatusInternalServerError, walkErr
  489. }
  490. if closeErr != nil {
  491. return http.StatusInternalServerError, closeErr
  492. }
  493. return 0, nil
  494. }
  495. func (h *Handler) handleProppatch(w http.ResponseWriter, r *http.Request) (status int, err error) {
  496. release, status, err := h.confirmLocks(r, r.URL.Path, "")
  497. if err != nil {
  498. return status, err
  499. }
  500. defer release()
  501. if _, err := h.FileSystem.Stat(r.URL.Path); err != nil {
  502. if err == os.ErrNotExist {
  503. return http.StatusNotFound, err
  504. }
  505. return http.StatusMethodNotAllowed, err
  506. }
  507. patches, status, err := readProppatch(r.Body)
  508. if err != nil {
  509. return status, err
  510. }
  511. pstats, err := h.PropSystem.Patch(r.URL.Path, patches)
  512. if err != nil {
  513. return http.StatusInternalServerError, err
  514. }
  515. mw := multistatusWriter{w: w}
  516. writeErr := mw.write(makePropstatResponse(r.URL.Path, pstats))
  517. closeErr := mw.close()
  518. if writeErr != nil {
  519. return http.StatusInternalServerError, writeErr
  520. }
  521. if closeErr != nil {
  522. return http.StatusInternalServerError, closeErr
  523. }
  524. return 0, nil
  525. }
  526. // davHeaderNames maps the names of DAV properties to their corresponding
  527. // HTTP response headers.
  528. var davHeaderNames = map[xml.Name]string{
  529. xml.Name{Space: "DAV:", Local: "getetag"}: "ETag",
  530. xml.Name{Space: "DAV:", Local: "getcontenttype"}: "Content-Type",
  531. }
  532. func writeDAVHeaders(w http.ResponseWriter, pstats []Propstat) {
  533. for _, pst := range pstats {
  534. if pst.Status == http.StatusOK {
  535. for _, p := range pst.Props {
  536. if n, ok := davHeaderNames[p.XMLName]; ok {
  537. w.Header().Set(n, string(p.InnerXML))
  538. }
  539. }
  540. break
  541. }
  542. }
  543. }
  544. func makePropstatResponse(href string, pstats []Propstat) *response {
  545. resp := response{
  546. Href: []string{href},
  547. Propstat: make([]propstat, 0, len(pstats)),
  548. }
  549. for _, p := range pstats {
  550. var xmlErr *xmlError
  551. if p.XMLError != "" {
  552. xmlErr = &xmlError{InnerXML: []byte(p.XMLError)}
  553. }
  554. resp.Propstat = append(resp.Propstat, propstat{
  555. Status: fmt.Sprintf("HTTP/1.1 %d %s", p.Status, StatusText(p.Status)),
  556. Prop: p.Props,
  557. ResponseDescription: p.ResponseDescription,
  558. Error: xmlErr,
  559. })
  560. }
  561. return &resp
  562. }
  563. const (
  564. infiniteDepth = -1
  565. invalidDepth = -2
  566. )
  567. // parseDepth maps the strings "0", "1" and "infinity" to 0, 1 and
  568. // infiniteDepth. Parsing any other string returns invalidDepth.
  569. //
  570. // Different WebDAV methods have further constraints on valid depths:
  571. // - PROPFIND has no further restrictions, as per section 9.1.
  572. // - COPY accepts only "0" or "infinity", as per section 9.8.3.
  573. // - MOVE accepts only "infinity", as per section 9.9.2.
  574. // - LOCK accepts only "0" or "infinity", as per section 9.10.3.
  575. // These constraints are enforced by the handleXxx methods.
  576. func parseDepth(s string) int {
  577. switch s {
  578. case "0":
  579. return 0
  580. case "1":
  581. return 1
  582. case "infinity":
  583. return infiniteDepth
  584. }
  585. return invalidDepth
  586. }
  587. // http://www.webdav.org/specs/rfc4918.html#status.code.extensions.to.http11
  588. const (
  589. StatusMulti = 207
  590. StatusUnprocessableEntity = 422
  591. StatusLocked = 423
  592. StatusFailedDependency = 424
  593. StatusInsufficientStorage = 507
  594. )
  595. func StatusText(code int) string {
  596. switch code {
  597. case StatusMulti:
  598. return "Multi-Status"
  599. case StatusUnprocessableEntity:
  600. return "Unprocessable Entity"
  601. case StatusLocked:
  602. return "Locked"
  603. case StatusFailedDependency:
  604. return "Failed Dependency"
  605. case StatusInsufficientStorage:
  606. return "Insufficient Storage"
  607. }
  608. return http.StatusText(code)
  609. }
  610. var (
  611. errDestinationEqualsSource = errors.New("webdav: destination equals source")
  612. errDirectoryNotEmpty = errors.New("webdav: directory not empty")
  613. errInvalidDepth = errors.New("webdav: invalid depth")
  614. errInvalidDestination = errors.New("webdav: invalid destination")
  615. errInvalidIfHeader = errors.New("webdav: invalid If header")
  616. errInvalidLockInfo = errors.New("webdav: invalid lock info")
  617. errInvalidLockToken = errors.New("webdav: invalid lock token")
  618. errInvalidPropfind = errors.New("webdav: invalid propfind")
  619. errInvalidProppatch = errors.New("webdav: invalid proppatch")
  620. errInvalidResponse = errors.New("webdav: invalid response")
  621. errInvalidTimeout = errors.New("webdav: invalid timeout")
  622. errNoFileSystem = errors.New("webdav: no file system")
  623. errNoLockSystem = errors.New("webdav: no lock system")
  624. errNoPropSystem = errors.New("webdav: no property system")
  625. errNotADirectory = errors.New("webdav: not a directory")
  626. errRecursionTooDeep = errors.New("webdav: recursion too deep")
  627. errUnsupportedLockInfo = errors.New("webdav: unsupported lock info")
  628. errUnsupportedMethod = errors.New("webdav: unsupported method")
  629. )