url-handlers.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. ;;; url-handlers.el --- file-name-handler stuff for URL loading -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1996-1999, 2004-2015 Free Software Foundation, Inc.
  3. ;; Keywords: comm, data, processes, hypermedia
  4. ;; This file is part of GNU Emacs.
  5. ;;
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; (require 'url)
  19. (require 'url-parse)
  20. ;; (require 'url-util)
  21. (eval-when-compile (require 'mm-decode))
  22. ;; (require 'mailcap)
  23. ;; The following are autoloaded instead of `require'd to avoid eagerly
  24. ;; loading all of URL when turning on url-handler-mode in the .emacs.
  25. (autoload 'url-expand-file-name "url-expand" "Convert url to a fully specified url, and canonicalize it.")
  26. (autoload 'mm-dissect-buffer "mm-decode" "Dissect the current buffer and return a list of MIME handles.")
  27. (autoload 'url-scheme-get-property "url-methods" "Get property of a URL SCHEME.")
  28. ;; Always used after mm-dissect-buffer and defined in the same file.
  29. (declare-function mm-save-part-to-file "mm-decode" (handle file))
  30. (declare-function mm-destroy-parts "mm-decode" (handles))
  31. ;; mm-decode loads mm-bodies.
  32. (declare-function mm-decode-string "mm-bodies" (string charset))
  33. ;; mm-decode loads mail-parse.
  34. (declare-function mail-content-type-get "mail-parse" (ct attribute))
  35. ;; Implementation status
  36. ;; ---------------------
  37. ;; Function Status
  38. ;; ------------------------------------------------------------
  39. ;; add-name-to-file Needs DAV Bindings
  40. ;; copy-file Broken (assumes 1st item is URL)
  41. ;; delete-directory Finished (DAV)
  42. ;; delete-file Finished (DAV)
  43. ;; diff-latest-backup-file
  44. ;; directory-file-name unnecessary
  45. ;; directory-files Finished (DAV)
  46. ;; dired-call-process
  47. ;; dired-compress-file
  48. ;; dired-uncache
  49. ;; expand-file-name Finished
  50. ;; file-accessible-directory-p
  51. ;; file-attributes Finished, better with DAV
  52. ;; file-directory-p Needs DAV, finished
  53. ;; file-executable-p Finished
  54. ;; file-exists-p Finished
  55. ;; file-local-copy
  56. ;; file-modes
  57. ;; file-name-all-completions Finished (DAV)
  58. ;; file-name-as-directory
  59. ;; file-name-completion Finished (DAV)
  60. ;; file-name-directory
  61. ;; file-name-nondirectory
  62. ;; file-name-sans-versions why?
  63. ;; file-newer-than-file-p
  64. ;; file-ownership-preserved-p No way to know
  65. ;; file-readable-p Finished
  66. ;; file-regular-p !directory_p
  67. ;; file-remote-p Finished
  68. ;; file-symlink-p Needs DAV bindings
  69. ;; file-truename Needs DAV bindings
  70. ;; file-writable-p Check for LOCK?
  71. ;; find-backup-file-name why?
  72. ;; get-file-buffer why?
  73. ;; insert-directory Use DAV
  74. ;; insert-file-contents Finished
  75. ;; load
  76. ;; make-directory Finished (DAV)
  77. ;; make-symbolic-link Needs DAV bindings
  78. ;; rename-file Finished (DAV)
  79. ;; set-file-modes Use mod_dav specific executable flag?
  80. ;; set-visited-file-modtime Impossible?
  81. ;; shell-command Impossible?
  82. ;; unhandled-file-name-directory
  83. ;; vc-registered Finished (DAV)
  84. ;; verify-visited-file-modtime
  85. ;; write-region
  86. (defvar url-handler-regexp) ; defined below to avoid recursive load (revno:108572)
  87. ;;;###autoload
  88. (define-minor-mode url-handler-mode
  89. "Toggle using `url' library for URL filenames (URL Handler mode).
  90. With a prefix argument ARG, enable URL Handler mode if ARG is
  91. positive, and disable it otherwise. If called from Lisp, enable
  92. the mode if ARG is omitted or nil."
  93. :global t :group 'url
  94. ;; Remove old entry, if any.
  95. (setq file-name-handler-alist
  96. (delq (rassq 'url-file-handler file-name-handler-alist)
  97. file-name-handler-alist))
  98. (if url-handler-mode
  99. (push (cons url-handler-regexp 'url-file-handler)
  100. file-name-handler-alist)))
  101. (defcustom url-handler-regexp "\\`\\(https?\\|ftp\\|file\\|nfs\\|ssh\\|scp\\|rsync\\|telnet\\)://"
  102. "Regular expression for URLs handled by `url-handler-mode'.
  103. When URL Handler mode is enabled, this regular expression is
  104. added to `file-name-handler-alist'.
  105. Some valid URL protocols just do not make sense to visit
  106. interactively \(about, data, info, irc, mailto, etc\). This
  107. regular expression avoids conflicts with local files that look
  108. like URLs \(Gnus is particularly bad at this\)."
  109. :group 'url
  110. :type 'regexp
  111. :version "25.1"
  112. :set (lambda (symbol value)
  113. (let ((enable url-handler-mode))
  114. (url-handler-mode 0)
  115. (set-default symbol value)
  116. (if enable
  117. (url-handler-mode)))))
  118. (defun url-run-real-handler (operation args)
  119. (let ((inhibit-file-name-handlers (cons 'url-file-handler
  120. (if (eq operation inhibit-file-name-operation)
  121. inhibit-file-name-handlers)))
  122. (inhibit-file-name-operation operation))
  123. (apply operation args)))
  124. (defvar url-file-handler-load-in-progress nil
  125. "Check for recursive load.")
  126. ;;;###autoload
  127. (defun url-file-handler (operation &rest args)
  128. "Function called from the `file-name-handler-alist' routines.
  129. OPERATION is what needs to be done (`file-exists-p', etc). ARGS are
  130. the arguments that would have been passed to OPERATION."
  131. ;; Avoid recursive load.
  132. (if (and load-in-progress url-file-handler-load-in-progress)
  133. (url-run-real-handler operation args)
  134. (let ((url-file-handler-load-in-progress load-in-progress))
  135. ;; Check, whether there are arguments we want pass to Tramp.
  136. (if (catch :do
  137. (dolist (url (cons default-directory args))
  138. (and (member
  139. (url-type (url-generic-parse-url (and (stringp url) url)))
  140. url-tramp-protocols)
  141. (throw :do t))))
  142. (apply 'url-tramp-file-handler operation args)
  143. ;; Otherwise, let's do the job.
  144. (let ((fn (get operation 'url-file-handlers))
  145. (val nil)
  146. (hooked nil))
  147. (if (and (not fn) (intern-soft (format "url-%s" operation))
  148. (fboundp (intern-soft (format "url-%s" operation))))
  149. (error "Missing URL handler mapping for %s" operation))
  150. (if fn
  151. (setq hooked t
  152. val (save-match-data (apply fn args)))
  153. (setq hooked nil
  154. val (url-run-real-handler operation args)))
  155. (url-debug 'handlers "%s %S%S => %S" (if hooked "Hooked" "Real")
  156. operation args val)
  157. val)))))
  158. (defun url-file-handler-identity (&rest args)
  159. ;; Identity function
  160. (car args))
  161. ;; These are operations that we can fully support
  162. (put 'file-readable-p 'url-file-handlers 'url-file-exists-p)
  163. (put 'substitute-in-file-name 'url-file-handlers 'url-file-handler-identity)
  164. (put 'file-name-absolute-p 'url-file-handlers (lambda (&rest ignored) t))
  165. (put 'expand-file-name 'url-file-handlers 'url-handler-expand-file-name)
  166. (put 'directory-file-name 'url-file-handlers 'url-handler-directory-file-name)
  167. (put 'unhandled-file-name-directory 'url-file-handlers 'url-handler-unhandled-file-name-directory)
  168. (put 'file-remote-p 'url-file-handlers 'url-handler-file-remote-p)
  169. ;; (put 'file-name-as-directory 'url-file-handlers 'url-handler-file-name-as-directory)
  170. ;; These are operations that we do not support yet (DAV!!!)
  171. (put 'file-writable-p 'url-file-handlers 'ignore)
  172. (put 'file-symlink-p 'url-file-handlers 'ignore)
  173. ;; Just like for ange-ftp: let's not waste time trying to look for RCS/foo,v
  174. ;; files and such since we can't do anything clever with them anyway.
  175. (put 'vc-registered 'url-file-handlers 'ignore)
  176. (defun url-handler-expand-file-name (file &optional base)
  177. ;; When we see "/foo/bar" in a file whose working dir is "http://bla/bla",
  178. ;; there are two interpretations possible: either it's a local "/foo/bar"
  179. ;; or it's "http:/bla/foo/bar". When working with URLs, the second
  180. ;; interpretation is the right one, but when working with Emacs file
  181. ;; names, the first is preferred.
  182. (if (file-name-absolute-p file)
  183. (expand-file-name file "/")
  184. (url-expand-file-name file base)))
  185. ;; directory-file-name and file-name-as-directory are kind of hard to
  186. ;; implement really right for URLs since URLs can have repeated / chars.
  187. ;; We'd want the following behavior:
  188. ;; idempotence: (d-f-n (d-f-n X) == (d-f-n X)
  189. ;; idempotence: (f-n-a-d (f-n-a-d X) == (f-n-a-d X)
  190. ;; reversible: (d-f-n (f-n-a-d (d-f-n X))) == (d-f-n X)
  191. ;; reversible: (f-n-a-d (d-f-n (f-n-a-d X))) == (f-n-a-d X)
  192. (defun url-handler-directory-file-name (dir)
  193. ;; When there's more than a single /, just don't touch the slashes at all.
  194. (if (string-match "//\\'" dir) dir
  195. (url-run-real-handler 'directory-file-name (list dir))))
  196. (defun url-handler-unhandled-file-name-directory (filename)
  197. (let ((url (url-generic-parse-url filename)))
  198. (if (equal (url-type url) "file")
  199. ;; `file' URLs are actually local. The filename part may be ""
  200. ;; which really stands for "/".
  201. ;; FIXME: maybe we should check that the host part is "" or "localhost"
  202. ;; or some name that represents the local host?
  203. (or (file-name-directory (url-filename url)) "/")
  204. ;; All other URLs are not expected to be directly accessible from
  205. ;; a local process.
  206. nil)))
  207. (defun url-handler-file-remote-p (filename &optional identification _connected)
  208. (let ((url (url-generic-parse-url filename)))
  209. (if (and (url-type url) (not (equal (url-type url) "file")))
  210. ;; Maybe we can find a suitable check for CONNECTED. For now,
  211. ;; we ignore it.
  212. (cond
  213. ((eq identification 'method) (url-type url))
  214. ((eq identification 'user) (url-user url))
  215. ((eq identification 'host) (url-host url))
  216. ((eq identification 'localname) (url-filename url))
  217. (t (url-recreate-url
  218. (url-parse-make-urlobj (url-type url) (url-user url) nil
  219. (url-host url) (url-port url)))))
  220. ;; If there is no URL type, or it is a "file://" URL, the
  221. ;; filename is expected to be non remote. A more subtle check
  222. ;; for "file://" URLs could be applied, as said in
  223. ;; `url-handler-unhandled-file-name-directory'.
  224. nil)))
  225. ;; The actual implementation
  226. ;;;###autoload
  227. (defun url-copy-file (url newname &optional ok-if-already-exists
  228. _keep-time _preserve-uid-gid)
  229. "Copy URL to NEWNAME. Both args must be strings.
  230. Signals a `file-already-exists' error if file NEWNAME already exists,
  231. unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
  232. A number as third arg means request confirmation if NEWNAME already exists.
  233. This is what happens in interactive use with M-x.
  234. Fourth arg KEEP-TIME non-nil means give the new file the same
  235. last-modified time as the old one. (This works on only some systems.)
  236. Fifth arg PRESERVE-UID-GID is ignored.
  237. A prefix arg makes KEEP-TIME non-nil."
  238. (if (and (file-exists-p newname)
  239. (not ok-if-already-exists))
  240. (error "Opening output file: File already exists, %s" newname))
  241. (let ((buffer (url-retrieve-synchronously url))
  242. (handle nil))
  243. (if (not buffer)
  244. (error "Opening input file: No such file or directory, %s" url))
  245. (with-current-buffer buffer
  246. (setq handle (mm-dissect-buffer t)))
  247. (mm-save-part-to-file handle newname)
  248. (kill-buffer buffer)
  249. (mm-destroy-parts handle)))
  250. (put 'copy-file 'url-file-handlers 'url-copy-file)
  251. ;;;###autoload
  252. (defun url-file-local-copy (url &rest ignored)
  253. "Copy URL into a temporary file on this machine.
  254. Returns the name of the local copy, or nil, if FILE is directly
  255. accessible."
  256. (let ((filename (make-temp-file "url")))
  257. (url-copy-file url filename 'ok-if-already-exists)
  258. filename))
  259. (put 'file-local-copy 'url-file-handlers 'url-file-local-copy)
  260. (defun url-insert (buffer &optional beg end)
  261. "Insert the body of a URL object.
  262. BUFFER should be a complete URL buffer as returned by `url-retrieve'.
  263. If the headers specify a coding-system, it is applied to the body before it is inserted.
  264. Returns a list of the form (SIZE CHARSET), where SIZE is the size in bytes
  265. of the inserted text and CHARSET is the charset that was specified in the header,
  266. or nil if none was found.
  267. BEG and END can be used to only insert a subpart of the body.
  268. They count bytes from the beginning of the body."
  269. (let* ((handle (with-current-buffer buffer (mm-dissect-buffer t)))
  270. (data (with-current-buffer (mm-handle-buffer handle)
  271. (if beg
  272. (buffer-substring (+ (point-min) beg)
  273. (if end (+ (point-min) end) (point-max)))
  274. (buffer-string))))
  275. (charset (mail-content-type-get (mm-handle-type handle)
  276. 'charset)))
  277. (mm-destroy-parts handle)
  278. (if charset
  279. (insert (mm-decode-string data (mm-charset-to-coding-system charset)))
  280. (insert data))
  281. (list (length data) charset)))
  282. (defvar url-http-codes)
  283. ;;;###autoload
  284. (defun url-insert-file-contents (url &optional visit beg end replace)
  285. (let ((buffer (url-retrieve-synchronously url)))
  286. (unless buffer (signal 'file-error (list url "No Data")))
  287. (with-current-buffer buffer
  288. ;; XXX: This is HTTP/S specific and should be moved to url-http
  289. ;; instead. See http://debbugs.gnu.org/17549.
  290. (when (bound-and-true-p url-http-response-status)
  291. (unless (and (>= url-http-response-status 200)
  292. (< url-http-response-status 300))
  293. (let ((desc (nth 2 (assq url-http-response-status url-http-codes))))
  294. (kill-buffer buffer)
  295. ;; Signal file-error per http://debbugs.gnu.org/16733.
  296. (signal 'file-error (list url desc))))))
  297. (if visit (setq buffer-file-name url))
  298. (save-excursion
  299. (let* ((start (point))
  300. (size-and-charset (url-insert buffer beg end)))
  301. (kill-buffer buffer)
  302. (when replace
  303. (delete-region (point-min) start)
  304. (delete-region (point) (point-max)))
  305. (unless (cadr size-and-charset)
  306. ;; If the headers don't specify any particular charset, use the
  307. ;; usual heuristic/rules that we apply to files.
  308. (decode-coding-inserted-region start (point) url
  309. visit beg end replace))
  310. (let ((inserted (car size-and-charset)))
  311. (when (fboundp 'after-insert-file-set-coding)
  312. (let ((insval (after-insert-file-set-coding inserted visit)))
  313. (if insval (setq inserted insval))))
  314. (list url inserted))))))
  315. (put 'insert-file-contents 'url-file-handlers 'url-insert-file-contents)
  316. (defun url-file-name-completion (url _directory &optional _predicate)
  317. ;; Even if it's not implemented, it's not an error to ask for completion,
  318. ;; in case it's available (bug#14806).
  319. ;; (error "Unimplemented")
  320. url)
  321. (put 'file-name-completion 'url-file-handlers 'url-file-name-completion)
  322. (defun url-file-name-all-completions (_file _directory)
  323. ;; Even if it's not implemented, it's not an error to ask for completion,
  324. ;; in case it's available (bug#14806).
  325. ;; (error "Unimplemented")
  326. nil)
  327. (put 'file-name-all-completions
  328. 'url-file-handlers 'url-file-name-all-completions)
  329. ;; All other handlers map onto their respective backends.
  330. (defmacro url-handlers-create-wrapper (method args)
  331. `(progn
  332. (defun ,(intern (format "url-%s" method)) ,args
  333. ,(format "URL file-name-handler wrapper for `%s' call.\n---\n%s" method
  334. (or (documentation method t) "No original documentation."))
  335. (setq url (url-generic-parse-url url))
  336. (when (url-type url)
  337. (funcall (url-scheme-get-property (url-type url) (quote ,method))
  338. ,@(remove '&rest (remove '&optional args)))))
  339. (unless (get ',method 'url-file-handlers)
  340. (put ',method 'url-file-handlers ',(intern (format "url-%s" method))))))
  341. (url-handlers-create-wrapper file-exists-p (url))
  342. (url-handlers-create-wrapper file-attributes (url &optional id-format))
  343. (url-handlers-create-wrapper file-symlink-p (url))
  344. (url-handlers-create-wrapper file-writable-p (url))
  345. (url-handlers-create-wrapper file-directory-p (url))
  346. (url-handlers-create-wrapper file-executable-p (url))
  347. (url-handlers-create-wrapper directory-files (url &optional full match nosort))
  348. (url-handlers-create-wrapper file-truename (url &optional counter prev-dirs))
  349. (add-hook 'find-file-hook 'url-handlers-set-buffer-mode)
  350. (defun url-handlers-set-buffer-mode ()
  351. "Set correct modes for the current buffer if visiting a remote file."
  352. (and (stringp buffer-file-name)
  353. (string-match url-handler-regexp buffer-file-name)
  354. (auto-save-mode 0)))
  355. (provide 'url-handlers)
  356. ;;; url-handlers.el ends here