Net::Async::HTTP::Server - serve HTTP with IO::Async
Net::Async::HTTP::Server
IO::Async
use Net::Async::HTTP::Server; use IO::Async::Loop; use HTTP::Response; my $loop = IO::Async::Loop->new(); my $httpserver = Net::Async::HTTP::Server->new( on_request => sub { my $self = shift; my ( $req ) = @_; my $response = HTTP::Response->new( 200 ); $response->add_content( "Hello, world!\n" ); $response->content_type( "text/plain" ); $response->content_length( length $response->content ); $req->respond( $response ); }, ); $loop->add( $httpserver ); $httpserver->listen( addr => { family => "inet6", socktype => "stream", port => 8080 }, )->get $loop->run;
This module allows a program to respond asynchronously to HTTP requests, as part of a program based on IO::Async. An object in this class listens on a single port and invokes the on_request callback or subclass method whenever an HTTP request is received, allowing the program to respond to it.
on_request
For accepting HTTP connections via PSGI and Plack, see also Plack::Handler::Net::Async::HTTP::Server.
Since version 0.11.
This module reports basic metrics about received requests and sent responses via Metrics::Any.
Invoked when a new HTTP request is received. It will be passed a Net::Async::HTTP::Server::Request object.
The following named parameters may be passed to new or configure:
new
configure
Gives the name of the class that make_request will construct. This is provided as an alternative to overriding the make_request method, for the case where no other methods need overriding or other behaviour changed.
make_request
As a small subclass of IO::Async::Listener, this class does not provide many new methods of its own. The superclass provides useful methods to control the basic operation of this server.
Specifically, see the "listen" in IO::Async::Listener method on how to actually bind the server to a listening socket to make it accept requests.
$request = $server->make_request( @args )
Invoked by the protocol stream handler to create a new request object representing an incoming request. This is provided as a method for subclasses to overload, if they wish to represent requests with subclasses of the basic request representation.
Don't use HTTP::Message objects as underlying implementation
Consider how to do streaming request inbound
Lots more testing
Paul Evans <leonerd@leonerd.org.uk>
To install Net::Async::HTTP::Server, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::Async::HTTP::Server
CPAN shell
perl -MCPAN -e shell install Net::Async::HTTP::Server
For more information on module installation, please visit the detailed CPAN module installation guide.