<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package HTTP::Request::Common;

use strict;
use warnings;

our $VERSION = '6.22';

our $DYNAMIC_FILE_UPLOAD ||= 0;  # make it defined (don't know why)

use Exporter 5.57 'import';

our @EXPORT =qw(GET HEAD PUT PATCH POST OPTIONS);
our @EXPORT_OK = qw($DYNAMIC_FILE_UPLOAD DELETE);

require HTTP::Request;
use Carp();

my $CRLF = "\015\012";   # "\r\n" is not portable

sub GET  { _simple_req('GET',  @_); }
sub HEAD { _simple_req('HEAD', @_); }
sub DELETE { _simple_req('DELETE', @_); }
sub PATCH { request_type_with_data('PATCH', @_); }
sub POST { request_type_with_data('POST', @_); }
sub PUT { request_type_with_data('PUT', @_); }
sub OPTIONS { request_type_with_data('OPTIONS', @_); }

sub request_type_with_data
{
    my $type = shift;
    my $url  = shift;
    my $req = HTTP::Request-&gt;new($type =&gt; $url);
    my $content;
    $content = shift if @_ and ref $_[0];
    my($k, $v);
    while (($k,$v) = splice(@_, 0, 2)) {
	if (lc($k) eq 'content') {
	    $content = $v;
	}
	else {
	    $req-&gt;push_header($k, $v);
	}
    }
    my $ct = $req-&gt;header('Content-Type');
    unless ($ct) {
	$ct = 'application/x-www-form-urlencoded';
    }
    elsif ($ct eq 'form-data') {
	$ct = 'multipart/form-data';
    }

    if (ref $content) {
	if ($ct =~ m,^multipart/form-data\s*(;|$),i) {
	    require HTTP::Headers::Util;
	    my @v = HTTP::Headers::Util::split_header_words($ct);
	    Carp::carp("Multiple Content-Type headers") if @v &gt; 1;
	    @v = @{$v[0]};

	    my $boundary;
	    my $boundary_index;
	    for (my @tmp = @v; @tmp;) {
		my($k, $v) = splice(@tmp, 0, 2);
		if ($k eq "boundary") {
		    $boundary = $v;
		    $boundary_index = @v - @tmp - 1;
		    last;
		}
	    }

	    ($content, $boundary) = form_data($content, $boundary, $req);

	    if ($boundary_index) {
		$v[$boundary_index] = $boundary;
	    }
	    else {
		push(@v, boundary =&gt; $boundary);
	    }

	    $ct = HTTP::Headers::Util::join_header_words(@v);
	}
	else {
	    # We use a temporary URI object to format
	    # the application/x-www-form-urlencoded content.
	    require URI;
	    my $url = URI-&gt;new('http:');
	    $url-&gt;query_form(ref($content) eq "HASH" ? %$content : @$content);
	    $content = $url-&gt;query;

	    # HTML/4.01 says that line breaks are represented as "CR LF" pairs (i.e., `%0D%0A')
	    $content =~ s/(?&lt;!%0D)%0A/%0D%0A/g if defined($content);
	}
    }

    $req-&gt;header('Content-Type' =&gt; $ct);  # might be redundant
    if (defined($content)) {
	$req-&gt;header('Content-Length' =&gt;
		     length($content)) unless ref($content);
	$req-&gt;content($content);
    }
    else {
        $req-&gt;header('Content-Length' =&gt; 0);
    }
    $req;
}


sub _simple_req
{
    my($method, $url) = splice(@_, 0, 2);
    my $req = HTTP::Request-&gt;new($method =&gt; $url);
    my($k, $v);
    my $content;
    while (($k,$v) = splice(@_, 0, 2)) {
	if (lc($k) eq 'content') {
	    $req-&gt;add_content($v);
            $content++;
	}
	else {
	    $req-&gt;push_header($k, $v);
	}
    }
    if ($content &amp;&amp; !defined($req-&gt;header("Content-Length"))) {
        $req-&gt;header("Content-Length", length(${$req-&gt;content_ref}));
    }
    $req;
}


