webdav.go 21 KB

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