[Bps-public-commit] plient branch, master, updated. 95f796eb3201912860c231a69ed4d8f24be50869

? sunnavy sunnavy at bestpractical.com
Thu Apr 29 22:03:00 EDT 2010


The branch, master has been updated
       via  95f796eb3201912860c231a69ed4d8f24be50869 (commit)
      from  1d6bdc01d1a747db3d88afd0a6df303c7147ae84 (commit)

Summary of changes:
 lib/Plient/Handler/HTTPLite.pm |   10 ++++++++++
 lib/Plient/Handler/LWP.pm      |    9 +++++++++
 lib/Plient/Handler/curl.pm     |   27 ++++++++++++++++++++-------
 lib/Plient/Handler/wget.pm     |   20 ++++++++++++++++----
 t/http/app.psgi                |   13 ++++++++++++-
 t/http/get.t                   |   10 +++++++++-
 6 files changed, 76 insertions(+), 13 deletions(-)

- Log -----------------------------------------------------------------
commit 95f796eb3201912860c231a69ed4d8f24be50869
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Apr 30 09:56:41 2010 +0800

    add http headers support

diff --git a/lib/Plient/Handler/HTTPLite.pm b/lib/Plient/Handler/HTTPLite.pm
index 3fd7be0..cf316eb 100644
--- a/lib/Plient/Handler/HTTPLite.pm
+++ b/lib/Plient/Handler/HTTPLite.pm
@@ -20,7 +20,9 @@ sub init {
     $method{http_get} = sub {
         my ( $uri, $args ) = @_;
         my $http  = HTTP::Lite->new;
+        add_headers( $http, $args->{headers} ) if $args->{headers};
         my $res = $http->request($uri) || '';
+
         if ( $res == 200 || $res == 301 || $res == 302 ) {
 
             # XXX TODO handle redirect
@@ -35,6 +37,7 @@ sub init {
     $method{http_post} = sub {
         my ( $uri, $args ) = @_;
         my $http  = HTTP::Lite->new;
+        add_headers( $http, $args->{headers} ) if $args->{headers};
         $http->prepare_post( $args->{body} ) if $args->{body};
         my $res = $http->request($uri) || '';
         if ( $res == 200 || $res == 301 || $res == 302 ) {
@@ -51,6 +54,13 @@ sub init {
     return 1;
 }
 
+sub add_headers {
+    my ( $http, $headers ) = @_;
+    for my $k ( keys %$headers ) {
+        $http->add_req_header( $k, $headers->{$k} );
+    }
+}
+
 __PACKAGE__->_add_to_plient if $Plient::bundle_mode;
 
 1;
diff --git a/lib/Plient/Handler/LWP.pm b/lib/Plient/Handler/LWP.pm
index 8db3fc8..2eb1cd1 100644
--- a/lib/Plient/Handler/LWP.pm
+++ b/lib/Plient/Handler/LWP.pm
@@ -29,6 +29,7 @@ sub init {
 
         # XXX TODO tweak the new arguments
         my $ua  = LWP::UserAgent->new;
+        add_headers( $ua, $args->{headers} ) if $args->{headers};
         my $res = $ua->get($uri);
         if ( $res->is_success ) {
             return $res->decoded_content;
@@ -44,6 +45,7 @@ sub init {
 
         # XXX TODO tweak the new arguments
         my $ua  = LWP::UserAgent->new;
+        add_headers( $ua, $args->{headers} ) if $args->{headers};
         my $res =
           $ua->post( $uri,
             $args->{body} ? ( content => $args->{body} ) : () );
@@ -84,6 +86,13 @@ sub init {
     return 1;
 }
 
+sub add_headers {
+    my ( $ua, $headers ) = @_;
+    for my $k ( keys %$headers ) {
+        $ua->default_header( $k, $headers->{$k} );
+    }
+}
+
 __PACKAGE__->_add_to_plient if $Plient::bundle_mode;
 
 1;
diff --git a/lib/Plient/Handler/curl.pm b/lib/Plient/Handler/curl.pm
index a0540f4..be0843e 100644
--- a/lib/Plient/Handler/curl.pm
+++ b/lib/Plient/Handler/curl.pm
@@ -34,7 +34,8 @@ sub init {
     if ( exists $protocol{http} ) {
         $method{http_get} = sub {
             my ( $uri, $args ) = @_;
-            if ( open my $fh, "$curl -s -L $uri |" ) {
+            my $headers = translate_headers( $args->{headers} );
+            if ( open my $fh, "$curl -s -L $uri $headers |" ) {
                 local $/;
                 <$fh>;
             }
@@ -46,7 +47,6 @@ sub init {
 
         $method{http_post} = sub {
             my ( $uri, $args ) = @_;
-            $args ||= {};
 
             my $data = '';
             if ( $args->{body} ) {
@@ -55,20 +55,22 @@ sub init {
                     if ( defined $kv{$k} ) {
                         if ( ref $kv{$k} && ref $kv{$k} eq 'ARRAY' ) {
                             for my $i ( @{ $kv{$k} } ) {
-                                $data .= " -d $k=$i";
+                                $data .= " -d '$k=$i'";
                             }
                         }
                         else {
-                            $data .= " -d $k=$kv{$k}";
+                            $data .= " -d $k='$kv{$k}'";
                         }
                     }
                     else {
-                        $data .= " -d $k=";
+                        $data .= " -d '$k='";
                     }
                 }
             }
 
-            if ( open my $fh, "$curl -s -L $uri $data |" ) {
+            my $headers = translate_headers( $args->{headers} );
+
+            if ( open my $fh, "$curl -s -L $uri $data $headers |" ) {
                 local $/;
                 <$fh>;
             }
@@ -79,7 +81,8 @@ sub init {
         };
         $method{http_head} = sub {
             my ( $uri, $args ) = @_;
-            if ( open my $fh, "$curl -s -I -L $uri |" ) {
+            my $headers = translate_headers( $args->{headers} );
+            if ( open my $fh, "$curl -s -I -L $uri $headers |" ) {
                 local $/;
                 my $head = <$fh>;
                 $head =~ s/\r\n$//;
@@ -101,6 +104,16 @@ sub init {
     return 1;
 }
 
+sub translate_headers {
+    my $headers = shift;
+    return '' unless $headers;
+    my $str;
+    for my $k ( keys %$headers ) {
+        $str .= " -H '$k:$headers->{$k}'";
+    }
+    return $str;
+}
+
 __PACKAGE__->_add_to_plient if $Plient::bundle_mode;
 1;
 
diff --git a/lib/Plient/Handler/wget.pm b/lib/Plient/Handler/wget.pm
index 15f3e40..6192f9e 100644
--- a/lib/Plient/Handler/wget.pm
+++ b/lib/Plient/Handler/wget.pm
@@ -37,7 +37,8 @@ sub init {
 
     $method{http_get} = sub {
         my ( $uri, $args ) = @_;
-        if ( open my $fh, "$wget -q -O - $uri |" ) {
+        my $headers = translate_headers( $args->{headers} );
+        if ( open my $fh, "$wget -q -O - $headers $uri |" ) {
             local $/;
             <$fh>;
         }
@@ -49,7 +50,7 @@ sub init {
 
     $method{http_post} = sub {
         my ( $uri, $args ) = @_;
-        $args ||= {};
+        my $headers = translate_headers( $args->{headers} );
 
         my $data = '';
         if ( $args->{body} ) {
@@ -71,7 +72,7 @@ sub init {
             }
         }
 
-        if ( open my $fh, "$wget -q -O - $data $uri |" ) {
+        if ( open my $fh, "$wget -q -O - $data $headers $uri |" ) {
             local $/;
             <$fh>;
         }
@@ -84,7 +85,8 @@ sub init {
     $method{http_head} = sub {
         my ( $uri, $args ) = @_;
         # we can't use -q here, or some version may not show the header
-        if ( open my $fh, "$wget -S --spider $uri 2>&1 |" ) {
+        my $headers = translate_headers( $args->{headers} );
+        if ( open my $fh, "$wget -S --spider $headers $uri 2>&1 |" ) {
             my $head = '';
             my $flag;
             while ( my $line = <$fh>) {
@@ -121,6 +123,16 @@ sub init {
     return 1;
 }
 
+sub translate_headers {
+    my $headers = shift;
+    return '' unless $headers;
+    my $str;
+    for my $k ( keys %$headers ) {
+        $str .= " --header '$k:$headers->{$k}'";
+    }
+    return $str;
+}
+
 __PACKAGE__->_add_to_plient if $Plient::bundle_mode;
 
 1;
diff --git a/t/http/app.psgi b/t/http/app.psgi
index 504b0be..0ed31b7 100644
--- a/t/http/app.psgi
+++ b/t/http/app.psgi
@@ -8,7 +8,18 @@ my $app = sub {
     my $req = Plack::Request->new($env);
     if ( $req->method eq 'GET' ) {
         if ( $req->path eq '/hello' ) {
-            return [ 200, [ 'Content-Type' => 'text/plain' ], ['hello'] ] 
+            my $headers = $req->headers;
+            my $agent = $headers->header('User-Agent');
+            if ($agent =~ /plient/) {
+                return [
+                    200,
+                    [ 'Content-Type' => 'text/plain' ],
+                    [ 'hello ', $agent ],
+                ];
+            }
+            else {
+                return [ 200, [ 'Content-Type' => 'text/plain' ], ['hello'] ];
+            }
         }
     }
     elsif ( $req->method eq 'POST' ) {
diff --git a/t/http/get.t b/t/http/get.t
index b1c799c..f0712be 100644
--- a/t/http/get.t
+++ b/t/http/get.t
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 6;
+use Test::More tests => 10;
 
 use_ok('Plient');
 use_ok('Plient::Test');
@@ -13,5 +13,13 @@ SKIP: {
     for my $handler (qw/curl wget HTTPLite LWP/) {
         Plient->handler_preference( http => [$handler] );
         is( plient( GET => "$url/hello" ), 'hello', "get /hello using $handler" );
+        is(
+            plient(
+                GET => "$url/hello",
+                { headers => { 'User-Agent' => 'plient/0.01' } }
+            ),
+            'hello plient/0.01',
+            "get /hello using $handler with customized agent"
+        );
     }
 }

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list