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

Commitfcf4888

Browse files
committed
More complete response functionality.
In this commit we add rudimentary header processing. The equality checkis not exactly correct as it's completely plausible that two differentresponse instances have different merged headers, but for the meantimewe're banking on internal equality rather than semantic equality.What needs to happen to fix this is to add a little bit ofcaching/merging logic to make sure that we're able to actually merge theactual headers waiting in the future with the added headers manuallyalong with the removed headers, and compare those instead. Unfortunatelythat's too much work for now and it's not really necessary at this time.
1 parent4ea2034 commitfcf4888

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

‎boost/network/protocol/http/response/response.ipp‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// http://www.boost.org/LICENSE_1_0.txt)
99

1010
#include<boost/network/protocol/http/response/response.hpp>
11-
#include<boost/optional/optional.hpp>
12-
#include<boost/utility/in_place_factory.hpp>
11+
#include<set>
1312

1413
namespaceboost {namespacenetwork {namespacehttp {
1514

@@ -52,19 +51,44 @@ struct response_pimpl {
5251

5352
voidappend_header(std::stringconst & name,
5453
std::stringconst & value) {
55-
// FIXME do something!
54+
added_headers_.insert(std::make_pair(name, value));
5655
}
5756

5857
voidremove_headers(std::stringconst &name) {
59-
// FIXME do something!
58+
removed_headers_.insert(name);
6059
}
6160

6261
voidremove_headers() {
63-
// FIXME do something!
62+
if (headers_future_.get_state() == future_state::uninitialized) {
63+
promise<std::multimap<std::string, std::string> > headers_promise;
64+
headers_promise.set_value(std::multimap<std::string, std::string>());
65+
unique_future<std::multimap<std::string, std::string> > tmp =
66+
headers_promise.get_future();
67+
std::multimap<std::string, std::string>().swap(added_headers_);
68+
std::set<std::string>().swap(removed_headers_);
69+
headers_future_ =move(tmp);
70+
}
6471
}
6572

6673
voidget_headers(
67-
function<void(std::stringconst &, std::stringconst &)> inserter) {/* FIXME: Do something!*/ }
74+
function<void(std::stringconst &, std::stringconst &)> inserter) {
75+
std::multimap<std::string, std::string>::const_iterator it;
76+
if (headers_future_.get_state() == future_state::uninitialized) {
77+
it = added_headers_.begin();
78+
for (;it != added_headers_.end(); ++it) {
79+
if (removed_headers_.find(it->first) == removed_headers_.end()) {
80+
inserter(it->first, it->second);
81+
}
82+
}
83+
}else {
84+
it = headers_future_.get().begin();
85+
for (;it != headers_future_.get().end(); ++it) {
86+
if (removed_headers_.find(it->first) == removed_headers_.end()) {
87+
inserter(it->first, it->second);
88+
}
89+
}
90+
}
91+
}
6892
voidget_headers(
6993
std::stringconst & name,
7094
function<void(std::stringconst &, std::stringconst &)> inserter) {/* FIXME: Do something!*/ }
@@ -237,6 +261,8 @@ struct response_pimpl {
237261
if (other.body_future_.get_state() != future_state::uninitialized)
238262
returnfalse;
239263
}
264+
if (other.added_headers_ != added_headers_ || other.removed_headers_ != removed_headers_)
265+
returnfalse;
240266
returntrue;
241267
}
242268

@@ -249,6 +275,9 @@ struct response_pimpl {
249275
mutable shared_future<std::string> status_message_future_;
250276
mutable shared_future<std::string> version_future_;
251277
mutable shared_future<std::string> body_future_;
278+
// TODO: use unordered_map and unordered_set here.
279+
std::multimap<std::string, std::string> added_headers_;
280+
std::set<std::string> removed_headers_;
252281

253282
response_pimpl(response_pimplconst &other)
254283
: source_future_(other.source_future_)
@@ -258,6 +287,8 @@ struct response_pimpl {
258287
, status_message_future_(other.status_message_future_)
259288
, version_future_(other.version_future_)
260289
, body_future_(other.body_future_)
290+
, added_headers_(other.added_headers_)
291+
, removed_headers_(other.removed_headers_)
261292
{}
262293
};
263294

‎libs/network/test/http/response_test.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,43 @@ BOOST_AUTO_TEST_CASE(response_value_semantics_test) {
2929
original = copy;
3030
BOOST_CHECK(original == copy);
3131
}
32+
33+
structmultimap_inserter {
34+
voidoperator()(std::stringconst &name, std::stringconst &value)const {
35+
multimap_.insert(std::make_pair(name, value));
36+
}
37+
explicitmultimap_inserter(std::multimap<std::string, std::string> &multimap)
38+
: multimap_(multimap)
39+
{}
40+
std::multimap<std::string,std::string> & multimap_;
41+
};
42+
43+
BOOST_AUTO_TEST_CASE(response_setters_and_getters_test) {
44+
http::response response;
45+
response.set_source("http://www.google.com/");
46+
response.set_destination("127.0.0.1");
47+
response.append_header("Connection","close");
48+
response.append_header("Content-Type","text/plain");
49+
response.set_body("Hello, World!");
50+
response.set_status(200u);
51+
response.set_status_message("OK");
52+
response.set_version("HTTP/1.1");
53+
std::string source, destination, body, status_message, version;
54+
std::multimap<std::string, std::string> headers, expected_headers;
55+
expected_headers.insert(std::make_pair("Connection","close"));
56+
expected_headers.insert(std::make_pair("Content-Type","text/plain"));
57+
boost::uint16_t status;
58+
response.get_source(source);
59+
response.get_destination(destination);
60+
response.get_body(body);
61+
response.get_status_message(status_message);
62+
response.get_version(version);
63+
response.get_headers(multimap_inserter(headers));
64+
response.get_status(status);
65+
BOOST_CHECK_EQUAL(source,std::string("http://www.google.com/"));
66+
BOOST_CHECK_EQUAL(destination,std::string("127.0.0.1"));
67+
BOOST_CHECK_EQUAL(body,std::string("Hello, World!"));
68+
BOOST_CHECK_EQUAL(status,200u);
69+
BOOST_CHECK_EQUAL(version,std::string("HTTP/1.1"));
70+
BOOST_CHECK(expected_headers == headers);
71+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp