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

? sunnavy sunnavy at bestpractical.com
Wed May 5 02:36:07 EDT 2010


The branch, master has been updated
       via  b5965d0f6390681d11bb6908883193a4c4e4b18b (commit)
       via  31481bd9ff9cb4ad3ab2fb0f851f6eefbbd67ee0 (commit)
      from  7d86f329f2e417e785809d641c5b6ea0962c257a (commit)

Summary of changes:
 bin/plient                     |   39 ++++++++++++++++++++++++++++++++---
 lib/Plient.pm                  |   43 +++++++++++++++++++++++++++++++++++++++-
 lib/Plient/Handler/HTTPLite.pm |    2 +-
 lib/Plient/Handler/LWP.pm      |   30 +++++++++------------------
 lib/Plient/Handler/curl.pm     |   42 +++++++++++---------------------------
 lib/Plient/Handler/wget.pm     |   23 ++++++--------------
 6 files changed, 107 insertions(+), 72 deletions(-)

- Log -----------------------------------------------------------------
commit 31481bd9ff9cb4ad3ab2fb0f851f6eefbbd67ee0
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed May 5 14:04:37 2010 +0800

    refactor body args to support both hashref and arrayref

diff --git a/lib/Plient.pm b/lib/Plient.pm
index 91aa663..26a1f2e 100644
--- a/lib/Plient.pm
+++ b/lib/Plient.pm
@@ -24,6 +24,46 @@ sub plient {
     # http://localhost:5000 => http://localhost:5000/
     $uri .= '/' if $uri =~ m{^https?://[^/]+$};
 
+    # generate both body_hash and body_array to make handlers' life easier
+    if ( $args->{body} ) {
+        if ( ref $args->{body} eq 'HASH' ) {
+            $args->{body_hash}  = $args->{body};
+            $args->{body_array} = [];
+            for my $k ( keys %{ $args->{body} } ) {
+                if ( ref $args->{body}{$k} eq 'ARRAY' ) {
+                    push @{ $args->{body_array} }, $k, $_
+                      for @{ $args->{body}{$k} };
+                }
+                else {
+                    push @{ $args->{body_array} }, $k, $args->{body}{$k};
+                }
+            }
+        }
+        elsif ( ref $args->{body} eq 'ARRAY' ) {
+            $args->{body_array} = $args->{body};
+            $args->{body_hash}  = {};
+            for ( my $i = 0 ; $i < $#{ $args->{body} } ; $i += 2 ) {
+                my $key = $args->{body}[$i];
+                my $value =
+                  defined $args->{body}[ $i + 1 ]
+                  ? $args->{body}[ $i + 1 ]
+                  : '';
+                if ( exists $args->{body_hash}{$key} ) {
+                    if ( ref $args->{body_hash}{$key} eq 'ARRAY' ) {
+                        push @{ $args->{body_hash}{$key} }, $value;
+                    }
+                    else {
+                        $args->{body_hash}{$key} =
+                          [ $args->{body_hash}{$key}, $value ];
+                    }
+                }
+            }
+        }
+        else {
+            die 'invalid body args, should be either hashref or arrayref';
+        }
+    }
+
     my $sub = dispatch( $method, $uri, $args );
     if ( $sub ) {
         if ( $args->{output_file} ) {
@@ -40,6 +80,7 @@ sub plient {
         warn "failed to $method on $uri"; 
         return;
     }
+
 }
 
 sub _extract_protocol {
@@ -312,7 +353,7 @@ hashref, this will be sent as HTTP(S) headers. e.g.
 
 =item body
 
-hashref, this will be sent as HTTP(S) post data. e.g.
+hashref or arrayref, this will be sent as HTTP(S) post data. e.g.
   {
     title => 'foo',
     body    => 'bar',
diff --git a/lib/Plient/Handler/HTTPLite.pm b/lib/Plient/Handler/HTTPLite.pm
index 5b91438..049f420 100644
--- a/lib/Plient/Handler/HTTPLite.pm
+++ b/lib/Plient/Handler/HTTPLite.pm
@@ -57,7 +57,7 @@ sub init {
         my $http  = HTTP::Lite->new;
         $http->proxy( $ENV{http_proxy} ) if $ENV{http_proxy};
         add_headers( $http, $uri, $args );
-        $http->prepare_post( $args->{body} ) if $args->{body};
+        $http->prepare_post( $args->{body_hash} ) if $args->{body_hash};
         my $res = $http->request($uri) || '';
         if ( $res == 200 || $res == 301 || $res == 302 ) {
 
diff --git a/lib/Plient/Handler/LWP.pm b/lib/Plient/Handler/LWP.pm
index ae3b237..5517346 100644
--- a/lib/Plient/Handler/LWP.pm
+++ b/lib/Plient/Handler/LWP.pm
@@ -56,36 +56,26 @@ sub init {
             && $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' ) {
-                        for my $i ( @{ $args->{body}{$k} } ) {
-                            if ( ref $i eq 'HASH' && $i->{file} ) {
-
-                                # file upload
-                                push @$content, $k, [ $i->{file} ];
-                            }
-                            else {
-                                push @$content, $k, $i;
-                            }
-                        }
-                    }
-                    elsif ( $ref eq 'HASH' ) {
+            if ( $args->{body_array} ) {
+                my $body = $args->{body_array};
+
+                for ( my $i = 0 ; $i < $#$body ; $i += 2 ) {
+                    my $key = $body->[$i];
+                    my $value =
+                      defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : '';
+                    if ( ref $value eq 'HASH' && $value->{file} ) {
 
                         # file upload
-                        push @$content, $k, [ $args->{body}{$k}{file} ];
+                        push @$content, $key, [ $value->{file} ];
                     }
                     else {
-                        warn "invalid body value of $k: $args->{body}{$k}";
+                        push @$content, $key, $value;
                     }
                 }
-                else {
-                    push @$content, $k, $args->{body}{$k};
-                }
             }
         }
         else {
-            $content = $args->{body};
+            $content = $args->{body_array};
         }
 
         my $res = $ua->post(
diff --git a/lib/Plient/Handler/curl.pm b/lib/Plient/Handler/curl.pm
index 5d78540..da3ca1d 100644
--- a/lib/Plient/Handler/curl.pm
+++ b/lib/Plient/Handler/curl.pm
@@ -63,38 +63,20 @@ sub init {
                 $post_opt = '-d';
             }
 
-            if ( $args->{body} ) {
-                my %kv = %{$args->{body}};
-                for my $k ( keys %kv ) {
-                    if ( defined $kv{$k} ) {
-                        if ( ref $kv{$k} ) {
-                            if ( ref $kv{$k} eq 'ARRAY' ) {
-                                for my $i ( @{ $kv{$k} } ) {
-                                    if ( ref $i eq 'HASH'
-                                        && $i->{file} )
-                                    {
-                                        # file upload
-                                        $data .= " $post_opt '$k=\@$i->{file}'";
-                                    }
-                                    else {
-                                        $data .= " $post_opt '$k=$i'";
-                                    }
-                                }
-                            }
-                            elsif ( ref $kv{$k} eq 'HASH' && $kv{$k}{file} ) {
-                                # file upload
-                                $data .= " $post_opt '$k=\@$kv{$k}{file}'";
-                            }
-                            else {
-                                warn "invalid body value of $k: $kv{$k}";
-                            }
-                        }
-                        else {
-                            $data .= " $post_opt $k='$kv{$k}'";
-                        }
+            if ( $args->{body_array} ) {
+                my $body = $args->{body_array};
+
+                for ( my $i = 0 ; $i < $#$body ; $i += 2 ) {
+                    my $key = $body->[$i];
+                    my $value =
+                      defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : '';
+                    if ( ref $value eq 'HASH' && $value->{file} ) {
+
+                        # file upload
+                        $data .= " $post_opt '$key=\@$value->{file}'";
                     }
                     else {
-                        $data .= " $post_opt '$k='";
+                        $data .= " $post_opt '$key=$value'";
                     }
                 }
             }
diff --git a/lib/Plient/Handler/wget.pm b/lib/Plient/Handler/wget.pm
index 51dc276..6fec8cb 100644
--- a/lib/Plient/Handler/wget.pm
+++ b/lib/Plient/Handler/wget.pm
@@ -67,22 +67,13 @@ sub init {
         my $auth    = translate_auth($args);
 
         my $data = '';
-        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 .= " --post-data $k=$i";
-                        }
-                    }
-                    else {
-                        $data .= " --post-data $k=$kv{$k}";
-                    }
-                }
-                else {
-                    $data .= " --post-data $k=";
-                }
+        if ( $args->{body_array} ) {
+            my $body = $args->{body_array};
+
+            for ( my $i = 0 ; $i < $#$body ; $i += 2 ) {
+                my $key = $body->[$i];
+                my $value = defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : '';
+                $data .= " --post-data $key=$value";
             }
         }
 

commit b5965d0f6390681d11bb6908883193a4c4e4b18b
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed May 5 14:29:27 2010 +0800

    add post data support for cli

diff --git a/bin/plient b/bin/plient
index 4154e40..1b3ea32 100755
--- a/bin/plient
+++ b/bin/plient
@@ -9,7 +9,7 @@ Plient->import( 'plient', 'plient_support' );
 my %args;
 
 GetOptions( \%args, 'help|h', 'support=s', 'request|X=s', 'output|o=s',
-    'user|u=s' )
+    'data|d=s', 'form|F=s', 'user|u=s' )
   or die 'unknown option';
 
 my $USAGE =<<EOF;
@@ -19,6 +19,9 @@ EXAMPLES:
     plient http://cpan.org/                         # fetch http://cpan.org
     plient -o /tmp/cpan.html http://cpan.org/       # write to file
     plient -u user:password http://foo.org          # use basic auth
+    plient -d foo=bar -d bar=baz http://foo.org     # post with urlencoded
+    plient -F foo=bar -F bar=baz http://foo.org     # post with form-data
+    plient -F foo=@/tmp/bar.log  http://foo.org     # post with file upload
 EOF
 
 if ( $args{help} ) {
@@ -38,14 +41,38 @@ if ( !$ARGV[0] ) {
     exit;
 }
 
-my ( @uri ) = @ARGV;
+if ( $args{data} && $args{form} ) {
+    die "--data and --form can't be both set";
+}
+
 my $method = $args{'request'} || 'get';
+$method = 'post' if $args{form} || $args{data};
+
+my @body;
+if ( $args{form} ) {
+    for my $data ( ref $args{form} eq 'ARRAY' ? @{ $args{form} } : $args{form} )
+    {
+        push @body, map {
+            my ( $k, $v ) = split /=/, $_, 2;
+            $v =~ s/\^@// ? ( $k, { file => $v } ) : ( $k, $v )
+        } split /;/, $data;
+    }
+}
+elsif ( $args{data} ) {
+# can specify multiple times like -d foo=bar -d bar=baz
+    for my $data ( ref $args{data} eq 'ARRAY' ? @{ $args{data} } : $args{data} )
+    {
+        push @body, map { split /=/, $_, 2 } split /;/, $data;
+    }
+}
+
+my ( @uri ) = @ARGV;
 
 for my $uri (@uri) {
     $uri = 'http://' . $uri unless $uri =~ /^\w+:/;
-    my $u = $args{user};
-    my ( $user, $password ) = split /:/, $args{user}, 2;
+    my ( $user, $password );
     if ($user) {
+        ( $user, $password ) = split /:/, $args{user}, 2;
         while ( !defined $password ) {
             $password = prompt_password("password for $user:");
         }
@@ -58,6 +85,8 @@ for my $uri (@uri) {
                 output_file => $args{output},
                 user        => $user,
                 password    => $password,
+                $args{form} ? ( content_type => 'form-data' ) : (),
+                $method =~ /post/i ? ( body => \@body ) : (),
             }
         );
     }
@@ -67,6 +96,8 @@ for my $uri (@uri) {
             {
                 user     => $user,
                 password => $password,
+                $args{form} ? ( content_type => 'form-data' ) : (),
+                $method =~ /post/i ? ( body => \@body ) : (),
             }
         );
     }

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



More information about the Bps-public-commit mailing list