HTTP-Request-FromCurl

 view release on metacpan or  search on metacpan

lib/HTTP/Request/CurlParameters.pm  view on Meta::CPAN


=cut

has cookie_jar_options => (
    is => 'ro',
    default => sub { {} },
);

=item *

C<credentials>

    credentials => 'hunter2:secret'

The credentials to use for basic authentication.

=cut

has credentials => (
    is => 'ro',
);

=item *

C<auth>

    auth => 'basic'

The authentication method to use.

lib/HTTP/Request/CurlParameters.pm  view on Meta::CPAN

    my $constructor_args = join ",",
                           $self->_pairlist([
                                     send_te => 0,
                               maybe local_address => $self->local_address,
                               maybe max_size      => $self->max_filesize,
                               maybe timeout       => $self->timeout,
                               maybe cookie_jar    => $init_cookie_jar->{code},
                               maybe SSL_opts      => keys %ssl_options ? \%ssl_options : undef,
                           ], '')
                           ;
    if( defined( my $credentials = $self->credentials )) {
        my( $user, $pass ) = split /:/, $credentials, 2;
        my $setup_credentials = sprintf qq{\$ua->credentials("%s","%s");},
            quotemeta $user,
            quotemeta $pass;
        push @setup_ua, $setup_credentials;
    };
    if( $self->show_error ) {
        push @postamble,
            '    die $res->message if $res->is_error;',
    } elsif( $self->fail ) {
        push @postamble,
            '    exit 1 if !$res->{success};',
    };

    @setup_ua = ()

lib/HTTP/Request/CurlParameters.pm  view on Meta::CPAN

    my $constructor_args = join ",",
                           $self->_pairlist([
                                     @ssl,
                               maybe timeout       => $self->timeout,
                               maybe local_address => $self->local_address,
                               maybe max_size      => $self->max_filesize,
                               maybe cookie_jar    => $init_cookie_jar->{code},
                               maybe SSL_options   => keys %ssl_options ? \%ssl_options : undef,
                           ], '')
                           ;
    if( defined( my $credentials = $self->credentials )) {
        my( $user, $pass ) = split /:/, $credentials, 2;
        my $setup_credentials = sprintf qq{\$ua->credentials("%s","%s");},
            quotemeta $user,
            quotemeta $pass;
        push @setup_ua, $setup_credentials;
    };

    @setup_ua = ()
        if @setup_ua == 1;

    @preamble = map { "$options{prefix}    $_\n" } @preamble;
    @postamble = map { "$options{prefix}    $_\n" } @postamble;
    @setup_ua = map { "$options{prefix}    $_\n" } @setup_ua;

    my @content = $self->_build_quoted_body();

lib/HTTP/Request/CurlParameters.pm  view on Meta::CPAN

    my $constructor_args = join ",",
                           $self->_pairlist([
                                     @ssl,
                                     keys %$socket_options ? ( socket_options => $socket_options ) : (),
                               maybe request_timeout    => $self->timeout,
                               maybe max_response_size  => $self->max_filesize,
                               maybe cookie_jar    => $init_cookie_jar->{code},
                               maybe SSL_options   => keys %ssl_options ? \%ssl_options : undef,
                           ], '')
                           ;
    if( defined( my $credentials = $self->credentials )) {
        my( $user, $pass ) = split /:/, $credentials, 2;
        my $setup_credentials = sprintf qq{\$ua->userinfo("%s","%s");},
            quotemeta $user,
            quotemeta $pass;
        push @setup_ua, $setup_credentials;
    };

    @setup_ua = ()
        if @setup_ua == 1;

    @preamble = map { "$options{prefix}    $_\n" } @preamble;
    @postamble = map { "$options{prefix}    $_\n" } @postamble;
    @setup_ua = map { "$options{prefix}    $_\n" } @setup_ua;

    my $content = $self->_build_quoted_body();

