Skip to content

HTTP::Response constructor to set Content-Length [rt.cpan.org #106990] #61

Open
@oalders

Description

@oalders

Migrated from rt.cpan.org#106990 (status was 'new')

Requestors:

From [email protected] on 2015-09-09 15:33:38:

Often it's convenient to send an HTTP response from an inline-constructed H:R object:

   $req->respond( HTTP::Response->new( 200, "OK", [ "Content-Type" => "text/plain" ],
      "Hello, world",
   ) );

Problem is if I do this, the response object has no Content-Length, no Connection=close and no chunked transfer encoding. The HTTP server/client now have no way to communicate the end of the response and the client will never see the end.

It's not always convenient to store the content in a variable first,

   my $content = ...
   $req->respond( HTTP::Response->new( 200, "OK", [ "Content-Type" => "text/plain", "Content-Length" => length $content ], $content ) );

It might be nice to have a constructor to do this

  sub new_with_content_length
  {
     my $self = shift->new( @_ );
     $self->content_length( length $self->content ) if defined $self->content;
     $self;
  }

-- 

Paul Evans

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions