Skip to content

Commit

Permalink
Patch for Issue #5, use Content-Type charset to decode content
Browse files Browse the repository at this point in the history
  • Loading branch information
CurtTilmes committed Oct 11, 2017
1 parent 95f975f commit 8f94a05
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/LibCurl/Easy.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,18 @@ class LibCurl::Easy
state $v = LibCurl::version.new.info
}

method content($encoding = 'utf-8') returns Str { $!buf.decode($encoding) }
method content($encoding is copy = 'utf-8') returns Str
{
with self.receiveheaders<Content-Type>
{
# Should be a stricter grammar, but dump regex will work for now
if /charset \s* '=' \s* \\? <['"]>? (<-[\\"']>*) \\? <['"]>? /
{
$encoding = $0.Str;
}
}
$!buf.decode($encoding)
}

method error() returns Str { nativecast(Str, $!errorbuffer) }

Expand Down
49 changes: 49 additions & 0 deletions xt/04-content.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use v6;

use Test;
use Test::When <online>;

use LibCurl::Easy;

my $version = LibCurl::Easy.version;

my $curl = LibCurl::Easy.new(URL => 'http://greenbytes.de/tech/tc/httpcontenttype/textplainutf8.asis')
.perform;

is $curl.content, "Umlaute: äöüÄÖÜ Euro: €\n",
'Check decoded content, charset=UTF-8';


$curl.URL('http://greenbytes.de/tech/tc/httpcontenttype/textplainutf8qs.asis')
.perform;

is $curl.content, "Umlaute: äöüÄÖÜ Euro: €\n",
'Check decoded content, charset="UTF-8"';

$curl.URL('http://greenbytes.de/tech/tc/httpcontenttype/textplainutf8ws.asis')
.perform;

is $curl.content, "Umlaute: äöüÄÖÜ Euro: €\n",
'Check decoded content, charset = UTF-8';


$curl.URL('http://greenbytes.de/tech/tc/httpcontenttype/textplainutf8qsa.asis')
.perform;

is $curl.content, "Umlaute: äöüÄÖÜ Euro: €\n",
"Check decoded content, charset='UTF-8'";

$curl.URL('http://greenbytes.de/tech/tc/httpcontenttype/textplainutf8extparam.asis')
.perform;

is $curl.content, "Umlaute: äöüÄÖÜ Euro: €\n",
'Check decoded content, charset="UTF-8"';


$curl.URL('http://greenbytes.de/tech/tc/httpcontenttype/textplainutf8extparamqs1.asis')
.perform;

is $curl.content, "Umlaute: äöüÄÖÜ Euro: €\n",
'Check decoded content, charset=\"UTF-8\"';

done-testing;

0 comments on commit 8f94a05

Please sign in to comment.