Skip Menu |

This queue is for tickets about the HTTP-Lite CPAN distribution.

Report information
The Basics
Id: 45805
Status: open
Priority: 0/
Queue: HTTP-Lite

People
Owner: Nobody in particular
Requestors: marko.nordberg [...] pp.inet.fi
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 2.1.6
Fixed in: (no value)



Subject: Content-Type is forced to be application/x-www-form-urlencoded if method is POST
HTTP::Lite forces Content-Type to application/x-www-form-urlencoded if method is POST. This is incorrect if POST is used for example to post a XML docoument used in XML-RPC protocol or similar. HTTP::Lite should use the content type set by method add_req_header(). Particulary XML:TreePP should support HTTP::Lite but this is support is now broken. Also XML::RPC uses XML::TreePP so it is also broken.
From: bram.stappers [...] gmail.com
See attached patch for HTTP-Lite (against version 2.3). This allows the following use: #Create http object my $http = HTTP::Lite->new; #Set the content type $http->add_req_header('Content-Type', 'application/octet-stream'); #Set the request method $http->method('POST'); #Set the file that has to be uploaded $http->{'content'} = {'file' => <FILENAME>}; #Make the request $http->request(<URL>); This code will send a 'POST' request to <URL> and the file <FILENAME> will be streamed as the body of the request.
Subject: HTTP-Lite.patch
Index: perl5/HTTP/Lite.pm =================================================================== --- perl5/HTTP/Lite.pm (revision 21952) +++ perl5/HTTP/Lite.pm (working copy) @@ -219,9 +219,10 @@ if (!defined($self->get_req_header("Accept"))) { $self->add_req_header("Accept", "*/*"); } - - if ($method eq 'POST') { - $self->http_write(*FH, "Content-Type: application/x-www-form-urlencoded$CRLF"); + my $content_type = + $self->get_req_header("Content-Type") || $method eq 'POST' && 'application/x-www-form-urlencoded'; + if ($content_type) { + $self->http_write(*FH, "Content-Type: $content_type$CRLF"); } # Purge a couple others @@ -238,7 +239,11 @@ my $content_length; if (defined($self->{content})) { - $content_length = length($self->{content}); + if (!ref $self->{content}) { + $content_length = length($self->{content}); + } elsif (ref $self->{content} eq 'HASH' && defined $self->{content}->{'file'}) { + $content_length = -s $self->{content}->{'file'}; + } } if (defined($callback_func)) { my $ncontent_length = &$callback_func($self, "content-length", undef, @$callback_params); @@ -271,9 +276,25 @@ } # Output content, if any - if (!$content_out && defined($self->{content})) - { - $self->http_write(*FH, $self->{content}); + if (!$content_out && defined($self->{content})) { + if (!ref $self->{content}) { + $self->http_write(*FH, $self->{content}); + } elsif ( + ref $self->{content} eq 'HASH' && + defined $self->{content}->{'file'} && + -r $self->{content}->{'file'} + ) { + local *FILE; + my $data; + + if (open(FILE, '<', $self->{content}->{'file'})) { + binmode(FILE); + while (read(FILE, $data, 1024)) { + $self->http_write(*FH, $data); + } + close(FILE); + } + } } if (defined($callback_func)) {
Hi, It seems that this issue is still affecting HTTP::Lite 2.4, so I am attaching a small patch which fix it (without introducing other functionnalities). I agree however that some way to control directly the content of the POST query would be good (maybe with a 'content' methodr which would override the parameter of the 'prepare_post' method). But this is only a feature request and not a bug (let me know if you want a patch to implement it). Anyway, thanks for your module which is very nice and convenient to use, Regards, Mathias
Subject: HTTP-Lite.diff
--- HTTP/Lite.pm.orig 2013-01-30 14:19:54.363697200 +0100 +++ HTTP/Lite.pm 2013-01-30 14:34:53.689828800 +0100 @@ -220,12 +220,12 @@ $self->add_req_header("Accept", "*/*"); } - if ($method eq 'POST') { - $self->http_write(*FH, "Content-Type: application/x-www-form-urlencoded$CRLF"); + if (!$self->get_req_header('Content-Type') && $method eq 'POST') { + # Anyway, this is done on line 594 + $self->add_req_header('Content-Type', "application/x-www-form-urlencoded$CRLF"); } # Purge a couple others - $self->delete_req_header("Content-Type"); $self->delete_req_header("Content-Length"); # Output headers