Outdated egg!
This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.
If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.
TOC »
uri-generic
Description
The uri-generic library contains procedures for parsing and manipulation of Uniform Resource Identifiers (RFC 3986). It is intended to conform more closely to the RFC, and uses combinator parsing and character classes rather than regular expressions.
This library should be considered to be a basis for creating scheme-specific URI parser libraries. This library only parses the generic components from a URI. Any specific library can further parse subcomponents. For this reason, encoding and decoding of percent-encoded characters is not done automatically. This should be handled by specific URI scheme implementations.
For a more practical library which deals with "common" URI schemes like http, ftp, file and such, see the uri-common egg, which is such a specific implementation.
Library Procedures
Constructors and predicates
As specified in section 2.3 of RFC 3986, URI constructors automatically decode percent-encoded octets in the range of unreserved characters. This means that the following holds true:
(equal? (uri-reference "http://example.com/foo-bar") (uri-reference "http://example.com/foo%2Dbar")) => #t
- uri-reference STRINGprocedure
A URI reference is either a URI or a relative reference (RFC 3986, Section 4.1). If the given string's prefix does not match the syntax of a scheme followed by a colon separator, then the given string is parsed as a relative reference. If STRING is neither a URI nor a relative reference, uri-reference returns #f.
- uri-reference? URIprocedure
Is the given object a URI reference? All objects created by URI-generic constructors are URI references; they are either URIs or relative references. The constructors below are just more strict checking versions of uri-reference. They all create URI references.
- absolute-uri STRINGprocedure
Parses the given string as an absolute URI, in which no fragments are allowed. If no URI scheme is found, or a fragment is detected, this raises an error.
Absolute URIs are defined by RFC 3986 as non-relative URI references without a fragment (RFC 3986, Section 4.2). Absolute URIs can be used as a base URI to resolve a relative-ref against, using uri-relative-to (see below).
- make-uri #!key authority scheme path query fragment host port username passwordprocedure
Constructs a URI from the given components.
- absolute-uri? URIprocedure
Is the given object an absolute URI?
- uri? URIprocedure
Is the given object a URI? URIs are all URI references that include a scheme part. The other type of URI references are relative references.
- relative-ref? URIprocedure
Is the given object a relative reference? Relative references are defined by RFC 3986 as URI references which are not URIs; they contain no URI scheme and can be resolved against an absolute URI to obtain a complete URI using uri-relative-to.
- uri-path-absolute? URIprocedure
Is the URI's path component an absolute path?
- uri-path-relative? URIprocedure
Is the URI's path component a relative path?
- uri-ipv6-host? URIprocedure
Is the URI's host component an IPv6 literal address?
- authority-ipv6-host? URI-AUTHprocedure
Is the URI-AUTH's host component an IPv6 literal address? You can get an URI-AUTH object from an uri using the uri-authority attribute accessor, see below.
Attribute accessors
- uri-authority URIprocedure
- uri-scheme URIprocedure
- uri-path URIprocedure
- uri-query URIprocedure
- uri-fragmentprocedure
- uri-host URIprocedure
- uri-port URIprocedure
- uri-username URIprocedure
- uri-password URIprocedure
- authority? URI-AUTHprocedure
- authority-host URI-AUTHprocedure
- authority-port URI-AUTHprocedure
- authority-username URI-AUTHprocedure
- authority-password URI-AUTHprocedure
If a component is not defined in the given URI, then the corresponding accessor returns #f, except for uri-path, which will always return a (possibly empty) list.
- update-uri URI #!key authority scheme path query fragment host port username passwordprocedure
- update-authority URI-AUTH #!key host port username passwordprocedure
Update the specified keys in the URI or URI-AUTH object in a functional way (ie, it creates a new copy with the modifications).
String and List Representations
- uri->string URI #!optional USERINFOprocedure
Reconstructs the given URI into a string; uses a supplied function LAMBDA USERNAME PASSWORD -> STRING to map the userinfo part of the URI. If not given, it represents the userinfo as the username followed by ":******".
- uri->list URI USERINFOprocedure
Returns a list of the form (SCHEME SPECIFIC FRAGMENT); SPECIFIC is of the form (AUTHORITY PATH QUERY).
Reference Resolution
- uri-relative-to URI URIprocedure
Resolve the first URI as a reference relative to the second URI, returning a new URI (RFC 3986, Section 5.2.2).
- uri-relative-from URI URIprocedure
Constructs a new, possibly relative, URI which represents the location of the first URI with respect to the second URI.
(use uri-generic) (uri->string (uri-relative-to (uri-reference "../qux") (uri-reference "http://example.com/foo/bar/"))) => "http://example.com/foo/qux" (uri->string (uri-relative-from (uri-reference "http://example.com/foo/qux") (uri-reference "http://example.com/foo/bar/"))) => "../qux"
String encoding and decoding
- uri-encode-string STRING #!optional CHAR-SETprocedure
Returns the percent-encoded form of the given string. The optional char-set argument controls which characters should be encoded. It defaults to the complement of char-set:uri-unreserved. This is always safe, but often overly careful; it is allowed to leave certain characters unquoted depending on the context.
- uri-decode-string STRING #!optional CHAR-SETprocedure
Returns the decoded form of the given string. The optional char-set argument controls which characters should be decoded. It defaults to char-set:full.
Normalization
- uri-normalize-case URIprocedure
URI case normalization (RFC 3986 section 6.2.2.1)
- uri-normalize-path-segments URIprocedure
URI path segment normalization (RFC 3986 section 6.2.2.3)
Character sets
As a convenience for sub-parsers or other special-purpose URI handling code, there are a couple of character sets exported by uri-generic.
- char-set:gen-delimsconstant
Generic delimiters.
gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
- char-set:sub-delimsconstant
Sub-delimiters.
sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
- char-set:uri-reservedconstant
The union of gen-delims and sub-delims; all reserved URI characters.
reserved = gen-delims / sub-delims
- char-set:uri-unreservedconstant
All unreserved characters that are allowed in a URI.
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
Note that this is _not_ the complement of char-set:uri-reserved! There are several characters (even printable, noncontrol characters) which are not allowed at all in a URI.
Requires
Version History
- 2.46 uri-relative-from now will always attempt to construct a relative path when possible. This makes it easier to construct URLs that still work behind proxies and other types of path manglers.
- 2.45 Don't break hard when setting host to #f using update-uri (reported via IRC by "off_world").
- 2.44 Fixed parsing of IPv6, which was completely broken (#1530, thanks to Vasilij Schneidermann).
- 2.43 Fixed handling of UTF-8 characters when percent-encoding/decoding (thanks to Adrien Ramos).
- 2.42 Improved performance.
- 2.41 Make code more portable by avoiding keyword arguments (thanks to Seth Alves).
- 2.39 Get rid of a compiler warning due to broken ipv4 address handling (thanks to Mario Goulart).
- 2.38 Fixed a bug that caused an error to be thrown when host contained percent-encoded characters (thanks to Roel van der Hoorn).
- 2.37 Fixed bug in make-uri when passed no path, added basic tests for make-uri.
- 2.36 Added procedure make-uri
- 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.
- 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.
- 2.33 Path component for empty absolute path directly followed by query is now represented the same as empty path without query.
- 2.32 Empty absolute path directly followed by query is now properly recognised as an URI reference.
- 2.31 Return #f in constructors if unconsumed input remains after parsing
- 2.3 Add predicates uri-path-relative? and uri-path-absolute?
- 2.2 Improvements to uri->string.
- 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.
- 2.0 Export char-sets, add char-set arg to uri-encode/uri-decode, do not decode query args as x-www-form-urlencoded, change path representation. Lots of bugfixes.
- 1.12 Fix relative path normalization when original path ends in a slash, remove consecutive slashes from paths in URIs
- 1.11 Added accessors for the authority components, functional update procedures. Fixed case-normalization.
- 1.10 Fixed edge case in uri-relative-to with empty path in base uri, fixed uri->string for URIs with query args, fixed uri->string to not add an extraneous slash after authority in case of empty path.
- 1.9 Fixed bug in uri-encode-string with reserved characters, added tests for decoding and encoding [Peter Bex]
- 1.8 Added uri-encode-string and uri-decode-string. URI constructors now perform automatic normalization of percent-encoded unreserved characters. [suggested by Peter Bex]
- 1.6 Added error message about missing scheme in absolute-uri.
- trunk Small bugfix in absolute-uri. [Peter Bex]
- 1.5 Bug fixes in uri->string and absolute-uri. [reported by Peter Bex]
- 1.3 Ported to Hygienic Chicken and the test egg [Peter Bex]
- 1.2 Now using defstruct instead of define-record [suggested by Peter Bex]
- 1.1 Added utf8 compatibility
- 1.0 Initial Release
License
Based on the Haskell URI library by Graham Klyne <gk@ninebynine.org>.
Copyright 2008-2018 Ivan Raikov, Peter Bex. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.