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