Ada 2.7.8
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 <algorithm>
11#include <string>
12
13namespace ada {
15 if (is_special()) {
16 // Return a new opaque origin.
17 if (type == scheme::FILE) {
18 return "null";
19 }
20 return ada::helpers::concat(get_protocol(), "//", get_host());
21 }
22
23 if (non_special_scheme == "blob") {
24 if (!path.empty()) {
25 auto result = ada::parse<ada::url>(path);
26 if (result &&
27 (result->type == scheme::HTTP || result->type == scheme::HTTPS)) {
28 // If pathURL's scheme is not "http" and not "https", then return a
29 // new opaque origin.
30 return ada::helpers::concat(result->get_protocol(), "//",
31 result->get_host());
32 }
33 }
34 }
35
36 // Return a new opaque origin.
37 return "null";
38}
39
41 if (is_special()) {
42 return helpers::concat(ada::scheme::details::is_special_list[type], ":");
43 }
44 // We only move the 'scheme' if it is non-special.
45 return helpers::concat(non_special_scheme, ":");
46}
47
49 // If url's host is null, then return the empty string.
50 // If url's port is null, return url's host, serialized.
51 // Return url's host, serialized, followed by U+003A (:) and url's port,
52 // serialized.
53 if (!host.has_value()) {
54 return "";
55 }
56 if (port.has_value()) {
57 return host.value() + ":" + get_port();
58 }
59 return host.value();
60}
61
63 return host.value_or("");
64}
65
66[[nodiscard]] std::string_view url::get_pathname() const noexcept {
67 return path;
68}
69
71 // If this's URL's query is either null or the empty string, then return the
72 // empty string. Return U+003F (?), followed by this's URL's query.
73 return (!query.has_value() || (query.value().empty())) ? ""
74 : "?" + query.value();
75}
76
77[[nodiscard]] const std::string& url::get_username() const noexcept {
78 return username;
79}
80
81[[nodiscard]] const std::string& url::get_password() const noexcept {
82 return password;
83}
84
86 return port.has_value() ? std::to_string(port.value()) : "";
87}
88
90 // If this's URL's fragment is either null or the empty string, then return
91 // the empty string. Return U+0023 (#), followed by this's URL's fragment.
92 return (!hash.has_value() || (hash.value().empty())) ? ""
93 : "#" + hash.value();
94}
95
96} // 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