Skip to content

Commit

Permalink
Refactor to remove web::http::details::http_network_handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
ras0219 authored and ras0219-msft committed Apr 8, 2016
1 parent a0a0f8d commit aff4a1a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 95 deletions.
1 change: 1 addition & 0 deletions Release/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
message("-- Setting msvc options")
set(WARNINGS)
add_compile_options(/bigobj)
else()
message("-- Unknown compiler, success is doubtful.")
message("CMAKE_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}")
Expand Down
2 changes: 0 additions & 2 deletions Release/include/cpprest/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,6 @@ class http_client

private:

void build_pipeline(const uri &base_uri, const http_client_config &client_config);

std::shared_ptr<::web::http::http_pipeline> m_pipeline;
};

Expand Down
27 changes: 10 additions & 17 deletions Release/src/http/client/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,32 +282,23 @@ void http_client::add_handler(const std::shared_ptr<http::http_pipeline_stage> &
m_pipeline->append(stage);
}

http_client::http_client(const uri &base_uri)
{
build_pipeline(base_uri, http_client_config());
}
http_client::http_client(const uri &base_uri) : http_client(base_uri, http_client_config())
{}

http_client::http_client(const uri &base_uri, const http_client_config &client_config)
{
build_pipeline(base_uri, client_config);
}

http_client::~http_client() CPPREST_NOEXCEPT {}

void http_client::build_pipeline(const uri &base_uri, const http_client_config &client_config)
{
if (base_uri.scheme().empty())
{
auto uribuilder = uri_builder(base_uri);
uribuilder.set_scheme(_XPLATSTR("http"));
uri uriWithScheme = uribuilder.to_uri();
verify_uri(uriWithScheme);
m_pipeline = ::web::http::http_pipeline::create_pipeline(std::make_shared<details::http_network_handler>(uriWithScheme, client_config));
m_pipeline = ::web::http::http_pipeline::create_pipeline(details::create_platform_final_pipeline_stage(uriWithScheme, client_config));
}
else
{
verify_uri(base_uri);
m_pipeline = ::web::http::http_pipeline::create_pipeline(std::make_shared<details::http_network_handler>(base_uri, client_config));
m_pipeline = ::web::http::http_pipeline::create_pipeline(details::create_platform_final_pipeline_stage(base_uri, client_config));
}

#if !defined(CPPREST_TARGET_XP)
Expand All @@ -319,16 +310,18 @@ void http_client::build_pipeline(const uri &base_uri, const http_client_config &
std::make_shared<oauth2::details::oauth2_handler>(client_config.oauth2())));
}

http_client::~http_client() CPPREST_NOEXCEPT {}

const http_client_config & http_client::client_config() const
{
auto ph = std::static_pointer_cast<details::http_network_handler>(m_pipeline->last_stage());
return ph->http_client_impl()->client_config();
auto ph = std::static_pointer_cast<details::_http_client_communicator>(m_pipeline->last_stage());
return ph->client_config();
}

const uri & http_client::base_uri() const
{
auto ph = std::static_pointer_cast<details::http_network_handler>(m_pipeline->last_stage());
return ph->http_client_impl()->base_uri();
auto ph = std::static_pointer_cast<details::_http_client_communicator>(m_pipeline->last_stage());
return ph->base_uri();
}


Expand Down
42 changes: 23 additions & 19 deletions Release/src/http/client/http_client_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@
#endif

#include "http_client_impl.h"
#include "cpprest/base_uri.h"
#include "cpprest/details/x509_cert_utilities.h"
#include <unordered_set>

using boost::asio::ip::tcp;

#define CRLF std::string("\r\n")

