Subject: | Per request proxy support |
This patch add posibility to override proxy for a specific request. I have so updated documentation also. So native langugage speakers please fix documentation if I made grammar mistakes.
Index: HTTP.pm
===================================================================
--- HTTP.pm (revision 203)
+++ HTTP.pm (working copy)
@@ -148,12 +148,17 @@
sub poco_weeble_request {
my (
$kernel, $heap, $sender,
- $response_event, $http_request, $tag, $progress_event
- ) = @_[KERNEL, HEAP, SENDER, ARG0, ARG1, ARG2, ARG3];
+ $response_event, $http_request, $tag, $progress_event,
+ $proxy_override
+ ) = @_[KERNEL, HEAP, SENDER, ARG0, ARG1, ARG2, ARG3, ARG4];
+ if (defined $proxy_override) {
+ POE::Component::Client::HTTP::RequestFactory->parse_proxy($proxy_override);
+ }
my $request = $heap->{factory}->create_request(
- $http_request, $response_event, $tag, $progress_event, $sender
+ $http_request, $response_event, $tag, $progress_event,
+ $proxy_override, $sender
);
$heap->{request}->{$request->ID} = $request;
@@ -833,13 +838,16 @@
GET 'http://poe.perl.org', # a simple HTTP request
'unique id', # a tag to identify the request
'progress', # an event to indicate progress
+ 'http://1.2.3.4:80/' # proxy to use for this request
);
Requests include the state to which responses will be posted. In the
previous example, the handler for a 'response' state will be called
with each HTTP response. The "progress" handler is optional and if
installed, the component will provide progress metrics (see sample
-handler below).
+handler below). The "proxy" parameter is optional and if not defined,
+default proxy will be used if configured, if not no proxy will be
+used.
=head2 pending_requests_count
Index: RequestFactory.pm
===================================================================
--- RequestFactory.pm (revision 203)
+++ RequestFactory.pm (working copy)
@@ -125,22 +125,8 @@
# Translate environment variable formats into internal versions.
- if (defined $proxy) {
- if (ref($proxy) eq 'ARRAY') {
- croak "Proxy must contain [HOST,PORT]" unless @$proxy == 2;
- $proxy = [ $proxy ];
- }
- else {
- my @proxies = split /\s*\,\s*/, $proxy;
- foreach (@proxies) {
- s/^http:\/+//;
- s/\/+$//;
- croak "Proxy must contain host:port" unless /^(.+):(\d+)$/;
- $_ = [ $1, $2 ];
- }
- $proxy = \@proxies;
- }
- }
+ $class->parse_proxy($proxy)
+ if (defined $proxy);
if (defined $no_proxy) {
unless (ref($no_proxy) eq 'ARRAY') {
@@ -235,8 +221,8 @@
=cut
sub create_request {
- my ($self, $http_request, $response_event, $tag, $progress_event, $sender) =
- @_;
+ my ($self, $http_request, $response_event, $tag,
+ $progress_event, $proxy_override, $sender) = @_;
# Add a protocol if one isn't included.
$http_request->protocol( $self->[FCT_PROTOCOL] ) unless (
@@ -287,7 +273,8 @@
# not in our no_proxy, then use the proxy. Otherwise use the
# request URI.
- my $proxy = $self->[FCT_PROXY];
+ my $proxy = $proxy_override || $self->[FCT_PROXY];
+
if (defined $proxy) {
# This request qualifies for proxying. Replace the host and port
# with the proxy's host and port. This comes after the Host:
@@ -386,4 +373,40 @@
}
return $self->[FCT_FOLLOWREDIRECTS];
}
+
+=head2 parse_proxy $proxy
+
+This static method is used for parsing proxies. The $proxy can be
+array reference like [host, port] or comma separated string like
+"http://1.2.3.4:80/, http://2.3.4.5:80/".
+
+Result will be array reference of two element tuples (also array
+references) of [ host, port ]
+
+=cut
+
+sub parse_proxy {
+ my $proxy = $_[1];
+
+ if (ref($proxy) eq 'ARRAY') {
+ croak "Proxy must contain [HOST,PORT]" unless @$proxy == 2;
+ $proxy = [ $proxy ];
+ } else {
+ my @proxies = split /\s*\,\s*/, $proxy;
+ foreach (@proxies) {
+ s/^http:\/+//;
+ s/\/+$//;
+ croak "Proxy must contain host:port" unless /^(.+):(\d+)$/;
+ $_ = [ $1, $2 ];
+ }
+ if (@proxies) {
+ $proxy = \@proxies;
+ } else {
+ undef $proxy; # Empty proxy list means not to use proxy
+ }
+ }
+
+ $_[1] = $proxy;
+}
+
1;