roar
Loading...
Searching...
No Matches
url.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "ipv4.hpp"
4#include "ipv6.hpp"
5#include <boost/fusion/include/adapt_struct.hpp>
6#include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
7#include <boost/leaf.hpp>
8
9#include <optional>
10#include <string>
11#include <unordered_map>
12#include <variant>
13#include <vector>
14
15namespace Roar
16{
17 struct Url : boost::spirit::x3::position_tagged
18 {
19 struct UserInfo : boost::spirit::x3::position_tagged
20 {
21 std::string user{};
22 std::optional<std::string> password{};
23 };
24
25 struct Host
26 : boost::spirit::x3::position_tagged
27 , std::variant<std::string, Ipv4, Ipv6>
28 {
29 using base_type = std::variant<std::string, Ipv4, Ipv6>;
30 using base_type::variant;
31
32 base_type const& as_variant() const
33 {
34 return *this;
35 }
37 {
38 return *this;
39 }
40 };
41
42 struct Authority : boost::spirit::x3::position_tagged
43 {
44 struct Remote : boost::spirit::x3::position_tagged
45 {
47 std::optional<unsigned short> port{};
48 };
49 std::optional<Url::UserInfo> userInfo{std::nullopt};
51 };
52
53 std::string scheme;
55 std::vector<std::string> path;
56 std::unordered_map<std::string, std::string> query;
57 std::string fragment;
58
66 std::string toString(bool doUrlEncode = true, bool includeFragment = true) const;
67
74 std::string pathAsString(bool doUrlEncode = true) const;
75
82 std::string hostAsString() const;
83
90 std::string getAuthority(bool doUrlEncode) const;
91
98 std::string schemeAndAuthority(bool doUrlEncode = true) const;
99
106 static boost::leaf::result<Url> fromString(std::string url);
107
115 static boost::leaf::result<std::vector<std::string>> parsePath(std::string const& path);
116 };
117} // namespace Roar
118
119// For parser to directly write to actual structs:
120BOOST_FUSION_ADAPT_STRUCT(Roar::Url::UserInfo, user, password)
121BOOST_FUSION_ADAPT_STRUCT(Roar::Url::Authority, userInfo, remote)
122BOOST_FUSION_ADAPT_STRUCT(Roar::Url::Authority::Remote, host, port)
123BOOST_FUSION_ADAPT_STRUCT(Roar::Url, scheme, authority, path, query, fragment)
static constexpr int port
Definition main.cpp:16
Definition authorization.hpp:10
Definition url.hpp:45
Url::Host host
Definition url.hpp:46
std::optional< unsigned short > port
Definition url.hpp:47
Definition url.hpp:43
Remote remote
Definition url.hpp:50
std::optional< Url::UserInfo > userInfo
Definition url.hpp:49
Definition url.hpp:28
base_type & as_variant()
Definition url.hpp:36
base_type const & as_variant() const
Definition url.hpp:32
std::variant< std::string, Ipv4, Ipv6 > base_type
Definition url.hpp:29
Definition url.hpp:20
std::optional< std::string > password
Definition url.hpp:22
std::string user
Definition url.hpp:21
Definition url.hpp:18
std::string scheme
Definition url.hpp:53
std::string fragment
Definition url.hpp:57
static boost::leaf::result< std::vector< std::string > > parsePath(std::string const &path)
Splits up a path string into a vector compatible with this class. An empty path returns an empty vect...
Definition url.cpp:303
std::vector< std::string > path
Definition url.hpp:55
std::string schemeAndAuthority(bool doUrlEncode=true) const
Converts the authority part and scheme to string: "https://user:password@domain.com:770".
Definition url.cpp:216
std::string hostAsString() const
Returns the host part of the url as a string. Does not include the port! IPv6 addresses will be enclo...
Definition url.cpp:223
std::string pathAsString(bool doUrlEncode=true) const
Will only create the path as a string with a leading slash: "/path/to/resource".
Definition url.cpp:180
std::string toString(bool doUrlEncode=true, bool includeFragment=true) const
Converts the url to a string containing all parts: https://u:p@bla.com:80/path?k=v.
Definition url.cpp:193
Authority authority
Definition url.hpp:54
std::string getAuthority(bool doUrlEncode) const
Converts the entire authority part to string: "user:password@domain.com:770".
Definition url.cpp:240
std::unordered_map< std::string, std::string > query
Definition url.hpp:56
static boost::leaf::result< Url > fromString(std::string url)
Parses a string that is a url.
Definition url.cpp:270