URIs and URLs
1 URI
1.1 URI Data Structures
Uri
Authority
make-uri
parse-uri
2 Url
2.1 Query Params
Param
param
Param?
encode-param
Params
parse-params
params->query
2.2 Path Utilties
path-split
7.0

URIs and URLs

Raymond Racine <[email protected]>

1 URI

The original intent of the URI library was for full support of URI genericity, however, the current incarnation is biased for URLs but probably usable for many other flavors of URIs in common use.

1.1 URI Data Structures

 (require uri) package: uri

struct

(struct Uri (scheme authority path query fragment)
    #:transparent)
  scheme : String
  authority : (U False Authority)
  path : String
  query : (Option String)
  fragment : (Option String)
The major compositional structures of a URI.

struct

(struct Authority (user host port)
    #:transparent)
  user : (Option String)
  host : String
  port : Integer
The authority structure of a URI. The host and port are required values.

procedure

(make-uri scheme    
  user    
  host    
  port    
  path    
  query    
  fragment)  Uri
  scheme : String
  user : (Option String)
  host : String
  port : Natural
  path : String
  query : String
  fragment : String
Construct a uri.

procedure

(parse-uri uristr)  (Option Uri)

  uristr : String
Parse the given string into a Uri. If the string fails to parse #f is returned.

2 Url

2.1 Query Params

 (require uri/url/param) package: uri
A Param is a name value pair intended for use in constructing the URL query string.

value

Param : (define-type Param (Pair String String))

A Param is typed defined as a pairof strings, the name and value.

procedure

(param name value)  Param

  name : String
  value : String
Constructor for the Param type.

procedure

(Param? any)  Boolean

  any : Any
Type predicate for the Param data type.

procedure

(encode-param param space-as-plus)  Param

  param : Param
  space-as-plus : Boolean
Construct a new Param from the given Param percent encoding URL query reserved characters. If space-as-plus is true spaces are encoded as + chars in lieu of %20. Both the name and value of the Param are encoded.

value

Params : (define-type Params (Listof Param))

A collection of Param. Params may contain any number of Param with the same name, even the same name and value.

procedure

(parse-params str)  (Listof Param)

  str : String
Parse out as many name value pairs as are legal from the given string. Given a partially malformed URL query string several Params may be parsed out with the remainder (unfortunately) silently failing.

procedure

(params->query params)  String

  params : Params
Generates a URL query string suitable for constructing a URL via make-uri. All Param’s name and values are encoded.

Names are concatenated with values separated by a = character. These string snips are then subsequently concatenated together with an interstitial &.

Example:
(params->query (list (param "name" "Elmer Fudd")
                     (param "code" "=Racket=")))
- : String
"name=Elmer%20Fudd&code=%3DRacket%3D"

2.2 Path Utilties

 (require uri/path) package: uri

procedure

(path-split path)  (Listof String)

  path : String
Split a URL path into its constituent segments. For reasons of arcania the first element of a split absolute path is the empty string.

Example:
(path-split "/a/b/c")
- : (Listof String)
'("" "a" "b" "c")
 
(path-split "a/b/c")
- : (Listof String)
'("a" "b" "c")