1 | [[tags: eggs]] |
---|
2 | [[toc:]] |
---|
3 | |
---|
4 | == uri-generic |
---|
5 | |
---|
6 | === Description |
---|
7 | |
---|
8 | The {{uri-generic}} library contains procedures for parsing and |
---|
9 | manipulation of Uniform Resource Identifiers |
---|
10 | ([[http://tools.ietf.org/html/rfc3986|RFC 3986]]). It is intended to |
---|
11 | conform more closely to the RFC, and uses combinator parsing and |
---|
12 | character classes rather than regular expressions. |
---|
13 | |
---|
14 | This library should be considered to be a ''basis'' for creating |
---|
15 | scheme-specific URI parser libraries. This library only parses |
---|
16 | the generic components from a URI. Any specific library can |
---|
17 | further parse subcomponents. For this reason, encoding and decoding |
---|
18 | of percent-encoded characters is not done automatically. |
---|
19 | This should be handled by specific URI scheme implementations. |
---|
20 | |
---|
21 | === Library Procedures |
---|
22 | |
---|
23 | ==== Constructors and predicates |
---|
24 | |
---|
25 | As specified in section 2.3 of RFC 3986, URI constructors |
---|
26 | automatically decode percent-encoded octets in the range of unreserved |
---|
27 | characters. This means that the following holds true: |
---|
28 | |
---|
29 | (equal? (uri-reference "http://example.com/foo-bar") |
---|
30 | (uri-reference "http://example.com/foo%2Dbar")) => #t |
---|
31 | |
---|
32 | <procedure>(uri-reference STRING) => URI</procedure> |
---|
33 | |
---|
34 | A URI reference is either a URI or a relative reference (RFC 3986, |
---|
35 | Section 4.1). If the given string's prefix does not match the syntax |
---|
36 | of a scheme followed by a colon separator, then the given string is |
---|
37 | parsed as a relative reference. |
---|
38 | |
---|
39 | <procedure>(uri-reference? URI) => BOOL</procedure> |
---|
40 | |
---|
41 | Is the given object a URI reference? '''All objects created by |
---|
42 | URI-generic constructors are URI references'''; they are either URIs |
---|
43 | or relative references. The constructors below are just more strict |
---|
44 | checking versions of {{uri-reference}}. They all create |
---|
45 | URI references. |
---|
46 | |
---|
47 | <procedure>(absolute-uri STRING) => URI</procedure> |
---|
48 | |
---|
49 | Parses the given string as an absolute URI, in which no fragments are |
---|
50 | allowed. If no URI scheme is found, or a fragment is detected, this |
---|
51 | raises an error. |
---|
52 | |
---|
53 | Absolute URIs are defined by RFC 3986 as non-relative URI references |
---|
54 | without a fragment (RFC 3986, Section 4.2). Absolute URIs can be used |
---|
55 | as a base URI to resolve a relative-ref against, using |
---|
56 | {{uri-relative-to}} (see below). |
---|
57 | |
---|
58 | <procedure>(absolute-uri? URI) => BOOL</procedure> |
---|
59 | |
---|
60 | Is the given object an absolute URI? |
---|
61 | |
---|
62 | <procedure>(uri? URI) => BOOL</procedure> |
---|
63 | |
---|
64 | Is the given object a URI? URIs are all URI references that include |
---|
65 | a scheme part. The other type of URI references are relative |
---|
66 | references. |
---|
67 | |
---|
68 | <procedure>(relative-ref? URI) => BOOL</procedure> |
---|
69 | |
---|
70 | Is the given object a relative reference? Relative references are |
---|
71 | defined by RFC 3986 as URI references which are not URIs; they contain |
---|
72 | no URI scheme and can be resolved against an absolute URI to obtain |
---|
73 | a complete URI using {{uri-relative-to}}. |
---|
74 | |
---|
75 | <procedure>(uri-path-absolute? URI) => BOOL</procedure> |
---|
76 | |
---|
77 | Is the {{URI}}'s path component an absolute path? |
---|
78 | |
---|
79 | <procedure>(uri-path-relative? URI) => BOOL</procedure> |
---|
80 | |
---|
81 | Is the {{URI}}'s path component a relative path? |
---|
82 | |
---|
83 | ==== Attribute accessors |
---|
84 | |
---|
85 | * <procedure>(uri-authority URI) => URI-AUTH</procedure> |
---|
86 | * <procedure>(uri-scheme URI) => SYMBOL</procedure> |
---|
87 | * <procedure>(uri-path URI) => LIST</procedure> |
---|
88 | * <procedure>(uri-query URI) => STRING</procedure> |
---|
89 | * <procedure>(uri-fragment) URI => STRING</procedure> |
---|
90 | * <procedure>(uri-host URI) => STRING</procedure> |
---|
91 | * <procedure>(uri-port URI) => INTEGER</procedure> |
---|
92 | * <procedure>(uri-username URI) => STRING</procedure> |
---|
93 | * <procedure>(uri-password URI) => STRING</procedure> |
---|
94 | * <procedure>(authority? URI-AUTH) => BOOL</procedure> |
---|
95 | * <procedure>(authority-host URI-AUTH) => STRING</procedure> |
---|
96 | * <procedure>(authority-port URI-AUTH) => INTEGER</procedure> |
---|
97 | * <procedure>(authority-username URI-AUTH) => STRING</procedure> |
---|
98 | * <procedure>(authority-password URI-AUTH) => STRING</procedure> |
---|
99 | |
---|
100 | If a component is not defined in the given URI, then the corresponding |
---|
101 | accessor returns {{#f}}. |
---|
102 | |
---|
103 | * <procedure>(update-uri URI #!key authority scheme path query fragment host port username password) => URI</procedure> |
---|
104 | * <procedure>(update-authority URI-AUTH #!key host port username password) => URI</procedure> |
---|
105 | |
---|
106 | Update the specified keys in the URI or URI-AUTH object in a |
---|
107 | functional way (ie, it creates a new copy with the modifications). |
---|
108 | |
---|
109 | ==== String and List Representations |
---|
110 | |
---|
111 | <procedure>(uri->string URI USERINFO) => STRING</procedure> |
---|
112 | |
---|
113 | Reconstructs the given URI into a string; uses a supplied function |
---|
114 | {{LAMBDA USERNAME PASSWORD -> STRING}} to map the userinfo part of the |
---|
115 | URI |
---|
116 | |
---|
117 | <procedure>(uri->list URI USERINFO) => LIST</procedure> |
---|
118 | |
---|
119 | Returns a list of the form {{(SCHEME SPECIFIC FRAGMENT)}}; |
---|
120 | {{SPECIFIC}} is of the form {{(AUTHORITY PATH QUERY)}}. |
---|
121 | |
---|
122 | ==== Reference Resolution |
---|
123 | |
---|
124 | <procedure>(uri-relative-to URI URI) => URI</procedure> |
---|
125 | |
---|
126 | Resolve the first URI as a reference relative to the second URI, |
---|
127 | returning a new URI (RFC 3986, Section 5.2.2). |
---|
128 | |
---|
129 | <procedure>(uri-relative-from URI URI) => URI</procedure> |
---|
130 | |
---|
131 | Constructs a new, possibly relative, URI which represents the location |
---|
132 | of the first URI with respect to the second URI. |
---|
133 | |
---|
134 | <examples> |
---|
135 | <example> |
---|
136 | <init>(use uri-generic)</init> |
---|
137 | <expr>(uri->string (uri-relative-to (uri-reference "../qux") (uri-reference "http://example.com/foo/bar/")))</expr> |
---|
138 | <result>"http://example.com/foo/qux"</result> |
---|
139 | </example> |
---|
140 | <example> |
---|
141 | <init>(use uri-generic)</init> |
---|
142 | <expr>(uri->string (uri-relative-from (uri-reference "http://example.com/foo/qux") (uri-reference "http://example.com/foo/bar/")))</expr> |
---|
143 | <result>"../qux"</result> |
---|
144 | </example> |
---|
145 | </examples> |
---|
146 | |
---|
147 | ==== String encoding and decoding |
---|
148 | |
---|
149 | <procedure>(uri-encode-string STRING [CHAR-SET]) => STRING</procedure> |
---|
150 | |
---|
151 | Returns the percent-encoded form of the given string. The optional |
---|
152 | char-set argument controls which characters should be encoded. |
---|
153 | It defaults to the complement of {{char-set:uri-unreserved}}. This is |
---|
154 | always safe, but often overly careful; it is allowed to leave certain |
---|
155 | characters unquoted depending on the context. |
---|
156 | |
---|
157 | <procedure>(uri-decode-string STRING [CHAR-SET]) => STRING</procedure> |
---|
158 | |
---|
159 | Returns the decoded form of the given string. The optional char-set |
---|
160 | argument controls which characters should be decoded. It defaults to |
---|
161 | {{char-set:full}}. |
---|
162 | |
---|
163 | |
---|
164 | ==== Normalization |
---|
165 | |
---|
166 | <procedure>(uri-normalize-case URI) => URI</procedure> |
---|
167 | |
---|
168 | URI case normalization (RFC 3986 section 6.2.2.1) |
---|
169 | |
---|
170 | <procedure>(uri-normalize-path-segments URI) => URI</procedure> |
---|
171 | |
---|
172 | URI path segment normalization (RFC 3986 section 6.2.2.3) |
---|
173 | |
---|
174 | |
---|
175 | ==== Character sets |
---|
176 | |
---|
177 | As a convenience for sub-parsers or other special-purpose URI handling |
---|
178 | code, there are a couple of character sets exported by uri-generic. |
---|
179 | |
---|
180 | <constant>char-set:gen-delims</constant> |
---|
181 | |
---|
182 | Generic delimiters. |
---|
183 | gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" |
---|
184 | |
---|
185 | <constant>char-set:sub-delims</constant> |
---|
186 | |
---|
187 | Sub-delimiters. |
---|
188 | sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" |
---|
189 | |
---|
190 | <constant>char-set:uri-reserved</constant> |
---|
191 | |
---|
192 | The union of {{gen-delims}} and {{sub-delims}}; all reserved URI characters. |
---|
193 | reserved = gen-delims / sub-delims |
---|
194 | |
---|
195 | <constant>char-set:uri-unreserved</constant> |
---|
196 | |
---|
197 | All unreserved characters that are allowed in a URI. |
---|
198 | unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
---|
199 | |
---|
200 | Note that this is _not_ the complement of {{char-set:uri-reserved}}! |
---|
201 | There are several characters (even printable, noncontrol characters) |
---|
202 | which are not allowed at all in a URI. |
---|
203 | |
---|
204 | |
---|
205 | === Requires |
---|
206 | |
---|
207 | * [[matchable]] |
---|
208 | * [[defstruct]] |
---|
209 | |
---|
210 | === Version History |
---|
211 | |
---|
212 | * 2.32 Empty absolute path directly followed by query is now properly recognised as an URI reference. |
---|
213 | * 2.31 Return {{#f}} in constructors if unconsumed input remains after parsing |
---|
214 | * 2.3 Add predicates uri-path-relative? and uri-path-absolute? |
---|
215 | * 2.2 Improvements to uri->string. |
---|
216 | * 2.1 Add new predicates for URIs, absolute URIs and relative references. Fix absolute-uri so it raises a condition when passing in a non-absolute uri string, instead of returning a string with the error. Also throw an error if a fragment is detected in the string. |
---|
217 | * 2.0 Export char-sets, add char-set arg to uri-encode/uri-decode, |
---|
218 | do not decode query args as x-www-form-urlencoded, change path |
---|
219 | representation. Lots of bugfixes. |
---|
220 | * 1.12 Fix relative path normalization when original path ends in a slash, remove consecutive slashes from paths in URIs |
---|
221 | * 1.11 Added accessors for the authority components, functional update procedures. Fixed case-normalization. |
---|
222 | * 1.10 Fixed edge case in {{uri-relative-to}} with empty path in base uri, |
---|
223 | fixed {{uri->string}} for URIs with query args, fixed {{uri->string}} |
---|
224 | to not add an extraneous slash after authority in case of empty path. |
---|
225 | * 1.9 Fixed bug in uri-encode-string with reserved characters, added |
---|
226 | tests for decoding and encoding [Peter Bex] |
---|
227 | * 1.8 Added uri-encode-string and uri-decode-string. |
---|
228 | URI constructors now perform automatic normalization |
---|
229 | of percent-encoded unreserved characters. [suggested by Peter Bex] |
---|
230 | * 1.6 Added error message about missing scheme in absolute-uri. |
---|
231 | * trunk Small bugfix in absolute-uri. [Peter Bex] |
---|
232 | * 1.5 Bug fixes in uri->string and absolute-uri. [reported by Peter Bex] |
---|
233 | * 1.3 Ported to Hygienic Chicken and the [[test]] egg [Peter Bex] |
---|
234 | * 1.2 Now using defstruct instead of define-record [suggested by Peter Bex] |
---|
235 | * 1.1 Added utf8 compatibility |
---|
236 | * 1.0 Initial Release |
---|
237 | |
---|
238 | === License |
---|
239 | |
---|
240 | Based on the |
---|
241 | [[http://www.ninebynine.org/Software/ReadMe-URI-Haskell.txt|Haskell |
---|
242 | URI library]] by Graham Klyne <gk@ninebynine.org>. |
---|
243 | |
---|
244 | |
---|
245 | Copyright 2008-2009 Ivan Raikov, Peter Bex. |
---|
246 | All rights reserved. |
---|
247 | |
---|
248 | Redistribution and use in source and binary forms, with or without |
---|
249 | modification, are permitted provided that the following conditions are |
---|
250 | met: |
---|
251 | |
---|
252 | Redistributions of source code must retain the above copyright |
---|
253 | notice, this list of conditions and the following disclaimer. |
---|
254 | |
---|
255 | Redistributions in binary form must reproduce the above copyright |
---|
256 | notice, this list of conditions and the following disclaimer in the |
---|
257 | documentation and/or other materials provided with the distribution. |
---|
258 | |
---|
259 | Neither the name of the author nor the names of its contributors may |
---|
260 | be used to endorse or promote products derived from this software |
---|
261 | without specific prior written permission. |
---|
262 | |
---|
263 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
264 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
265 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
---|
266 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
---|
267 | COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
---|
268 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
269 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
270 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
271 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
---|
272 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
273 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
---|
274 | OF THE POSSIBILITY OF SUCH DAMAGE. |
---|