webdav.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package webdav etc etc TODO.
  5. package webdav // import "golang.org/x/net/webdav"
  6. // TODO: ETag, properties.
  7. import (
  8. "errors"
  9. "io"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "time"
  14. )
  15. // TODO: define the PropSystem interface.
  16. type PropSystem interface{}
  17. type Handler struct {
  18. // FileSystem is the virtual file system.
  19. FileSystem FileSystem
  20. // LockSystem is the lock management system.
  21. LockSystem LockSystem
  22. // PropSystem is an optional property management system. If non-nil, TODO.
  23. PropSystem PropSystem
  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) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  29. status, err := http.StatusBadRequest, errUnsupportedMethod
  30. if h.FileSystem == nil {
  31. status, err = http.StatusInternalServerError, errNoFileSystem
  32. } else if h.LockSystem == nil {
  33. status, err = http.StatusInternalServerError, errNoLockSystem
  34. } else {
  35. // TODO: PROPFIND, PROPPATCH methods.
  36. switch r.Method {
  37. case "OPTIONS":
  38. status, err = h.handleOptions(w, r)
  39. case "GET", "HEAD", "POST":
  40. status, err = h.handleGetHeadPost(w, r)
  41. case "DELETE":
  42. status, err = h.handleDelete(w, r)
  43. case "PUT":
  44. status, err = h.handlePut(w, r)
  45. case "MKCOL":
  46. status, err = h.handleMkcol(w, r)
  47. case "COPY", "MOVE":
  48. status, err = h.handleCopyMove(w, r)
  49. case "LOCK":
  50. status, err = h.handleLock(w, r)
  51. case "UNLOCK":
  52. status, err = h.handleUnlock(w, r)
  53. }
  54. }
  55. if status != 0 {
  56. w.WriteHeader(status)
  57. if status != http.StatusNoContent {
  58. w.Write([]byte(StatusText(status)))
  59. }
  60. }
  61. if h.Logger != nil {
  62. h.Logger(r, err)
  63. }
  64. }
  65. func (h *Handler) lock(now time.Time, root string) (token string, status int, err error) {
  66. token, err = h.LockSystem.Create(now, LockDetails{
  67. Root: root,
  68. Duration: infiniteTimeout,
  69. ZeroDepth: true,
  70. })
  71. if err != nil {
  72. if err == ErrLocked {
  73. return "", StatusLocked, err
  74. }
  75. return "", http.StatusInternalServerError, err
  76. }
  77. return token, 0, nil
  78. }
  79. func (h *Handler) confirmLocks(r *http.Request, src, dst string) (release func(), status int, err error) {
  80. hdr := r.Header.Get("If")
  81. if hdr == "" {
  82. // An empty If header means that the client hasn't previously created locks.
  83. // Even if this client doesn't care about locks, we still need to check that
  84. // the resources aren't locked by another client, so we create temporary
  85. // locks that would conflict with another client's locks. These temporary
  86. // locks are unlocked at the end of the HTTP request.
  87. now, srcToken, dstToken := time.Now(), "", ""
  88. if src != "" {
  89. srcToken, status, err = h.lock(now, src)
  90. if err != nil {
  91. return nil, status, err
  92. }
  93. }
  94. if dst != "" {
  95. dstToken, status, err = h.lock(now, dst)
  96. if err != nil {
  97. if srcToken != "" {
  98. h.LockSystem.Unlock(now, srcToken)
  99. }
  100. return nil, status, err
  101. }
  102. }
  103. return func() {
  104. if dstToken != "" {
  105. h.LockSystem.Unlock(now, dstToken)
  106. }
  107. if srcToken != "" {
  108. h.LockSystem.Unlock(now, srcToken)
  109. }
  110. }, 0, nil
  111. }
  112. ih, ok := parseIfHeader(hdr)
  113. if !ok {
  114. return nil, http.StatusBadRequest, errInvalidIfHeader
  115. }
  116. // ih is a disjunction (OR) of ifLists, so any ifList will do.
  117. for _, l := range ih.lists {
  118. lsrc := l.resourceTag
  119. if lsrc == "" {
  120. lsrc = src
  121. } else {
  122. u, err := url.Parse(lsrc)
  123. if err != nil {
  124. continue
  125. }
  126. if u.Host != r.Host {
  127. continue
  128. }
  129. lsrc = u.Path
  130. }
  131. release, err = h.LockSystem.Confirm(time.Now(), lsrc, dst, l.conditions...)
  132. if err == ErrConfirmationFailed {
  133. continue
  134. }
  135. if err != nil {
  136. return nil, http.StatusInternalServerError, err
  137. }
  138. return release, 0, nil
  139. }
  140. // Section 10.4.1 says that "If this header is evaluated and all state lists
  141. // fail, then the request must fail with a 412 (Precondition Failed) status."
  142. // We follow the spec even though the cond_put_corrupt_token test case from
  143. // the litmus test warns on seeing a 412 instead of a 423 (Locked).
  144. return nil, http.StatusPreconditionFailed, ErrLocked
  145. }
  146. func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) (status int, err error) {
  147. allow := "OPTIONS, LOCK, PUT, MKCOL"
  148. if fi, err := h.FileSystem.Stat(r.URL.Path); err == nil {
  149. if fi.IsDir() {
  150. allow = "OPTIONS, LOCK, GET, HEAD, POST, DELETE, PROPPATCH, COPY, MOVE, UNLOCK, PROPFIND"
  151. } else {
  152. allow = "OPTIONS, LOCK, GET, HEAD, POST, DELETE, PROPPATCH, COPY, MOVE, UNLOCK, PROPFIND, PUT"
  153. }
  154. }
  155. w.Header().Set("Allow", allow)
  156. // http://www.webdav.org/specs/rfc4918.html#dav.compliance.classes
  157. w.Header().Set("DAV", "1, 2")
  158. // http://msdn.microsoft.com/en-au/library/cc250217.aspx
  159. w.Header().Set("MS-Author-Via", "DAV")
  160. return 0, nil
  161. }
  162. func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request) (status int, err error) {
  163. // TODO: check locks for read-only access??
  164. f, err := h.FileSystem.OpenFile(r.URL.Path, os.O_RDONLY, 0)
  165. if err != nil {
  166. return http.StatusNotFound, err
  167. }
  168. defer f.Close()
  169. fi, err := f.Stat()
  170. if err != nil {
  171. return http.StatusNotFound, err
  172. }
  173. http.ServeContent(w, r, r.URL.Path, fi.ModTime(), f)
  174. return 0, nil
  175. }
  176. func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request) (status int, err error) {
  177. release, status, err := h.confirmLocks(r, r.URL.Path, "")
  178. if err != nil {
  179. return status, err
  180. }
  181. defer release()
  182. // TODO: return MultiStatus where appropriate.
  183. // "godoc os RemoveAll" says that "If the path does not exist, RemoveAll
  184. // returns nil (no error)." WebDAV semantics are that it should return a
  185. // "404 Not Found". We therefore have to Stat before we RemoveAll.
  186. if _, err := h.FileSystem.Stat(r.URL.Path); err != nil {
  187. if os.IsNotExist(err) {
  188. return http.StatusNotFound, err
  189. }
  190. return http.StatusMethodNotAllowed, err
  191. }
  192. if err := h.FileSystem.RemoveAll(r.URL.Path); err != nil {
  193. return http.StatusMethodNotAllowed, err
  194. }
  195. return http.StatusNoContent, nil
  196. }
  197. func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int, err error) {
  198. release, status, err := h.confirmLocks(r, r.URL.Path, "")
  199. if err != nil {
  200. return status, err
  201. }
  202. defer release()
  203. f, err := h.FileSystem.OpenFile(r.URL.Path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
  204. if err != nil {
  205. return http.StatusNotFound, err
  206. }
  207. defer f.Close()
  208. if _, err := io.Copy(f, r.Body); err != nil {
  209. return http.StatusMethodNotAllowed, err
  210. }
  211. return http.StatusCreated, nil
  212. }
  213. func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request) (status int, err error) {
  214. release, status, err := h.confirmLocks(r, r.URL.Path, "")
  215. if err != nil {
  216. return status, err
  217. }
  218. defer release()
  219. if r.ContentLength > 0 {
  220. return http.StatusUnsupportedMediaType, nil
  221. }
  222. if err := h.FileSystem.Mkdir(r.URL.Path, 0777); err != nil {
  223. if os.IsNotExist(err) {
  224. return http.StatusConflict, err
  225. }
  226. return http.StatusMethodNotAllowed, err
  227. }
  228. return http.StatusCreated, nil
  229. }
  230. func (h *Handler) handleCopyMove(w http.ResponseWriter, r *http.Request) (status int, err error) {
  231. // TODO: COPY/MOVE for Properties, as per sections 9.8.2 and 9.9.1.
  232. hdr := r.Header.Get("Destination")
  233. if hdr == "" {
  234. return http.StatusBadRequest, errInvalidDestination
  235. }
  236. u, err := url.Parse(hdr)
  237. if err != nil {
  238. return http.StatusBadRequest, errInvalidDestination
  239. }
  240. if u.Host != r.Host {
  241. return http.StatusBadGateway, errInvalidDestination
  242. }
  243. // TODO: do we need a webdav.StripPrefix HTTP handler that's like the
  244. // standard library's http.StripPrefix handler, but also strips the
  245. // prefix in the Destination header?
  246. dst, src := u.Path, r.URL.Path
  247. if dst == "" {
  248. return http.StatusBadGateway, errInvalidDestination
  249. }
  250. if dst == src {
  251. return http.StatusForbidden, errDestinationEqualsSource
  252. }
  253. if r.Method == "COPY" {
  254. // Section 7.5.1 says that a COPY only needs to lock the destination,
  255. // not both destination and source. Strictly speaking, this is racy,
  256. // even though a COPY doesn't modify the source, if a concurrent
  257. // operation modifies the source. However, the litmus test explicitly
  258. // checks that COPYing a locked-by-another source is OK.
  259. release, status, err := h.confirmLocks(r, "", dst)
  260. if err != nil {
  261. return status, err
  262. }
  263. defer release()
  264. // Section 9.8.3 says that "The COPY method on a collection without a Depth
  265. // header must act as if a Depth header with value "infinity" was included".
  266. depth := infiniteDepth
  267. if hdr := r.Header.Get("Depth"); hdr != "" {
  268. depth = parseDepth(hdr)
  269. if depth != 0 && depth != infiniteDepth {
  270. // Section 9.8.3 says that "A client may submit a Depth header on a
  271. // COPY on a collection with a value of "0" or "infinity"."
  272. return http.StatusBadRequest, errInvalidDepth
  273. }
  274. }
  275. return copyFiles(h.FileSystem, src, dst, r.Header.Get("Overwrite") != "F", depth, 0)
  276. }
  277. release, status, err := h.confirmLocks(r, src, dst)
  278. if err != nil {
  279. return status, err
  280. }
  281. defer release()
  282. // Section 9.9.2 says that "The MOVE method on a collection must act as if
  283. // a "Depth: infinity" header was used on it. A client must not submit a
  284. // Depth header on a MOVE on a collection with any value but "infinity"."
  285. if hdr := r.Header.Get("Depth"); hdr != "" {
  286. if parseDepth(hdr) != infiniteDepth {
  287. return http.StatusBadRequest, errInvalidDepth
  288. }
  289. }
  290. return moveFiles(h.FileSystem, src, dst, r.Header.Get("Overwrite") == "T")
  291. }
  292. func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request) (retStatus int, retErr error) {
  293. duration, err := parseTimeout(r.Header.Get("Timeout"))
  294. if err != nil {
  295. return http.StatusBadRequest, err
  296. }
  297. li, status, err := readLockInfo(r.Body)
  298. if err != nil {
  299. return status, err
  300. }
  301. token, ld, now, created := "", LockDetails{}, time.Now(), false
  302. if li == (lockInfo{}) {
  303. // An empty lockInfo means to refresh the lock.
  304. ih, ok := parseIfHeader(r.Header.Get("If"))
  305. if !ok {
  306. return http.StatusBadRequest, errInvalidIfHeader
  307. }
  308. if len(ih.lists) == 1 && len(ih.lists[0].conditions) == 1 {
  309. token = ih.lists[0].conditions[0].Token
  310. }
  311. if token == "" {
  312. return http.StatusBadRequest, errInvalidLockToken
  313. }
  314. ld, err = h.LockSystem.Refresh(now, token, duration)
  315. if err != nil {
  316. if err == ErrNoSuchLock {
  317. return http.StatusPreconditionFailed, err
  318. }
  319. return http.StatusInternalServerError, err
  320. }
  321. } else {
  322. // Section 9.10.3 says that "If no Depth header is submitted on a LOCK request,
  323. // then the request MUST act as if a "Depth:infinity" had been submitted."
  324. depth := infiniteDepth
  325. if hdr := r.Header.Get("Depth"); hdr != "" {
  326. depth = parseDepth(hdr)
  327. if depth != 0 && depth != infiniteDepth {
  328. // Section 9.10.3 says that "Values other than 0 or infinity must not be
  329. // used with the Depth header on a LOCK method".
  330. return http.StatusBadRequest, errInvalidDepth
  331. }
  332. }
  333. ld = LockDetails{
  334. Root: r.URL.Path,
  335. Duration: duration,
  336. OwnerXML: li.Owner.InnerXML,
  337. ZeroDepth: depth == 0,
  338. }
  339. token, err = h.LockSystem.Create(now, ld)
  340. if err != nil {
  341. if err == ErrLocked {
  342. return StatusLocked, err
  343. }
  344. return http.StatusInternalServerError, err
  345. }
  346. defer func() {
  347. if retErr != nil {
  348. h.LockSystem.Unlock(now, token)
  349. }
  350. }()
  351. // Create the resource if it didn't previously exist.
  352. if _, err := h.FileSystem.Stat(r.URL.Path); err != nil {
  353. f, err := h.FileSystem.OpenFile(r.URL.Path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
  354. if err != nil {
  355. // TODO: detect missing intermediate dirs and return http.StatusConflict?
  356. return http.StatusInternalServerError, err
  357. }
  358. f.Close()
  359. created = true
  360. }
  361. // http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
  362. // Lock-Token value is a Coded-URL. We add angle brackets.
  363. w.Header().Set("Lock-Token", "<"+token+">")
  364. }
  365. w.Header().Set("Content-Type", "application/xml; charset=utf-8")
  366. if created {
  367. // This is "w.WriteHeader(http.StatusCreated)" and not "return
  368. // http.StatusCreated, nil" because we write our own (XML) response to w
  369. // and Handler.ServeHTTP would otherwise write "Created".
  370. w.WriteHeader(http.StatusCreated)
  371. }
  372. writeLockInfo(w, token, ld)
  373. return 0, nil
  374. }
  375. func (h *Handler) handleUnlock(w http.ResponseWriter, r *http.Request) (status int, err error) {
  376. // http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the
  377. // Lock-Token value is a Coded-URL. We strip its angle brackets.
  378. t := r.Header.Get("Lock-Token")
  379. if len(t) < 2 || t[0] != '<' || t[len(t)-1] != '>' {
  380. return http.StatusBadRequest, errInvalidLockToken
  381. }
  382. t = t[1 : len(t)-1]
  383. switch err = h.LockSystem.Unlock(time.Now(), t); err {
  384. case nil:
  385. return http.StatusNoContent, err
  386. case ErrForbidden:
  387. return http.StatusForbidden, err
  388. case ErrLocked:
  389. return StatusLocked, err
  390. case ErrNoSuchLock:
  391. return http.StatusConflict, err
  392. default:
  393. return http.StatusInternalServerError, err
  394. }
  395. }
  396. const (
  397. infiniteDepth = -1
  398. invalidDepth = -2
  399. )
  400. // parseDepth maps the strings "0", "1" and "infinity" to 0, 1 and
  401. // infiniteDepth. Parsing any other string returns invalidDepth.
  402. //
  403. // Different WebDAV methods have further constraints on valid depths:
  404. // - PROPFIND has no further restrictions, as per section 9.1.
  405. // - COPY accepts only "0" or "infinity", as per section 9.8.3.
  406. // - MOVE accepts only "infinity", as per section 9.9.2.
  407. // - LOCK accepts only "0" or "infinity", as per section 9.10.3.
  408. // These constraints are enforced by the handleXxx methods.
  409. func parseDepth(s string) int {
  410. switch s {
  411. case "0":
  412. return 0
  413. case "1":
  414. return 1
  415. case "infinity":
  416. return infiniteDepth
  417. }
  418. return invalidDepth
  419. }
  420. // http://www.webdav.org/specs/rfc4918.html#status.code.extensions.to.http11
  421. const (
  422. StatusMulti = 207
  423. StatusUnprocessableEntity = 422
  424. StatusLocked = 423
  425. StatusFailedDependency = 424
  426. StatusInsufficientStorage = 507
  427. )
  428. func StatusText(code int) string {
  429. switch code {
  430. case StatusMulti:
  431. return "Multi-Status"
  432. case StatusUnprocessableEntity:
  433. return "Unprocessable Entity"
  434. case StatusLocked:
  435. return "Locked"
  436. case StatusFailedDependency:
  437. return "Failed Dependency"
  438. case StatusInsufficientStorage:
  439. return "Insufficient Storage"
  440. }
  441. return http.StatusText(code)
  442. }
  443. var (
  444. errDestinationEqualsSource = errors.New("webdav: destination equals source")
  445. errDirectoryNotEmpty = errors.New("webdav: directory not empty")
  446. errInvalidDepth = errors.New("webdav: invalid depth")
  447. errInvalidDestination = errors.New("webdav: invalid destination")
  448. errInvalidIfHeader = errors.New("webdav: invalid If header")
  449. errInvalidLockInfo = errors.New("webdav: invalid lock info")
  450. errInvalidLockToken = errors.New("webdav: invalid lock token")
  451. errInvalidPropfind = errors.New("webdav: invalid propfind")
  452. errInvalidResponse = errors.New("webdav: invalid response")
  453. errInvalidTimeout = errors.New("webdav: invalid timeout")
  454. errNoFileSystem = errors.New("webdav: no file system")
  455. errNoLockSystem = errors.New("webdav: no lock system")
  456. errNotADirectory = errors.New("webdav: not a directory")
  457. errRecursionTooDeep = errors.New("webdav: recursion too deep")
  458. errUnsupportedLockInfo = errors.New("webdav: unsupported lock info")
  459. errUnsupportedMethod = errors.New("webdav: unsupported method")
  460. )