1 ;;; url-expand.el --- expand-file-name for URLs
2 ;; Author: $Author: monnier $
3 ;; Created: $Date: 2004/04/04 01:21:46 $
4 ;; Version: $Revision: 1.1.1.1 $
5 ;; Keywords: comm, data, processes
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; Copyright (c) 1999 Free Software Foundation, Inc.
10 ;;; This file is part of GNU Emacs.
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2, or (at your option)
15 ;;; any later version.
17 ;;; GNU Emacs is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;;; Boston, MA 02111-1307, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 (require 'url-methods
)
32 (defun url-expander-remove-relative-links (name)
33 ;; Strip . and .. from pathnames
34 (let ((new (if (not (string-match "^/" name
))
38 ;; If it ends with a '/.' or '/..', tack on a trailing '/' sot hat
39 ;; the tests that follow are not too complicated in terms of
40 ;; looking for '..' or '../', etc.
41 (if (string-match "/\\.+$" new
)
42 (setq new
(concat new
"/")))
45 (while (string-match "/\\(\\./\\)" new
)
46 (setq new
(concat (substring new
0 (match-beginning 1))
47 (substring new
(match-end 1)))))
50 (while (string-match "/\\([^/]*/\\.\\./\\)" new
)
51 (setq new
(concat (substring new
0 (match-beginning 1))
52 (substring new
(match-end 1)))))
54 ;; Remove cruft at the beginning of the string, so people that put
55 ;; in extraneous '..' because they are morons won't lose.
56 (while (string-match "^/\\.\\.\\(/\\)" new
)
57 (setq new
(substring new
(match-beginning 1) nil
)))
60 (defun url-expand-file-name (url &optional default
)
61 "Convert URL to a fully specified URL, and canonicalize it.
62 Second arg DEFAULT is a URL to start with if URL is relative.
63 If DEFAULT is nil or missing, the current buffer's URL is used.
64 Path components that are `.' are removed, and
65 path components followed by `..' are removed, along with the `..' itself."
66 (if (and url
(not (string-match "^#" url
)))
67 ;; Need to nuke newlines and spaces in the URL, or we open
68 ;; ourselves up to potential security holes.
69 (setq url
(mapconcat (function (lambda (x)
70 (if (memq x
'(? ?
\n ?
\r))
75 ;; Need to figure out how/where to expand the fragment relative to
78 ;; Default URL has already been parsed
81 ;; They gave us a default URL in non-parsed format
82 (url-generic-parse-url default
))
84 ;; We are in a URL-based buffer, use the pre-parsed object
86 ((string-match url-nonrelative-link url
)
87 ;; The URL they gave us is absolute, go for it.
90 ;; Hmmm - this shouldn't ever happen.
91 (error "url-expand-file-name confused - no default?"))))
94 ((= (length url
) 0) ; nil or empty string
95 (url-recreate-url default
))
96 ((string-match "^#" url
) ; Offset link, use it raw
98 ((string-match url-nonrelative-link url
) ; Fully-qualified URL, return it immediately
101 (let* ((urlobj (url-generic-parse-url url
))
102 (inhibit-file-name-handlers t
)
103 (expander (url-scheme-get-property (url-type default
) 'expand-file-name
)))
104 (if (string-match "^//" url
)
105 (setq urlobj
(url-generic-parse-url (concat (url-type default
) ":"
107 (funcall expander urlobj default
)
108 (url-recreate-url urlobj
)))))
110 (defun url-identity-expander (urlobj defobj
)
111 (url-set-type urlobj
(or (url-type urlobj
) (url-type defobj
))))
113 (defun url-default-expander (urlobj defobj
)
114 ;; The default expansion routine - urlobj is modified by side effect!
115 (if (url-type urlobj
)
116 ;; Well, they told us the scheme, let's just go with it.
118 (url-set-type urlobj
(or (url-type urlobj
) (url-type defobj
)))
119 (url-set-port urlobj
(or (url-port urlobj
)
120 (and (string= (url-type urlobj
)
123 (if (not (string= "file" (url-type urlobj
)))
124 (url-set-host urlobj
(or (url-host urlobj
) (url-host defobj
))))
125 (if (string= "ftp" (url-type urlobj
))
126 (url-set-user urlobj
(or (url-user urlobj
) (url-user defobj
))))
127 (if (string= (url-filename urlobj
) "")
128 (url-set-filename urlobj
"/"))
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 (setq file
(url-expander-remove-relative-links
140 (concat (url-basepath (url-filename defobj
)) file
)))
141 (url-set-filename urlobj
(if query
(concat file sepchar query
) file
))))))
143 (provide 'url-expand
)
145 ;;; arch-tag: 7b5f744b-b721-49da-be47-484631680a5a