Skip to content

Commit

Permalink
Fix and test for case with no content length or chunking
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldxs committed Oct 22, 2012
1 parent 7d798b7 commit 149f3ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/LWP/Simple.pm
Expand Up @@ -17,6 +17,7 @@ our $.class_default_encoding = 'utf-8';
# these were intended to be constant but that hit pre-compilation issue
my Buf $crlf = Buf.new(13, 10);
my Buf $http_header_end_marker = Buf.new(13, 10, 13, 10);
my Int constant $default_stream_read_len = 2 * 1024;

method base64encode ($user, $pass) {
my MIME::Base64 $mime .= new();
Expand Down Expand Up @@ -194,19 +195,11 @@ method make_request (

$sock.send($req_str);

my Buf $resp = $sock.read(2 * 1024);
my Buf $resp = $sock.read($default_stream_read_len);

my ($status, $resp_headers, $resp_content) = self.parse_response($resp);


if ( $resp_headers<Content-Length> &&
$resp_content.bytes < $resp_headers<Content-Length>
) {
$resp_content ~= $sock.read(
$resp_headers<Content-Length> - $resp_content.bytes
);
}

if (($resp_headers<Transfer-Encoding> || '') eq 'chunked') {
my Bool $is_last_chunk;
my Buf $resp_content_chunk;
Expand All @@ -222,6 +215,19 @@ method make_request (
$resp_content ~= $resp_content_chunk;
}
}
elsif ( $resp_headers<Content-Length> &&
$resp_content.bytes < $resp_headers<Content-Length>
) {
$resp_content ~= $sock.read(
$resp_headers<Content-Length> - $resp_content.bytes
);
}
else { # a bit hacky for now but should be ok
while ($resp.bytes > 0) {
$resp = $sock.read($default_stream_read_len);
$resp_content ~= $resp;
}
}

$sock.close();

Expand Down
16 changes: 16 additions & 0 deletions t/get-unsized.t
@@ -0,0 +1,16 @@
use v6;
use Test;

use LWP::Simple;

# this page is, for now, delivered by a server that does not provide
# a content length or do chunking
my $html = LWP::Simple.get('http://www.rosettacode.org/wiki/Rosetta_Code');

ok(
$html.match('About Rosetta Code') &&
$html.match('</html>') && $html.chars > 12_000,
'make sure we pulled down whole document for some substantial size'
);

done;

0 comments on commit 149f3ce

Please sign in to comment.