uri-bytestring-0.2.3.3: Haskell URI parsing as ByteStrings

Copyright(c) Soostone Inc., 2014-2015 Michael Xavier, 2014-2015
LicenseBSD3
Maintainermichael.xavier@soostone.com
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

URI.ByteString

Contents

Description

URI.ByteString aims to be an RFC3986 compliant URI parser that uses efficient ByteStrings for parsing and representing the data. This module provides a URI datatype as well as a parser and serializer.

Note that this library is an early release and may have issues. It is currently being used in production and no issues have been encountered, however. Please report any issues encountered to the issue tracker.

This module also provides analogs to Lens over the various types in this library. These are written in a generic way to avoid a dependency on any particular lens library. You should be able to use these with a number of packages including lens and lens-family-core.

Synopsis

URI-related types

newtype Scheme Source

Required first component to referring to a specification for the remainder of the URI's components, e.g. "http" or "https"

Constructors

Scheme 

Fields

schemeBS :: ByteString
 

newtype Port Source

While some libraries have chosen to limit this to a Word16, the spec only specifies that the string be comprised of digits.

Constructors

Port 

Fields

portNumber :: Int
 

data SchemaError Source

URI Parser Types

Constructors

NonAlphaLeading

Scheme must start with an alphabet character

InvalidChars

Subsequent characters in the schema were invalid

MissingColon

Schemas must be followed by a colon

data URIParserOptions Source

Options for the parser. You will probably want to use either "strictURIParserOptions" or "laxURIParserOptions"

Constructors

URIParserOptions 

strictURIParserOptions :: URIParserOptions Source

Strict URI Parser config. Follows RFC3986 as-specified. Use this if you can be certain that your URIs are properly encoded or if you want parsing to fail if they deviate from the spec at all.

laxURIParserOptions :: URIParserOptions Source

Lax URI Parser config. Use this if you you want to handle common deviations from the spec gracefully.

  • Allows non-encoded [ and ] in query string

data URINormalizationOptions Source

Constructors

URINormalizationOptions 

Fields

unoDowncaseScheme :: Bool

hTtP -> http

unoDowncaseHost :: Bool

eXaMpLe.org -> example.org

unoDropDefPort :: Bool

If the scheme is known and the port is the default (e.g. 80 for http) it is removed.

unoSlashEmptyPath :: Bool

If the path is empty, set it to /

unoDropExtraSlashes :: Bool

Rewrite path from /foo//bar///baz to /foo/bar/baz

unoSortParameters :: Bool

Sorts parameters by parameter name

unoRemoveDotSegments :: Bool

Remove dot segments as per RFC3986 Section 5.2.4

unoDefaultPorts :: Map Scheme Port

Map of known schemes to their default ports. Used when unoDropDefPort is enabled.

noNormalization :: URINormalizationOptions Source

All normalization options disabled

rfc3986Normalization :: URINormalizationOptions Source

Only normalizations deemed appropriate for all protocols by RFC3986 enabled, namely:

  • Downcase Scheme
  • Downcase Host
  • Remove Dot Segments

httpNormalization :: URINormalizationOptions Source

The same as rfc3986Normalization but with additional enabled features if you're working with HTTP URIs:

httpDefaultPorts :: Map Scheme Port Source

The set of known default ports to schemes. Currently only contains http/80 and https/443. Feel free to extend it if needed with unoDefaultPorts.

Operations

toAbsolute :: Scheme -> URIRef a -> URIRef Absolute Source

toAbsolute scheme ref converts ref to an absolute URI. If ref is already absolute, then it is unchanged.

Parsing

parseURI :: URIParserOptions -> ByteString -> Either URIParseError (URIRef Absolute) Source

Parse a strict ByteString into a URI or an error.

Example:

>>> parseURI strictURIParserOptions "http://www.example.org/foo?bar=baz#quux"
Right (URI {uriScheme = Scheme {schemeBS = "http"}, uriAuthority = Just (Authority {authorityUserInfo = Nothing, authorityHost = Host {hostBS = "www.example.org"}, authorityPort = Nothing}), uriPath = "/foo", uriQuery = Query {queryPairs = [("bar","baz")]}, uriFragment = Just "quux"})
>>> parseURI strictURIParserOptions "$$$$://badurl.example.org"
Left (MalformedScheme NonAlphaLeading)

There are some urls that you'll encounter which defy the spec, such as those with square brackets in the query string. If you must be able to parse those, you can use "laxURIParserOptions" or specify your own