lib/HTTP/Request/FromCurl.pm  view on Meta::CPAN

        };

        if( $options->{ 'user' } ) {
            if(    $options->{anyauth}
                || $options->{digest}
                || $options->{ntlm}
                || $options->{negotiate}
                ) {
                # Nothing to do here, just let LWP::UserAgent do its thing
                # This means one additional request to fetch the appropriate
                # 401 response asking for credentials, but ...
            } else {
                # $options->{basic} or none at all
                my $info = delete $options->{'user'};
                # We need to bake this into the header here?!
                push @headers, sprintf 'Authorization: Basic %s', encode_base64( $info );
            }
        };

        my %headers;
        for my $kv (

lib/HTTP/Request/FromCurl.pm  view on Meta::CPAN

        };

        push @res, HTTP::Request::CurlParameters->new({
                  method => $method,
                  uri    => $uri,
                  headers => \%headers,
                  body   => $body,
            maybe auth => $auth,
            maybe cert => $options->{cert},
            maybe capath => $options->{capath},
            maybe credentials => $options->{ user },
            maybe output => $options->{ output },
            maybe timeout => $options->{ 'max-time' },
            maybe cookie_jar => $options->{'cookie-jar'},
            maybe cookie_jar_options => $options->{'cookie-jar-options'},
            maybe insecure => $options->{'insecure'},
            maybe max_filesize => $options->{'max-filesize'},
            maybe show_error => $options->{'show-error'},
            maybe fail => $options->{'fail'},
            maybe unix_socket => $options->{'unix-socket'},
            maybe local_address => $options->{'interface'},

lib/HTTP/Request/FromFetch.pm  view on Meta::CPAN

=head1 NAME

HTTP::Request::FromFetch - turn a Javascript fetch() statement into HTTP::Request

=head1 SYNOPSIS

  my $ua = LWP::UserAgent->new();
  my $req = HTTP::Request::FromFetch->new(<<'JS')->as_request;

      await fetch("https://www.example.com/index.html", {
          "credentials": "include",
          "headers": {
              "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0",
              "Accept": "text/javascript, text/html, application/xml, text/xml, */*",
              "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
              "X-CSRF-Token": "secret",
              "X-Requested-With": "XMLHttpRequest"
          },
          "referrer": "https://www.example.com/",
          "method": "GET",
          "mode": "cors"

lib/HTTP/Request/FromFetch.pm  view on Meta::CPAN

    if( defined $o and $o =~ /\S/ ) {
        $options = decode_json($o);
    } else {
        $options = {};
    };

    $options->{uri} = $u;
    $options->{method} ||= 'GET';
    $options->{mode} ||= 'cors';
    $options->{cache} ||= 'default';
    $options->{credentials} ||= 'same-origin';
    $options->{headers} ||= {};


    HTTP::Request::CurlParameters->new({
            method     => delete $options->{method} || 'GET',
            uri        => $options->{uri},
            headers    => $options->{headers},
            maybe body => $options->{body},
            #maybe credentials => $options->{ user },
    });
}

1;

=head1 SEE ALSO

L<Javascript Fetch API|https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>

=head1 REPOSITORY

lib/HTTP/Request/FromWget.pm  view on Meta::CPAN

            $request_default_headers{ 'Content-Length' } = length $body;
        };

        if( $options->{ 'user' } || $options->{'http-user'} ) {
            if(    $options->{anyauth}
                || $options->{ntlm}
                || $options->{negotiate}
                ) {
                # Nothing to do here, just let LWP::UserAgent do its thing
                # This means one additional request to fetch the appropriate
                # 401 response asking for credentials, but ...
            } else {
                # $options->{basic} or none at all
                my $info = delete $options->{'user'} || delete $options->{'http-user'};
                # We need to bake this into the header here?!
                push @headers, sprintf 'Authorization: Basic %s', encode_base64( $info );
            }
        };

        my %headers;
        for my $kv (

lib/HTTP/Request/FromWget.pm  view on Meta::CPAN

        };

        push @res, HTTP::Request::CurlParameters->new({
            method => $method,
            uri    => $uri,
            headers => \%headers,
            body   => $body,
            maybe local_address => $options->{local_address},
            maybe cert => $options->{certificate},
            maybe capath => $options->{'ca-directory'},
            maybe credentials => $options->{ user },
            maybe output => $options->{ output },
            maybe timeout => $options->{ 'max-time' },
            maybe cookie_jar => $options->{'cookie-jar'},
            maybe cookie_jar_options => $options->{'cookie-jar-options'},
            maybe insecure => !$options->{'check-certificate'},
            maybe show_error => $options->{'show_error'},
            maybe fail => $options->{'fail'},
        });
    }

t/fetch-identity.t  view on Meta::CPAN

    { js => <<'JS', name => 'empty options'},
        fetch("https://example.com/",{})
JS

    { js => <<'JS', name => 'empty options'},
        fetch("https://example.com/",{ "method":"GET" })
JS

    { js => <<'JS', name => 'full options'},
        fetch("https://example.com/foo",{
        "credentials": "include",
        "headers": {
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0",
            "Accept": "text/javascript, text/html, application/xml, text/xml, */*",
            "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
            "X-Requested-With": "XMLHttpRequest"
        },
        "referrer": "https://www.example.com/",
        "method": "GET",
        "mode": "cors"
    })
JS

# This is unsupported so far
#    { js => <<'JS', name => 'single quotes'},
#        fetch("https://example.com/foo",{
#        'credentials': 'include'
#    })
#JS
);

sub compiles_ok( $code, $name ) {
    my( $fh, $tempname ) = tempfile( UNLINK => 1 );
    binmode $fh, ':raw';
    print $fh $code;
    close $fh;



( run in 0.263 second using v1.01-cache-2.11-cpan-4d50c553e7e )