webdav.go 20 KB

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