Ada 2.8.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url-getters.cpp
Go to the documentation of this file.
1
5#include "ada.h"
7#include "ada/helpers.h"
8#include "ada/scheme.h"
9
10#include <string>
11
12namespace ada {
13[[nodiscard]] std::string url::get_origin() const noexcept {
14 if (is_special()) {
15 // Return a new opaque origin.
16 if (type == scheme::FILE) {
17 return "null";
18 }
19 return ada::helpers::concat(get_protocol(), "//", get_host());
20 }
21
22 if (non_special_scheme == "blob") {
23 if (!path.empty()) {
24 auto result = ada::parse<ada::url>(path);
25 if (result &&
26 (result->type == scheme::HTTP || result->type == scheme::HTTPS)) {
27 // If pathURL's scheme is not "http" and not "https", then return a
28 // new opaque origin.
29 return ada::helpers::concat(result->get_protocol(), "//",
30 result->get_host());
31 }
32 }
33 }
34
35 // Return a new opaque origin.
36 return "null";
37}
38
39[[nodiscard]] std::string url::get_protocol() const noexcept {
40 if (is_special()) {
41 return helpers::concat(ada::scheme::details::is_special_list[type], ":");
42 }
43 // We only move the 'scheme' if it is non-special.
44 return helpers::concat(non_special_scheme, ":");
45}
46
47[[nodiscard]] std::string url::get_host() const noexcept {
48 // If url's host is null, then return the empty string.
49 // If url's port is null, return url's host, serialized.
50 // Return url's host, serialized, followed by U+003A (:) and url's port,
51 // serialized.
52 if (!host.has_value()) {
53 return "";
54 }
55 if (port.has_value()) {
56 return host.value() + ":" + get_port();
57 }
58 return host.value();
59}
60
61[[nodiscard]] std::string url::get_hostname() const noexcept {
62 return host.value_or("");
63}
64
65[[nodiscard]] std::string_view url::get_pathname() const noexcept {
66 return path;
67}
68
69[[nodiscard]] std::string url::get_search() const noexcept {
70 // If this's URL's query is either null or the empty string, then return the
71 // empty string. Return U+003F (?), followed by this's URL's query.
72 return (!query.has_value() || (query.value().empty())) ? ""
73 : "?" + query.value();
74}
75
76[[nodiscard]] const std::string& url::get_username() const noexcept {
77 return username;
78}
79
80[[nodiscard]] const std::string& url::get_password() const noexcept {
81 return password;
82}
83
84[[nodiscard]] std::string url::get_port() const noexcept {
85 return port.has_value() ? std::to_string(port.value()) : "";
86}
87
88[[nodiscard]] std::string url::get_hash() const noexcept {
89 // If this's URL's fragment is either null or the empty string, then return
90 // the empty string. Return U+0023 (#), followed by this's URL's fragment.
91 return (!hash.has_value() || (hash.value().empty())) ? ""
92 : "#" + hash.value();
93}
94
95} // namespace ada
Includes all definitions for Ada.
Definitions for helper functions used within Ada.
Definitions for user facing functions for parsing URL and it's components.
constexpr std::string_view is_special_list[]
Definition scheme-inl.h:19
Definition ada_idna.h:13
tl::expected< result_type, ada::errors > result
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
Declarations for the URL scheme.
ada_really_inline bool is_special() const noexcept
std::string get_search() const noexcept
std::string_view get_pathname() const noexcept
std::string get_host() const noexcept
std::string get_hash() const noexcept
std::string get_origin() const noexcept override
std::string get_hostname() const noexcept
const std::string & get_password() const noexcept
std::string get_port() const noexcept
const std::string & get_username() const noexcept
std::string get_protocol() const noexcept