From e319675fa2b85dd92f59bf74c29c810110d76bb0 Mon Sep 17 00:00:00 2001 From: Jan van Nunen Date: Tue, 6 Mar 2012 14:44:52 +0100 Subject: [PATCH] Altered HTTP::Lite library to support streaming uploading of files. To upload a file use the following code: #Create http object my $http = new HTTP::Lite; #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' => }; #Make the request $http->request(); This code will send a 'POST' request to and the file will be streamed as the body of the request. --- lib/HTTP/Lite.pm | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/HTTP/Lite.pm b/lib/HTTP/Lite.pm index fa3c114..0f1b035 100644 --- a/lib/HTTP/Lite.pm +++ b/lib/HTTP/Lite.pm @@ -217,9 +217,10 @@ sub request 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 @@ -236,7 +237,11 @@ sub request 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); @@ -269,9 +274,25 @@ sub request } # 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)) {