Net-Async-HTTP-Server
view release on metacpan or search on metacpan
lib/Net/Async/HTTP/Server/Request.pm view on Meta::CPAN
return @headers;
}
=head2 body
$body = $request->body;
Return the body content from the request as a string of bytes.
=cut
sub body
{
my $self = shift;
return $self->{req}->content;
}
# Called by NaHTTP::Server::Protocol
sub _write_to_stream
{
my $self = shift;
my ( $stream ) = @_;
while( defined( my $next = shift @{ $self->{pending} } ) ) {
$stream->write( $next,
on_write => sub {
$self->{bytes_written} += $_[1];
},
$self->protocol eq "HTTP/1.0" ?
( on_flush => sub { $stream->close } ) :
(),
);
}
# An empty ->write to ensure we capture the written byte count correctly
$stream->write( "",
on_write => sub {
$self->{conn}->parent->_done_request( $self );
}
) if $self->{is_done};
return $self->{is_done};
}
=head2 write
$request->write( $data );
Append more data to the response to be written to the client. C<$data> can
either be a plain string, or a C<CODE> reference to be used in the underlying
L<IO::Async::Stream>'s C<write> method.
=cut
sub write
{
my $self = shift;
my ( $data ) = @_;
unless( defined $self->{response_status_line} ) {
( $self->{response_status_line} ) = split m/$CRLF/, $data;
}
return if $self->{is_closed};
$self->{is_done} and croak "This request has already been completed";
push @{ $self->{pending} }, $data;
$self->{conn}->_flush_requests;
}
=head2 write_chunk
$request->write_chunk( $data );
Append more data to the response in the form of an HTTP chunked-transfer
chunk. This convenience is a shortcut wrapper for prepending the chunk header.
=cut
sub write_chunk
{
my $self = shift;
my ( $data ) = @_;
return if $self->{is_closed};
return unless my $len = length $data; # Must not write zero-byte chunks
$self->write( sprintf "%X$CRLF%s$CRLF", $len, $data );
}
=head2 done
$request->done;
Marks this response as completed.
=cut
sub done
{
my $self = shift;
return if $self->{is_closed};
$self->{is_done} and croak "This request has already been completed";
$self->{is_done} = 1;
$self->{conn}->_flush_requests;
}
=head2 write_chunk_eof
$request->write_chunk_eof;
Sends the final EOF chunk and marks this response as completed.
=cut
sub write_chunk_eof
{
lib/Net/Async/HTTP/Server/Request.pm view on Meta::CPAN
$response->protocol( $self->protocol );
defined $response->header( "Transfer-Encoding" ) or
$response->header( "Transfer-Encoding" => "chunked" );
my $content = $response->content;
my $header = $response->as_string( $CRLF );
# Trim any content from the header as it would need to be chunked
$header =~ s/$CRLF$CRLF.*$/$CRLF$CRLF/s;
$self->write( $header );
$self->write_chunk( $response->content ) if length $response->content;
}
=head2 stream
$stream = $request->stream;
Returns the L<IO::Async::Stream> object representing this connection. Usually
this would be used for such things as inspecting the client's connection
address on the C<read_handle> of the stream. It should not be necessary to
directly perform IO operations on this stream itself.
=cut
sub stream
{
my $self = shift;
return $self->{conn};
}
=head2 response_status_line
$status = $request->response_status_line;
If a response header has been written by calling the C<write> method, returns
the first line of it.
=cut
sub response_status_line
{
my $self = shift;
return $self->{response_status_line};
}
=head2 response_status_code
$code = $request->response_status_code;
If a response header has been written by calling the C<write> method, returns
the status code from it.
=cut
sub response_status_code
{
my $self = shift;
my $line = $self->{response_status_line} or return undef;
return +( split m/ /, $line )[1];
}
# For metrics
sub bytes_written
{
my $self = shift;
return $self->{bytes_written};
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
( run in 2.009 seconds using v1.01-cache-2.11-cpan-71847e10f99 )