« CPAN/Maker/Bootstrapper/Role/CreateDeps.pm
↑ Summary
CPAN/Maker/Bootstrapper/Role/ReconcileDeps.pm »
| 1 | package CPAN::Maker::Bootstrapper::Role::PAUSEUpload; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use CLI::Simple::Utils qw(slurp); | ||
| 8 | use Data::Dumper; | ||
| 9 | use English qw(-no_match_vars); | ||
| 10 | use English; | ||
| 11 | use File::Basename qw(basename); | ||
| 12 | |||
| 13 | use Role::Tiny; | ||
| 14 | |||
| 15 | use Readonly; | ||
| 16 | Readonly::Scalar our $CRLF => sprintf '%c%c', 0x0d, 0x0a; | ||
| 17 | Readonly::Scalar our $PAUSE_URL => 'https://pause.perl.org/pause/authenquery?ACTION=add_uri'; | ||
| 18 | |||
| 19 | ######################################################################## | ||
| 20 | sub cmd_publish_to_cpan { | ||
| 21 | ######################################################################## | ||
| 22 | my ($self) = @_; | ||
| 23 | |||
| 24 | require HTTP::Tiny; | ||
| 25 | require MIME::Base64; | ||
| 26 | |||
| 27 | my ( $file, $user, $pass ) = $self->get_args; | ||
| 28 | $user //= $ENV{PAUSE_USER}; | ||
| 29 | $pass //= $ENV{PAUSE_PASSWORD}; | ||
| 30 | |||
| 31 | die "ERROR: distribution file is required\n" | ||
| 32 | if !$file || !-f $file; | ||
| 33 | |||
| 34 | die "ERROR: PAUSE username is required\n" | ||
| 35 | if !$user; | ||
| 36 | |||
| 37 | die "ERROR: PAUSE password is required\n" | ||
| 38 | if !$pass; | ||
| 39 | |||
| 40 | open my $fh, '<:raw', $file | ||
| 41 | or die "ERROR: could not open $file\n$OS_ERROR"; | ||
| 42 | |||
| 43 | my $content = slurp($fh); | ||
| 44 | close $fh; | ||
| 45 | |||
| 46 | my $filename = basename($file); | ||
| 47 | |||
| 48 | my @fields = ( | ||
| 49 | HIDDENNAME => uc $user, | ||
| 50 | CAN_MULTIPART => 1, | ||
| 51 | pause99_add_uri_upload => $filename, | ||
| 52 | pause99_add_uri_uri => q{}, | ||
| 53 | SUBMIT_pause99_add_uri_httpupload => ' Upload this file from my disk ', | ||
| 54 | ); | ||
| 55 | |||
| 56 | # boundary must not occur in the payload | ||
| 57 | my $boundary; | ||
| 58 | do { $boundary = sprintf 'pause%08x%08x', rand 2**32, rand 2**32 } while index( $content, $boundary ) >= 0; | ||
| 59 | |||
| 60 | my $body = q{}; | ||
| 61 | |||
| 62 | while ( my ( $name, $value ) = splice @fields, 0, 2 ) { | ||
| 63 | $body .= "--$boundary$CRLF" . qq{Content-Disposition: form-data; name="$name"$CRLF$CRLF} . "$value$CRLF"; | ||
| 64 | } | ||
| 65 | |||
| 66 | $body | ||
| 67 | .= "--$boundary$CRLF" | ||
| 68 | . qq{Content-Disposition: form-data; name="pause99_add_uri_httpupload"; filename="$filename"$CRLF} | ||
| 69 | . "Content-Type: application/gzip$CRLF$CRLF" | ||
| 70 | . $content | ||
| 71 | . $CRLF | ||
| 72 | . "--$boundary--$CRLF"; | ||
| 73 | |||
| 74 | my $res = HTTP::Tiny->new( verify_SSL => $TRUE, timeout => 30 )->request( | ||
| 75 | POST => $PAUSE_URL, | ||
| 76 | { headers => { | ||
| 77 | 'Content-Type' => "multipart/form-data; boundary=$boundary", | ||
| 78 | 'Authorization' => 'Basic ' . MIME::Base64::encode_base64( "$user:$pass", q{} ), | ||
| 79 | }, | ||
| 80 | content => $body, | ||
| 81 | } | ||
| 82 | ); | ||
| 83 | |||
| 84 | die sprintf "ERROR: upload failed - %s %s\n", $res->{status}, $res->{reason} | ||
| 85 | if !$res->{success}; | ||
| 86 | |||
| 87 | print {*STDERR} "Successfully uploaded $file for PAUSE_USER $user\n"; | ||
| 88 | |||
| 89 | return $SUCCESS; | ||
| 90 | } | ||
| 91 | |||
| 92 | 1; |