Don't require url-auto.
[bpt/emacs.git] / lisp / url / url-dav.el
CommitLineData
8c8b8430
SM
1;;; url-dav.el --- WebDAV support
2
3;; Copyright (C) 2001 Free Software Foundation, Inc.
4
5;; Author: Bill Perry <wmperry@gnu.org>
6;; Maintainer: Bill Perry <wmperry@gnu.org>
7;; Version: $Revision: 1.6 $
8;; Keywords: url, vc
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25(eval-when-compile
26 (require 'cl))
27
28(require 'xml)
29(require 'url-util)
30(require 'url-handlers)
31
32(defvar url-dav-supported-protocols '(1 2)
33 "List of supported DAV versions.")
34
35;;;###autoload
36(defun url-dav-supported-p (url)
37 (and (featurep 'xml)
38 (fboundp 'xml-expand-namespace)
39 (intersection url-dav-supported-protocols
40 (plist-get (url-http-options url) 'dav))))
41
42(defun url-dav-node-text (node)
43 "Return the text data from the XML node NODE."
44 (mapconcat (lambda (txt)
45 (if (stringp txt)
46 txt
47 "")) (xml-node-children node) " "))
48
49\f
50;;; Parsing routines for the actual node contents.
51;;;
52;;; I am not incredibly happy with how this code looks/works right
53;;; now, but it DOES work, and if we get the API right, our callers
54;;; won't have to worry about the internal representation.
55
56(defconst url-dav-datatype-attribute
57 'urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/dt)
58
59(defun url-dav-process-integer-property (node)
60 (truncate (string-to-number (url-dav-node-text node))))
61
62(defun url-dav-process-number-property (node)
63 (string-to-number (url-dav-node-text node)))
64
65(defconst url-dav-iso8601-regexp
66 (let* ((dash "-?")
67 (colon ":?")
68 (4digit "\\([0-9][0-9][0-9][0-9]\\)")
69 (2digit "\\([0-9][0-9]\\)")
70 (date-fullyear 4digit)
71 (date-month 2digit)
72 (date-mday 2digit)
73 (time-hour 2digit)
74 (time-minute 2digit)
75 (time-second 2digit)
76 (time-secfrac "\\(\\.[0-9]+\\)?")
77 (time-numoffset (concat "[-+]\\(" time-hour "\\):" time-minute))
78 (time-offset (concat "Z" time-numoffset))
79 (partial-time (concat time-hour colon time-minute colon time-second
80 time-secfrac))
81 (full-date (concat date-fullyear dash date-month dash date-mday))
82 (full-time (concat partial-time time-offset))
83 (date-time (concat full-date "T" full-time)))
84 (list (concat "^" full-date)
85 (concat "T" partial-time)
86 (concat "Z" time-numoffset)))
87 "List of regular expressions matching iso8601 dates.
881st regular expression matches the date.
892nd regular expression matches the time.
903rd regular expression matches the (optional) timezone specification.
91")
92
93(defun url-dav-process-date-property (node)
94 (require 'parse-time)
95 (let* ((date-re (nth 0 url-dav-iso8601-regexp))
96 (time-re (nth 1 url-dav-iso8601-regexp))
97 (tz-re (nth 2 url-dav-iso8601-regexp))
98 (date-string (url-dav-node-text node))
99 re-start
100 time seconds minute hour fractional-seconds
101 day month year day-of-week dst tz)
102 ;; We need to populate 'time' with
103 ;; (SEC MIN HOUR DAY MON YEAR DOW DST TZ)
104
105 ;; Nobody else handles iso8601 correctly, lets do it ourselves.
106 (when (string-match date-re date-string re-start)
107 (setq year (string-to-int (match-string 1 date-string))
108 month (string-to-int (match-string 2 date-string))
109 day (string-to-int (match-string 3 date-string))
110 re-start (match-end 0))
111 (when (string-match time-re date-string re-start)
112 (setq hour (string-to-int (match-string 1 date-string))
113 minute (string-to-int (match-string 2 date-string))
114 seconds (string-to-int (match-string 3 date-string))
115 fractional-seconds (string-to-int (or
116 (match-string 4 date-string)
117 "0"))
118 re-start (match-end 0))
119 (when (string-match tz-re date-string re-start)
120 (setq tz (match-string 1 date-string)))
121 (url-debug 'dav "Parsed iso8601%s date" (if tz "tz" ""))
122 (setq time (list seconds minute hour day month year day-of-week dst tz))))
123
124 ;; Fall back to having Gnus do fancy things for us.
125 (when (not time)
126 (setq time (parse-time-string date-string)))
127
128 (if time
129 (setq time (apply 'encode-time time))
130 (url-debug 'dav "Unable to decode date (%S) (%s)"
131 (xml-node-name node) date-string))
132 time))
133
134(defun url-dav-process-boolean-property (node)
135 (/= 0 (string-to-int (url-dav-node-text node))))
136
137(defun url-dav-process-uri-property (node)
138 ;; Returns a parsed representation of the URL...
139 (url-generic-parse-url (url-dav-node-text node)))
140
141(defun url-dav-find-parser (node)
142 "Find a function to parse the XML node NODE."
143 (or (get (xml-node-name node) 'dav-parser)
144 (let ((fn (intern (format "url-dav-process-%s" (xml-node-name node)))))
145 (if (not (fboundp fn))
146 (setq fn 'url-dav-node-text)
147 (put (xml-node-name node) 'dav-parser fn))
148 fn)))
149
150(defmacro url-dav-dispatch-node (node)
151 `(funcall (url-dav-find-parser ,node) ,node))
152
153(defun url-dav-process-DAV:prop (node)
154 ;; A prop node has content model of ANY
155 ;;
156 ;; Some predefined nodes have special meanings though.
157 ;;
158 ;; DAV:supportedlock - list of DAV:lockentry
159 ;; DAV:source
160 ;; DAV:iscollection - boolean
161 ;; DAV:getcontentlength - integer
162 ;; DAV:ishidden - boolean
163 ;; DAV:getcontenttype - string
164 ;; DAV:resourcetype - node who's name is the resource type
165 ;; DAV:getlastmodified - date
166 ;; DAV:creationdate - date
167 ;; DAV:displayname - string
168 ;; DAV:getetag - unknown
169 (let ((children (xml-node-children node))
170 (node-type nil)
171 (props nil)
172 (value nil)
173 (handler-func nil))
174 (when (not children)
175 (error "No child nodes in DAV:prop"))
176
177 (while children
178 (setq node (car children)
179 node-type (intern
180 (or
181 (cdr-safe (assq url-dav-datatype-attribute
182 (xml-node-attributes node)))
183 "unknown"))
184 value nil)
185
186 (case node-type
187 ((dateTime.iso8601tz
188 dateTime.iso8601
189 dateTime.tz
190 dateTime.rfc1123
191 dateTime
192 date) ; date is our 'special' one...
193 ;; Some type of date/time string.
194 (setq value (url-dav-process-date-property node)))
195 (int
196 ;; Integer type...
197 (setq value (url-dav-process-integer-property node)))
198 ((number float)
199 (setq value (url-dav-process-number-property node)))
200 (boolean
201 (setq value (url-dav-process-boolean-property node)))
202 (uri
203 (setq value (url-dav-process-uri-property node)))
204 (otherwise
205 (if (not (eq node-type 'unknown))
206 (url-debug 'dav "Unknown data type in url-dav-process-prop: %s"
207 node-type))
208 (setq value (url-dav-dispatch-node node))))
209
210 (setq props (plist-put props (xml-node-name node) value)
211 children (cdr children)))
212 props))
213
214(defun url-dav-process-DAV:supportedlock (node)
215 ;; DAV:supportedlock is a list of DAV:lockentry items.
216 ;; DAV:lockentry in turn contains a DAV:lockscope and DAV:locktype.
217 ;; The DAV:lockscope must have a single node beneath it, ditto for
218 ;; DAV:locktype.
219 (let ((children (xml-node-children node))
220 (results nil)
221 scope type)
222 (while children
223 (when (and (not (stringp (car children)))
224 (eq (xml-node-name (car children)) 'DAV:lockentry))
225 (setq scope (assq 'DAV:lockscope (xml-node-children (car children)))
226 type (assq 'DAV:locktype (xml-node-children (car children))))
227 (when (and scope type)
228 (setq scope (xml-node-name (car (xml-node-children scope)))
229 type (xml-node-name (car (xml-node-children type))))
230 (push (cons type scope) results)))
231 (setq children (cdr children)))
232 results))
233
234(defun url-dav-process-subnode-property (node)
235 ;; Returns a list of child node names.
236 (delq nil (mapcar 'car-safe (xml-node-children node))))
237
238(defalias 'url-dav-process-DAV:depth 'url-dav-process-integer-property)
239(defalias 'url-dav-process-DAV:resourcetype 'url-dav-process-subnode-property)
240(defalias 'url-dav-process-DAV:locktype 'url-dav-process-subnode-property)
241(defalias 'url-dav-process-DAV:lockscope 'url-dav-process-subnode-property)
242(defalias 'url-dav-process-DAV:getcontentlength 'url-dav-process-integer-property)
243(defalias 'url-dav-process-DAV:getlastmodified 'url-dav-process-date-property)
244(defalias 'url-dav-process-DAV:creationdate 'url-dav-process-date-property)
245(defalias 'url-dav-process-DAV:iscollection 'url-dav-process-boolean-property)
246(defalias 'url-dav-process-DAV:ishidden 'url-dav-process-boolean-property)
247
248(defun url-dav-process-DAV:locktoken (node)
249 ;; DAV:locktoken can have one or more DAV:href children.
250 (delq nil (mapcar (lambda (n)
251 (if (stringp n)
252 n
253 (url-dav-dispatch-node n)))
254 (xml-node-children node))))
255
256(defun url-dav-process-DAV:owner (node)
257 ;; DAV:owner can contain anything.
258 (delq nil (mapcar (lambda (n)
259 (if (stringp n)
260 n
261 (url-dav-dispatch-node n)))
262 (xml-node-children node))))
263
264(defun url-dav-process-DAV:activelock (node)
265 ;; DAV:activelock can contain:
266 ;; DAV:lockscope
267 ;; DAV:locktype
268 ;; DAV:depth
269 ;; DAV:owner (optional)
270 ;; DAV:timeout (optional)
271 ;; DAV:locktoken (optional)
272 (let ((children (xml-node-children node))
273 (results nil))
274 (while children
275 (if (listp (car children))
276 (push (cons (xml-node-name (car children))
277 (url-dav-dispatch-node (car children)))
278 results))
279 (setq children (cdr children)))
280 results))
281
282(defun url-dav-process-DAV:lockdiscovery (node)
283 ;; Can only contain a list of DAV:activelock objects.
284 (let ((children (xml-node-children node))
285 (results nil))
286 (while children
287 (cond
288 ((stringp (car children))
289 ;; text node? why?
290 nil)
291 ((eq (xml-node-name (car children)) 'DAV:activelock)
292 (push (url-dav-dispatch-node (car children)) results))
293 (t
294 ;; Ignore unknown nodes...
295 nil))
296 (setq children (cdr children)))
297 results))
298
299(defun url-dav-process-DAV:status (node)
300 ;; The node contains a standard HTTP/1.1 response line... we really
301 ;; only care about the numeric status code.
302 (let ((status (url-dav-node-text node)))
303 (if (string-match "\\`[ \r\t\n]*HTTP/[0-9.]+ \\([0-9]+\\)" status)
304 (string-to-int (match-string 1 status))
305 500)))
306
307(defun url-dav-process-DAV:propstat (node)
308 ;; A propstate node can have the following children...
309 ;;
310 ;; DAV:prop - a list of properties and values
311 ;; DAV:status - An HTTP/1.1 status line
312 (let ((children (xml-node-children node))
313 (props nil)
314 (status nil))
315 (when (not children)
316 (error "No child nodes in DAV:propstat"))
317
318 (setq props (url-dav-dispatch-node (assq 'DAV:prop children))
319 status (url-dav-dispatch-node (assq 'DAV:status children)))
320
321 ;; Need to parse out the HTTP status
322 (setq props (plist-put props 'DAV:status status))
323 props))
324
325(defun url-dav-process-DAV:response (node)
326 (let ((children (xml-node-children node))
327 (propstat nil)
328 (href))
329 (when (not children)
330 (error "No child nodes in DAV:response"))
331
332 ;; A response node can have the following children...
333 ;;
334 ;; DAV:href - URL the response is for.
335 ;; DAV:propstat - see url-dav-process-propstat
336 ;; DAV:responsedescription - text description of the response
337 (setq propstat (assq 'DAV:propstat children)
338 href (assq 'DAV:href children))
339
340 (when (not href)
341 (error "No href in DAV:response"))
342
343 (when (not propstat)
344 (error "No propstat in DAV:response"))
345
346 (setq propstat (url-dav-dispatch-node propstat)
347 href (url-dav-dispatch-node href))
348 (cons href propstat)))
349
350(defun url-dav-process-DAV:multistatus (node)
351 (let ((children (xml-node-children node))
352 (results nil))
353 (while children
354 (push (url-dav-dispatch-node (car children)) results)
355 (setq children (cdr children)))
356 results))
357
358\f
359;;; DAV request/response generation/processing
360(defun url-dav-process-response (buffer url)
361 "Parses a WebDAV response from BUFFER, interpreting it relative to URL.
362
363The buffer must have been retrieved by HTTP or HTTPS and contain an
364XML document.
365"
366 (declare (special url-http-content-type
367 url-http-response-status
368 url-http-end-of-headers))
369 (let ((tree nil)
370 (overall-status nil))
371 (when buffer
372 (unwind-protect
373 (save-excursion
374 (set-buffer buffer)
375 (goto-char url-http-end-of-headers)
376 (setq overall-status url-http-response-status)
377
378 ;; XML documents can be transferred as either text/xml or
379 ;; application/xml, and we are required to accept both of
380 ;; them.
381 (if (and
382 url-http-content-type
383 (or (string-match "^text/xml" url-http-content-type)
384 (string-match "^application/xml" url-http-content-type)))
385 (setq tree (xml-parse-region (point) (point-max)))))
386 ;; Clean up after ourselves.
387 '(kill-buffer buffer)))
388
389 ;; We should now be
390 (if (eq (xml-node-name (car tree)) 'DAV:multistatus)
391 (url-dav-dispatch-node (car tree))
392 (url-debug 'dav "Got back singleton response for URL(%S)" url)
393 (let ((properties (url-dav-dispatch-node (car tree))))
394 ;; We need to make sure we have a DAV:status node in there for
395 ;; higher-level code;
396 (setq properties (plist-put properties 'DAV:status overall-status))
397 ;; Make this look like a DAV:multistatus parse tree so that
398 ;; nobody but us needs to know the difference.
399 (list (cons url properties))))))
400
401(defun url-dav-request (url method tag body
402 &optional depth headers namespaces)
403 "Performs WebDAV operation METHOD on URL. Returns the parsed responses.
404Automatically creates an XML request body if TAG is non-nil.
405BODY is the XML document fragment to be enclosed by <TAG></TAG>.
406
407DEPTH is how deep the request should propogate. Default is 0, meaning
408it should apply only to URL. A negative number means to use
409`Infinity' for the depth. Not all WebDAV servers support this depth
410though.
411
412HEADERS is an assoc list of extra headers to send in the request.
413
414NAMESPACES is an assoc list of (NAMESPACE . EXPANSION), and these are
415added to the <TAG> element. The DAV=DAV: namespace is automatically
416added to this list, so most requests can just pass in nil.
417"
418 ;; Take care of the default value for depth...
419 (setq depth (or depth 0))
420
421 ;; Now lets translate it into something webdav can understand.
422 (if (< depth 0)
423 (setq depth "Infinity")
424 (setq depth (int-to-string depth)))
425 (if (not (assoc "DAV" namespaces))
426 (setq namespaces (cons '("DAV" . "DAV:") namespaces)))
427
428 (let* ((url-request-extra-headers `(("Depth" . ,depth)
429 ("Content-type" . "text/xml")
430 ,@headers))
431 (url-request-method method)
432 (url-request-data
433 (if tag
434 (concat
435 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
436 "<" (symbol-name tag) " "
437 ;; add in the appropriate namespaces...
438 (mapconcat (lambda (ns)
439 (concat "xmlns:" (car ns) "='" (cdr ns) "'"))
440 namespaces "\n ")
441 ">\n"
442 body
443 "</" (symbol-name tag) ">\n"))))
444 (url-dav-process-response (url-retrieve-synchronously url) url)))
445
446;;;###autoload
447(defun url-dav-get-properties (url &optional attributes depth namespaces)
448 "Return properties for URL, up to DEPTH levels deep.
449
450Returns an assoc list, where the key is the filename (possibly a full
451URI), and the value is a standard property list of DAV property
452names (ie: DAV:resourcetype).
453"
454 (url-dav-request url "PROPFIND" 'DAV:propfind
455 (if attributes
456 (mapconcat (lambda (attr)
457 (concat "<DAV:prop><"
458 (symbol-name attr)
459 "/></DAV:prop>"))
460 attributes "\n ")
461 " <DAV:allprop/>")
462 depth nil namespaces))
463
464(defmacro url-dav-http-success-p (status)
465 "Return whether PROPERTIES was the result of a successful DAV request."
466 `(= (/ (or ,status 500) 100) 2))
467
468\f
469;;; Locking support
470(defvar url-dav-lock-identifier (concat "mailto:" user-mail-address)
471 "*URL used as contact information when creating locks in DAV.
472This will be used as the contents of the DAV:owner/DAV:href tag to
473identify the owner of a LOCK when requesting it. This will be shown
474to other users when the DAV:lockdiscovery property is requested, so
475make sure you are comfortable with it leaking to the outside world.
476")
477
478;;;###autoload
479(defun url-dav-lock-resource (url exclusive &optional depth)
480 "Request a lock on URL. If EXCLUSIVE is non-nil, get an exclusive lock.
481Optional 3rd argument DEPTH says how deep the lock should go, default is 0
482\(lock only the resource and none of its children\).
483
484Returns a cons-cell of (SUCCESSFUL-RESULTS . FAILURE-RESULTS).
485SUCCESSFUL-RESULTS is a list of (URL STATUS locktoken).
486FAILURE-RESULTS is a list of (URL STATUS).
487"
488 (setq exclusive (if exclusive "<DAV:exclusive/>" "<DAV:shared/>"))
489 (let* ((body
490 (concat
491 " <DAV:lockscope>" exclusive "</DAV:lockscope>\n"
492 " <DAV:locktype> <DAV:write/> </DAV:locktype>\n"
493 " <DAV:owner>\n"
494 " <DAV:href>" url-dav-lock-identifier "</DAV:href>\n"
495 " </DAV:owner>\n"))
496 (response nil) ; Responses to the LOCK request
497 (result nil) ; For walking thru the response list
498 (child-url nil)
499 (child-status nil)
500 (failures nil) ; List of failure cases (URL . STATUS)
501 (successes nil)) ; List of success cases (URL . STATUS)
502 (setq response (url-dav-request url "LOCK" 'DAV:lockinfo body
503 depth '(("Timeout" . "Infinite"))))
504
505 ;; Get the parent URL ready for expand-file-name
506 (if (not (vectorp url))
507 (setq url (url-generic-parse-url url)))
508
509 ;; Walk thru the response list, fully expand the URL, and grab the
510 ;; status code.
511 (while response
512 (setq result (pop response)
513 child-url (url-expand-file-name (pop result) url)
514 child-status (or (plist-get result 'DAV:status) 500))
515 (if (url-dav-http-success-p child-status)
516 (push (list url child-status "huh") successes)
517 (push (list url child-status) failures)))
518 (cons successes failures)))
519
520;;;###autoload
521(defun url-dav-active-locks (url &optional depth)
522 "Return an assoc list of all active locks on URL."
523 (let ((response (url-dav-get-properties url '(DAV:lockdiscovery) depth))
524 (properties nil)
525 (child nil)
526 (child-url nil)
527 (child-results nil)
528 (results nil))
529 (if (not (vectorp url))
530 (setq url (url-generic-parse-url url)))
531
532 (while response
533 (setq child (pop response)
534 child-url (pop child)
535 child-results nil)
536 (when (and (url-dav-http-success-p (plist-get child 'DAV:status))
537 (setq child (plist-get child 'DAV:lockdiscovery)))
538 ;; After our parser has had its way with it, The
539 ;; DAV:lockdiscovery property is a list of DAV:activelock
540 ;; objects, which are comprised of DAV:activelocks, which
541 ;; assoc lists of properties and values.
542 (while child
543 (if (assq 'DAV:locktoken (car child))
544 (let ((tokens (cdr (assq 'DAV:locktoken (car child))))
545 (owners (cdr (assq 'DAV:owner (car child)))))
546 (dolist (token tokens)
547 (dolist (owner owners)
548 (push (cons token owner) child-results)))))
549 (pop child)))
550 (if child-results
551 (push (cons (url-expand-file-name child-url url) child-results)
552 results)))
553 results))
554
555;;;###autoload
556(defun url-dav-unlock-resource (url lock-token)
557 "Release the lock on URL represented by LOCK-TOKEN.
558Returns `t' iff the lock was successfully released.
559"
560 (declare (special url-http-response-status))
561 (let* ((url-request-extra-headers (list (cons "Lock-Token"
562 (concat "<" lock-token ">"))))
563 (url-request-method "UNLOCK")
564 (url-request-data nil)
565 (buffer (url-retrieve-synchronously url))
566 (result nil))
567 (when buffer
568 (unwind-protect
569 (save-excursion
570 (set-buffer buffer)
571 (setq result (url-dav-http-success-p url-http-response-status)))
572 (kill-buffer buffer)))
573 result))
574
575\f
576;;; file-name-handler stuff
577(defun url-dav-file-attributes-mode-string (properties)
578 (let ((modes (make-string 10 ?-))
579 (supported-locks (plist-get properties 'DAV:supportedlock))
580 (executable-p (equal (plist-get properties 'http://apache.org/dav/props/executable)
581 "T"))
582 (directory-p (memq 'DAV:collection (plist-get properties 'DAV:resourcetype)))
583 (readable t)
584 (lock nil))
585 ;; Assume we can read this, otherwise the PROPFIND would have
586 ;; failed.
587 (when readable
588 (aset modes 1 ?r)
589 (aset modes 4 ?r)
590 (aset modes 7 ?r))
591
592 (when directory-p
593 (aset modes 0 ?d))
594
595 (when executable-p
596 (aset modes 3 ?x)
597 (aset modes 6 ?x)
598 (aset modes 9 ?x))
599
600 (while supported-locks
601 (setq lock (car supported-locks)
602 supported-locks (cdr supported-locks))
603 (case (car lock)
604 (DAV:write
605 (case (cdr lock)
606 (DAV:shared ; group permissions (possibly world)
607 (aset modes 5 ?w))
608 (DAV:exclusive
609 (aset modes 2 ?w)) ; owner permissions?
610 (otherwise
611 (url-debug 'dav "Unrecognized DAV:lockscope (%S)" (cdr lock)))))
612 (otherwise
613 (url-debug 'dav "Unrecognized DAV:locktype (%S)" (car lock)))))
614 modes))
615
616;;;###autoload
617(defun url-dav-file-attributes (url)
618 (let ((properties (cdar (url-dav-get-properties url)))
619 (attributes nil))
620 (if (and properties
621 (url-dav-http-success-p (plist-get properties 'DAV:status)))
622 ;; We got a good DAV response back..
623 (setq attributes
624 (list
625 ;; t for directory, string for symbolic link, or nil
626 ;; Need to support DAV Bindings to figure out the
627 ;; symbolic link issues.
628 (if (memq 'DAV:collection (plist-get properties 'DAV:resourcetype)) t nil)
629
630 ;; Number of links to file... Needs DAV Bindings.
631 1
632
633 ;; File uid - no way to figure out?
634 0
635
636 ;; File gid - no way to figure out?
637 0
638
639 ;; Last access time - ???
640 nil
641
642 ;; Last modification time
643 (plist-get properties 'DAV:getlastmodified)
644
645 ;; Last status change time... just reuse last-modified
646 ;; for now.
647 (plist-get properties 'DAV:getlastmodified)
648
649 ;; size in bytes
650 (or (plist-get properties 'DAV:getcontentlength) 0)
651
652 ;; file modes as a string like `ls -l'
653 ;;
654 ;; Should be able to build this up from the
655 ;; DAV:supportedlock attribute pretty easily. Getting
656 ;; the group info could be impossible though.
657 (url-dav-file-attributes-mode-string properties)
658
659 ;; t iff file's gid would change if it were deleted &
660 ;; recreated. No way for us to know that thru DAV.
661 nil
662
663 ;; inode number - meaningless
664 nil
665
666 ;; device number - meaningless
667 nil))
668 ;; Fall back to just the normal http way of doing things.
669 (setq attributes (url-http-head-file-attributes url)))
670 attributes))
671
672;;;###autoload
673(defun url-dav-save-resource (url obj &optional content-type lock-token)
674 "Save OBJ as URL using WebDAV.
675URL must be a fully qualified URL.
676OBJ may be a buffer or a string."
677 (let ((buffer nil)
678 (result nil)
679 (url-request-extra-headers nil)
680 (url-request-method "PUT")
681 (url-request-data
682 (cond
683 ((bufferp obj)
684 (save-excursion
685 (set-buffer obj)
686 (buffer-string)))
687 ((stringp obj)
688 obj)
689 (t
690 (error "Invalid object to url-dav-save-resource")))))
691
692 (if lock-token
693 (push
694 (cons "If" (concat "(<" lock-token ">)"))
695 url-request-extra-headers))
696
697 ;; Everything must always have a content-type when we submit it.
698 (push
699 (cons "Content-type" (or content-type "application/octet-stream"))
700 url-request-extra-headers)
701
702 ;; Do the save...
703 (setq buffer (url-retrieve-synchronously url))
704
705 ;; Sanity checking
706 (when buffer
707 (unwind-protect
708 (save-excursion
709 (set-buffer buffer)
710 (setq result (url-dav-http-success-p url-http-response-status)))
711 (kill-buffer buffer)))
712 result))
713
714(eval-when-compile
715 (defmacro url-dav-delete-something (url lock-token &rest error-checking)
716 "Delete URL completely, with no sanity checking whatsoever. DO NOT USE.
717This is defined as a macro that will not be visible from compiled files.
718Use with care, and even then think three times.
719"
720 `(progn
721 ,@error-checking
722 (url-dav-request ,url "DELETE" nil nil -1
723 (if ,lock-token
724 (list
725 (cons "If"
726 (concat "(<" ,lock-token ">)"))))))))
727
728
729;;;###autoload
730(defun url-dav-delete-directory (url &optional recursive lock-token)
731 "Delete the WebDAV collection URL.
732If optional second argument RECURSIVE is non-nil, then delete all
733files in the collection as well.
734"
735 (let ((status nil)
736 (props nil)
737 (props nil))
738 (setq props (url-dav-delete-something
739 url lock-token
740 (setq props (url-dav-get-properties url '(DAV:getcontenttype) 1))
741 (if (and (not recursive)
742 (/= (length props) 1))
743 (signal 'file-error (list "Removing directory"
744 "directory not empty" url)))))
745
746 (mapc (lambda (result)
747 (setq status (plist-get (cdr result) 'DAV:status))
748 (if (not (url-dav-http-success-p status))
749 (signal 'file-error (list "Removing directory"
750 "Errror removing"
751 (car result) status))))
752 props))
753 nil)
754
755;;;###autoload
756(defun url-dav-delete-file (url &optional lock-token)
757 "Delete file named URL."
758 (let ((props nil)
759 (status nil))
760 (setq props (url-dav-delete-something
761 url lock-token
762 (setq props (url-dav-get-properties url))
763 (if (eq (plist-get (cdar props) 'DAV:resourcetype) 'DAV:collection)
764 (signal 'file-error (list "Removing old name" "is a collection" url)))))
765
766 (mapc (lambda (result)
767 (setq status (plist-get (cdr result) 'DAV:status))
768 (if (not (url-dav-http-success-p status))
769 (signal 'file-error (list "Removing old name"
770 "Errror removing"
771 (car result) status))))
772 props))
773 nil)
774
775;;;###autoload
776(defun url-dav-directory-files (url &optional full match nosort files-only)
777 "Return a list of names of files in DIRECTORY.
778There are three optional arguments:
779If FULL is non-nil, return absolute file names. Otherwise return names
780 that are relative to the specified directory.
781If MATCH is non-nil, mention only file names that match the regexp MATCH.
782If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
783 NOSORT is useful if you plan to sort the result yourself.
784"
785 (let ((properties (url-dav-get-properties url '(DAV:resourcetype) 1))
786 (child-url nil)
787 (child-props nil)
788 (files nil)
789 (parsed-url (url-generic-parse-url url)))
790
791 (if (= (length properties) 1)
792 (signal 'file-error (list "Opening directory" "not a directory" url)))
793
794 (while properties
795 (setq child-props (pop properties)
796 child-url (pop child-props))
797 (if (and (eq (plist-get child-props 'DAV:resourcetype) 'DAV:collection)
798 files-only)
799 ;; It is a directory, and we were told to return just files.
800 nil
801
802 ;; Fully expand the URL and then rip off the beginning if we
803 ;; are not supposed to return fully-qualified names.
804 (setq child-url (url-expand-file-name child-url parsed-url))
805 (if (not full)
806 (setq child-url (substring child-url (length url))))
807
808 ;; We don't want '/' as the last character in filenames...
809 (if (string-match "/$" child-url)
810 (setq child-url (substring child-url 0 -1)))
811
812 ;; If we have a match criteria, then apply it.
813 (if (or (and match (not (string-match match child-url)))
814 (string= child-url "")
815 (string= child-url url))
816 nil
817 (push child-url files))))
818
819 (if nosort
820 files
821 (sort files 'string-lessp))))
822
823;;;###autoload
824(defun url-dav-file-directory-p (url)
825 "Return t if URL names an existing DAV collection."
826 (let ((properties (cdar (url-dav-get-properties url '(DAV:resourcetype)))))
827 (eq (plist-get properties 'DAV:resourcetype) 'DAV:collection)))
828
829;;;###autoload
830(defun url-dav-make-directory (url &optional parents)
831 "Create the directory DIR and any nonexistent parent dirs."
832 (declare (special url-http-response-status))
833 (let* ((url-request-extra-headers nil)
834 (url-request-method "MKCOL")
835 (url-request-data nil)
836 (buffer (url-retrieve-synchronously url))
837 (result nil))
838 (when buffer
839 (unwind-protect
840 (save-excursion
841 (set-buffer buffer)
842 (case url-http-response-status
843 (201 ; Collection created in its entirety
844 (setq result t))
845 (403 ; Forbidden
846 nil)
847 (405 ; Method not allowed
848 nil)
849 (409 ; Conflict
850 nil)
851 (415 ; Unsupported media type (WTF?)
852 nil)
853 (507 ; Insufficient storage
854 nil)
855 (otherwise
856 nil)))
857 (kill-buffer buffer)))
858 result))
859
860;;;###autoload
861(defun url-dav-rename-file (oldname newname &optional overwrite)
862 (if (not (and (string-match url-handler-regexp oldname)
863 (string-match url-handler-regexp newname)))
864 (signal 'file-error "Cannot rename between different URL backends" oldname newname))
865
866 (let* ((headers nil)
867 (props nil)
868 (status nil)
869 (directory-p (url-dav-file-directory-p oldname))
870 (exists-p (url-http-file-exists-p newname)))
871
872 (if (and exists-p
873 (or
874 (null overwrite)
875 (and (numberp overwrite)
876 (not (yes-or-no-p
877 (format "File %s already exists; rename to it anyway? "
878 newname))))))
879 (signal 'file-already-exists (list "File already exists" newname)))
880
881 ;; Honor the overwrite flag...
882 (if overwrite (push '("Overwrite" . "T") headers))
883
884 ;; Have to tell them where to copy it to!
885 (push (cons "Destination" newname) headers)
886
887 ;; Always send a depth of -1 in case we are moving a collection.
888 (setq props (url-dav-request oldname "MOVE" nil nil (if directory-p -1 0)
889 headers))
890
891 (mapc (lambda (result)
892 (setq status (plist-get (cdr result) 'DAV:status))
893
894 (if (not (url-dav-http-success-p status))
895 (signal 'file-error (list "Renaming" oldname newname status))))
896 props)
897 t))
898
899;;;###autoload
900(defun url-dav-file-name-all-completions (file url)
901 "Return a list of all completions of file name FILE in directory DIRECTORY.
902These are all file names in directory DIRECTORY which begin with FILE.
903"
904 (url-dav-directory-files url nil (concat "^" file ".*")))
905
906;;;###autoload
907(defun url-dav-file-name-completion (file url)
908 "Complete file name FILE in directory DIRECTORY.
909Returns the longest string
910common to all file names in DIRECTORY that start with FILE.
911If there is only one and FILE matches it exactly, returns t.
912Returns nil if DIR contains no name starting with FILE.
913"
914 (let ((matches (url-dav-file-name-all-completions file url))
915 (result nil))
916 (cond
917 ((null matches)
918 ;; No matches
919 nil)
920 ((and (= (length matches) 1)
921 (string= file (car matches)))
922 ;; Only one file and FILE matches it exactly...
923 t)
924 (t
925 ;; Need to figure out the longest string that they have in commmon
926 (setq matches (sort matches (lambda (a b) (> (length a) (length b)))))
927 (let ((n (length file))
928 (searching t)
929 (regexp nil)
930 (failed nil))
931 (while (and searching
932 (< n (length (car matches))))
933 (setq regexp (concat "^" (substring (car matches) 0 (1+ n)))
934 failed nil)
935 (dolist (potential matches)
936 (if (not (string-match regexp potential))
937 (setq failed t)))
938 (if failed
939 (setq searching nil)
940 (incf n)))
941 (substring (car matches) 0 n))))))
942
943(defun url-dav-register-handler (op)
944 (put op 'url-file-handlers (intern-soft (format "url-dav-%s" op))))
945
946(mapcar 'url-dav-register-handler
947 '(file-name-all-completions
948 file-name-completion
949 rename-file
950 make-directory
951 file-directory-p
952 directory-files
953 delete-file
954 delete-directory
955 file-attributes))
956
957\f
958;;; Version Control backend cruft
959
960;(put 'vc-registered 'url-file-handlers 'url-dav-vc-registered)
961
962;;;###autoload
963(defun url-dav-vc-registered (url)
964 (if (and (string-match "\\`https?" url)
965 (plist-get (url-http-options url) 'dav))
966 (progn
967 (vc-file-setprop url 'vc-backend 'dav)
968 t)))
969
970\f
971;;; Miscellaneous stuff.
972
973(provide 'url-dav)