Mercurial > emacs
annotate lisp/url/url-expand.el @ 101274:4bcf5c96d5ee
* linum.el (linum-update-window): Use `delq' instead of `delete';
compare strings with `equal-including-properties'.
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Sun, 18 Jan 2009 01:45:34 +0000 |
parents | a9dc0e7c3f2b |
children | fa76b8415858 |
rev | line source |
---|---|
54695 | 1 ;;; url-expand.el --- expand-file-name for URLs |
57612 | 2 |
100908 | 3 ;; Copyright (C) 1999, 2004, 2005, 2006, 2007, 2008, 2009 |
4 ;; Free Software Foundation, Inc. | |
57612 | 5 |
54695 | 6 ;; Keywords: comm, data, processes |
7 | |
57612 | 8 ;; This file is part of GNU Emacs. |
9 | |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
91500
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
57612 | 11 ;; it under the terms of the GNU General Public License as published by |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
91500
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
91500
diff
changeset
|
13 ;; (at your option) any later version. |
57612 | 14 |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
94668
8259d0d8e107
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
91500
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
57612 | 22 |
23 ;;; Code: | |
54695 | 24 |
25 (require 'url-methods) | |
26 (require 'url-util) | |
27 (require 'url-parse) | |
91500
0fd27561386b
Require cl when compiling, for setf.
Magnus Henoch <mange@freemail.hu>
parents:
87649
diff
changeset
|
28 (eval-when-compile (require 'cl)) |
54695 | 29 |
30 (defun url-expander-remove-relative-links (name) | |
31 ;; Strip . and .. from pathnames | |
32 (let ((new (if (not (string-match "^/" name)) | |
33 (concat "/" name) | |
34 name))) | |
35 | |
36 ;; If it ends with a '/.' or '/..', tack on a trailing '/' sot hat | |
37 ;; the tests that follow are not too complicated in terms of | |
38 ;; looking for '..' or '../', etc. | |
39 (if (string-match "/\\.+$" new) | |
40 (setq new (concat new "/"))) | |
41 | |
42 ;; Remove '/./' first | |
43 (while (string-match "/\\(\\./\\)" new) | |
44 (setq new (concat (substring new 0 (match-beginning 1)) | |
45 (substring new (match-end 1))))) | |
46 | |
47 ;; Then remove '/../' | |
48 (while (string-match "/\\([^/]*/\\.\\./\\)" new) | |
49 (setq new (concat (substring new 0 (match-beginning 1)) | |
50 (substring new (match-end 1))))) | |
51 | |
52 ;; Remove cruft at the beginning of the string, so people that put | |
53 ;; in extraneous '..' because they are morons won't lose. | |
54 (while (string-match "^/\\.\\.\\(/\\)" new) | |
55 (setq new (substring new (match-beginning 1) nil))) | |
56 new)) | |
57 | |
58 (defun url-expand-file-name (url &optional default) | |
59 "Convert URL to a fully specified URL, and canonicalize it. | |
60 Second arg DEFAULT is a URL to start with if URL is relative. | |
61 If DEFAULT is nil or missing, the current buffer's URL is used. | |
62 Path components that are `.' are removed, and | |
63 path components followed by `..' are removed, along with the `..' itself." | |
64 (if (and url (not (string-match "^#" url))) | |
65 ;; Need to nuke newlines and spaces in the URL, or we open | |
66 ;; ourselves up to potential security holes. | |
67 (setq url (mapconcat (function (lambda (x) | |
68 (if (memq x '(? ?\n ?\r)) | |
69 "" | |
70 (char-to-string x)))) | |
71 url ""))) | |
72 | |
73 ;; Need to figure out how/where to expand the fragment relative to | |
74 (setq default (cond | |
75 ((vectorp default) | |
76 ;; Default URL has already been parsed | |
77 default) | |
78 (default | |
79 ;; They gave us a default URL in non-parsed format | |
80 (url-generic-parse-url default)) | |
81 (url-current-object | |
82 ;; We are in a URL-based buffer, use the pre-parsed object | |
83 url-current-object) | |
84 ((string-match url-nonrelative-link url) | |
85 ;; The URL they gave us is absolute, go for it. | |
86 nil) | |
87 (t | |
88 ;; Hmmm - this shouldn't ever happen. | |
89 (error "url-expand-file-name confused - no default?")))) | |
90 | |
91 (cond | |
92 ((= (length url) 0) ; nil or empty string | |
93 (url-recreate-url default)) | |
94 ((string-match "^#" url) ; Offset link, use it raw | |
95 url) | |
96 ((string-match url-nonrelative-link url) ; Fully-qualified URL, return it immediately | |
97 url) | |
98 (t | |
99 (let* ((urlobj (url-generic-parse-url url)) | |
100 (inhibit-file-name-handlers t) | |
101 (expander (url-scheme-get-property (url-type default) 'expand-file-name))) | |
102 (if (string-match "^//" url) | |
103 (setq urlobj (url-generic-parse-url (concat (url-type default) ":" | |
104 url)))) | |
105 (funcall expander urlobj default) | |
106 (url-recreate-url urlobj))))) | |
107 | |
108 (defun url-identity-expander (urlobj defobj) | |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
109 (setf (url-type urlobj) (or (url-type urlobj) (url-type defobj)))) |
54695 | 110 |
111 (defun url-default-expander (urlobj defobj) | |
112 ;; The default expansion routine - urlobj is modified by side effect! | |
113 (if (url-type urlobj) | |
114 ;; Well, they told us the scheme, let's just go with it. | |
115 nil | |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
116 (setf (url-type urlobj) (or (url-type urlobj) (url-type defobj))) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
117 (setf (url-port urlobj) (or (url-port urlobj) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
118 (and (string= (url-type urlobj) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
119 (url-type defobj)) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
120 (url-port defobj)))) |
54695 | 121 (if (not (string= "file" (url-type urlobj))) |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
122 (setf (url-host urlobj) (or (url-host urlobj) (url-host defobj)))) |
54695 | 123 (if (string= "ftp" (url-type urlobj)) |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
124 (setf (url-user urlobj) (or (url-user urlobj) (url-user defobj)))) |
54695 | 125 (if (string= (url-filename urlobj) "") |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
126 (setf (url-filename urlobj) "/")) |
54695 | 127 (if (string-match "^/" (url-filename urlobj)) |
128 nil | |
129 (let ((query nil) | |
130 (file nil) | |
131 (sepchar nil)) | |
132 (if (string-match "[?#]" (url-filename urlobj)) | |
133 (setq query (substring (url-filename urlobj) (match-end 0)) | |
134 file (substring (url-filename urlobj) 0 (match-beginning 0)) | |
135 sepchar (substring (url-filename urlobj) (match-beginning 0) (match-end 0))) | |
136 (setq file (url-filename urlobj))) | |
137 (setq file (url-expander-remove-relative-links | |
79066
9e536c40d1f7
(url-default-expander): Use `url-file-directory'.
Richard M. Stallman <rms@gnu.org>
parents:
78222
diff
changeset
|
138 (expand-file-name file |
9e536c40d1f7
(url-default-expander): Use `url-file-directory'.
Richard M. Stallman <rms@gnu.org>
parents:
78222
diff
changeset
|
139 (url-file-directory (url-filename defobj))))) |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
140 (setf (url-filename urlobj) |
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
141 (if query (concat file sepchar query) file)))))) |
54695 | 142 |
143 (provide 'url-expand) | |
54699 | 144 |
83823
dd2bcc6758a0
* url-parse.el (url): Use defstruct rather than macros. Update all callers.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
78222
diff
changeset
|
145 ;; arch-tag: 7b5f744b-b721-49da-be47-484631680a5a |
57612 | 146 ;;; url-expand.el ends here |