Blame lib/HTTP/Request.pm

Packit a09cf7
package HTTP::Request;
Packit a09cf7
Packit a09cf7
use strict;
Packit a09cf7
use warnings;
Packit a09cf7
Packit a09cf7
our $VERSION = '6.18';
Packit a09cf7
Packit a09cf7
use base 'HTTP::Message';
Packit a09cf7
Packit a09cf7
sub new
Packit a09cf7
{
Packit a09cf7
    my($class, $method, $uri, $header, $content) = @_;
Packit a09cf7
    my $self = $class->SUPER::new($header, $content);
Packit a09cf7
    $self->method($method);
Packit a09cf7
    $self->uri($uri);
Packit a09cf7
    $self;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
Packit a09cf7
sub parse
Packit a09cf7
{
Packit a09cf7
    my($class, $str) = @_;
Packit a09cf7
    Carp::carp('Undefined argument to parse()') if $^W && ! defined $str;
Packit a09cf7
    my $request_line;
Packit a09cf7
    if (defined $str && $str =~ s/^(.*)\n//) {
Packit a09cf7
	$request_line = $1;
Packit a09cf7
    }
Packit a09cf7
    else {
Packit a09cf7
	$request_line = $str;
Packit a09cf7
	$str = "";
Packit a09cf7
    }
Packit a09cf7
Packit a09cf7
    my $self = $class->SUPER::parse($str);
Packit a09cf7
    if (defined $request_line) {
Packit a09cf7
        my($method, $uri, $protocol) = split(' ', $request_line);
Packit a09cf7
        $self->method($method);
Packit a09cf7
        $self->uri($uri) if defined($uri);
Packit a09cf7
        $self->protocol($protocol) if $protocol;
Packit a09cf7
    }
Packit a09cf7
    $self;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
Packit a09cf7
sub clone
Packit a09cf7
{
Packit a09cf7
    my $self = shift;
Packit a09cf7
    my $clone = bless $self->SUPER::clone, ref($self);
Packit a09cf7
    $clone->method($self->method);
Packit a09cf7
    $clone->uri($self->uri);
Packit a09cf7
    $clone;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
Packit a09cf7
sub method
Packit a09cf7
{
Packit a09cf7
    shift->_elem('_method', @_);
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
Packit a09cf7
sub uri
Packit a09cf7
{
Packit a09cf7
    my $self = shift;
Packit a09cf7
    my $old = $self->{'_uri'};
Packit a09cf7
    if (@_) {
Packit a09cf7
	my $uri = shift;
Packit a09cf7
	if (!defined $uri) {
Packit a09cf7
	    # that's ok
Packit a09cf7
	}
Packit a09cf7
	elsif (ref $uri) {
Packit a09cf7
	    Carp::croak("A URI can't be a " . ref($uri) . " reference")
Packit a09cf7
		if ref($uri) eq 'HASH' or ref($uri) eq 'ARRAY';
Packit a09cf7
	    Carp::croak("Can't use a " . ref($uri) . " object as a URI")
Packit a09cf7
		unless $uri->can('scheme') && $uri->can('canonical');
Packit a09cf7
	    $uri = $uri->clone;
Packit a09cf7
	    unless ($HTTP::URI_CLASS eq "URI") {
Packit a09cf7
		# Argh!! Hate this... old LWP legacy!
Packit a09cf7
		eval { local $SIG{__DIE__}; $uri = $uri->abs; };
Packit a09cf7
		die $@ if $@ && $@ !~ /Missing base argument/;
Packit a09cf7
	    }
Packit a09cf7
	}
Packit a09cf7
	else {
Packit a09cf7
	    $uri = $HTTP::URI_CLASS->new($uri);
Packit a09cf7
	}
Packit a09cf7
	$self->{'_uri'} = $uri;
Packit a09cf7
        delete $self->{'_uri_canonical'};
Packit a09cf7
    }
Packit a09cf7
    $old;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
*url = \&uri;  # legacy
Packit a09cf7
Packit a09cf7
sub uri_canonical
Packit a09cf7
{
Packit a09cf7
    my $self = shift;
Packit a09cf7
    return $self->{'_uri_canonical'} ||= $self->{'_uri'}->canonical;
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
Packit a09cf7
sub accept_decodable
Packit a09cf7
{
Packit a09cf7
    my $self = shift;
Packit a09cf7
    $self->header("Accept-Encoding", scalar($self->decodable));
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub as_string
Packit a09cf7
{
Packit a09cf7
    my $self = shift;
Packit a09cf7
    my($eol) = @_;
Packit a09cf7
    $eol = "\n" unless defined $eol;
Packit a09cf7
Packit a09cf7
    my $req_line = $self->method || "-";
Packit a09cf7
    my $uri = $self->uri;
Packit a09cf7
    $uri = (defined $uri) ? $uri->as_string : "-";
Packit a09cf7
    $req_line .= " $uri";
Packit a09cf7
    my $proto = $self->protocol;
Packit a09cf7
    $req_line .= " $proto" if $proto;
Packit a09cf7
Packit a09cf7
    return join($eol, $req_line, $self->SUPER::as_string(@_));
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
sub dump
Packit a09cf7
{
Packit a09cf7
    my $self = shift;
Packit a09cf7
    my @pre = ($self->method || "-", $self->uri || "-");
Packit a09cf7
    if (my $prot = $self->protocol) {
Packit a09cf7
	push(@pre, $prot);
Packit a09cf7
    }
Packit a09cf7
Packit a09cf7
    return $self->SUPER::dump(
Packit a09cf7
        preheader => join(" ", @pre),
Packit a09cf7
	@_,
Packit a09cf7
    );
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
Packit a09cf7
1;
Packit a09cf7
Packit a09cf7
=pod
Packit a09cf7
Packit a09cf7
=encoding UTF-8
Packit a09cf7
Packit a09cf7
=head1 NAME
Packit a09cf7
Packit a09cf7
HTTP::Request - HTTP style request message
Packit a09cf7
Packit a09cf7
=head1 VERSION
Packit a09cf7
Packit a09cf7
version 6.18
Packit a09cf7
Packit a09cf7
=head1 SYNOPSIS
Packit a09cf7
Packit a09cf7
 require HTTP::Request;
Packit a09cf7
 $request = HTTP::Request->new(GET => 'http://www.example.com/');
Packit a09cf7
Packit a09cf7
and usually used like this:
Packit a09cf7
Packit a09cf7
 $ua = LWP::UserAgent->new;
Packit a09cf7
 $response = $ua->request($request);
Packit a09cf7
Packit a09cf7
=head1 DESCRIPTION
Packit a09cf7
Packit a09cf7
C<HTTP::Request> is a class encapsulating HTTP style requests,
Packit a09cf7
consisting of a request line, some headers, and a content body. Note
Packit a09cf7
that the LWP library uses HTTP style requests even for non-HTTP
Packit a09cf7
protocols.  Instances of this class are usually passed to the
Packit a09cf7
request() method of an C<LWP::UserAgent> object.
Packit a09cf7
Packit a09cf7
C<HTTP::Request> is a subclass of C<HTTP::Message> and therefore
Packit a09cf7
inherits its methods.  The following additional methods are available:
Packit a09cf7
Packit a09cf7
=over 4
Packit a09cf7
Packit a09cf7
=item $r = HTTP::Request->new( $method, $uri )
Packit a09cf7
Packit a09cf7
=item $r = HTTP::Request->new( $method, $uri, $header )
Packit a09cf7
Packit a09cf7
=item $r = HTTP::Request->new( $method, $uri, $header, $content )
Packit a09cf7
Packit a09cf7
Constructs a new C<HTTP::Request> object describing a request on the
Packit a09cf7
object $uri using method $method.  The $method argument must be a
Packit a09cf7
string.  The $uri argument can be either a string, or a reference to a
Packit a09cf7
C<URI> object.  The optional $header argument should be a reference to
Packit a09cf7
an C<HTTP::Headers> object or a plain array reference of key/value
Packit a09cf7
pairs.  The optional $content argument should be a string of bytes.
Packit a09cf7
Packit a09cf7
=item $r = HTTP::Request->parse( $str )
Packit a09cf7
Packit a09cf7
This constructs a new request object by parsing the given string.
Packit a09cf7
Packit a09cf7
=item $r->method
Packit a09cf7
Packit a09cf7
=item $r->method( $val )
Packit a09cf7
Packit a09cf7
This is used to get/set the method attribute.  The method should be a
Packit a09cf7
short string like "GET", "HEAD", "PUT", "PATCH" or "POST".
Packit a09cf7
Packit a09cf7
=item $r->uri
Packit a09cf7
Packit a09cf7
=item $r->uri( $val )
Packit a09cf7
Packit a09cf7
This is used to get/set the uri attribute.  The $val can be a
Packit a09cf7
reference to a URI object or a plain string.  If a string is given,
Packit a09cf7
then it should be parsable as an absolute URI.
Packit a09cf7
Packit a09cf7
=item $r->header( $field )
Packit a09cf7
Packit a09cf7
=item $r->header( $field => $value )
Packit a09cf7
Packit a09cf7
This is used to get/set header values and it is inherited from
Packit a09cf7
C<HTTP::Headers> via C<HTTP::Message>.  See L<HTTP::Headers> for
Packit a09cf7
details and other similar methods that can be used to access the
Packit a09cf7
headers.
Packit a09cf7
Packit a09cf7
=item $r->accept_decodable
Packit a09cf7
Packit a09cf7
This will set the C<Accept-Encoding> header to the list of encodings
Packit a09cf7
that decoded_content() can decode.
Packit a09cf7
Packit a09cf7
=item $r->content
Packit a09cf7
Packit a09cf7
=item $r->content( $bytes )
Packit a09cf7
Packit a09cf7
This is used to get/set the content and it is inherited from the
Packit a09cf7
C<HTTP::Message> base class.  See L<HTTP::Message> for details and
Packit a09cf7
other methods that can be used to access the content.
Packit a09cf7
Packit a09cf7
Note that the content should be a string of bytes.  Strings in perl
Packit a09cf7
can contain characters outside the range of a byte.  The C<Encode>
Packit a09cf7
module can be used to turn such strings into a string of bytes.
Packit a09cf7
Packit a09cf7
=item $r->as_string
Packit a09cf7
Packit a09cf7
=item $r->as_string( $eol )
Packit a09cf7
Packit a09cf7
Method returning a textual representation of the request.
Packit a09cf7
Packit a09cf7
=back
Packit a09cf7
Packit a09cf7
=head1 EXAMPLES
Packit a09cf7
Packit a09cf7
Creating requests to be sent with L<LWP::UserAgent> or others can be easy. Here
Packit a09cf7
are a few examples.
Packit a09cf7
Packit a09cf7
=head2 Simple POST
Packit a09cf7
Packit a09cf7
Here, we'll create a simple POST request that could be used to send JSON data
Packit a09cf7
to an endpoint.
Packit a09cf7
Packit a09cf7
    #!/usr/bin/env perl
Packit a09cf7
Packit a09cf7
    use strict;
Packit a09cf7
    use warnings;
Packit a09cf7
Packit a09cf7
    use Encode qw(encode_utf8);
Packit a09cf7
    use HTTP::Request ();
Packit a09cf7
    use JSON::MaybeXS qw(encode_json);
Packit a09cf7
Packit a09cf7
    my $url = 'https://www.example.com/api/user/123';
Packit a09cf7
    my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
Packit a09cf7
    my $data = {foo => 'bar', baz => 'quux'};
Packit a09cf7
    my $encoded_data = encode_utf8(encode_json($data));
Packit a09cf7
Packit a09cf7
    my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);
Packit a09cf7
    # at this point, we could send it via LWP::UserAgent
Packit a09cf7
    # my $ua = LWP::UserAgent->new();
Packit a09cf7
    # my $res = $ua->request($r);
Packit a09cf7
Packit a09cf7
=head2 Batch POST Request
Packit a09cf7
Packit a09cf7
Some services, like Google, allow multiple requests to be sent in one batch.
Packit a09cf7
L<https://developers.google.com/drive/v3/web/batch> for example. Using the
Packit a09cf7
C<add_part> method from L<HTTP::Message> makes this simple.
Packit a09cf7
Packit a09cf7
    #!/usr/bin/env perl
Packit a09cf7
Packit a09cf7
    use strict;
Packit a09cf7
    use warnings;
Packit a09cf7
Packit a09cf7
    use Encode qw(encode_utf8);
Packit a09cf7
    use HTTP::Request ();
Packit a09cf7
    use JSON::MaybeXS qw(encode_json);
Packit a09cf7
Packit a09cf7
    my $auth_token = 'auth_token';
Packit a09cf7
    my $batch_url = 'https://www.googleapis.com/batch';
Packit a09cf7
    my $url = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id';
Packit a09cf7
    my $url_no_email = 'https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false';
Packit a09cf7
Packit a09cf7
    # generate a JSON post request for one of the batch entries
Packit a09cf7
    my $req1 = build_json_request($url, {
Packit a09cf7
        emailAddress => 'example@appsrocks.com',
Packit a09cf7
        role => "writer",
Packit a09cf7
        type => "user",
Packit a09cf7
    });
Packit a09cf7
Packit a09cf7
    # generate a JSON post request for one of the batch entries
Packit a09cf7
    my $req2 = build_json_request($url_no_email, {
Packit a09cf7
        domain => "appsrocks.com",
Packit a09cf7
        role => "reader",
Packit a09cf7
        type => "domain",
Packit a09cf7
    });
Packit a09cf7
Packit a09cf7
    # generate a multipart request to send all of the other requests
Packit a09cf7
    my $r = HTTP::Request->new('POST', $batch_url, [
Packit a09cf7
        'Accept-Encoding' => 'gzip',
Packit a09cf7
        # if we don't provide a boundary here, HTTP::Message will generate
Packit a09cf7
        # one for us. We could use UUID::uuid() here if we wanted.
Packit a09cf7
        'Content-Type' => 'multipart/mixed; boundary=END_OF_PART'
Packit a09cf7
    ]);
Packit a09cf7
Packit a09cf7
    # add the two POST requests to the main request
Packit a09cf7
    $r->add_part($req1, $req2);
Packit a09cf7
    # at this point, we could send it via LWP::UserAgent
Packit a09cf7
    # my $ua = LWP::UserAgent->new();
Packit a09cf7
    # my $res = $ua->request($r);
Packit a09cf7
    exit();
Packit a09cf7
Packit a09cf7
    sub build_json_request {
Packit a09cf7
        my ($url, $href) = @_;
Packit a09cf7
        my $header = ['Authorization' => "Bearer $auth_token", 'Content-Type' => 'application/json; charset=UTF-8'];
Packit a09cf7
        return HTTP::Request->new('POST', $url, $header, encode_utf8(encode_json($href)));
Packit a09cf7
    }
Packit a09cf7
Packit a09cf7
=head1 SEE ALSO
Packit a09cf7
Packit a09cf7
L<HTTP::Headers>, L<HTTP::Message>, L<HTTP::Request::Common>,
Packit a09cf7
L<HTTP::Response>
Packit a09cf7
Packit a09cf7
=head1 AUTHOR
Packit a09cf7
Packit a09cf7
Gisle Aas <gisle@activestate.com>
Packit a09cf7
Packit a09cf7
=head1 COPYRIGHT AND LICENSE
Packit a09cf7
Packit a09cf7
This software is copyright (c) 1994-2017 by Gisle Aas.
Packit a09cf7
Packit a09cf7
This is free software; you can redistribute it and/or modify it under
Packit a09cf7
the same terms as the Perl 5 programming language system itself.
Packit a09cf7
Packit a09cf7
=cut
Packit a09cf7
Packit a09cf7
__END__
Packit a09cf7
Packit a09cf7
Packit a09cf7
#ABSTRACT: HTTP style request message