File Coverage

blib/lib/Starman/Server.pm
Criterion Covered Total %
statement 273 336 81.2
branch 100 160 62.5
condition 31 70 44.2
subroutine 39 45 86.6
pod 7 10 70.0
total 450 621 72.4


line stmt bran cond sub pod time code
1             package Starman::Server;
2 105     105   4403870 use strict;
  105         655  
  105         6649  
3 105     105   805 use base 'Net::Server::PreFork';
  105         327  
  105         83150  
4              
5 105     105   4071974 use Data::Dump qw(dump);
  105         569366  
  105         10547  
6 105     105   767 use Socket qw(IPPROTO_TCP TCP_NODELAY);
  105         202  
  105         11773  
7 105     105   522 use IO::Socket qw(:crlf);
  105         201  
  105         2604  
8 105     105   79121 use HTTP::Parser::XS qw(parse_http_request);
  105         127966  
  105         8807  
9 105     105   4567 use HTTP::Status qw(status_message);
  105         35239  
  105         49837  
10 105     105   46186 use HTTP::Date qw(time2str);
  105         416618  
  105         8233  
11 105     105   640 use POSIX qw(EINTR EPIPE ECONNRESET);
  105         179  
  105         697  
12 105     105   11030 use Symbol;
  105         154  
  105         11274  
13              
14 105     105   10957 use Plack::Util;
  105         62180  
  105         3754  
15 105     105   40156 use Plack::TempBuffer;
  105         609750  
  105         4455  
16              
17 105   50 105   709 use constant DEBUG => $ENV{STARMAN_DEBUG} || 0;
  105         206  
  105         8211  
18 105     105   604 use constant CHUNKSIZE => 64 * 1024;
  105         526  
  105         8244  
19              
20             my $null_io = do { open my $io, "<", \""; $io };
21              
22 105     105   713 use Net::Server::SIG qw(register_sig);
  105         415  
  105         105169  
