Skip to content

Commit

Permalink
Long road to functional http::request.
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
deanberris committed Mar 13, 2012
1 parent 1df7415 commit 2c69f64
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 16 deletions.
3 changes: 1 addition & 2 deletions boost/network/protocol/http/client/client_connection.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
namespace boost { namespace network { namespace http {

client_connection::~client_connection() {
// For exposition only.
BOOST_ASSERT(false && "This should not ever be called.");
// Do nothing here.
}

client_connection * client_connection::clone() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/network/protocol/http/client/simple_connection_manager.hpp>
#include <boost/network/protocol/http/client/connection/simple_connection_factory.hpp>
#include <boost/network/protocol/http/client/options.hpp>

namespace boost { namespace network { namespace http {
Expand All @@ -16,7 +17,11 @@ struct simple_connection_manager_pimpl {
simple_connection_manager_pimpl(client_options const &options)
: options_(options)
, connection_factory_(options.connection_factory())
{}
{
if (!connection_factory_.get())
connection_factory_.reset(
new (std::nothrow) simple_connection_factory());
}

shared_ptr<client_connection> get_connection(asio::io_service & service,
request_base const & request,
Expand Down
33 changes: 29 additions & 4 deletions boost/network/protocol/http/request/request.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,41 @@ BOOST_CONCEPT_ASSERT((boost::network::http::ClientRequest<boost::network::http::
namespace boost { namespace network { namespace http {

struct request_pimpl {
explicit request_pimpl(std::string const & url) {}
request_pimpl() {}

explicit request_pimpl(std::string const & url)
: uri_(url)
{}

request_pimpl* clone() {
return new (std::nothrow) request_pimpl(*this);
}

void set_uri(std::string const & uri) {
uri_ = uri;
}

void get_uri(std::string &uri) {
uri = uri_.string();
}

private:
uri::uri uri_;
request_pimpl(request_pimpl const &other)
: uri_(other.uri_)
{}
};

request::~request() {
// do nothing here
}

request::request()
: pimpl_(new (std::nothrow) request_pimpl())
{}

request::request(std::string const & url)
: pimpl_(new request_pimpl(url))
: pimpl_(new (std::nothrow) request_pimpl(url))
{}

request::request(request const &other)
Expand Down Expand Up @@ -73,12 +92,18 @@ void request::set_method(std::string const & method){}
void request::set_status(std::string const & status){}
void request::set_status_message(std::string const & status_message){}
void request::set_body_writer(function<void(char*, size_t)> writer){}
void request::set_uri(std::string const &uri){}
void request::set_uri(std::string const &uri) {
pimpl_->set_uri(uri);
}
void request::set_uri(network::uri::uri const &uri){}

// Getters
void request::get_uri(network::uri::uri &uri) const{}
void request::get_uri(std::string &uri) const{}

void request::get_uri(std::string &uri) const {
pimpl_->get_uri(uri);
}

void request::get_method(std::string & method) const{}
void request::get_status(std::string & status) const{}
void request::get_status_message(std::string & status_message) const{}
Expand Down
29 changes: 20 additions & 9 deletions libs/network/test/http/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@ endif()

if (Boost_FOUND)
# These are the internal (simple) tests.
add_executable(cpp-netlib-http-request_base_test request_base_test.cpp)
target_link_libraries(cpp-netlib-http-request_base_test
${Boost_LIBRARIES}
cppnetlib-message
cppnetlib-http-message)
set_target_properties(cpp-netlib-http-request_base_test
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
add_test(cpp-netlib-http-request_base_test
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-request_base_test)
set ( MESSAGE_TESTS
request_base_test
request_test
)
foreach ( test ${MESSAGE_TESTS} )
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
set_source_files_properties(${test}.cpp
PROPERTIES COMPILE_FLAGS "-Wall")
endif()
add_executable(cpp-netlib-http-${test} ${test}.cpp)
target_link_libraries(cpp-netlib-http-${test}
${Boost_LIBRARIES}
cppnetlib-message
cppnetlib-uri
cppnetlib-http-message)
set_target_properties(cpp-netlib-http-${test}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
add_test(cpp-netlib-http-${test}
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-${test})
endforeach(test)

set ( TESTS
client_constructor_test
Expand Down
27 changes: 27 additions & 0 deletions libs/network/test/http/request_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2012 Dean Michael Berris <dberris@google.com>.
// Copyright 2012 Google, Inc.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#define BOOST_TEST_MODULE HTTP Request Test
#include <boost/network/protocol/http/request.hpp>
#include <boost/test/unit_test.hpp>

namespace http = boost::network::http;

BOOST_AUTO_TEST_CASE(request_construction) {
http::request request;
http::request other(request);
}

BOOST_AUTO_TEST_CASE(request_uri_test) {
http::request request;
request.set_uri("http://www.google.com/");
http::request other(request);
std::string original, copied;
request.get_uri(original);
other.get_uri(copied);
BOOST_CHECK_EQUAL(std::string("http://www.google.com/"), original);
BOOST_CHECK_EQUAL(original, copied);
}

0 comments on commit 2c69f64

Please sign in to comment.