No description, website, or topics provided.
Perl 6
Switch branches/tags
Nothing to show
Latest commit 984981e Oct 10, 2017 @tony-o tony-o Update


HTTP server.

Currently handles:

  • Parsing Headers
  • Chunked Transfer
  • Hooks for middleware or route handling
  • Simple response handling

Doesn't handle

  • Route handling


use HTTP::Server::Threaded;

my $s =;

$s.handler(sub ($request, $response) {
  $response.headers<Content-Type> = 'text/plain';
  $response.status = 200;
  $response.write("Hello ");




:port - port to listen on :host - ip to listen on

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

Any Sub 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 False;``` when the server should discontinue processing the request or close the request to discontinue processing.

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

If the server runs out of handlers or a handler not explicitly closing the connection and also returning False will leave the connection open.

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

If the .handler returns a Promise then the server waits for the Promise to be kept with a True or False value before continuing

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

Same as .handler but is called directly following header's being parsed.

If the middleware sub returns a False value, then the server will stop processing further data and leave the connection open. This allows a connections to be hijacked by middleware for streaming or whatever you want to do with it.

If the middleware returns a Promise then the server waits for the Promise to be kept with a True or False value before continuing


Starts the server and does 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 sucket, 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