Raku Land




Build Status

Asynchronous HTTP server.

Currently handles:

Doesn't handle


use HTTP::Server::Async;

my $s = HTTP::Server::Async.new;

$s.handler(sub ($request, $response) {
  $response.headers<Content-Type> = 'text/plain';
  $response.status = 200;
  $response.write("Hello ");
  $response.close("world!"); #keeps a promise in the response and ends the server handler processing




:port - port to listen on :host - ip to listen on :buffered - Boolean value for whether responses should be buffered or not

.handler ( Callable($request, $response) )

Any Callable passed to this method is called in the order it was registered on every incoming request. Any method/sub registered with the server should return True to continue processing. A False value will discontinue processing. Alternatively a promise can be returned, if the promise is broken then the server will discontinue, if the promise is kept then processing will continue.

Callable will receive two parameters from the server, a HTTP::Server::Async::Request and a HTTP::Server::Async::Response. More about the Response and Request object below.

Note that the server will wait for the request body to be complete before calling handler subs.

.middleware ( Callable($request, $response) )

The same as the handler except will NOT wait for a complete request body. The middleware can hijack the connection by explicitly returning False and continuing processing using $request.connection to gain control of the socket.

.listen ( Bool $block? = False )

Starts the server and does not block


This handles the parsing of the incoming request





Key/value pair containing the header values


Requested resource


HTTP/1.1 or HTTP/1.0 (or whatever was in the request)


String containing the data included with the request


Response object, handles writing and closing the socket


$.buffered (Bool) = True

Whether or not the response object should buffer the response and write on close, or write directly to the socket

$.status (Int)

Set the status of the response, uses HTTP status codes. See here for more info


Response headers to be sent, accessed directly. Modifying these after writing to the socket will have no effect on the response unless the $.buffered is set to True



Write data to the socket, will call the appropriate method for the socket (Str = $connection.write, anything else is $connection.send)


Close takes optional parameter of data to send out. Will call write if a parameter is provided. Closes the socket, writes headers if the response is buffered, etc

Closing Credits

thanks to ugexe, btyler, jnthn, and timotimo for helping figure out bugs, answer a bunch of questions, etc