Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two small fixes. #18

Merged
merged 4 commits into from
Oct 24, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ Current status
As of 2011-04-22, runs with all recent rakudo builds.
It correctly follows redirects, but no infinite redirects
detection yet.

A known problem is that it corrupts binary files downloads.
24 changes: 15 additions & 9 deletions lib/LWP/Simple.pm
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 whole document without, we believe, sizing from server'
);

done;
3 changes: 1 addition & 2 deletions t/get-w3-latin1-utf8.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use LWP::Simple;

my $html = LWP::Simple.get('http://www.w3.org/2006/11/mwbp-tests/test-encoding-8.html');

my $find_char = Buf.new(0xE9).decode('iso-8859-1');
my $find_char = chr(233); # small e with acute
ok(
$html.match('</html>') && $html.match($find_char),
'Got latin-1 page'
);

$html = LWP::Simple.get('http://www.w3.org/2006/11/mwbp-tests/test-encoding-3.html');
$find_char = Buf.new(0xC3, 0xA9).decode('utf-8');
ok(
$html.match('</html>') && $html.match($find_char),
'Got utf-8 page'
Expand Down