libwww-perl / HTTP-Message

The HTTP-Message distribution contains classes useful for representing the messages passed in HTTP style communication.

Home Page:https://metacpan.org/pod/HTTP::Message

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Please add a "fix content length" mutator

leonerd opened this issue · comments

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;

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;
}

hrm... I'm iffy here.

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