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

Please add a "fix content length" mutator #136

Open
leonerd opened this issue May 1, 2020 · 3 comments
Open

Please add a "fix content length" mutator #136

leonerd opened this issue May 1, 2020 · 3 comments

Comments

@leonerd
Copy link

leonerd commented May 1, 2020

99% of the time I create an HTTP::Response I forget to set the Content-Length header, thus stalling the HTTP pipeline. To fix this one simply has to remember to

$resp->content_length( length $resp->content );

Which is reasonable enough if you have the response in a variable. However, #135 wishes to add chaining mutators in order to support fully-constructing a response object in a single expression without needing such a temporary. Without it it becomes hard to fix the content length.

I'd therefore suggest either a special "fix the content length" method, or else the default behaviour of a ->set_content_length method with no additional arguments, to do this.

return HTTP::Response->new( 200 )
    ->set_content( join "\n", @lines )
    ->set_content_length;
@leonerd
Copy link
Author

leonerd commented May 1, 2020

An easy implementation here is simply:

sub set_content_length
{
  my $self = shift;
  my $length = @_ ? shift : length $self->content;
  $self->content_length($length);
  return $self;
}

@genio
Copy link
Member

genio commented May 1, 2020

hrm... I'm iffy here.

@leonerd
Copy link
Author

leonerd commented May 1, 2020

@genio Well name it something better like ->fix_content_length ;)

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