webdav.go 21 KB

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