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

The .Str method gets confused by binary $.content in HTTP::Message #171

Open
zostay opened this issue Apr 23, 2017 · 3 comments
Open

The .Str method gets confused by binary $.content in HTTP::Message #171

zostay opened this issue Apr 23, 2017 · 3 comments

Comments

@zostay
Copy link

zostay commented Apr 23, 2017

If I build an HTTP::Response object that places a Buf in $.content, the .Str method will fail with the following exception:

Cannot use a Buf as a string, but you called the Stringy method on it
  in method Str at <snip>/.rakudobrew/moar-nom/install/share/perl6/site/sources/36<snip>64 (HTTP::Message) line 231
  in method Str at <snip>/.rakudobrew/moar-nom/install/share/perl6/site/sources/99<snip>B5 (HTTP::Response) line 117
  ...
@jonathanstowe
Copy link
Collaborator

Hi,
as it currently stands you can pass :bin to the .Str method and it will attempt to do something sensible. Obviously this doesn't help if the .Str is being called implicitly somehow.

I guess, as long as the content-type is set appropriately, it could try harder, bearing in mind that it both wants to send actually binary data as well as text data that may be in an encoding that Perl doesn't know about (and thus needs to be a Blob)

@zostay
Copy link
Author

zostay commented Apr 23, 2017

The :bin arg is irrelevant in this case. The same error occurs. Here's a code snippet demonstrating:

my $res = HTTP::Response.new(200);
$res.content = buf8.new; # $.content has to be primed for binary
$res.add-content("Hello World".encode);
say $res.Str(:bin); # fails with or without bin

The need for "priming" is a minor annoyance but might be considered a bug as well.

@zostay
Copy link
Author

zostay commented May 3, 2017

Actually, that example does not work. I had not realized that "".encode ~ "" was a legal operation. This code actually demonstrates the problem I'm seeing:

my $res = HTTP::Response.new(200);
$res.content = buf8.new; # $.content has to be primed for binary
$res.add-content: buf8.new(0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64);
say $res.Str(bin => ?(rand <= 0.5)); # fails with or without bin

The UserAgent itself avoids this problem. It would only come up with code that was putting bufs into the HTTP::Message without the help of UA, which had not yet been careful about encoding. Perhaps the onus is on the caller to be that careful instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants