guile feature
[bpt/emacs.git] / lisp / url / url-expand.el
CommitLineData
8c8b8430 1;;; url-expand.el --- expand-file-name for URLs
00eef4de 2
ba318903 3;; Copyright (C) 1999, 2004-2014 Free Software Foundation, Inc.
00eef4de 4
8c8b8430
SM
5;; Keywords: comm, data, processes
6
00eef4de
LH
7;; This file is part of GNU Emacs.
8
4936186e 9;; GNU Emacs is free software: you can redistribute it and/or modify
00eef4de 10;; it under the terms of the GNU General Public License as published by
4936186e
GM
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
00eef4de
LH
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
4936186e 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
00eef4de
LH
21
22;;; Code:
8c8b8430
SM
23
24(require 'url-methods)
25(require 'url-util)
26(require 'url-parse)
27
28(defun url-expander-remove-relative-links (name)
29 ;; Strip . and .. from pathnames
30 (let ((new (if (not (string-match "^/" name))
31 (concat "/" name)
32 name)))
33
34 ;; If it ends with a '/.' or '/..', tack on a trailing '/' sot hat
35 ;; the tests that follow are not too complicated in terms of
36 ;; looking for '..' or '../', etc.
37 (if (string-match "/\\.+$" new)
38 (setq new (concat new "/")))
39
40 ;; Remove '/./' first
41 (while (string-match "/\\(\\./\\)" new)
42 (setq new (concat (substring new 0 (match-beginning 1))
43 (substring new (match-end 1)))))
44
45 ;; Then remove '/../'
46 (while (string-match "/\\([^/]*/\\.\\./\\)" new)
47 (setq new (concat (substring new 0 (match-beginning 1))
48 (substring new (match-end 1)))))
49
50 ;; Remove cruft at the beginning of the string, so people that put
51 ;; in extraneous '..' because they are morons won't lose.
52 (while (string-match "^/\\.\\.\\(/\\)" new)
53 (setq new (substring new (match-beginning 1) nil)))
54 new))
55
56(defun url-expand-file-name (url &optional default)
57 "Convert URL to a fully specified URL, and canonicalize it.
58Second arg DEFAULT is a URL to start with if URL is relative.
59If DEFAULT is nil or missing, the current buffer's URL is used.
60Path components that are `.' are removed, and
61path components followed by `..' are removed, along with the `..' itself."
62 (if (and url (not (string-match "^#" url)))
63 ;; Need to nuke newlines and spaces in the URL, or we open
64 ;; ourselves up to potential security holes.
65 (setq url (mapconcat (function (lambda (x)
66 (if (memq x '(? ?\n ?\r))
67 ""
68 (char-to-string x))))
69 url "")))
70
71 ;; Need to figure out how/where to expand the fragment relative to
72 (setq default (cond
73 ((vectorp default)
74 ;; Default URL has already been parsed
75 default)
76 (default
77 ;; They gave us a default URL in non-parsed format
78 (url-generic-parse-url default))
79 (url-current-object
80 ;; We are in a URL-based buffer, use the pre-parsed object
81 url-current-object)
82 ((string-match url-nonrelative-link url)
83 ;; The URL they gave us is absolute, go for it.
84 nil)
85 (t
86 ;; Hmmm - this shouldn't ever happen.
87 (error "url-expand-file-name confused - no default?"))))
88
89 (cond
90 ((= (length url) 0) ; nil or empty string
91 (url-recreate-url default))
92 ((string-match "^#" url) ; Offset link, use it raw
93 url)
94 ((string-match url-nonrelative-link url) ; Fully-qualified URL, return it immediately
95 url)
96 (t
97 (let* ((urlobj (url-generic-parse-url url))
98 (inhibit-file-name-handlers t)
99 (expander (url-scheme-get-property (url-type default) 'expand-file-name)))
100 (if (string-match "^//" url)
101 (setq urlobj (url-generic-parse-url (concat (url-type default) ":"
102 url))))
103 (funcall expander urlobj default)
104 (url-recreate-url urlobj)))))
105
106(defun url-identity-expander (urlobj defobj)
d18ec89f 107 (setf (url-type urlobj) (or (url-type urlobj) (url-type defobj))))
8c8b8430
SM
108
109(defun url-default-expander (urlobj defobj)
110 ;; The default expansion routine - urlobj is modified by side effect!
111 (if (url-type urlobj)
112 ;; Well, they told us the scheme, let's just go with it.
113 nil
d18ec89f 114 (setf (url-type urlobj) (or (url-type urlobj) (url-type defobj)))
33d35987 115 (setf (url-port urlobj) (or (url-portspec urlobj)
d18ec89f
SM
116 (and (string= (url-type urlobj)
117 (url-type defobj))
ac9f0b75 118 (url-port defobj))))
8c8b8430 119 (if (not (string= "file" (url-type urlobj)))
d18ec89f 120 (setf (url-host urlobj) (or (url-host urlobj) (url-host defobj))))
8c8b8430 121 (if (string= "ftp" (url-type urlobj))
d18ec89f 122 (setf (url-user urlobj) (or (url-user urlobj) (url-user defobj))))
8c8b8430 123 (if (string= (url-filename urlobj) "")
d18ec89f 124 (setf (url-filename urlobj) "/"))
ac9f0b75
LMI
125 ;; If the object we're expanding from is full, then we are now
126 ;; full.
127 (unless (url-fullness urlobj)
128 (setf (url-fullness urlobj) (url-fullness defobj)))
8c8b8430
SM
129 (if (string-match "^/" (url-filename urlobj))
130 nil
131 (let ((query nil)
132 (file nil)
133 (sepchar nil))
134 (if (string-match "[?#]" (url-filename urlobj))
135 (setq query (substring (url-filename urlobj) (match-end 0))
136 file (substring (url-filename urlobj) 0 (match-beginning 0))
137 sepchar (substring (url-filename urlobj) (match-beginning 0) (match-end 0)))
138 (setq file (url-filename urlobj)))
5ca45407
JR
139 ;; We use concat rather than expand-file-name to combine
140 ;; directory and file name, since urls do not follow the same
141 ;; rules as local files on all platforms.
8c8b8430 142 (setq file (url-expander-remove-relative-links
5ca45407 143 (concat (url-file-directory (url-filename defobj)) file)))
d18ec89f
SM
144 (setf (url-filename urlobj)
145 (if query (concat file sepchar query) file))))))
8c8b8430
SM
146
147(provide 'url-expand)
e5566bd5 148
00eef4de 149;;; url-expand.el ends here