| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||
| 3 | # | ||||||||||||
| 4 | # (C) Paul Evans, 2013-2024 -- leonerd@leonerd.org.uk | ||||||||||||
| 5 | |||||||||||||
| 6 | package Net::Async::HTTP::Server 0.15; | ||||||||||||
| 7 | |||||||||||||
| 8 | 13 | ▶ | use v5.14; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 13 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 13 | ▶ | use base qw( IO::Async::Listener ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 13 | IO::Async::Listener->VERSION( '0.61' ); | |||||||||||
| 12 | |||||||||||||
| 13 | 13 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 13 | ▶ | use Net::Async::HTTP::Server::Protocol; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 13 | ▶ | use Net::Async::HTTP::Server::Request; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 13 | ▶ | use Metrics::Any 0.05 '$metrics', | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | strict => 1, | ||||||||||||
| 20 | 13 | name_prefix => [qw( http server )]; | |||||||||||
| 21 | |||||||||||||
| 22 | 13 | $metrics->make_gauge( requests_in_flight => | |||||||||||
| 23 | description => "Count of the number of requests received that have not yet been completed", | ||||||||||||
| 24 | # no labels | ||||||||||||
| 25 | ); | ||||||||||||
| 26 | 13 | $metrics->make_counter( requests => | |||||||||||
| 27 | description => "Number of HTTP requests received", | ||||||||||||
| 28 | labels => [qw( method )], | ||||||||||||
| 29 | ); | ||||||||||||
| 30 | 13 | $metrics->make_counter( responses => | |||||||||||
| 31 | description => "Number of HTTP responses served", | ||||||||||||
| 32 | labels => [qw( method code )], | ||||||||||||
| 33 | ); | ||||||||||||
| 34 | 13 | $metrics->make_timer( request_duration => | |||||||||||
| 35 | description => "Duration of time spent processing requests", | ||||||||||||
| 36 | # no labels | ||||||||||||
| 37 | ); | ||||||||||||
| 38 | 13 | $metrics->make_distribution( response_bytes => | |||||||||||
| 39 | description => "The size in bytes of responses sent", | ||||||||||||
| 40 | units => "bytes", | ||||||||||||
| 41 | # no labels | ||||||||||||
| 42 | ); | ||||||||||||
| 43 | |||||||||||||
| 44 | =head1 NAME | ||||||||||||
| 45 | |||||||||||||
| 46 | C<Net::Async::HTTP::Server> - serve HTTP with C<IO::Async> | ||||||||||||
| 47 | |||||||||||||
| 48 | =head1 SYNOPSIS | ||||||||||||
| 49 | |||||||||||||
| 50 | use Net::Async::HTTP::Server; | ||||||||||||
| 51 | use IO::Async::Loop; | ||||||||||||
| 52 | |||||||||||||
| 53 | use HTTP::Response; | ||||||||||||
| 54 | |||||||||||||
| 55 | my $loop = IO::Async::Loop->new(); | ||||||||||||
| 56 | |||||||||||||
| 57 | my $httpserver = Net::Async::HTTP::Server->new( | ||||||||||||
| 58 | on_request => sub { | ||||||||||||
| 59 | my $self = shift; | ||||||||||||
| 60 | my ( $req ) = @_; | ||||||||||||
| 61 | |||||||||||||
| 62 | my $response = HTTP::Response->new( 200 ); | ||||||||||||
| 63 | $response->add_content( "Hello, world!\n" ); | ||||||||||||
| 64 | $response->content_type( "text/plain" ); | ||||||||||||
| 65 | $response->content_length( length $response->content ); | ||||||||||||
| 66 | |||||||||||||
| 67 | $req->respond( $response ); | ||||||||||||
| 68 | }, | ||||||||||||
| 69 | ); | ||||||||||||
| 70 | |||||||||||||
| 71 | $loop->add( $httpserver ); | ||||||||||||
| 72 | |||||||||||||
| 73 | $httpserver->listen( | ||||||||||||
| 74 | addr => { family => "inet6", socktype => "stream", port => 8080 }, | ||||||||||||
| 75 | )->get | ||||||||||||
| 76 | |||||||||||||
| 77 | $loop->run; | ||||||||||||
| 78 | |||||||||||||
| 79 | =head1 DESCRIPTION | ||||||||||||
| 80 | |||||||||||||
| 81 | This module allows a program to respond asynchronously to HTTP requests, as | ||||||||||||
| 82 | part of a program based on L<IO::Async>. An object in this class listens on a | ||||||||||||
| 83 | single port and invokes the C<on_request> callback or subclass method whenever | ||||||||||||
| 84 | an HTTP request is received, allowing the program to respond to it. | ||||||||||||
| 85 | |||||||||||||
| 86 | For accepting HTTP connections via L<PSGI> and L<Plack>, see also | ||||||||||||
| 87 | L<Plack::Handler::Net::Async::HTTP::Server>. | ||||||||||||
| 88 | |||||||||||||
| 89 | =head2 Metrics | ||||||||||||
| 90 | |||||||||||||
| 91 | I<Since version 0.11.> | ||||||||||||
| 92 | |||||||||||||
| 93 | This module reports basic metrics about received requests and sent responses | ||||||||||||
| 94 | via L<Metrics::Any>. | ||||||||||||
| 95 | |||||||||||||
| 96 | =cut | ||||||||||||
| 97 | |||||||||||||
| 98 | =head1 EVENTS | ||||||||||||
| 99 | |||||||||||||
| 100 | =head2 on_request $req | ||||||||||||
| 101 | |||||||||||||
| 102 | Invoked when a new HTTP request is received. It will be passed a | ||||||||||||
| 103 | L<Net::Async::HTTP::Server::Request> object. | ||||||||||||
| 104 | |||||||||||||
| 105 | =cut | ||||||||||||
| 106 | |||||||||||||
| 107 | =head1 PARAMETERS | ||||||||||||
| 108 | |||||||||||||
| 109 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||||
| 110 | |||||||||||||
| 111 | =head2 request_class => STRING | ||||||||||||
| 112 | |||||||||||||
| 113 | Gives the name of the class that C<make_request> will construct. This is | ||||||||||||
| 114 | provided as an alternative to overriding the C<make_request> method, for the | ||||||||||||
| 115 | case where no other methods need overriding or other behaviour changed. | ||||||||||||
| 116 | |||||||||||||
| 117 | =cut | ||||||||||||
| 118 | |||||||||||||
| 119 | =head1 METHODS | ||||||||||||
| 120 | |||||||||||||
| 121 | As a small subclass of L<IO::Async::Listener>, this class does not provide many | ||||||||||||
| 122 | new methods of its own. The superclass provides useful methods to control the | ||||||||||||
| 123 | basic operation of this server. | ||||||||||||
| 124 | |||||||||||||
| 125 | Specifically, see the L<IO::Async::Listener/listen> method on how to actually | ||||||||||||
| 126 | bind the server to a listening socket to make it accept requests. | ||||||||||||
| 127 | |||||||||||||
| 128 | =cut | ||||||||||||
| 129 | |||||||||||||
| 130 | sub _init | ||||||||||||
| 131 | 10 | ▶ | { | ||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.0
| |||||||||||||
| 132 | 10 | my $self = shift; | |||||||||||
| 133 | 10 | my ( $params ) = @_; | |||||||||||
| 134 | |||||||||||||
| 135 | 10 | $params->{handle_class} = "Net::Async::HTTP::Server::Protocol"; | |||||||||||
| 136 | |||||||||||||
| 137 | 10 | ▶ | $params->{request_class} ||= "Net::Async::HTTP::Server::Request"; | ||||||||||
|
Condition50%1/2
$params->{"request_class"} ||= "Net::Async::HTTP::Server::Request"
MC/DC0%0/1
$params->{"request_class"} ||= "Net::Async::HTTP::Server::Request"
$params->{"request_class"}
Truth table50%1/2
A
A= $params->{"request_class"}
| |||||||||||||
| 138 | |||||||||||||
| 139 | 10 | $self->SUPER::_init( $params ); | |||||||||||
| 140 | } | ||||||||||||
| 141 | |||||||||||||
| 142 | sub configure | ||||||||||||
| 143 | 22 | ▶ | { | ||||||||||
|
Subroutine
configure: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 144 | 22 | my $self = shift; | |||||||||||
| 145 | 22 | my %params = @_; | |||||||||||
| 146 | |||||||||||||
| 147 | 22 | foreach (qw( on_request request_class )) { | |||||||||||
| 148 | 44 | ▶ | $self->{$_} = delete $params{$_} if exists $params{$_}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 22 | $self->SUPER::configure( %params ); | |||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | sub _add_to_loop | ||||||||||||
| 155 | 10 | ▶ | { | ||||||||||
|
Subroutine
_add_to_loop: called CC=2 SCAR=7.1
| |||||||||||||
| 156 | 10 | my $self = shift; | |||||||||||
| 157 | |||||||||||||
| 158 | 10 | ▶ | $self->can_event( "on_request" ) or croak "Expected either a on_request callback or an ->on_request method"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 159 | |||||||||||||
| 160 | 10 | $self->SUPER::_add_to_loop( @_ ); | |||||||||||
| 161 | } | ||||||||||||
| 162 | |||||||||||||
| 163 | sub on_accept | ||||||||||||
| 164 | 18 | ▶ | { | ||||||||||
|
Subroutine
on_accept: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 165 | 18 | my $self = shift; | |||||||||||
| 166 | 18 | my ( $conn ) = @_; | |||||||||||
| 167 | |||||||||||||
| 168 | $conn->configure( | ||||||||||||
| 169 | 8 | ▶ | on_closed => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 170 | 8 | my $conn = shift; | |||||||||||
| 171 | 8 | $conn->on_closed(); | |||||||||||
| 172 | |||||||||||||
| 173 | 8 | $conn->remove_from_parent; | |||||||||||
| 174 | }, | ||||||||||||
| 175 | 18 | ); | |||||||||||
| 176 | |||||||||||||
| 177 | 18 | $self->add_child( $conn ); | |||||||||||
| 178 | |||||||||||||
| 179 | 18 | return $conn; | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | =head2 make_request | ||||||||||||
| 183 | |||||||||||||
| 184 | $request = $server->make_request( @args ); | ||||||||||||
| 185 | |||||||||||||
| 186 | Invoked by the protocol stream handler to create a new request object | ||||||||||||
| 187 | representing an incoming request. This is provided as a method for subclasses | ||||||||||||
| 188 | to overload, if they wish to represent requests with subclasses of the basic | ||||||||||||
| 189 | request representation. | ||||||||||||
| 190 | |||||||||||||
| 191 | =cut | ||||||||||||
| 192 | |||||||||||||
| 193 | sub make_request | ||||||||||||
| 194 | 25 | ▶ | { | ||||||||||
|
Subroutine
make_request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 195 | 25 | my $self = shift; | |||||||||||
| 196 | 25 | return $self->{request_class}->new( @_ ); | |||||||||||
| 197 | } | ||||||||||||
| 198 | |||||||||||||
| 199 | sub _received_request | ||||||||||||
| 200 | 25 | ▶ | { | ||||||||||
|
Subroutine
_received_request: called CC=2 SCAR=6.9
| |||||||||||||
| 201 | 25 | my $self = shift; | |||||||||||
| 202 | 25 | my ( $request ) = @_; | |||||||||||
| 203 | |||||||||||||
| 204 | 25 | ▶ | if( $metrics ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 205 | 10 | $metrics->inc_gauge( requests_in_flight => ); | |||||||||||
| 206 | |||||||||||||
| 207 | 10 | $metrics->inc_counter( requests => [ method => $request->method ] ); | |||||||||||
| 208 | 10 | $self->{request_received_timestamp}{$request} = $self->loop->time; | |||||||||||
| 209 | } | ||||||||||||
| 210 | |||||||||||||
| 211 | 25 | $self->invoke_event( on_request => $request ); | |||||||||||
| 212 | } | ||||||||||||
| 213 | |||||||||||||
| 214 | sub _done_request | ||||||||||||
| 215 | 19 | ▶ | { | ||||||||||
|
Subroutine
_done_request: called CC=2 SCAR=6.9
| |||||||||||||
| 216 | 19 | my $self = shift; | |||||||||||
| 217 | 19 | my ( $request ) = @_; | |||||||||||
| 218 | |||||||||||||
| 219 | 19 | ▶ | if( $metrics ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 220 | 1 | my $received_timestamp = delete $self->{request_received_timestamp}{$request}; | |||||||||||
| 221 | |||||||||||||
| 222 | 1 | $metrics->dec_gauge( requests_in_flight => ); | |||||||||||
| 223 | |||||||||||||
| 224 | 1 | $metrics->inc_counter( responses => [ method => $request->method, code => $request->response_status_code ] ); | |||||||||||
| 225 | 1 | $metrics->report_timer( request_duration => $self->loop->time - $received_timestamp ); | |||||||||||
| 226 | 1 | $metrics->report_distribution( response_bytes => $request->bytes_written ); | |||||||||||
| 227 | } | ||||||||||||
| 228 | } | ||||||||||||
| 229 | |||||||||||||
| 230 | =head1 TODO | ||||||||||||
| 231 | |||||||||||||
| 232 | =over 2 | ||||||||||||
| 233 | |||||||||||||
| 234 | =item * | ||||||||||||
| 235 | |||||||||||||
| 236 | Don't use L<HTTP::Message> objects as underlying implementation | ||||||||||||
| 237 | |||||||||||||
| 238 | =item * | ||||||||||||
| 239 | |||||||||||||
| 240 | Consider how to do streaming request inbound | ||||||||||||
| 241 | |||||||||||||
| 242 | =item * | ||||||||||||
| 243 | |||||||||||||
| 244 | Lots more testing | ||||||||||||
| 245 | |||||||||||||
| 246 | =back | ||||||||||||
| 247 | |||||||||||||
| 248 | =cut | ||||||||||||
| 249 | |||||||||||||
| 250 | =head1 AUTHOR | ||||||||||||
| 251 | |||||||||||||
| 252 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||
| 253 | |||||||||||||
| 254 | =cut | ||||||||||||
| 255 | |||||||||||||
| 256 | 13 | 0x55AA; | |||||||||||