webdav.go 20 KB

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