-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.cpp
More file actions
191 lines (157 loc) · 3.49 KB
/
Copy pathRequest.cpp
File metadata and controls
191 lines (157 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
*
* @file Request.cpp
* @author Gaspard Kirira
*
* @brief HTTP request object implementation.
*
* Copyright 2026, Gaspard Kirira.
* All rights reserved.
* https://github.com/vixcpp/requests
*
* Use of this source code is governed by a MIT license
* that can be found in the LICENSE file.
*
* Vix Requests
*
*/
#include <vix/requests/Request.hpp>
#include <vix/requests/Error.hpp>
#include <utility>
namespace vix::requests
{
Request::Request()
: method_("GET")
{
}
Request::Request(
Method method,
std::string_view url,
RequestOptions options,
Body body)
: method_(std::string(to_string(method))),
url_(Url::parse(url)),
options_(std::move(options)),
body_(std::move(body))
{
}
Request::Request(
std::string_view method,
std::string_view url,
RequestOptions options,
Body body)
: method_(make_method(method)),
url_(Url::parse(url)),
options_(std::move(options)),
body_(std::move(body))
{
}
const std::string &Request::method() const noexcept
{
return method_;
}
std::optional<Method> Request::known_method() const
{
return method_from_string(method_);
}
const Url &Request::url() const noexcept
{
return url_;
}
Url Request::final_url() const
{
return url_.with_params(options_.params);
}
std::string Request::request_target() const
{
return final_url().request_target();
}
const RequestOptions &Request::options() const noexcept
{
return options_;
}
RequestOptions &Request::options() noexcept
{
return options_;
}
const Body &Request::body() const noexcept
{
return body_;
}
void Request::set_body(Body body)
{
body_ = std::move(body);
}
bool Request::has_body() const noexcept
{
return !body_.empty();
}
Headers Request::effective_headers() const
{
Headers headers = options_.headers;
if (!headers.has("Host"))
{
headers.set("Host", host_header());
}
if (!headers.has("User-Agent") && options_.has_user_agent())
{
headers.set("User-Agent", options_.user_agent);
}
if (!headers.has("Accept"))
{
headers.set("Accept", "*/*");
}
if (!headers.has("Connection"))
{
headers.set(
"Connection",
options_.keep_alive ? "keep-alive" : "close");
}
if (has_body())
{
if (body_.has_content_type() && !headers.has("Content-Type"))
{
headers.set("Content-Type", body_.content_type());
}
if (!headers.has("Content-Length"))
{
headers.set("Content-Length", std::to_string(body_.size()));
}
}
return headers;
}
std::string Request::host_header() const
{
if (options_.has_host_override())
{
return *options_.host_override;
}
return final_url().authority();
}
bool Request::expects_response_body() const
{
const auto known = known_method();
if (!known.has_value())
{
return true;
}
return method_expects_response_body(*known);
}
bool Request::allows_request_body() const
{
const auto known = known_method();
if (!known.has_value())
{
return true;
}
return method_allows_request_body(*known);
}
std::string Request::make_method(std::string_view method)
{
if (!is_valid_method_token(method))
{
throw RequestException("invalid HTTP method");
}
return normalize_method(method);
}
} // namespace vix::requests