Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita96eac6

Browse files
committed
Added url member function to request.
1 parent4a30fc9 commita96eac6

File tree

4 files changed

+68
-33
lines changed

4 files changed

+68
-33
lines changed

‎http/src/http/v2/client/client.cpp‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,29 +122,17 @@ namespace network {
122122
std::future<client::response> res = helper->response_promise_.get_future();
123123

124124
// TODO see linearize.hpp
125-
// TODO write User-Agent: cpp-netlib/NETLIB_VERSION (if no user-agent is supplied)
126-
127-
// HTTP 1.1
128-
auto it =std::find_if(std::begin(helper->request_.headers()),
129-
std::end(helper->request_.headers()),
130-
[] (const std::pair<uri::string_type, uri::string_type> &header) {
131-
return (boost::iequals(header.first,"host"));
132-
});
133-
if (it ==std::end(helper->request_.headers())) {
134-
// set error
135-
helper->response_promise_.set_value(response());
136-
return res;
137-
}
138125

139-
uri_builder builder;
140-
builder
141-
.authority(it->second)
142-
;
126+
// If there is no user-agent, provide one as a default.
127+
auto user_agent = helper->request_.header("User-Agent");
128+
if (!user_agent) {
129+
helper->request_.append_header("User-Agent", options_.user_agent());
130+
}
143131

144-
autoauth =builder.uri();
145-
auto host =auth.host()?
146-
uri::string_type(std::begin(*auth.host()),std::end(*auth.host())) :uri::string_type();
147-
auto port =auth.port<std::uint16_t>()? *auth.port<std::uint16_t>() :80;
132+
autourl =helper->request_.url();
133+
auto host =url.host()?
134+
uri::string_type(std::begin(*url.host()),std::end(*url.host())) :uri::string_type();
135+
auto port =url.port<std::uint16_t>()? *url.port<std::uint16_t>() :80;
148136

149137
resolver_->async_resolve(host, port,
150138
strand_.wrap(

‎http/src/network/http/v2/client/request.hpp‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,23 +243,35 @@ namespace network {
243243
* \brief Constructor.
244244
*/
245245
request()
246-
: is_https_(false),
247-
byte_source_(nullptr) { }
246+
: byte_source_(nullptr) { }
248247

249248
/**
250249
* \brief Constructor.
251250
*/
252251
explicitrequest(uri url)
253-
:is_https_(false) {
252+
:url_(url) {
254253
if (auto scheme = url.scheme()) {
255254
if ((!boost::equal(*scheme,boost::as_literal("http"))) &&
256255
(!boost::equal(*scheme,boost::as_literal("https")))) {
257256
throwinvalid_url();
258257
}
259258

260-
is_https_ =boost::equal(*scheme,boost::as_literal("https"));
261-
path_.assign(std::begin(*url.path()),std::end(*url.path()));
262-
// TODO append query and fragment to path_
259+
if (auto path = url.path()) {
260+
std::copy(std::begin(*path),std::end(*path),
261+
std::back_inserter(path_));
262+
}
263+
264+
if (auto query = url.query()) {
265+
path_.push_back('?');
266+
std::copy(std::begin(*query),std::end(*query),
267+
std::back_inserter(path_));
268+
}
269+
270+
if (auto fragment = url.fragment()) {
271+
path_.push_back('#');
272+
std::copy(std::begin(*fragment),std::end(*fragment),
273+
std::back_inserter(path_));
274+
}
263275

264276
std::ostringstream oss;
265277
std::copy(std::begin(*url.host()),std::end(*url.host()),
@@ -280,7 +292,7 @@ namespace network {
280292
* \brief Copy constructor.
281293
*/
282294
request(const request &other)
283-
:is_https_(other.is_https_)
295+
:url_(other.url_)
284296
, method_(other.method_)
285297
, path_(other.path_)
286298
, version_(other.version_)
@@ -291,7 +303,7 @@ namespace network {
291303
* \brief Move constructor.
292304
*/
293305
request(request &&other)noexcept
294-
:is_https_(std::move(other.is_https_))
306+
:url_(std::move(other.url_))
295307
, method_(std::move(other.method_))
296308
, path_(std::move(other.path_))
297309
, version_(std::move(other.version_))
@@ -319,20 +331,30 @@ namespace network {
319331
*/
320332
voidswap(request &other)noexcept {
321333
using std::swap;
322-
swap(is_https_, other.is_https_);
334+
swap(url_, other.url_);
323335
swap(method_, other.method_);
324336
swap(path_, other.path_);
325337
swap(version_, other.version_);
326338
swap(headers_, other.headers_);
327339
swap(byte_source_, other.byte_source_);
328340
}
329341

342+
request &url(const uri &url) {
343+
// throw invalid_url
344+
url_ = url;
345+
return *this;
346+
}
347+
348+
uriurl()const {
349+
return url_;
350+
}
351+
330352
/**
331353
* \brief Checks whether this is an HTTPS request.
332354
* \returns \c true if it is HTTPS, \c false otherwise.
333355
*/
334356
boolis_https()const {
335-
returnis_https_;
357+
returnurl_.scheme() &&boost::equal(*url_.scheme(),boost::as_literal("https"));
336358
}
337359

338360
/**
@@ -446,7 +468,7 @@ namespace network {
446468

447469
private:
448470

449-
bool is_https_;
471+
network::uri url_;
450472
network::http::v2::method method_;
451473
string_type path_;
452474
string_type version_;

‎http/test/v2/client/features/http_client_test.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Describe(http_client) {
2020
}
2121

2222
It(gets_a_header_response) {
23-
http::client::request request;
23+
http::client::request request{network::uri{"http://www.boost.org/"}};
2424
request
2525
.method(http::method::get)
2626
.path("/LICENSE_1_0.txt")

‎http/test/v2/client/units/request_test.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ TEST(request_test, constructor_invalid_url) {
2323
http::invalid_url);
2424
}
2525

26+
TEST(request_test, get_url) {
27+
http_cm::request instance{network::uri{"http://www.example.com/"}};
28+
ASSERT_EQ("http://www.example.com/", instance.url());
29+
}
30+
2631
TEST(request_test, constructor_empty_uri) {
2732
ASSERT_THROW(http_cm::request{network::uri{}},
2833
http::invalid_url);
@@ -194,3 +199,23 @@ TEST(request_test, is_https) {
194199
http_cm::request instance{network::uri{"https://www.example.com/"}};
195200
ASSERT_TRUE(instance.is_https());
196201
}
202+
203+
TEST(request_test, path) {
204+
http_cm::request instance{network::uri{"http://www.example.com/path/"}};
205+
ASSERT_EQ("/path/", instance.path());
206+
}
207+
208+
TEST(request_test, path_with_query) {
209+
http_cm::request instance{network::uri{"http://www.example.com/path/?foo=bar"}};
210+
ASSERT_EQ("/path/?foo=bar", instance.path());
211+
}
212+
213+
TEST(request_test, path_with_fragment) {
214+
http_cm::request instance{network::uri{"http://www.example.com/path/#fragment"}};
215+
ASSERT_EQ("/path/#fragment", instance.path());
216+
}
217+
218+
TEST(request_test, path_with_query_and_fragment) {
219+
http_cm::request instance{network::uri{"http://www.example.com/path/?foo=bar#fragment"}};
220+
ASSERT_EQ("/path/?foo=bar#fragment", instance.path());
221+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp