Skip to content
Browse files

Fix and test for case with no content length or chunking

  • Loading branch information...
1 parent 7d798b7 commit 149f3ce7668dd0a6bc51e76e2c6827340639352d @ronaldxs committed
Showing with 31 additions and 9 deletions.
  1. +15 −9 lib/LWP/Simple.pm
  2. +16 −0 t/get-unsized.t
View
24 lib/LWP/Simple.pm
@@ -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();
@@ -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;
@@ -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();
View
16 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.
Something went wrong with that request. Please try again.