Ada is a fast and spec-compliant URL parser written in C++.
- It's widely tested by both Web Platform Tests and Google OSS Fuzzer.
- It is extremely fast.
- It's the default URL parser of Node.js since Node 18.16.0.
- It supports the Unicode Technical Standard.
The Ada library passes the full range of tests from the specification, across a wide range of platforms (e.g., Windows, Linux, macOS).
You can read the WHATWG URL specification here
Types
HostType {.pure, size: 1.} = enum Domain = 0, IPv4 = 1, IPv6 = 2
- This enum defines the type of host.
SchemeType {.pure, size: 1.} = enum HTTP = 0, NotSpecial = 1, HTTPS = 2, WS = 3, FTP = 4, WSS = 5, File = 6
- This enum defines the type of the URL's scheme.
URL = object
-
The URL object.
Internally, this holds a handle to a ada_url (or void *), but you needn't worry about that. If you wish to obtain the aforementioned low-level handle, use the func getHandle(URL) function.
URLParseError = object of ValueError
- This exception is raised by parseURL when the specified input cannot be parsed into a meaningful URL.
Procs
proc `$`(str: ada_string): string {....raises: [], tags: [], forbids: [].}
- Turn an ada_string into a string. This handles the sentinel value properly.
proc `=destroy`(url: URL) {....raises: [], tags: [], forbids: [].}
proc fragment(url: URL): Option[string] {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the fragment identifier of this URL, if it is non-empty. A fragment is also called a "hash", as it is the part of the URL which begins with the # symbol. It is an optional segment of the URL. If it exists, it generally points to a sub-resource within the document, generally a section heading of a document.
For more details, read the WHATWG URL specification
proc host(url: URL): Option[string] {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the parsed representation of the host for this URL with an optional port number.
For more details, read the WHATWG URL specification
proc hostname(url: URL): string {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the parsed representation of the host for this URL. Non-ASCII domain labels are punycode-encoded per IDNA if this is the host of a special URL, or percent-encoded for non-special URLs. The hostname is devoid of a port number.
For more details, read the WHATWG URL specification
proc href(url: URL): string {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the parsed version of the URL with all its components.
For more details, read the WHATWG URL specification
proc isValidURL(str: string): bool {....raises: [], inline, ...tags: [], forbids: [].}
-
This function takes in a string and determine whether it is a valid, intelligible URL. If so, it will return true. Otherwise, it'll return false.
Keep in mind that this function will return true in cases that the programmer might see as odd. You need to keep in mind that the WHATWG URL specification is very forgiving, and as such, many odd-looking cases will pass with no issues.
You are advised to read the WHATWG URL specification to learn more: WHATWG URL specification
proc maybeParseURL(str: string): Option[URL] {....raises: [], inline, ...tags: [], forbids: [].}
-
This function parses a string into a URL. If successful, it returns an Option[URL] with the parsed URL object. Upon failure, it returns an empty Option[URL].
See More:
- proc parseURL(string) for a function which uses exceptions instead of optionals.
proc origin(url: URL): string {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the origin of this URL.
For more details, read the WHATWG URL specification
proc parseURL(str: string): URL {....raises: [URLParseError], inline, ...tags: [], forbids: [].}
-
This function parses a string into a URL. Upon failure, it raises the URLParseError.
Also See:
- proc maybeParseURL(string) for a function which returns an Option[URL] instead of using exceptions.
proc password(url: URL): Option[string] {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the username for this URL.
For more details, read the WHATWG URL specification
proc pathname(url: URL): string {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the path for this URL.
For more details, read the WHATWG URL specification
proc port(url: URL): Option[uint] {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the port number for this URL, if specified and parseable into an unsigned integer.
For more details, read the WHATWG URL specification
proc portString(url: URL): Option[string] {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the port number for this URL, if specified.
For more details, read the WHATWG URL specification
proc protocol(url: URL): string {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the URL's scheme, lower-cased and suffixed with the ':' delimiter.
For more details, read the WHATWG URL specification
Also See:
- proc scheme(URL) which has the same behaviour as this function.
proc query(url: URL): Option[string] {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the URL's query string, if it exists.
For more details, read the WHATWG URL specification
Also See:
- proc search(URL) which has the same behaviour as this function.
proc scheme(url: URL): string {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the URL's scheme, lower-cased and suffixed with the ':' delimiter.
For more details, read the WHATWG URL specification
Also See:
- proc protocol(URL) which has the same behaviour as this function.
proc schemeType(url: URL): SchemeType {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the URL's scheme's type.
Also See:
proc search(url: URL): Option[string] {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the URL's query string, if it exists.
For more details, read the WHATWG URL specification
Also See:
- proc query(URL) which has the same behaviour as this function.
proc username(url: URL): Option[string] {.inline, ...raises: [], tags: [], forbids: [].}
-
Return the username for this URL.
For more details, read the WHATWG URL specification