Permalink
Browse files

Patch for Issue #5, use Content-Type charset to decode content

  • Loading branch information...
CurtTilmes committed Oct 11, 2017
1 parent 95f975f commit 8f94a05801feb99058ed92e2f9b5a2ab35431220
Showing with 61 additions and 1 deletion.
  1. +12 −1 lib/LibCurl/Easy.pm6
  2. +49 −0 xt/04-content.t
View
@@ -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) }
View
@@ -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.