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

? sunnavy sunnavy at bestpractical.com
Tue May 4 04:28:04 EDT 2010


The branch, master has been updated
       via  d2f5c5e3b67b3f614efdf831d15f5e1eb665d210 (commit)
      from  833788123eed328198b1e7316694ec0d34cb9a03 (commit)

Summary of changes:
 lib/Plient/Handler/LWP.pm  |   45 +++++++++++++++++++++++++++++++++++++++++--
 lib/Plient/Handler/curl.pm |   34 +++++++++++++++++++++++++-------
 2 files changed, 68 insertions(+), 11 deletions(-)

- Log -----------------------------------------------------------------
commit d2f5c5e3b67b3f614efdf831d15f5e1eb665d210
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue May 4 16:28:09 2010 +0800

    add form-data support for curl and LWP

diff --git a/lib/Plient/Handler/LWP.pm b/lib/Plient/Handler/LWP.pm
index 5a91ed1..287b72a 100644
--- a/lib/Plient/Handler/LWP.pm
+++ b/lib/Plient/Handler/LWP.pm
@@ -48,9 +48,48 @@ sub init {
         my $ua  = LWP::UserAgent->new;
         $ua->env_proxy;
         add_headers( $ua, $uri, $args );
-        my $res =
-          $ua->post( $uri,
-            $args->{body} ? ( content => $args->{body} ) : () );
+
+        my $content = [];
+        my $is_form_data;
+        if (   $args->{body}
+            && $args->{content_type}
+            && $args->{content_type} =~ /form-data/ )
+        {
+            $is_form_data = 1;
+            for my $k ( keys %{ $args->{body} } ) {
+                if ( my $ref = ref $args->{body}{$k} ) {
+                    if ( $ref eq 'ARRAY' ) {
+                        push @$content, $k, $args->{body}{$k}{$_}
+                          for @{ $args->{body}{$k} };
+                    }
+                    elsif ( $ref eq 'HASH' ) {
+
+                        # file upload
+                        push @$content, $k, [ $args->{body}{$k}{file} ];
+                    }
+                    else {
+                        warn "invalid body value of $k: $args->{body}{$k}";
+                    }
+                }
+                else {
+                    push @$content, $k, $args->{body}{$k};
+                }
+            }
+        }
+        else {
+            $content = $args->{body};
+        }
+
+        my $res = $ua->post(
+            $uri,
+            (
+                $is_form_data
+                ? ( Content_Type => 'form−data' )
+                : ()
+            ),
+            @$content ? ( Content => $content ) : (),
+        );
+
         if ( $res->is_success ) {
             return $res->decoded_content;
         }
diff --git a/lib/Plient/Handler/curl.pm b/lib/Plient/Handler/curl.pm
index e833274..5d4ff44 100644
--- a/lib/Plient/Handler/curl.pm
+++ b/lib/Plient/Handler/curl.pm
@@ -49,29 +49,47 @@ sub init {
         $method{http_post} = sub {
             my ( $uri, $args ) = @_;
 
+
+            my $headers = translate_headers($args);
+            my $auth    = translate_auth($args);
+
             my $data = '';
+            my $post_opt;
+            if ( $args->{content_type} && $args->{content_type} =~ /form-data/ )
+            {
+                $post_opt = '-F';
+            }
+            else {
+                $post_opt = '-d';
+            }
+
             if ( $args->{body} ) {
                 my %kv = %{$args->{body}};
                 for my $k ( keys %kv ) {
                     if ( defined $kv{$k} ) {
-                        if ( ref $kv{$k} && ref $kv{$k} eq 'ARRAY' ) {
-                            for my $i ( @{ $kv{$k} } ) {
-                                $data .= " -d '$k=$i'";
+                        if ( ref $kv{$k} ) {
+                            if ( ref $kv{$k} eq 'ARRAY' ) {
+                                for my $i ( @{ $kv{$k} } ) {
+                                    $data .= " $post_opt '$k=$i'";
+                                }
+                            }
+                            elsif ( ref $kv{$k} eq 'HASH' && $kv{$k}{file} ) {
+                                $data .= " $post_opt '\@$kv{$k}{file}'";
+                            }
+                            else {
+                                warn "invalid body value of $k: $kv{$k}";
                             }
                         }
                         else {
-                            $data .= " -d $k='$kv{$k}'";
+                            $data .= " $post_opt $k='$kv{$k}'";
                         }
                     }
                     else {
-                        $data .= " -d '$k='";
+                        $data .= " $post_opt '$k='";
                     }
                 }
             }
 
-            my $headers = translate_headers($args);
-            my $auth    = translate_auth($args);
-
             if ( open my $fh, "$curl -s -L $data $headers $auth $uri |" ) {
                 local $/;
                 <$fh>;

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



More information about the Bps-public-commit mailing list