Net::A...Server(3pm)
π Net::Async::HTTP::Server β serve HTTP with IO::Async π
okay bestie, sit down, hydrate, because we're about to serve HTTP. this is a User Contributed Perl Documentation page for the module Net::Async::HTTP::Server, and yes it's a Perl module, and yes it's iconic. let's go π¦
β¨ SYNOPSIS β¨
look at this lil baby example. it's giving "hello world but make it 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;
no cap, that's the whole vibe. copy it, run it, feel powerful. π
πΈ DESCRIPTION πΈ
so this module lets your program respond to HTTP requests asynchronously, as part of a program based on IO::Async. literally the whole point.
an object in this class listens on a single port and invokes the on_request callback (or the subclass method, if you're fancy like that) whenever an HTTP request shows up, so your program can respond to it. main character energy fr fr. π
if you wanna accept HTTP connections via PSGI and Plack instead, bestie, see also Plack::Handler::Net::Async::HTTP::Server.
π Metrics π
since version 0.11. β¨
this module reports basic metrics about received requests and sent responses via Metrics::Any. numbers, but make them cute. π
π₯ EVENTS π₯
on_request $req
invoked when a new HTTP request is received. π«Ά it gets passed a Net::Async::HTTP::Server::Request object. that's your moment, bestie β go respond.
π PARAMETERS π
these named parameters can be passed to new or configure:
request_class => STRING
tells you the name of the class that make_request will construct. this exists as an alternative to overriding the make_request method itself β perfect for when you don't need to override any other methods or change any other behaviour. lowkey the lazy-girl route and we respect it. π
π METHODS π
okay so this is a small subclass of IO::Async::Listener, so it doesn't have a ton of its own new methods β the superclass handles the useful stuff for controlling the basic operation of this server. we stan inheritance.
specifically, check out the "listen" in IO::Async::Listener method to learn how to actually bind the server to a listening socket so it starts accepting requests. that's the part that makes it real.
make_request
$request = $server->make_request( @args );
invoked by the protocol stream handler to create a new request object representing an incoming request. π¦
it's provided as a method so subclasses can overload it, if they wanna represent requests with subclasses of the basic request representation. go off, subclass queen.
π TODO π
the honest-to-god to-do list, unchanged, because we keep the facts:
- don't use
HTTP::Messageobjects as underlying implementation - consider how to do streaming request inbound
- lots more testing
ngl that last one is so real. π
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk> β the icon, the legend. π
that's the whole page bestie!! now go forth and serve some HTTP. periodt. π«Άβ¨
this was Net::Async::HTTP::Server, a User Contributed Perl Documentation page, brought to you by perl v5.42.0 and dated 2025-07-25. mwah, ily, bye!! πππ¦
π¦ where this page comes from
- fedora 43: perl-Net-Async-HTTP-Server 0.15-2.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms