Rodent of Unusual Size <[EMAIL PROTECTED]> writes:

> Gisle Aas wrote:
> > 
> > Rodent of Unusual Size <[EMAIL PROTECTED]> writes:
> > 
> > > I'm trying to find documentation on how to force a request
> > > content-body to be chunked, and your name came up.  Can
> > > you point me to any documentation on how to do this in
> > > LWP?
> > 
> > It should happen if you put a code reference as the content of the
> > request.  The code will be called until it returns an empty string (or
> > undef) and each chunk it returns will be passed to the server using
> > the Transfer-Encoding: chunked.
> 
> Uh, colour me Perl-reference-challenged and pardon my ignorance.. :-/
> 
> So the size of the chunks is entirely up to the LWP caller,
> and not something I can ask LWP to transparently do for me
> on a 10MB content-body?  Okey, I can live with that.

The function that LWP protocol handler calls for each chunk decides
itself how much data to return.  The chunk size will be the length of
this data.

> So, given something like
> 
>  $ua = new LWP::UserAgent;
>  $req = new HTTP::Request(GET => '/cgi-bin/chunk-test')
>  $resp = $us->request($req);
> 
> how would I modify it to do this code-reference thing?

This is an example that will make LWP use chunked transfer encoding in
the request.  In this case each chunk will be 2 bytes long.

#!/usr/bin/perl

require LWP::UserAgent;
my $req = HTTP::Request->new(POST => 'http://eik.aas.no:2426');
$req->content_type("text/plain");
$req->content(chunk_it("hi dude\n"));

my $ua = LWP::UserAgent->new;
my $res = $ua->request($req);
print $res->as_string;


sub chunk_it {
    my $str = shift;
    my $delay = shift;
    $delay = 1 unless defined $delay;

    return sub {
        select(undef, undef, undef, $delay) if $delay;
        return substr($str, 0, 2, "");
    }
}

Reply via email to