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

first attempt at adding proxy support#757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
AndrewFromMelbourne wants to merge1 commit intocpp-netlib:0.11-devel
base:0.11-devel
Choose a base branch
Loading
fromAndrewFromMelbourne:0.11-devel-with-proxy
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletionsboost/network/protocol/http/client/async_impl.hpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -37,14 +37,19 @@ struct async_client
typedef function<bool(string_type&)> body_generator_function_type;

async_client(bool cache_resolved, bool follow_redirect,
bool always_verify_peer, int timeout,
bool always_verify_peer, int timeout, bool remove_chunk_markers,
optional<string_type> const& proxy_host,
optional<string_type> const& proxy_port,
optional<string_type> const& proxy_username,
optional<string_type> const& proxy_password,
boost::shared_ptr<boost::asio::io_service> service,
optional<string_type> const& certificate_filename,
optional<string_type> const& verify_path,
optional<string_type> const& certificate_file,
optional<string_type> const& private_key_file,
optional<string_type> const& ciphers, long ssl_options)
: connection_base(cache_resolved, follow_redirect, timeout),
: connection_base(cache_resolved, follow_redirect, timeout, remove_chunk_markers,
proxy_host, proxy_port, proxy_username, proxy_password),
service_ptr(service.get()
? service
: boost::make_shared<boost::asio::io_service>()),
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,7 +39,11 @@ struct async_connection_base {
// tag.
static connection_ptr new_connection(
resolve_function resolve, resolver_type &resolver, bool follow_redirect,
bool always_verify_peer, bool https, int timeout,
bool always_verify_peer, bool https, int timeout, bool remove_chunk_markers,
optional<string_type> proxy_host = optional<string_type>(),
optional<string_type> proxy_port = optional<string_type>(),
optional<string_type> proxy_username = optional<string_type>(),
optional<string_type> proxy_password = optional<string_type>(),
optional<string_type> certificate_filename = optional<string_type>(),
optional<string_type> const &verify_path = optional<string_type>(),
optional<string_type> certificate_file = optional<string_type>(),
Expand All@@ -51,7 +55,8 @@ struct async_connection_base {
typedef typename delegate_factory<Tag>::type delegate_factory_type;
connection_ptr temp;
temp.reset(new async_connection(
resolver, resolve, follow_redirect, timeout,
resolver, resolve, follow_redirect, timeout, remove_chunk_markers,
proxy_host, proxy_port, proxy_username, proxy_password,
delegate_factory_type::new_connection_delegate(
resolver.get_io_service(), https, always_verify_peer,
certificate_filename, verify_path, certificate_file,
Expand Down
143 changes: 116 additions & 27 deletionsboost/network/protocol/http/client/connection/async_normal.hpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
#include <boost/network/traits/istream.hpp>
#include <boost/logic/tribool.hpp>
#include <boost/network/protocol/http/parser/incremental.hpp>
#include <boost/network/protocol/http/message.hpp>
#include <boost/network/protocol/http/message/wrappers/uri.hpp>
#include <boost/network/protocol/http/client/connection/async_protocol_handler.hpp>
#include <boost/network/protocol/http/algorithms/linearize.hpp>
Expand DownExpand Up@@ -68,11 +69,20 @@ struct http_async_connection
connection_delegate_ptr;

http_async_connection(resolver_type& resolver, resolve_function resolve,
bool follow_redirect, int timeout,
bool follow_redirect, int timeout, bool remove_chunk_markers,
optional<string_type> proxy_host,
optional<string_type> proxy_port,
optional<string_type> proxy_username,
optional<string_type> proxy_password,
connection_delegate_ptr delegate)
: timeout_(timeout),
timer_(resolver.get_io_service()),
is_timedout_(false),
remove_chunk_markers_(remove_chunk_markers),
proxy_host_(proxy_host),
proxy_port_(proxy_port),
proxy_username_(proxy_username),
proxy_password_(proxy_password),
follow_redirect_(follow_redirect),
resolver_(resolver),
resolve_(resolve),
Expand All@@ -89,15 +99,24 @@ struct http_async_connection
body_generator_function_type generator) {
response response_;
this->init_response(response_, get_body);

boost::uint16_t port_ = port(request);
string_type host_ = host(request);
boost::uint16_t source_port = request.source_port();

string_type tcp_host = host_;

if (connect_via_proxy())
{
tcp_host = *proxy_host_;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This could be more compact as:

string_type tcp_host = connect_via_proxy() ? *proxy_host_ : host_;


linearize(request, method, version_major, version_minor,
std::ostreambuf_iterator<typename char_<Tag>::type>(
&command_streambuf));
this->method = method;
boost::uint16_t port_ = port(request);
string_type host_ = host(request);
boost::uint16_t source_port = request.source_port();

resolve_(resolver_,host_, port_,
resolve_(resolver_,tcp_host, port_,
request_strand_.wrap(boost::bind(
&this_type::handle_resolved, this_type::shared_from_this(),
host_, port_, source_port, get_body, callback,
Expand DownExpand Up@@ -140,15 +159,23 @@ struct http_async_connection
// Here we deal with the case that there was an error encountered
// and
// that there's still more endpoints to try connecting to.

boost::uint16_t tcp_port = port;

if (connect_via_proxy()) {
tcp_port = boost::lexical_cast<boost::uint16_t>(*proxy_port_);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Same potential approach here.

boost::uint16_t tcp_port = connect_via_proxy() ? ...;

If yoyu're doing this a lot of times in the same function, I suggest doing it once and storing the bool.


resolver_iterator iter = boost::begin(endpoint_range);
asio::ip::tcp::endpoint endpoint(iter->endpoint().address(),port);
asio::ip::tcp::endpoint endpoint(iter->endpoint().address(),tcp_port);
delegate_->connect(
endpoint, host, source_port,
endpoint, host,port,source_port,
request_strand_.wrap(boost::bind(
&this_type::handle_connected, this_type::shared_from_this(), host,
port, source_port, get_body, callback, generator,
std::make_pair(++iter, resolver_iterator()),
placeholders::error)));
placeholders::error)),
connect_via_proxy(), proxy_username_, proxy_password_);
} else {
set_errors(ec ? ec : boost::asio::error::host_not_found);
boost::iterator_range<const char*> range;
Expand DownExpand Up@@ -176,12 +203,13 @@ struct http_async_connection
resolver_iterator iter = boost::begin(endpoint_range);
asio::ip::tcp::endpoint endpoint(iter->endpoint().address(), port);
delegate_->connect(
endpoint, host, source_port,
endpoint, host,port,source_port,
request_strand_.wrap(boost::bind(
&this_type::handle_connected, this_type::shared_from_this(),
host, port, source_port, get_body, callback, generator,
std::make_pair(++iter, resolver_iterator()),
placeholders::error)));
placeholders::error)),
connect_via_proxy(), proxy_username_, proxy_password_);
} else {
set_errors(ec ? ec : boost::asio::error::host_not_found);
boost::iterator_range<const char*> range;
Expand DownExpand Up@@ -322,10 +350,24 @@ struct http_async_connection
// has already been parsed appropriately and we're
// looking to treat everything that remains in the
// buffer.
typename protocol_base::buffer_type::const_iterator begin =
this->part_begin;
typename protocol_base::buffer_type::const_iterator end = begin;
std::advance(end, remainder);

this->partial_parsed.append(this->part_begin, remainder);
this->part_begin = this->part.begin();
string_type body_string;

if (remove_chunk_markers_ && this->is_chunk_encoding)
{
body_string = parse_chunk_encoding(this->partial_parsed, true);
}
else
{
body_string.swap(this->partial_parsed);
}

typename protocol_base::buffer_type::const_iterator
begin = body_string.c_str(),
end = begin;
std::advance(end, body_string.size());

// We're setting the body promise here to an empty string
// because
Expand All@@ -346,7 +388,7 @@ struct http_async_connection
&this_type::handle_received_data,
this_type::shared_from_this(), body, get_body, callback,
placeholders::error, placeholders::bytes_transferred)));
} else {
} else {
// Here we handle the body data ourself and append to an
// ever-growing string buffer.
this->parse_body(
Expand All@@ -367,10 +409,24 @@ struct http_async_connection
// the end
// of the body processing chain.
if (callback) {

this->partial_parsed.append(this->part_begin, bytes_transferred);
this->part_begin = this->part.begin();
string_type body_string;

if (remove_chunk_markers_ && this->is_chunk_encoding)
{
body_string = parse_chunk_encoding(this->partial_parsed, true);
}
else
{
body_string.swap(this->partial_parsed);
}

typename protocol_base::buffer_type::const_iterator
begin =this->part.begin(),
begin =body_string.c_str(),
end = begin;
std::advance(end,bytes_transferred);
std::advance(end,body_string.size());

// We call the callback function synchronously passing the
// error
Expand All@@ -383,9 +439,9 @@ struct http_async_connection
std::swap(body_string, this->partial_parsed);
body_string.append(this->part.begin(), bytes_transferred);
if (this->is_chunk_encoding)
this->body_promise.set_value(parse_chunk_encoding(body_string));
else
this->body_promise.set_value(body_string);
body_string =parse_chunk_encoding(body_string);

this->body_promise.set_value(body_string);
}
// TODO set the destination value somewhere!
this->destination_promise.set_value("");
Expand All@@ -403,10 +459,25 @@ struct http_async_connection
// callback from here and make sure we're getting more
// data
// right after.
typename protocol_base::buffer_type::const_iterator begin =
this->part.begin();
typename protocol_base::buffer_type::const_iterator end = begin;
std::advance(end, bytes_transferred);

this->partial_parsed.append(this->part_begin, bytes_transferred);
this->part_begin = this->part.begin();
string_type body_string;

if (remove_chunk_markers_ && this->is_chunk_encoding)
{
body_string = parse_chunk_encoding(this->partial_parsed, true);
}
else
{
body_string.swap(this->partial_parsed);
}

typename protocol_base::buffer_type::const_iterator
begin = body_string.c_str(),
end = begin;
std::advance(end, body_string.size());

callback(make_iterator_range(begin, end), ec);
delegate_->read_some(
boost::asio::mutable_buffers_1(this->part.c_array(),
Expand All@@ -415,7 +486,7 @@ struct http_async_connection
&this_type::handle_received_data,
this_type::shared_from_this(), body, get_body, callback,
placeholders::error, placeholders::bytes_transferred)));
} else {
} else {
// Here we don't have a body callback. Let's
// make sure that we deal with the remainder
// from the headers part in case we do have data
Expand DownExpand Up@@ -462,7 +533,7 @@ struct http_async_connection
}
}

string_type parse_chunk_encoding(string_type& body_string) {
string_type parse_chunk_encoding(string_type& body_string, bool update = false) {
string_type body;
string_type crlf = "\r\n";

Expand All@@ -482,16 +553,34 @@ struct http_async_connection
if (len <= body_string.end() - iter) {
body.insert(body.end(), iter, iter + len);
std::advance(iter, len + 2);
begin = iter;
}
else
{
break;
}
begin = iter;
}

if (update)
{
body_string.erase(body_string.begin(), begin);
}
return body;
}

bool connect_via_proxy() const
{
return (proxy_host_ && proxy_port_);
}

int timeout_;
boost::asio::deadline_timer timer_;
bool is_timedout_;
bool remove_chunk_markers_;
optional<string_type> proxy_host_;
optional<string_type> proxy_port_;
optional<string_type> proxy_username_;
optional<string_type> proxy_password_;
bool follow_redirect_;
resolver_type& resolver_;
resolve_function resolve_;
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,8 +13,12 @@ namespace http {
namespace impl {

struct connection_delegate {
virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host, boost::uint16_t source_port,
function<void(system::error_code const &)> handler) = 0;
virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host,
boost::uint16_t port, boost::uint16_t source_port,
function<void(system::error_code const &)> handler,
bool connect_via_proxy,
optional<std::string> proxy_username,
optional<std::string> proxy_password) = 0;
virtual void write(
asio::streambuf &command_streambuf,
function<void(system::error_code const &, size_t)> handler) = 0;
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,20 +7,27 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/optional.hpp>
#include <boost/network/protocol/http/client/connection/connection_delegate.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/enable_shared_from_this.hpp>

namespace boost {
namespace network {
namespace http {
namespace impl {

struct normal_delegate : connection_delegate {
struct normal_delegate : connection_delegate,
enable_shared_from_this<normal_delegate> {
normal_delegate(asio::io_service &service);

virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host, boost::uint16_t source_port,
function<void(system::error_code const &)> handler);
virtual void connect(asio::ip::tcp::endpoint &endpoint, std::string host,
boost::uint16_t port, boost::uint16_t source_port,
function<void(system::error_code const &)> handler,
bool connect_via_proxy,
optional<std::string> proxy_username,
optional<std::string> proxy_password);
virtual void write(
asio::streambuf &command_streambuf,
function<void(system::error_code const &, size_t)> handler);
Expand All@@ -33,9 +40,26 @@ struct normal_delegate : connection_delegate {
private:
asio::io_service &service_;
scoped_ptr<asio::ip::tcp::socket> socket_;
asio::streambuf response_buffer_;

normal_delegate(normal_delegate const &); // = delete
normal_delegate &operator=(normal_delegate); // = delete

void handle_connected(system::error_code const &ec,
function<void(system::error_code const &)> handler,
bool connect_via_proxy,
std::string const &host,
boost::uint16_t port,
optional<std::string> proxy_username,
optional<std::string> proxy_password);

void handle_proxy_sent_request(function<void(system::error_code const &)> handler,
boost::system::error_code const& ec,
std::size_t bytes_transferred);

void handle_proxy_received_data(function<void(system::error_code const &)> handler,
boost::system::error_code const& ec,
std::size_t bytes_transferred);
};

} /* impl */
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp