source: project/wiki/eggref/4/uri-generic @ 30090

Last change on this file since 30090 was 30090, checked in by sjamaan, 7 years ago

Update uri-generic changelog

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