UniEvent::HTTP::ServerResponse - HTTP server response class
$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();
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.
UniEvent::HTTP::ServerResponse
respond()
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.
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().
chunked
true
send_chunk()
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.
response()
$request->respond({ code => 200, chunked => 1, }); $request->response->send_chunk($data1); ... $request->response->send_chunk($dataN); $request->response->send_final_chunk();
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.
compress
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();
All methods of Protocol::HTTP::Response also apply.
Should only be called on responses where chunked is true. Sends an http chunk (applying possible compression, etc).
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.
$data
Returns true if response has been completed, i.e. user sent response and its body.
To install UniEvent::HTTP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm UniEvent::HTTP
CPAN shell
perl -MCPAN -e shell install UniEvent::HTTP
For more information on module installation, please visit the detailed CPAN module installation guide.