sub form_data   # RFC1867
{
    my($data, $boundary, $req) = @_;
    my @data = ref($data) eq "HASH" ? %$data : @$data;  # copy
    my $fhparts;
    my @parts;
    while (my ($k,$v) = splice(@data, 0, 2)) {
	if (!ref($v)) {
	    $k =~ s/([\\\"])/\\$1/g;  # escape quotes and backslashes
            no warnings 'uninitialized';
	    push(@parts,
		 qq(Content-Disposition: form-data; name="$k"$CRLF$CRLF$v));
	}
	else {
	    my($file, $usename, @headers) = @$v;
	    unless (defined $usename) {
		$usename = $file;
		$usename =~ s,.*/,, if defined($usename);
	    }
            $k =~ s/([\\\"])/\\$1/g;
	    my $disp = qq(form-data; name="$k");
            if (defined($usename) and length($usename)) {
                $usename =~ s/([\\\"])/\\$1/g;
                $disp .= qq(; filename="$usename");
            }
	    my $content = "";
	    my $h = HTTP::Headers-&gt;new(@headers);
	    if ($file) {
		open(my $fh, "&lt;", $file) or Carp::croak("Can't open file $file: $!");
		binmode($fh);
		if ($DYNAMIC_FILE_UPLOAD) {
		    # will read file later, close it now in order to
                    # not accumulate to many open file handles
                    close($fh);
		    $content = \$file;
		}
		else {
		    local($/) = undef; # slurp files
		    $content = &lt;$fh&gt;;
		    close($fh);
		}
		unless ($h-&gt;header("Content-Type")) {
		    require LWP::MediaTypes;
		    LWP::MediaTypes::guess_media_type($file, $h);
		}
	    }
	    if ($h-&gt;header("Content-Disposition")) {
		# just to get it sorted first
		$disp = $h-&gt;header("Content-Disposition");
		$h-&gt;remove_header("Content-Disposition");
	    }
	    if ($h-&gt;header("Content")) {
		$content = $h-&gt;header("Content");
		$h-&gt;remove_header("Content");
	    }
	    my $head = join($CRLF, "Content-Disposition: $disp",
			           $h-&gt;as_string($CRLF),
			           "");
	    if (ref $content) {
		push(@parts, [$head, $$content]);
		$fhparts++;
	    }
	    else {
		push(@parts, $head . $content);
	    }
	}
    }
    return ("", "none") unless @parts;

    my $content;
    if ($fhparts) {
	$boundary = boundary(10) # hopefully enough randomness
	    unless $boundary;

	# add the boundaries to the @parts array
	for (1..@parts-1) {
	    splice(@parts, $_*2-1, 0, "$CRLF--$boundary$CRLF");
	}
	unshift(@parts, "--$boundary$CRLF");
	push(@parts, "$CRLF--$boundary--$CRLF");

	# See if we can generate Content-Length header
	my $length = 0;
	for (@parts) {
	    if (ref $_) {
	 	my ($head, $f) = @$_;
		my $file_size;
		unless ( -f $f &amp;&amp; ($file_size = -s _) ) {
		    # The file is either a dynamic file like /dev/audio
		    # or perhaps a file in the /proc file system where
		    # stat may return a 0 size even though reading it
		    # will produce data.  So we cannot make
		    # a Content-Length header.
		    undef $length;
		    last;
		}
	    	$length += $file_size + length $head;
	    }
	    else {
		$length += length;
	    }
        }
        $length &amp;&amp; $req-&gt;header('Content-Length' =&gt; $length);

	# set up a closure that will return content piecemeal
	$content = sub {
	    for (;;) {
		unless (@parts) {
		    defined $length &amp;&amp; $length != 0 &amp;&amp;
		    	Carp::croak "length of data sent did not match calculated Content-Length header.  Probably because uploaded file changed in size during transfer.";
		    return;
		}
		my $p = shift @parts;
		unless (ref $p) {
		    $p .= shift @parts while @parts &amp;&amp; !ref($parts[0]);
		    defined $length &amp;&amp; ($length -= length $p);
		    return $p;
		}
		my($buf, $fh) = @$p;
                unless (ref($fh)) {
                    my $file = $fh;
                    undef($fh);
                    open($fh, "&lt;", $file) || Carp::croak("Can't open file $file: $!");
                    binmode($fh);
                }
		my $buflength = length $buf;
		my $n = read($fh, $buf, 2048, $buflength);
		if ($n) {
		    $buflength += $n;
		    unshift(@parts, ["", $fh]);
		}
		else {
		    close($fh);
		}
		if ($buflength) {
		    defined $length &amp;&amp; ($length -= $buflength);
		    return $buf
	    	}
	    }
	};

    }
    else {
	$boundary = boundary() unless $boundary;

	my $bno = 0;
      CHECK_BOUNDARY:
	{
	    for (@parts) {
		if (index($_, $boundary) &gt;= 0) {
		    # must have a better boundary
		    $boundary = boundary(++$bno);
		    redo CHECK_BOUNDARY;
		}
	    }
	    last;
	}
	$content = "--$boundary$CRLF" .
	           join("$CRLF--$boundary$CRLF", @parts) .
		   "$CRLF--$boundary--$CRLF";
    }

    wantarray ? ($content, $boundary) : $content;
}


sub boundary
{
    my $size = shift || return "xYzZY";
    require MIME::Base64;
    my $b = MIME::Base64::encode(join("", map chr(rand(256)), 1..$size*3), "");
    $b =~ s/[\W]/X/g;  # ensure alnum only
    $b;
}

1;

=pod

=encoding UTF-8

=head1 NAME

HTTP::Request::Common - Construct common HTTP::Request objects

=head1 VERSION

version 6.22

=head1 SYNOPSIS

  use HTTP::Request::Common;
  $ua = LWP::UserAgent-&gt;new;
  $ua-&gt;request(GET 'http://www.sn.no/');
  $ua-&gt;request(POST 'http://somewhere/foo', [foo =&gt; bar, bar =&gt; foo]);
  $ua-&gt;request(PATCH 'http://somewhere/foo', [foo =&gt; bar, bar =&gt; foo]);
  $ua-&gt;request(PUT 'http://somewhere/foo', [foo =&gt; bar, bar =&gt; foo]);
  $ua-&gt;request(OPTIONS 'http://somewhere/foo', [foo =&gt; bar, bar =&gt; foo]);

=head1 DESCRIPTION

This module provides functions that return newly created C&lt;HTTP::Request&gt;
objects.  These functions are usually more convenient to use than the
standard C&lt;HTTP::Request&gt; constructor for the most common requests.

Note that L&lt;LWP::UserAgent&gt; has several convenience methods, including
C&lt;get&gt;, C&lt;head&gt;, C&lt;delete&gt;, C&lt;post&gt; and C&lt;put&gt;.

The following functions are provided:

=over 4

=item GET $url

=item GET $url, Header =&gt; Value,...

The C&lt;GET&gt; function returns an L&lt;HTTP::Request&gt; object initialized with
the "GET" method and the specified URL.  It is roughly equivalent to the
following call

  HTTP::Request-&gt;new(
     GET =&gt; $url,
     HTTP::Headers-&gt;new(Header =&gt; Value,...),
  )

but is less cluttered.  What is different is that a header named
C&lt;Content&gt; will initialize the content part of the request instead of
setting a header field.  Note that GET requests should normally not
have a content, so this hack makes more sense for the C&lt;PUT&gt;, C&lt;PATCH&gt;
 and C&lt;POST&gt; functions described below.

The C&lt;get(...)&gt; method of L&lt;LWP::UserAgent&gt; exists as a shortcut for
C&lt;&lt; $ua-&gt;request(GET ...) &gt;&gt;.

=item HEAD $url

=item HEAD $url, Header =&gt; Value,...

Like GET() but the method in the request is "HEAD".

The C&lt;head(...)&gt;  method of L&lt;LWP::UserAgent&gt; exists as a shortcut for
C&lt;&lt; $ua-&gt;request(HEAD ...) &gt;&gt;.

=item DELETE $url

=item DELETE $url, Header =&gt; Value,...

Like C&lt;GET&gt; but the method in the request is C&lt;DELETE&gt;.  This function
is not exported by default.

=item PATCH $url

=item PATCH $url, Header =&gt; Value,...

=item PATCH $url, $form_ref, Header =&gt; Value,...

=item PATCH $url, Header =&gt; Value,..., Content =&gt; $form_ref

=item PATCH $url, Header =&gt; Value,..., Content =&gt; $content

The same as C&lt;POST&gt; below, but the method in the request is C&lt;PATCH&gt;.

=item PUT $url

=item PUT $url, Header =&gt; Value,...

=item PUT $url, $form_ref, Header =&gt; Value,...

=item PUT $url, Header =&gt; Value,..., Content =&gt; $form_ref

=item PUT $url, Header =&gt; Value,..., Content =&gt; $content

The same as C&lt;POST&gt; below, but the method in the request is C&lt;PUT&gt;

=item OPTIONS $url

=item OPTIONS $url, Header =&gt; Value,...

=item OPTIONS $url, $form_ref, Header =&gt; Value,...

=item OPTIONS $url, Header =&gt; Value,..., Content =&gt; $form_ref

=item OPTIONS $url, Header =&gt; Value,..., Content =&gt; $content

The same as C&lt;POST&gt; below, but the method in the request is C&lt;OPTIONS&gt;

=item POST $url

=item POST $url, Header =&gt; Value,...

=item POST $url, $form_ref, Header =&gt; Value,...

=item POST $url, Header =&gt; Value,..., Content =&gt; $form_ref

=item POST $url, Header =&gt; Value,..., Content =&gt; $content

C&lt;POST&gt;, C&lt;PATCH&gt; and C&lt;PUT&gt; all work with the same parameters.

  %data = ( title =&gt; 'something', body =&gt; something else' );
  $ua = LWP::UserAgent-&gt;new();
  $request = HTTP::Request::Common::POST( $url, [ %data ] );
  $response = $ua-&gt;request($request);

They take a second optional array or hash reference
parameter C&lt;$form_ref&gt;.  The content can also be specified
directly using the C&lt;Content&gt; pseudo-header, and you may also provide
the C&lt;$form_ref&gt; this way.

The C&lt;Content&gt; pseudo-header steals a bit of the header field namespace as
there is no way to directly specify a header that is actually called
"Content".  If you really need this you must update the request
returned in a separate statement.

The C&lt;$form_ref&gt; argument can be used to pass key/value pairs for the
form content.  By default we will initialize a request using the
C&lt;application/x-www-form-urlencoded&gt; content type.  This means that
you can emulate an HTML E&lt;lt&gt;form&gt; POSTing like this:

  POST 'http://www.perl.org/survey.cgi',
       [ name   =&gt; 'Gisle Aas',
         email  =&gt; 'gisle@aas.no',
         gender =&gt; 'M',
         born   =&gt; '1964',
         perc   =&gt; '3%',
       ];

This will create an L&lt;HTTP::Request&gt; object that looks like this:

  POST http://www.perl.org/survey.cgi
  Content-Length: 66
  Content-Type: application/x-www-form-urlencoded

  name=Gisle%20Aas&amp;email=gisle%40aas.no&amp;gender=M&amp;born=1964&amp;perc=3%25

Multivalued form fields can be specified by either repeating the field
name or by passing the value as an array reference.

The POST method also supports the C&lt;multipart/form-data&gt; content used
for I&lt;Form-based File Upload&gt; as specified in RFC 1867.  You trigger
this content format by specifying a content type of C&lt;'form-data'&gt; as
one of the request headers.  If one of the values in the C&lt;$form_ref&gt; is
an array reference, then it is treated as a file part specification
with the following interpretation:

  [ $file, $filename, Header =&gt; Value... ]
  [ undef, $filename, Header =&gt; Value,..., Content =&gt; $content ]

The first value in the array ($file) is the name of a file to open.
This file will be read and its content placed in the request.  The
routine will croak if the file can't be opened.  Use an C&lt;undef&gt; as
$file value if you want to specify the content directly with a
C&lt;Content&gt; header.  The $filename is the filename to report in the
request.  If this value is undefined, then the basename of the $file
will be used.  You can specify an empty string as $filename if you
want to suppress sending the filename when you provide a $file value.

If a $file is provided by no C&lt;Content-Type&gt; header, then C&lt;Content-Type&gt;
and C&lt;Content-Encoding&gt; will be filled in automatically with the values
returned by C&lt;LWP::MediaTypes::guess_media_type()&gt;

Sending my F&lt;~/.profile&gt; to the survey used as example above can be
achieved by this:

  POST 'http://www.perl.org/survey.cgi',
       Content_Type =&gt; 'form-data',
       Content      =&gt; [ name  =&gt; 'Gisle Aas',
                         email =&gt; 'gisle@aas.no',
                         gender =&gt; 'M',
                         born   =&gt; '1964',
                         init   =&gt; ["$ENV{HOME}/.profile"],
                       ]

This will create an L&lt;HTTP::Request&gt; object that almost looks this (the
boundary and the content of your F&lt;~/.profile&gt; is likely to be
different):

  POST http://www.perl.org/survey.cgi
  Content-Length: 388
  Content-Type: multipart/form-data; boundary="6G+f"

  --6G+f
  Content-Disposition: form-data; name="name"

  Gisle Aas
  --6G+f
  Content-Disposition: form-data; name="email"

  gisle@aas.no
  --6G+f
  Content-Disposition: form-data; name="gender"

  M
  --6G+f
  Content-Disposition: form-data; name="born"

  1964
  --6G+f
  Content-Disposition: form-data; name="init"; filename=".profile"
  Content-Type: text/plain

  PATH=/local/perl/bin:$PATH
  export PATH

  --6G+f--

If you set the C&lt;$DYNAMIC_FILE_UPLOAD&gt; variable (exportable) to some TRUE
value, then you get back a request object with a subroutine closure as
the content attribute.  This subroutine will read the content of any
files on demand and return it in suitable chunks.  This allow you to
upload arbitrary big files without using lots of memory.  You can even
upload infinite files like F&lt;/dev/audio&gt; if you wish; however, if
the file is not a plain file, there will be no C&lt;Content-Length&gt; header
defined for the request.  Not all servers (or server
applications) like this.  Also, if the file(s) change in size between
the time the C&lt;Content-Length&gt; is calculated and the time that the last
chunk is delivered, the subroutine will C&lt;Croak&gt;.

The C&lt;post(...)&gt;  method of L&lt;LWP::UserAgent&gt; exists as a shortcut for
C&lt;&lt; $ua-&gt;request(POST ...) &gt;&gt;.

=back

=head1 SEE ALSO

L&lt;HTTP::Request&gt;, L&lt;LWP::UserAgent&gt;

Also, there are some examples in L&lt;HTTP::Request/"EXAMPLES"&gt; that you might
find useful. For example, batch requests are explained there.

=head1 AUTHOR

Gisle Aas &lt;gisle@activestate.com&gt;

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 1994-2017 by Gisle Aas.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut

__END__


#ABSTRACT: Construct common HTTP::Request objects
</pre></body></html>