| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package HTTP::Request::AsCurl; | 
| 2 | 2 |  |  | 2 |  | 125596 | use 5.008005; | 
|  | 2 |  |  |  |  | 58 |  | 
|  | 2 |  |  |  |  | 91 |  | 
| 3 | 2 |  |  | 2 |  | 13 | use strict; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 287 |  | 
| 4 | 2 |  |  | 2 |  | 22 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 92 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = "0.03"; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 9 | use Carp; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 142 |  | 
| 9 | 2 |  |  | 2 |  | 2845 | use String::ShellQuote qw/ shell_quote /; | 
|  | 2 |  |  |  |  | 1787 |  | 
|  | 2 |  |  |  |  | 145 |  | 
| 10 | 2 |  |  | 2 |  | 3605 | use Win32::ShellQuote qw/ cmd_escape /; | 
|  | 2 |  |  |  |  | 3993 |  | 
|  | 2 |  |  |  |  | 225 |  | 
| 11 | 2 |  |  | 2 |  | 8889 | use Exporter::Shiny qw/ as_curl /; | 
|  | 2 |  |  |  |  | 8285 |  | 
|  | 2 |  |  |  |  | 16 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub as_curl { | 
| 14 | 15 |  |  | 15 | 1 | 26883 | my ($request, %params) = @_; | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 15 |  |  |  |  | 59 | my $content = $request->content; | 
| 17 | 15 |  |  |  |  | 225 | my @data    = split '&', $content; | 
| 18 | 15 |  |  |  |  | 52 | my $method  = $request->method; | 
| 19 | 15 |  |  |  |  | 185 | my $uri     = $request->uri; | 
| 20 | 15 |  |  |  |  | 168 | my $headers = $request->headers; | 
| 21 | 15 |  |  |  |  | 432 | my $user    = $headers->authorization_basic; | 
| 22 | 15 |  |  |  |  | 3244 | my @h       = grep { $_ !~ /(authorization|content-length|content-type)/i } | 
|  | 21 |  |  |  |  | 296 |  | 
| 23 |  |  |  |  |  |  | $headers->header_field_names; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 15 |  |  |  |  | 106 | my @cmd = (["curl"]); | 
| 26 | 15 |  |  |  |  | 38 | push(@cmd, ["--request", $method, $uri]); | 
| 27 | 15 |  |  |  |  | 37 | push(@cmd, ["--dump-header", "-"]); | 
| 28 | 15 | 100 |  |  |  | 47 | push(@cmd, ["--user",   $user]) if $user; | 
| 29 | 15 |  |  |  |  | 51 | push(@cmd, ["--header", "$_: " . $headers->header($_)]) for sort @h; | 
| 30 | 15 |  |  |  |  | 61 | push(@cmd, ["--data", $_]) for sort @data; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 15 | 100 |  |  |  | 48 | return map { @$_ } @cmd unless keys %params; | 
|  | 20 |  |  |  |  | 79 |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 10 |  |  |  |  | 39 | return _make_it_pretty(\@cmd, %params); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub _make_it_pretty { | 
| 38 | 10 |  |  | 10 |  | 27 | my ($cmd, %params) = @_; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 10 |  | 33 |  |  | 35 | $params{shell}   = $params{shell}   || _default_shell_escape(); | 
| 41 | 10 |  | 50 |  |  | 59 | $params{newline} = $params{newline} || "\n"; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 10 |  |  |  |  | 13 | my $string; | 
| 44 | 10 |  |  |  |  | 21 | for my $part (@$cmd) { | 
| 45 | 40 | 100 |  |  |  | 116 | if ($params{shell} eq 'win32') { | 
|  |  | 50 |  |  |  |  |  | 
| 46 | 20 |  |  |  |  | 94 | $string .= cmd_escape join " ", @$part; | 
| 47 | 20 |  |  |  |  | 189 | $string .= ' ^' . $params{newline}; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | elsif ($params{shell} eq 'bourne') { | 
| 50 | 20 |  |  |  |  | 70 | $string .= shell_quote @$part; | 
| 51 | 20 |  |  |  |  | 1317 | $string .= ' \\' . $params{newline}; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | else { | 
| 54 | 0 |  |  |  |  | 0 | croak "this shell is not currently supported: $params{shell}"; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 10 |  |  |  |  | 79 | return $string; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 | 0 |  | 0 |  |  | sub _default_shell_escape { $^O eq 'MSWin32' ? 'win32' : 'bourne' } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | 1; | 
| 66 |  |  |  |  |  |  | __END__ |