Net::A...Server(3pm)

πŸ“¦
version 0.15 Β· 3 versions
, in fedora 43

πŸ’– 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::Message objects 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