>>> parseURI strictURIParserOptions "http://www.example.org/foo?bar[]=baz"
Left MalformedQuery
>>> parseURI laxURIParserOptions "http://www.example.org/foo?bar[]=baz"
Right (URI {uriScheme = Scheme {schemeBS = "http"}, uriAuthority = Just (Authority {authorityUserInfo = Nothing, authorityHost = Host {hostBS = "www.example.org"}, authorityPort = Nothing}), uriPath = "/foo", uriQuery = Query {queryPairs = [("bar[]","baz")]}, uriFragment = Nothing})
>>> let myLaxOptions = URIParserOptions { upoValidQueryChar = liftA2 (||) (upoValidQueryChar strictURIParserOptions) (inClass "[]")}
>>> parseURI myLaxOptions "http://www.example.org/foo?bar[]=baz"
Right (URI {uriScheme = Scheme {schemeBS = "http"}, uriAuthority = Just (Authority {authorityUserInfo = Nothing, authorityHost = Host {hostBS = "www.example.org"}, authorityPort = Nothing}), uriPath = "/foo", uriQuery = Query {queryPairs = [("bar[]","baz")]}, uriFragment = Nothing})

uriParser :: URIParserOptions -> Parser (URIRef Absolute) Source

Underlying attoparsec parser. Useful for composing with your own parsers.

relativeRefParser :: URIParserOptions -> Parser (URIRef Relative) Source

Underlying attoparsec parser. Useful for composing with your own parsers.

Serializing

serializeURIRef :: URIRef a -> Builder Source

URI Serializer

Serialize a URI reference into a Builder.

Example of serializing + converting to a lazy Data.ByteString.Lazy.ByteString:

>>> BB.toLazyByteString $ serializeURIRef $ URI {uriScheme = Scheme {schemeBS = "http"}, uriAuthority = Just (Authority {authorityUserInfo = Nothing, authorityHost = Host {hostBS = "www.example.org"}, authorityPort = Nothing}), uriPath = "/foo", uriQuery = Query {queryPairs = [("bar","baz")]}, uriFragment = Just "quux"}
"http://www.example.org/foo?bar=baz#quux"

serializeURIRef' :: URIRef a -> ByteString Source

Like serializeURIRef, with conversion into a strict ByteString.

Normalized Serialization

normalizeURIRef :: URINormalizationOptions -> URIRef a -> Builder Source

Similar to serializeURIRef but performs configurable degrees of URI normalization. If your goal is the fastest serialization speed possible, serializeURIRef will be fine. If you intend on comparing URIs (say for caching purposes), you'll want to use this.

Low level utility functions

urlDecode Source

Arguments

:: Bool

Whether to decode + to ' '

-> ByteString 
-> ByteString 

This function was extracted from the http-types package. The license can be found in licenseshttp-typesLICENSE

urlDecodeQuery :: ByteString -> ByteString Source

ByteString Utilities

Decoding specifically for the query string, which decodes + as space. Shorthand for urlDecode True

urlEncodeQuery :: ByteString -> Builder Source

Encode a ByteString for use in the query section of a URL

urlEncodePath :: ByteString -> Builder Source

Encode a ByteString for use in the path section of a URL

urlEncode :: [Word8] -> ByteString -> Builder Source

Percent-encoding for URLs. Specify a list of additional unreserved characters to permit.

Lenses

Lenses over Scheme

Lenses over Host

Lenses over Port

Lenses over Authority

Lenses over UserInfo

Lenses over Query

Lenses over URIRef

queryL :: Lens' (URIRef a) Query Source

Lenses over URIParserOptions

Deprecated

serializeURI :: URIRef Absolute -> Builder Source

Deprecated: Use serializeURIRef instead

Serialize a URI into a Builder.

serializeURI' :: URIRef Absolute -> ByteString Source

Deprecated: Use serializeURIRef' instead

Like serializeURI, with conversion into a strict ByteString.

serializeRelativeRef :: URIRef Relative -> Builder Source

Deprecated: Use serializeURIRef instead

Like serializeURI, but do not render scheme.

serializeRelativeRef' :: URIRef Relative -> ByteString Source

Deprecated: Use serializeURIRef' instead

Like serializeRelativeRef, with conversion into a strict ByteString.

uriAuthorityL :: Lens' URI (Maybe Authority) Source

Deprecated: Use authorityL instead

uriPathL :: Lens' URI ByteString Source

Deprecated: Use pathL instead

uriQueryL :: Lens' URI Query Source

Deprecated: Use queryL instead

uriFragmentL :: Lens' URI (Maybe ByteString) Source

Deprecated: Use fragmentL instead

rrAuthorityL :: Lens' RelativeRef (Maybe Authority) Source

Deprecated: Use authorityL instead

rrPathL :: Lens' RelativeRef ByteString Source

Deprecated: Use pathL instead

rrQueryL :: Lens' RelativeRef Query Source

Deprecated: Use queryL instead

rrFragmentL :: Lens' RelativeRef (Maybe ByteString) Source

Deprecated: Use fragmentL instead