1 ;;; url-expand.el --- expand-file-name for URLs
3 ;; Copyright (C) 1999, 2004-2014 Free Software Foundation, Inc.
5 ;; Keywords: comm, data, processes
7 ;; This file is part of GNU Emacs.
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 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 (require 'url-methods
)
28 (defun url-expander-remove-relative-links (name)
29 ;; Strip . and .. from pathnames
30 (let ((new (if (not (string-match "^/" name
))
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
"/")))
41 (while (string-match "/\\(\\./\\)" new
)
42 (setq new
(concat (substring new
0 (match-beginning 1))
43 (substring new
(match-end 1)))))
46 (while (string-match "/\\([^/]*/\\.\\./\\)" new
)
47 (setq new
(concat (substring new
0 (match-beginning 1))
48 (substring new
(match-end 1)))))
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
)))
56 (defun url-expand-file-name (url &optional default
)
57 "Convert URL to a fully specified URL, and canonicalize it.
58 Second arg DEFAULT is a URL to start with if URL is relative.
59 If DEFAULT is nil or missing, the current buffer's URL is used.
60 Path components that are `.' are removed, and
61 path 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))
71 ;; Need to figure out how/where to expand the fragment relative to
74 ;; Default URL has already been parsed
77 ;; They gave us a default URL in non-parsed format
78 (url-generic-parse-url default
))
80 ;; We are in a URL-based buffer, use the pre-parsed object
82 ((string-match url-nonrelative-link url
)
83 ;; The URL they gave us is absolute, go for it.
86 ;; Hmmm - this shouldn't ever happen.
87 (error "url-expand-file-name confused - no default?"))))
90 ((= (length url
) 0) ; nil or empty string
91 (url-recreate-url default
))
92 ((string-match "^#" url
) ; Offset link, use it raw
94 ((string-match url-nonrelative-link url
) ; Fully-qualified URL, return it immediately
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
) ":"
103 (funcall expander urlobj default
)
104 (url-recreate-url urlobj
)))))
106 (defun url-identity-expander (urlobj defobj
)
107 (setf (url-type urlobj
) (or (url-type urlobj
) (url-type defobj
))))
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.
114 (setf (url-type urlobj
) (or (url-type urlobj
) (url-type defobj
)))
115 (setf (url-port urlobj
) (or (url-portspec urlobj
)
116 (and (string= (url-type urlobj
)
119 (if (not (string= "file" (url-type urlobj
)))
120 (setf (url-host urlobj
) (or (url-host urlobj
) (url-host defobj
))))
121 (if (string= "ftp" (url-type urlobj
))
122 (setf (url-user urlobj
) (or (url-user urlobj
) (url-user defobj
))))
123 (if (string= (url-filename urlobj
) "")
124 (setf (url-filename urlobj
) "/"))
125 ;; If the object we're expanding from is full, then we are now
127 (unless (url-fullness urlobj
)
128 (setf (url-fullness urlobj
) (url-fullness defobj
)))
129 (if (string-match "^/" (url-filename urlobj
))
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
)))
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.
142 (setq file
(url-expander-remove-relative-links
143 (concat (url-file-directory (url-filename defobj
)) file
)))
144 (setf (url-filename urlobj
)
145 (if query
(concat file sepchar query
) file
))))))
147 (provide 'url-expand
)
149 ;;; url-expand.el ends here