uri (ex_stdlib v0.2.0)

View Source

URI parsing and manipulation module inspired by Elixir's URI module.

This module provides functions for parsing, manipulating, and encoding URIs. It handles the standard URI components: scheme, userinfo, host, port, path, query, and fragment.

Examples:

   URI = uri:parse("https://user:pass@example.com:8080/path?query=value#fragment"),
   EncodedPath = uri:encode("/hello world", :path),
   QueryString = uri:encode_query([{name, "John Doe"}, {age, 30}]).

Summary

Functions

Decodes a percent-encoded string.

Decodes a query string into a list of key-value pairs.

Encodes a string for safe use in a URI component.

Encodes a list of key-value pairs into a query string.

Extracts the fragment from a URI.

Extracts the host from a URI.

Merges two URIs, with the second URI taking precedence.

Creates a new URI from the given components map.

Parses a URI string into a URI map.

Extracts the path from a URI.

Extracts the port from a URI.

Extracts the query from a URI.

Resolves a relative URI against a base URI.

Extracts the scheme from a URI.

Converts a URI map back to a string.

Extracts the userinfo from a URI.

Checks if a URI is valid according to basic URI syntax rules.

Types

component/0

-type component() :: scheme | userinfo | host | port | path | query | fragment.

encode_type/0

-type encode_type() :: path | query | fragment | userinfo.

uri/0

-type uri() ::
          #{scheme => binary() | undefined,
            userinfo => binary() | undefined,
            host => binary() | undefined,
            port => integer() | undefined,
            path => binary() | undefined,
            query => binary() | undefined,
            fragment => binary() | undefined}.

Functions

decode(String)

-spec decode(string() | binary()) -> binary().

Decodes a percent-encoded string.

decode_query(Query)

-spec decode_query(string() | binary()) -> [{binary(), binary()}].

Decodes a query string into a list of key-value pairs.

Returns a list of {Key, Value} tuples where both are binaries.

encode(String, Type)

-spec encode(string() | binary(), encode_type()) -> binary().

Encodes a string for safe use in a URI component.

The encoding type determines which characters are safe to leave unencoded.

encode_query(Params)

-spec encode_query([{term(), term()}]) -> binary().

Encodes a list of key-value pairs into a query string.

Keys and values are automatically percent-encoded.

fragment(URI)

-spec fragment(uri()) -> binary() | undefined.

Extracts the fragment from a URI.

host(URI)

-spec host(uri()) -> binary() | undefined.

Extracts the host from a URI.

merge(URI1, URI2)

-spec merge(uri(), uri()) -> uri().

Merges two URIs, with the second URI taking precedence.

Components from the second URI override those in the first URI.

new(Components)

-spec new(map()) -> uri().

Creates a new URI from the given components map.

Any missing components are set to undefined.

parse(URIString)

-spec parse(string() | binary()) -> uri().

Parses a URI string into a URI map.

Returns a map with the URI components. Missing components are set to undefined.

path(URI)

-spec path(uri()) -> binary() | undefined.

Extracts the path from a URI.

port(URI)

-spec port(uri()) -> integer() | undefined.

Extracts the port from a URI.

query(URI)

-spec query(uri()) -> binary() | undefined.

Extracts the query from a URI.

resolve(BaseURI, RelativeURI)

-spec resolve(uri(), uri() | string() | binary()) -> uri().

Resolves a relative URI against a base URI.

Implements RFC 3986 reference resolution.

scheme(URI)

-spec scheme(uri()) -> binary() | undefined.

Extracts the scheme from a URI.

to_string(URI)

-spec to_string(uri()) -> binary().

Converts a URI map back to a string.

userinfo(URI)

-spec userinfo(uri()) -> binary() | undefined.

Extracts the userinfo from a URI.

valid(URI)

-spec valid(uri() | string() | binary()) -> boolean().

Checks if a URI is valid according to basic URI syntax rules.