Skip to content

Commit 2c69f64

Browse files
committed
Long road to functional http::request.
One of the larger casualties of this refactor is http::request which has been turned into a pimpl'ed value type. The implementation still has a heavy-weight copy but at some point we can actually go ahead and do some "copy-on-write" behavior if we really want to. For now we're going to cover all the supported public APIs for the http::request object. Once we're done with that we can then continue to debugging, testing, and mocking out certain parts for the HTTP client objects. I have a sneaking suspicion that if we get done with the http::request and http::response objects then the HTTP clients might "just work".
1 parent 1df7415 commit 2c69f64

File tree

5 files changed

+83
-16
lines changed

5 files changed

+83
-16
lines changed

boost/network/protocol/http/client/client_connection.ipp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
namespace boost { namespace network { namespace http {
1414

1515
client_connection::~client_connection() {
16-
// For exposition only.
17-
BOOST_ASSERT(false && "This should not ever be called.");
16+
// Do nothing here.
1817
}
1918

2019
client_connection * client_connection::clone() const {

boost/network/protocol/http/client/simple_connection_manager.ipp

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

1010
#include <boost/network/protocol/http/client/simple_connection_manager.hpp>
11+
#include <boost/network/protocol/http/client/connection/simple_connection_factory.hpp>
1112
#include <boost/network/protocol/http/client/options.hpp>
1213

1314
namespace boost { namespace network { namespace http {
@@ -16,7 +17,11 @@ struct simple_connection_manager_pimpl {
1617
simple_connection_manager_pimpl(client_options const &options)
1718
: options_(options)
1819
, connection_factory_(options.connection_factory())
19-
{}
20+
{
21+
if (!connection_factory_.get())
22+
connection_factory_.reset(
23+
new (std::nothrow) simple_connection_factory());
24+
}
2025

2126
shared_ptr<client_connection> get_connection(asio::io_service & service,
2227
request_base const & request,

boost/network/protocol/http/request/request.ipp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,41 @@ BOOST_CONCEPT_ASSERT((boost::network::http::ClientRequest<boost::network::http::
1717
namespace boost { namespace network { namespace http {
1818

1919
struct request_pimpl {
20-
explicit request_pimpl(std::string const & url) {}
20+
request_pimpl() {}
21+
22+
explicit request_pimpl(std::string const & url)
23+
: uri_(url)
24+
{}
25+
2126
request_pimpl* clone() {
2227
return new (std::nothrow) request_pimpl(*this);
2328
}
2429

30+
void set_uri(std::string const & uri) {
31+
uri_ = uri;
32+
}
33+
34+
void get_uri(std::string &uri) {
35+
uri = uri_.string();
36+
}
37+
2538
private:
39+
uri::uri uri_;
2640
request_pimpl(request_pimpl const &other)
41+
: uri_(other.uri_)
2742
{}
2843
};
2944

3045
request::~request() {
3146
// do nothing here
3247
}
3348

49+
request::request()
50+
: pimpl_(new (std::nothrow) request_pimpl())
51+
{}
52+
3453
request::request(std::string const & url)
35-
: pimpl_(new request_pimpl(url))
54+
: pimpl_(new (std::nothrow) request_pimpl(url))
3655
{}
3756

3857
request::request(request const &other)
@@ -73,12 +92,18 @@ void request::set_method(std::string const & method){}
7392
void request::set_status(std::string const & status){}
7493
void request::set_status_message(std::string const & status_message){}
7594
void request::set_body_writer(function<void(char*, size_t)> writer){}
76-
void request::set_uri(std::string const &uri){}
95+
void request::set_uri(std::string const &uri) {
96+
pimpl_->set_uri(uri);
97+
}
7798
void request::set_uri(network::uri::uri const &uri){}
7899

79100
// Getters
80101
void request::get_uri(network::uri::uri &uri) const{}
81-
void request::get_uri(std::string &uri) const{}
102+
103+
void request::get_uri(std::string &uri) const {
104+
pimpl_->get_uri(uri);
105+
}
106+
82107
void request::get_method(std::string & method) const{}
83108
void request::get_status(std::string & status) const{}
84109
void request::get_status_message(std::string & status_message) const{}

libs/network/test/http/CMakeLists.txt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,26 @@ endif()
1313

1414
if (Boost_FOUND)
1515
# These are the internal (simple) tests.
16-
add_executable(cpp-netlib-http-request_base_test request_base_test.cpp)
17-
target_link_libraries(cpp-netlib-http-request_base_test
18-
${Boost_LIBRARIES}
19-
cppnetlib-message
20-
cppnetlib-http-message)
21-
set_target_properties(cpp-netlib-http-request_base_test
22-
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
23-
add_test(cpp-netlib-http-request_base_test
24-
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-request_base_test)
16+
set ( MESSAGE_TESTS
17+
request_base_test
18+
request_test
19+
)
20+
foreach ( test ${MESSAGE_TESTS} )
21+
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
22+
set_source_files_properties(${test}.cpp
23+
PROPERTIES COMPILE_FLAGS "-Wall")
24+
endif()
25+
add_executable(cpp-netlib-http-${test} ${test}.cpp)
26+
target_link_libraries(cpp-netlib-http-${test}
27+
${Boost_LIBRARIES}
28+
cppnetlib-message
29+
cppnetlib-uri
30+
cppnetlib-http-message)
31+
set_target_properties(cpp-netlib-http-${test}
32+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
33+
add_test(cpp-netlib-http-${test}
34+
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-${test})
35+
endforeach(test)
2536

2637
set ( TESTS
2738
client_constructor_test
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2012 Dean Michael Berris <dberris@google.com>.
2+
// Copyright 2012 Google, Inc.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#define BOOST_TEST_MODULE HTTP Request Test
8+
#include <boost/network/protocol/http/request.hpp>
9+
#include <boost/test/unit_test.hpp>
10+
11+
namespace http = boost::network::http;
12+
13+
BOOST_AUTO_TEST_CASE(request_construction) {
14+
http::request request;
15+
http::request other(request);
16+
}
17+
18+
BOOST_AUTO_TEST_CASE(request_uri_test) {
19+
http::request request;
20+
request.set_uri("http://www.google.com/");
21+
http::request other(request);
22+
std::string original, copied;
23+
request.get_uri(original);
24+
other.get_uri(copied);
25+
BOOST_CHECK_EQUAL(std::string("http://www.google.com/"), original);
26+
BOOST_CHECK_EQUAL(original, copied);
27+
}

0 commit comments

Comments
 (0)