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