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