namespace web { namespace http
{
namespace client
Expand Down Expand Up @@ -227,8 +230,6 @@ class asio_connection
m_is_reused = true;
}

void handle_pool_timer(const boost::system::error_code& ec);

// Guards concurrent access to socket/ssl::stream. This is necessary
// because timeouts and cancellation can touch the socket at the same time
// as normal message processing.
Expand Down Expand Up @@ -331,7 +332,7 @@ class asio_connection_pool



class asio_client : public _http_client_communicator, public std::enable_shared_from_this<asio_client>
class asio_client : public _http_client_communicator
{
public:
asio_client(http::uri address, http_client_config client_config)
Expand All @@ -347,6 +348,8 @@ class asio_client : public _http_client_communicator, public std::enable_shared_

unsigned long open() override { return 0; }

virtual pplx::task<http_response> propagate(http_request request) override;

asio_connection_pool m_pool;
tcp::resolver m_resolver;
};
Expand Down Expand Up @@ -656,7 +659,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
extra_headers.append(": no-cache" + CRLF);
}

request_stream << flatten_http_headers(ctx->m_request.headers());
request_stream << ::web::http::details::flatten_http_headers(ctx->m_request.headers());
request_stream << extra_headers;
// Enforce HTTP connection keep alive (even for the old HTTP/1.0 protocol).
request_stream << "Connection: Keep-Alive" << CRLF << CRLF;
Expand Down Expand Up @@ -1446,22 +1449,9 @@ class asio_context : public request_context, public std::enable_shared_from_this
};



http_network_handler::http_network_handler(const uri &base_uri, const http_client_config &client_config) :
m_http_client_impl(std::make_shared<asio_client>(base_uri, client_config))
{}

pplx::task<http_response> http_network_handler::propagate(http_request request)
std::shared_ptr<_http_client_communicator> create_platform_final_pipeline_stage(uri base_uri, const http_client_config& client_config)
{
auto context = details::asio_context::create_request_context(m_http_client_impl, request);

// Use a task to externally signal the final result and completion of the task.
auto result_task = pplx::create_task(context->m_request_completion);

// Asynchronously send the response with the HTTP client implementation.
m_http_client_impl->async_send_request(context);

return result_task;
return std::make_shared<asio_client>(base_uri, client_config);
}

void asio_client::send_request(const std::shared_ptr<request_context> &request_ctx)
Expand All @@ -1488,4 +1478,18 @@ void asio_client::send_request(const std::shared_ptr<request_context> &request_c
ctx->start_request();
}

pplx::task<http_response> asio_client::propagate(http_request request)
{
auto self = std::static_pointer_cast<_http_client_communicator>(shared_from_this());
auto context = details::asio_context::create_request_context(self, request);

// Use a task to externally signal the final result and completion of the task.
auto result_task = pplx::create_task(context->m_request_completion);

// Asynchronously send the response with the HTTP client implementation.
this->async_send_request(context);

return result_task;
}

}}}} // namespaces
27 changes: 5 additions & 22 deletions Release/src/http/client/http_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class request_context
// Interface used by client implementations. Concrete implementations are responsible for
// sending HTTP requests and receiving the responses.
//
class _http_client_communicator
class _http_client_communicator : public http_pipeline_stage
{
public:

Expand Down Expand Up @@ -143,26 +143,9 @@ class _http_client_communicator
int m_scheduled;
};

class http_network_handler : public http_pipeline_stage
{
public:
/// <summary>
/// The constructor is separately defined by each subsystem (winhttp, winrt, asio) to create the platform-specific _http_client_communicator.
/// </summary>
http_network_handler(const uri &base_uri, const http_client_config &client_config);

/// <summary>
/// This method is separately defined by each subsystem (winhttp, winrt, asio) to enable the platform-specific handling behavior.
/// </summary>
virtual pplx::task<http_response> propagate(http_request request) override;

const std::shared_ptr<details::_http_client_communicator>& http_client_impl() const
{
return m_http_client_impl;
}

private:
std::shared_ptr<_http_client_communicator> m_http_client_impl;
};
/// <summary>
/// Factory function implemented by the separate platforms to construct their subclasses of _http_client_communicator
/// </summary>
std::shared_ptr<_http_client_communicator> create_platform_final_pipeline_stage(uri base_uri, const http_client_config& client_config);

}}}}
40 changes: 22 additions & 18 deletions Release/src/http/client/http_client_winhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ class winhttp_client : public _http_client_communicator
{
public:
winhttp_client(http::uri address, http_client_config client_config)
: _http_client_communicator(std::move(address), std::move(client_config)), m_secure(m_uri.scheme() == _XPLATSTR("https")), m_hSession(nullptr), m_hConnection(nullptr) { }
: _http_client_communicator(std::move(address), std::move(client_config))
, m_secure(m_uri.scheme() == _XPLATSTR("https"))
, m_hSession(nullptr)
, m_hConnection(nullptr) { }

// Closes session.
~winhttp_client()
Expand All @@ -335,6 +338,20 @@ class winhttp_client : public _http_client_communicator
}
}

