Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Url.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_HTTP_URL_HPP
18#define TOOLBOX_HTTP_URL_HPP
19
21
22#include <toolbox/contrib/http_parser.h>
23
24namespace toolbox {
25inline namespace http {
26
27template <typename DerivedT>
28class BasicUrl {
29 public:
31
32 // Copy.
33 BasicUrl(const BasicUrl&) noexcept = default;
34 BasicUrl& operator=(const BasicUrl&) noexcept = default;
35
36 // Move.
39
41 {
42 const auto& field = parser_.field_data[UF_SCHEMA];
43 return url().substr(field.off, field.len);
44 }
46 {
47 const auto& field = parser_.field_data[UF_HOST];
48 return url().substr(field.off, field.len);
49 }
51 {
52 const auto& field = parser_.field_data[UF_PORT];
53 return url().substr(field.off, field.len);
54 }
56 {
57 const auto& field = parser_.field_data[UF_PATH];
58 return url().substr(field.off, field.len);
59 }
61 {
62 const auto& field = parser_.field_data[UF_QUERY];
63 return url().substr(field.off, field.len);
64 }
66 {
67 const auto& field = parser_.field_data[UF_FRAGMENT];
68 return url().substr(field.off, field.len);
69 }
71 {
72 const auto& field = parser_.field_data[UF_USERINFO];
73 return url().substr(field.off, field.len);
74 }
75
76 protected:
77 ~BasicUrl() = default;
78
79 void reset() noexcept { http_parser_url_init(&parser_); }
80 void parse(bool is_connect = false)
81 {
82 const auto rc
83 = http_parser_parse_url(url().data(), url().size(), is_connect ? 1 : 0, &parser_);
84 if (rc != 0) {
85 throw Exception{Status::BadRequest, err_msg() << "invalid url: " << url()};
86 }
87 }
88
89 private:
90 decltype(auto) url() const noexcept { return static_cast<const DerivedT*>(this)->url(); }
91 http_parser_url parser_{};
92};
93
94class Url : public BasicUrl<Url> {
95 public:
96 explicit Url(const std::string& url)
97 : url_{url}
98 {
99 parse();
100 }
102
103 // Copy.
106
107 // Move.
110
111 const auto& url() const noexcept { return url_; }
112
113 private:
114 std::string url_;
115};
116
117class UrlView : public BasicUrl<UrlView> {
118 public:
119 explicit UrlView(std::string_view url)
120 : url_{url}
121 {
122 parse();
123 }
125
126 // Copy.
129
130 // Move.
133
134 const auto& url() const noexcept { return url_; }
135
136 private:
137 std::string_view url_;
138};
139
140} // namespace http
141} // namespace toolbox
142
143#endif // TOOLBOX_HTTP_URL_HPP
void reset() noexcept
Definition Url.hpp:79
auto host() const noexcept
Definition Url.hpp:45
auto schema() const noexcept
Definition Url.hpp:40
BasicUrl() noexcept
Definition Url.hpp:30
auto path() const noexcept
Definition Url.hpp:55
auto query() const noexcept
Definition Url.hpp:60
auto fragment() const noexcept
Definition Url.hpp:65
auto port() const noexcept
Definition Url.hpp:50
BasicUrl(const BasicUrl &) noexcept=default
auto user_info() const noexcept
Definition Url.hpp:70
BasicUrl(BasicUrl &&) noexcept=default
void parse(bool is_connect=false)
Definition Url.hpp:80
BasicUrl & operator=(const BasicUrl &) noexcept=default
UrlView(std::string_view url)
Definition Url.hpp:119
~UrlView() noexcept=default
const auto & url() const noexcept
Definition Url.hpp:134
Url(const std::string &url)
Definition Url.hpp:96
const auto & url() const noexcept
Definition Url.hpp:111
~Url() noexcept=default
const DataT & data(const MsgEvent &ev) noexcept
Definition Event.hpp:54
ErrMsg & err_msg() noexcept
Definition Exception.cpp:57
constexpr std::size_t size(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s)
Definition Struct.hpp:98
constexpr auto bind() noexcept
Definition Slot.hpp:92