NAME
UniEvent::HTTP::ServerResponse - HTTP server response class
SYNOPSIS
$request->respond({
code => 200,
body => "hello",
});
my $response = UE::HTTP::ServerResponse->new({
code => 200,
body = $long_body,
headers => {Foo => "bar"},
compress => Protocol::HTTP::Compression::gzip,
});
$request->respond($response);
my $response = UE::HTTP::ServerResponse->new({
code => 200,
chunked => 1,
});
$request->respond($response);
...
$response->send_chunk($data1);
...
$response->send_chunk($dataN);
$response->send_final_chunk();
DESCRIPTION
UniEvent::HTTP::ServerResponse
represents response for client's request on server. It holds all the properties and state of http response. Objects of this class are created by user and then passed to respond()
method of UniEvent::HTTP::Request.
ServerResponse has API to construct response and control its transmission.
UniEvent::HTTP::ServerResponse
inherits from Protocol::HTTP::Response. So for complete documenation you need to also read Protocol::HTTP::Response's docs.
CHUNKED RESPONSE
To respond with chunks, set chunked
property of the response to a true
value, and do not define body. In this case the response given to respond()
method of the request will be incomplete and you will need to call send_chunk()
as many times and needed and then finish transmission via send_final_chunk()
.
For conveniece, to help with sending chunked responses, request object has special accessor response()
which returns response object which has been previously passed to respond()
method of request. This is useful if you don't want to explicitly create response object and remember the request-response pair.
$request->respond({
code => 200,
chunked => 1,
});
$request->response->send_chunk($data1);
...
$request->response->send_chunk($dataN);
$request->response->send_final_chunk();
COMPRESSION
To respond with compression, set compress
property of response object, see Protocol::HTTP::Response for documentation as comression logic is transparently implemented in Protocol::HTTP
.
Example:
$request->respond({
code => 200,
body => $long_body,
compress => Protocol::HTTP::Compression::gzip,
});
Chunked example:
$request->respond({
code => 200,
chunked => 1,
compress => Protocol::HTTP::Compression::gzip,
});
$request->response->send_chunk($data1);
...
$request->response->send_chunk($dataN);
$request->response->send_final_chunk();
METHODS
All methods of Protocol::HTTP::Response also apply.
send_chunk($data)
Should only be called on responses where chunked
is true. Sends an http chunk (applying possible compression, etc).
send_final_chunk([$data])
Should only be called on responses where chunked
is true. Sends final (empty) http chunk and finishes sending chunked http response. If optional $data
is provided, then sends $data
as normal chunk before final chunk.
completed()
Returns true if response has been completed, i.e. user sent response and its body.