virtual pplx::task<http_response> propagate(http_request request) override
{
auto self = std::static_pointer_cast<_http_client_communicator>(shared_from_this());
auto context = details::winhttp_request_context::create_request_context(self, request);

// Use a task to externally signal the final result and completion of the task.
auto result_task = pplx::create_task(context->m_request_completion);

// Asynchronously send the response with the HTTP client implementation.
this->async_send_request(context);

return result_task;
}

protected:

unsigned long report_failure(const utility::string_t& errorMessage)
Expand Down Expand Up @@ -1274,26 +1291,13 @@ class winhttp_client : public _http_client_communicator
bool m_secure;

// No copy or assignment.
winhttp_client(const winhttp_client&);
winhttp_client &operator=(const winhttp_client&);
winhttp_client(const winhttp_client&) = delete;
winhttp_client &operator=(const winhttp_client&) = delete;
};

http_network_handler::http_network_handler(const uri &base_uri, const http_client_config &client_config) :
m_http_client_impl(std::make_shared<details::winhttp_client>(base_uri, client_config))
std::shared_ptr<_http_client_communicator> create_platform_final_pipeline_stage(uri base_uri, const http_client_config& client_config)
{
}

pplx::task<http_response> http_network_handler::propagate(http_request request)
{
auto context = details::winhttp_request_context::create_request_context(m_http_client_impl, request);

// Use a task to externally signal the final result and completion of the task.
auto result_task = pplx::create_task(context->m_request_completion);

// Asynchronously send the response with the HTTP client implementation.
m_http_client_impl->async_send_request(context);

return result_task;
return std::make_shared<details::winhttp_client>(std::move(base_uri), client_config);
}

}}}}
35 changes: 18 additions & 17 deletions Release/src/http/client/http_client_winrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ class winrt_client : public _http_client_communicator
winrt_client(http::uri address, http_client_config client_config)
: _http_client_communicator(std::move(address), std::move(client_config)) { }

virtual pplx::task<http_response> propagate(http_request request) override
{
auto self = std::static_pointer_cast<_http_client_communicator>(shared_from_this());
auto context = details::winrt_request_context::create_request_context(self, request);

// Use a task to externally signal the final result and completion of the task.
auto result_task = pplx::create_task(context->m_request_completion);

// Asynchronously send the response with the HTTP client implementation.
this->async_send_request(context);

return result_task;
}

protected:

// Method to open client.
Expand Down Expand Up @@ -545,26 +559,13 @@ class winrt_client : public _http_client_communicator
private:

// No copy or assignment.
winrt_client(const winrt_client&);
winrt_client &operator=(const winrt_client&);
winrt_client(const winrt_client&) = delete;
winrt_client &operator=(const winrt_client&) = delete;
};

http_network_handler::http_network_handler(const uri &base_uri, const http_client_config &client_config) :
m_http_client_impl(std::make_shared<details::winrt_client>(base_uri, client_config))
std::shared_ptr<_http_client_communicator> create_platform_final_pipeline_stage(uri base_uri, const http_client_config& client_config)
{
}

pplx::task<http_response> http_network_handler::propagate(http_request request)
{
auto context = details::winrt_request_context::create_request_context(m_http_client_impl, request);

// Use a task to externally signal the final result and completion of the task.
auto result_task = pplx::create_task(context->m_request_completion);

// Asynchronously send the response with the HTTP client implementation.
m_http_client_impl->async_send_request(context);

return result_task;
return std::make_shared<details::winrt_client>(std::move(base_uri), client_config);
}

}}}}

0 comments on commit aff4a1a

Please sign in to comment.