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