sergot / http-useragent

Web user agent class for Perl 6.
MIT License
36 stars 39 forks source link

Binary file support for multipart/form-data #189

Open zostay opened 6 years ago

zostay commented 6 years ago

I happily discovered that the multipart/form-data handling here is close to what you can do with LWP in Perl. However, while something like this will work fine:

my $req = POST('http://localhost/', content => [a => '1', b => ['META6.json']], Content-type => 'multipart/form-data');
my $res = $ua.request($req);

This will not:

my $req = POST('http://localhost/', content => [a => '1', b => ['backups.tar.gz']], Content-type => 'multipart/form-data');
my $res = $ua.request($req);

The exception is:

Malformed UTF-8 at line 1 col 2 in block at /usr/local/Cellar/rakudo-star/2018.01/share/perl6/site/sources/C1F4E39233DAF4BE7A977583897637177143784D (HTTP::Request) line 220 in method form-data at /usr/local/Cellar/rakudo-star/2018.01/share/perl6/site/sources/C1F4E39233DAF4BE7A977583897637177143784D (HTTP::Request) line 199 in block at /usr/local/Cellar/rakudo-star/2018.01/share/perl6/site/sources/C1F4E39233DAF4BE7A977583897637177143784D (HTTP::Request) line 183 in method add-form-data at /usr/local/Cellar/rakudo-star/2018.01/share/perl6/site/sources/C1F4E39233DAF4BE7A977583897637177143784D (HTTP::Request) line 169 in sub POST at /usr/local/Cellar/rakudo-star/2018.01/share/perl6/site/sources/8C3E66EA3422F50B53B12301D43EDC5DCB9BE169 (HTTP::Request::Common) line 24 in sub POST at /usr/local/Cellar/rakudo-star/2018.01/share/perl6/site/sources/8C3E66EA3422F50B53B12301D43EDC5DCB9BE169 (HTTP::Request::Common) line 28 in block at /Users/sterling/test.p6 line 1

The problem is that the file is being slurped as text, when there's no way of knowing what the format of the original file is. The slurping ought to be done in bin mode, but that will lead to "Cannot use a Buf as a string, but you called the Stringy method on it" as soon as the binary content is added to the otherwise stringy content used to construct the headers and multipart boundaries.