23              
24             # Override Net::Server's HUP handling - just restart all the workers and that's about it
25             sub sig_hup {
26 0     0 0 0 my $self = shift;
27 0         0 $self->hup_children;
28             }
29              
30             sub run {
31 101     101 1 2568402 my($self, $app, $options) = @_;
32              
33 101         1599 $self->{app} = $app;
34 101         921 $self->{options} = $options;
35              
36 101         409 my %extra = ();
37              
38 101 50       1384 if ($options->{net_server_args}) {
39 0         0 %extra = %{ $options->{net_server_args} };
  0         0  
40             }
41              
42 101 50       792 if ( $options->{pid} ) {
43 0         0 $extra{pid_file} = $options->{pid};
44             }
45 101 50       1178 if ( $options->{daemonize} ) {
46 0         0 $extra{setsid} = $extra{background} = 1;
47             }
48 101 50       725 if ( $options->{error_log} ) {
49 0         0 $extra{log_file} = $options->{error_log};
50             }
51 101         418 if ( DEBUG ) {
52             $extra{log_level} = 4;
53             }
54 101 100       916 if ( $options->{ssl_cert} ) {
55 14         518 $extra{SSL_cert_file} = $options->{ssl_cert};
56             }
57 101 100       1110 if ( $options->{ssl_key} ) {
58 14         476 $extra{SSL_key_file} = $options->{ssl_key};
59             }
60 101 50       617 if (! exists $options->{keepalive}) {
61 101         451 $options->{keepalive} = 1;
62             }
63 101 50       803 if (! exists $options->{keepalive_timeout}) {
64 101         735 $options->{keepalive_timeout} = 1;
65             }
66 101 50       851 if (! exists $options->{read_timeout}) {
67 101         840 $options->{read_timeout} = 5;
68             }
69 101 50       490 if (! exists $options->{proctitle}) {
70 101         495 $options->{proctitle} = 1;
71             }
72              
73 101         326 my @port;
74 101 50       223 for my $listen (@{$options->{listen} || [ "$options->{host}:$options->{port}" ]}) {
  101         2514  
75 101         241 my %listen;
76 101 50       1400 if ($listen =~ /:/) {
77 101         669 my($h, $p, $opt) = split /:/, $listen, 3;
78 101 50       484 $listen{host} = $h if $h;
79 101         312 $listen{port} = $p;
80 101 50 33     821 $listen{proto} = 'ssl' if defined $opt && lc $opt eq 'ssl';
81             } else {
82 0         0 %listen = (
83             host => 'localhost',
84             port => $listen,
85             proto => 'unix',
86             );
87             }
88 101         815 push @port, \%listen;
89             }
90              
91 101   50     1382 my $workers = $options->{workers} || 5;
92 101         720 local @ARGV = ();
93              
94             $self->SUPER::run(
95             port => \@port,
96             host => '*', # default host
97             proto => $options->{ssl} ? 'ssl' : 'tcp', # default proto
98             serialize => ( $^O =~ m!(linux|darwin|bsd|cygwin)$! ) ? 'none' : 'flock',
99             min_servers => $options->{min_servers} || $workers,
100             min_spare_servers => $options->{min_spare_servers} || $workers - 1,
101             max_spare_servers => $options->{max_spare_servers} || $workers - 1,
102             max_servers => $options->{max_servers} || $workers,
103             max_requests => $options->{max_requests} || 1000,
104             user => $options->{user} || $>,
105             group => $options->{group} || $),
106 101 100 33     10037 listen => $options->{backlog} || 1024,
    50 33        
      33        
      33        
      50        
      33        
      33        
      50        
107             check_for_waiting => 1,
108             no_client_stdout => 1,
109             %extra
110             );
111             }
112              
113             sub pre_loop_hook {
114 101     101 1 2529528 my $self = shift;
115              
116 101         349 my $port = $self->{server}->{port}->[0];
117             my $proto = $port->{proto} eq 'ssl' ? 'https' :
118 101 50       648 $port->{proto} eq 'unix' ? 'unix' :
    100          
119             'http';
120              
121             $self->{options}{server_ready}->({
122             host => $port->{host},
123             port => $port->{port},
124             proto => $proto,
125             server_software => 'Starman',
126 101 50       366 }) if $self->{options}{server_ready};
127              
128             register_sig(
129 0     0   0 TTIN => sub { $self->{server}->{$_}++ for qw( min_servers max_servers ) },
130 0     0   0 TTOU => sub { $self->{server}->{$_}-- for qw( min_servers max_servers ) },
131 0     0   0 QUIT => sub { $self->server_close(1) },
132 101         1762 );
133             }
134              
135             sub server_close {
136 14     14 1 4223386 my($self, $quit) = @_;
137              
138 14 50       482 if ($quit) {
139 0         0 $self->log(2, $self->log_time . " Received QUIT. Running a graceful shutdown\n");
140 0         0 $self->{server}->{$_} = 0 for qw( min_servers max_servers );
141 0         0 $self->hup_children;
142 0         0 while (1) {
143 0         0 Net::Server::SIG::check_sigs();
144 0         0 $self->coordinate_children;
145 0 0       0 last if !keys %{$self->{server}{children}};
  0         0  
146 0         0 sleep 1;
147             }
148 0         0 $self->log(2, $self->log_time . " Worker processes cleaned up\n");
149             }
150              
151 14         1021 $self->SUPER::server_close();
152             }
153              
154             sub run_parent {
155 31     31 0 313263 my $self = shift;
156 31 50       3590 $0 = "starman master " . join(" ", @{$self->{options}{argv} || []})
157 31 50       1234 if $self->{options}{proctitle};
158 105     105   743 no warnings 'redefine';
  105         534  
  105         169775  
159             local *Net::Server::PreFork::register_sig = sub {
160 31     31   8066 my %args = @_;
161 31         277 delete $args{QUIT};
162 31         1483 Net::Server::SIG::register_sig(%args);
163 31         3852 };
164 31         1795 $self->SUPER::run_parent(@_);
165             }
166              
167             # The below methods run in the child process
168              
169             sub child_init_hook {
170 87     87 1 13788945 my $self = shift;
171 87         6760 srand();
172 87 50       3463 if ($self->{options}->{psgi_app_builder}) {
173 0         0 DEBUG && warn "[$$] Initializing the PSGI app\n";
174 0         0 $self->{app} = $self->{options}->{psgi_app_builder}->();
175             }
176 87 50       8494 $0 = "starman worker " . join(" ", @{$self->{options}{argv} || []})
177 87 50       2368 if $self->{options}{proctitle};
178              
179             }
180              
181             sub post_accept_hook {
182 65     65 1 1133437 my $self = shift;
183              
184             $self->{client} = {
185 65         2365 headerbuf => '',
186             inputbuf => '',
187             keepalive => 1,
188             };
189             }
190              
191             sub dispatch_request {
192 89     89 0 193 my ($self, $env) = @_;
193              
194             # Run PSGI apps
195 89         1730 my $res = Plack::Util::run_app($self->{app}, $env);
196              
197 89 100       15339 if (ref $res eq 'CODE') {
198 5     5   185 $res->(sub { $self->_finalize_response($env, $_[0]) });
  5         206  
199             } else {
200 84         559 $self->_finalize_response($env, $res);
201             }
202             }
203              
204             sub process_request {
205 65     65 1 5379 my $self = shift;
206 65         530 my $conn = $self->{server}->{client};
207              
208 65 100       808 if ($conn->NS_proto eq 'TCP') {
209 63 50       1699 setsockopt($conn, IPPROTO_TCP, TCP_NODELAY, 1)
210             or die $!;
211             }
212              
213 65         849 while ( $self->{client}->{keepalive} ) {
214 136 50       242652 last if !$conn->connected;
215              
216             # Read until we see all headers
217 136 100       4976 last if !$self->_read_headers;
218              
219             my $env = {
220             REMOTE_ADDR => $self->{server}->{peeraddr},
221             REMOTE_HOST => $self->{server}->{peerhost} || $self->{server}->{peeraddr},
222             REMOTE_PORT => $self->{server}->{peerport} || 0,
223             SERVER_NAME => $self->{server}->{sockaddr} || 0, # XXX: needs to be resolved?
224             SERVER_PORT => $self->{server}->{sockport} || 0,
225             SCRIPT_NAME => '',
226             'psgi.version' => [ 1, 1 ],
227             'psgi.errors' => *STDERR,
228             'psgi.url_scheme' => ($conn->NS_proto eq 'SSL' ? 'https' : 'http'),
229             'psgi.nonblocking' => Plack::Util::FALSE,
230             'psgi.streaming' => Plack::Util::TRUE,
231             'psgi.run_once' => Plack::Util::FALSE,
232             'psgi.multithread' => Plack::Util::FALSE,
233             'psgi.multiprocess' => Plack::Util::TRUE,
234             'psgix.io' => $conn,
235             'psgix.input.buffered' => Plack::Util::TRUE,
236             'psgix.harakiri' => Plack::Util::TRUE,
237 1     1   88 'psgix.informational' => sub { _write_informational($conn, @_) },
238 89 100 33     2072 };
      50        
      50        
      50        
239              
240             # Parse headers
241 89         8239 my $reqlen = parse_http_request(delete $self->{client}->{headerbuf}, $env);
242 89 50       350 if ( $reqlen == -1 ) {
243             # Bad request
244 0         0 DEBUG && warn "[$$] Bad request\n";
245 0         0 $self->_http_error(400, { SERVER_PROTOCOL => "HTTP/1.0" });
246 0         0 last;
247             }
248              
249             # Initialize PSGI environment
250             # Determine whether we will keep the connection open after the request
251 89         173 my $connection = delete $env->{HTTP_CONNECTION};
252 89         241 my $proto = $env->{SERVER_PROTOCOL};
253 89 50 33     1403 if ( $proto && $proto eq 'HTTP/1.0' ) {
    50 33        
254 0 0 0     0 if ( $connection && $connection =~ /^keep-alive$/i ) {
255             # Keep-alive only with explicit header in HTTP/1.0
256 0         0 $self->{client}->{keepalive} = 1;
257             }
258             else {
259 0         0 $self->{client}->{keepalive} = 0;
260             }
261             }
262             elsif ( $proto && $proto eq 'HTTP/1.1' ) {
263 89 50 66     541 if ( $connection && $connection =~ /^close$/i ) {
264 0         0 $self->{client}->{keepalive} = 0;
265             }
266             else {
267             # Keep-alive assumed in HTTP/1.1
268 89         274 $self->{client}->{keepalive} = 1;
269             }
270              
271             # Do we need to send 100 Continue?
272 89 100       305 if ( $env->{HTTP_EXPECT} ) {
273 1 50       4 if ( lc $env->{HTTP_EXPECT} eq '100-continue' ) {
274 1         12 _syswrite($conn, \('HTTP/1.1 100 Continue' . $CRLF . $CRLF));
275 1         3 DEBUG && warn "[$$] Sent 100 Continue response\n";
276             }
277             else {
278 0         0 DEBUG && warn "[$$] Invalid Expect header, returning 417\n";
279 0         0 $self->_http_error( 417, $env );
280 0         0 last;
281             }
282             }
283              
284 89 50       249 unless ($env->{HTTP_HOST}) {
285             # No host, bad request
286 0         0 DEBUG && warn "[$$] Bad request, HTTP/1.1 without Host header\n";
287 0         0 $self->_http_error( 400, $env );
288 0         0 last;
289             }
290             }
291              
292 89 50       250 unless ($self->{options}->{keepalive}) {
293 0         0 DEBUG && warn "[$$] keep-alive is disabled. Closing the connection after this request\n";
294 0         0 $self->{client}->{keepalive} = 0;
295             }
296              
297 89         1134 $self->_prepare_env($env);
298              
299 89         994 $self->dispatch_request($env);
300              
301 89         207 DEBUG && warn "[$$] Request done\n";
302              
303 89 100       533 if ( $self->{client}->{keepalive} ) {
304             # If we still have data in the input buffer it may be a pipelined request
305 71 50       205 if ( $self->{client}->{inputbuf} ne '' ) {
306 0 0       0 if ( $self->{client}->{inputbuf} =~ /^(?:GET|HEAD)/ ) {
307 0         0 if ( DEBUG ) {
308             warn "Pipelined GET/HEAD request in input buffer: "
309             . dump( $self->{client}->{inputbuf} ) . "\n";
310             }
311              
312             # Continue processing the input buffer
313 0         0 next;
314             }
315             else {
316             # Input buffer just has junk, clear it
317 0         0 if ( DEBUG ) {
318             warn "Clearing junk from input buffer: "
319             . dump( $self->{client}->{inputbuf} ) . "\n";
320             }
321              
322 0         0 $self->{client}->{inputbuf} = '';
323             }
324             }
325              
326 71         106 DEBUG && warn "[$$] Waiting on previous connection for keep-alive request...\n";
327              
328 71         957 my $sel = IO::Select->new($conn);
329 71 50       5516 last unless $sel->can_read($self->{options}->{keepalive_timeout});
330             }
331             }
332              
333 65         239 DEBUG && warn "[$$] Closing connection\n";
334             }
335              
336             sub _read_headers {
337 136     136   322 my $self = shift;
338              
339 136         365 eval {
340 136     0   3802 local $SIG{ALRM} = sub { die "Timed out\n"; };
  0         0  
341              
342 136         1023 alarm( $self->{options}->{read_timeout} );
343              
344 136         534 while (1) {
345             # Do we have a full header in the buffer?
346             # This is before sysread so we don't read if we have a pipelined request
347             # waiting in the buffer
348 227 100 100     7217 last if $self->{client}->{inputbuf} ne '' && $self->{client}->{inputbuf} =~ /$CR?$LF$CR?$LF/s;
349              
350             # If not, read some data
351 138         2216 my $read = _sysread($self->{server}->{client}, my $buf, CHUNKSIZE);
352              
353 138 100 66     1981 if ( !defined $read || $read == 0 ) {
354 47         903 die "Read error: $!\n";
355             }
356              
357 91         270 if ( DEBUG ) {
358             warn "[$$] Read $read bytes: " . dump($buf) . "\n";
359             }
360              
361 91         361 $self->{client}->{inputbuf} .= $buf;
362             }
363             };
364              
365 136         716 alarm(0);
366              
367 136 100       999 if ( $@ ) {
368 47 50       642 if ( $@ =~ /Timed out/ ) {
369 0         0 DEBUG && warn "[$$] Client connection timed out\n";
370 0         0 return;
371             }
372              
373 47 50       317 if ( $@ =~ /Read error/ ) {
374 47         76 DEBUG && warn "[$$] Read error: $!\n";
375 47         162 return;
376             }
377             }
378              
379             # Pull out the complete header into a new buffer
380 89         351 $self->{client}->{headerbuf} = $self->{client}->{inputbuf};
381              
382             # Save any left-over data, possibly body data or pipelined requests
383 89         3035 $self->{client}->{inputbuf} =~ s/.*?$CR?$LF$CR?$LF//s;
384              
385 89         482 return 1;
386             }
387              
388             sub _http_error {
389 0     0   0 my ( $self, $code, $env ) = @_;
390              
391 0   0     0 my $status = $code || 500;
392 0         0 my $msg = status_message($status);
393              
394 0         0 my $res = [
395             $status,
396             [ 'Content-Type' => 'text/plain', 'Content-Length' => length($msg) ],
397             [ $msg ],
398             ];
399              
400 0         0 $self->{client}->{keepalive} = 0;
401 0         0 $self->_finalize_response($env, $res);
402             }
403              
404             sub _prepare_env {
405 90     90   163360 my($self, $env) = @_;
406              
407             my $get_chunk = sub {
408 18 100   18   72 if ($self->{client}->{inputbuf} ne '') {
409 3         16 my $chunk = delete $self->{client}->{inputbuf};
410 3         16 return ($chunk, length $chunk);
411             }
412 15         56 my $read = _sysread($self->{server}->{client}, my($chunk), CHUNKSIZE);
413 15         150 return ($chunk, $read);
414 90         1092 };
415              
416 105     105   742 my $chunked = do { no warnings; lc delete $env->{HTTP_TRANSFER_ENCODING} eq 'chunked' };
  105         152  
  105         155959  
  90         238  
  90         382  
417              
418 90 100       420 if ($chunked) {
    100          
419 4         188 my $buf = Plack::TempBuffer->new;
420 4         884 my $chunk_buffer = '';
421 4         16 my $length;
422              
423             DECHUNK:
424 4         7 while (1) {
425 13         34 my($chunk, $read) = $get_chunk->();
426 13         54 $chunk_buffer .= $chunk;
427              
428 13         186 while ( $chunk_buffer =~ s/^(([0-9a-fA-F]+).*\015\012)// ) {
429 28         125 my $trailer = $1;
430 28         79 my $chunk_len = hex $2;
431              
432 28 100       85 if ($chunk_len == 0) {
    100          
433 4         17 last DECHUNK;
434             } elsif (length $chunk_buffer < $chunk_len + 2) {
435 1         2 $chunk_buffer = $trailer . $chunk_buffer;
436 1         2 last;
437             }
438              
439 23         179 $buf->print(substr $chunk_buffer, 0, $chunk_len, '');
440 23         546 $chunk_buffer =~ s/^\015\012//;
441              
442 23         88 $length += $chunk_len;
443             }
444              
445 9 50 33     97 last unless $read && $read > 0;
446             }
447              
448 4         23 $env->{CONTENT_LENGTH} = $length;
449 4         31 $env->{'psgi.input'} = $buf->rewind;
450             } elsif (my $cl = $env->{CONTENT_LENGTH}) {
451 4         152 my $buf = Plack::TempBuffer->new($cl);
452 4         523 while ($cl > 0) {
453 5         209 my($chunk, $read) = $get_chunk->();
454              
455 5 50 33     62 if ( !defined $read || $read == 0 ) {
456 0         0 die "Read error: $!\n";
457             }
458              
459 5         9 $cl -= $read;
460 5         31 $buf->print($chunk);
461             }
462 4         206 $env->{'psgi.input'} = $buf->rewind;
463             } else {
464 82         784 $env->{'psgi.input'} = $null_io;
465             }
466             }
467              
468             sub _finalize_response {
469 89     89   521 my($self, $env, $res) = @_;
470              
471 89 100       299 if ($env->{'psgix.harakiri.commit'}) {
472 18         51 $self->{client}->{keepalive} = 0;
473 18         220 $self->{client}->{harakiri} = 1;
474             }
475              
476 89         189 my $protocol = $env->{SERVER_PROTOCOL};
477 89         187 my $status = $res->[0];
478 89         1692 my $message = status_message($status);
479              
480 89         1007 my(@headers, %headers);
481 89         279 push @headers, "$protocol $status $message";
482              
483             # Switch on Transfer-Encoding: chunked if we don't know Content-Length.
484 89         181 my $chunked;
485 89         455 my $headers = $res->[1];
486 89         311 for (my $i = 0; $i < @$headers; $i += 2) {
487 101         339 my $k = $headers->[$i];
488 101         289 my $v = $headers->[$i + 1];
489 101 50       297 next if $k eq 'Connection';
490 101         228 push @headers, "$k: $v";
491 101         878 $headers{lc $k} = $v;
492             }
493              
494 89 50       282 if ( $protocol eq 'HTTP/1.1' ) {
495 89 100       263 if ( !exists $headers{'content-length'} ) {
    50          
496 84 100 100     1300 if ( $status !~ /^1\d\d|[23]04$/ && $env->{REQUEST_METHOD} ne 'HEAD' ) {
497 82         134 DEBUG && warn "[$$] Using chunked transfer-encoding to send unknown length body\n";
498 82         257 push @headers, 'Transfer-Encoding: chunked';
499 82         114 $chunked = 1;
500             }
501             }
502             elsif ( my $te = $headers{'transfer-encoding'} ) {
503 0 0       0 if ( $te eq 'chunked' ) {
504 0         0 DEBUG && warn "[$$] Chunked transfer-encoding set for response\n";
505 0         0 $chunked = 1;
506             }
507             }
508             } else {
509 0 0       0 if ( !exists $headers{'content-length'} ) {
510 0         0 DEBUG && warn "[$$] Disabling keep-alive after sending unknown length body on $protocol\n";
511 0         0 $self->{client}->{keepalive} = 0;
512             }
513             }
514              
515 89 50       326 if ( ! $headers{date} ) {
516 89         1568 push @headers, "Date: " . time2str( time() );
517             }
518              
519             # Should we keep the connection open?
520 89 100       1939 if ( $self->{client}->{keepalive} ) {
521 71         187 push @headers, 'Connection: keep-alive';
522             } else {
523 18         252 push @headers, 'Connection: close';
524             }
525              
526 89         191 my $conn = $self->{server}->{client};
527              
528             # Buffer the headers so they are sent with the first write() call
529             # This reduces the number of TCP packets we are sending
530 89         712 _syswrite($conn, \(join( $CRLF, @headers, '' ) . $CRLF));
531              
532 89 100       316 if (defined $res->[2]) {
533             Plack::Util::foreach($res->[2], sub {
534 86     86   1878 my $buffer = $_[0];
535 86 100       251 if ($chunked) {
536 81         144 my $len = length $buffer;
537 81 50       173 return unless $len;
538 81         617 $buffer = sprintf( "%x", $len ) . $CRLF . $buffer . $CRLF;
539             }
540 86         230 _syswrite($conn, \$buffer);
541 85         1309 });
542 85 100       4940 _syswrite($conn, \"0$CRLF$CRLF") if $chunked;
543             } else {
544             return Plack::Util::inline_object
545             write => sub {
546 6     6   535 my $buffer = $_[0];
547 6 50       17 if ($chunked) {
548 6         12 my $len = length $buffer;
549 6 100       21 return unless $len;
550 5         26 $buffer = sprintf( "%x", $len ) . $CRLF . $buffer . $CRLF;
551             }
552 5         14 _syswrite($conn, \$buffer);
553             },
554             close => sub {
555 4 50   4   260 _syswrite($conn, \"0$CRLF$CRLF") if $chunked;
556 4         225 };
557             }
558             }
559              
560             sub _syswrite {
561 264     264   575 my ($conn, $buffer_ref) = @_;
562              
563 264         443 my $amount = length $$buffer_ref;
564 264         321 my $offset = 0;
565              
566 264         590 while ($amount > 0) {
567 264         11986 my $len = syswrite($conn, $$buffer_ref, $amount, $offset);
568              
569 264 50       1671 if (not defined $len) {
570 0 0       0 return if $! == EPIPE;
571 0 0       0 return if $! == ECONNRESET;
572 0 0       0 redo if $! == EINTR;
573 0         0 die "write error: $!";
574             }
575              
576 264         432 $amount -= $len;
577 264         339 $offset += $len;
578              
579 264         1341 DEBUG && warn "[$$] Wrote $len byte", ($len == 1 ? '' : 's'), "\n";
580             }
581             }
582              
583             sub _sysread {
584 153     153   612 while (1) {
585 157         1131567 my $len = sysread $_[0], $_[1], $_[2];
586 157 100 66     101943 return $len if defined $len or $! != EINTR;
587             }
588             }
589              
590             sub _write_informational {
591 1     1   3 my ($conn, $code, $headers) = @_;
592 1         31 my $message = HTTP::Status::status_message($code);
593 1         22 my @lines = "HTTP/1.1 $code $message";
594 1         3 for (my $i = 0; $i < @$headers; $i += 2) {
595 1         2 my $k = $headers->[$i];
596 1         3 my $v = $headers->[$i + 1];
597 1         5 push @lines, "$k: $v" ;
598             }
599 1         7 _syswrite($conn, \join($CRLF, @lines, $CRLF));
600              
601 1         3 DEBUG && warn "[$$] Sent $code $message response\n";
602             }
603              
604             sub post_client_connection_hook {
605 65     65 1 11514 my $self = shift;
606 65 100       453 if ($self->{client}->{harakiri}) {
607 18         6170 exit;
608             }
609             }
610              
611             1;