Fmlib_browser.UrlConstruct and parse URLs.
The protocol (a.k.a. scheme) of the URL. Only web-related protocols are supported.
type t = {protocol : protocol;host : string;port : int option;path : string;query : string option;fragment : string option;}The parts of a URL.
The URL spec defines the parts like this:
https://example.com:8042/over/there?name=ferret#nose
\___/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragmentThe strings in this type are not transformed in any way yet. The Parser module has functions for that, e.g. splitting the path into segments, accessing specific query parameters and decoding the results into custom data types.
Other parts of this library like Command.http_request require URLs as strings. Those should be constructed directly using functions from the Builder module, e.g Builder.absolute or Builder.custom.
NOTE: Not all URL features from the URL spec are supported. For example, the userinfo segment as part of the authority is not supported.
val of_string : string -> t optionof_string s tries to split s into its URL parts.
Examples:
of_string "http://example.com:443"
=
Some
{
protocol = Http;
host = "example.com";
port = Some 443;
path = "/";
query = None;
fragment = None;
}
of_string "https://example.com/hats?q=top%20hat"
=
Some
{
protocol = Https;
host: "example.com";
port = None;
path = "/hats";
query = Some "q=top%20hat";
fragment = None;
}
of_string "example.com:443" = None (* no protocol *)
of_string "http://tom@example.com" = None (* userinfo not allowed *)
of_string "http://#cats" = None (* no host *)val to_string : t -> stringto_string url serializes the given url into a string.
This is useful, when we have a Url.t and need to pass it on as string to Command.push_url or debug. For constructing URL strings from scratch, it is more convenient to use functions from the Builder module, e.g Builder.absolute or Builder.custom.
percent_encode_part s encodes a part of a URL by escaping special characters like ?, / or non-ASCII characters. This uses Javascript's encodeURIComponent internally.
Normally the Builder module should be used for constructing URLs. This function is here for exceptional cases that require more control.
percent_decode-part s decodes a part of a URL recovering special characters like ?, / or non-ASCII characters. This uses Javascript's decodeURIComponent internally.
Normally the Parser module should be used for decoding URLs. This function is here for exceptional cases that require more control.
module Builder : sig ... endConstruct URLs.
module Parser : sig ... endParse URLs.