line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- perl -*- |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Net::Server::PSGI - Extensible Perl HTTP PSGI base server |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Copyright (C) 2011-2017 |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# Paul Seamons |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# This package may be distributed under the terms of either the |
10
|
|
|
|
|
|
|
# GNU General Public License |
11
|
|
|
|
|
|
|
# or the |
12
|
|
|
|
|
|
|
# Perl Artistic License |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
################################################################ |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
package Net::Server::PSGI; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
2
|
|
18640
|
use strict; |
|
2
|
|
|
|
|
16
|
|
|
2
|
|
|
|
|
104
|
|
19
|
2
|
|
|
2
|
|
14
|
use base qw(Net::Server::HTTP); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1690
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
0
|
4
|
sub net_server_type { __PACKAGE__ } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub options { |
24
|
2
|
|
|
2
|
0
|
6
|
my $self = shift; |
25
|
2
|
|
|
|
|
59
|
my $ref = $self->SUPER::options(@_); |
26
|
2
|
|
|
|
|
6
|
my $prop = $self->{'server'}; |
27
|
2
|
|
|
|
|
18
|
$ref->{$_} = \$prop->{$_} for qw(app); |
28
|
2
|
|
|
|
|
13
|
return $ref; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub post_configure { |
32
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
33
|
1
|
|
|
|
|
4
|
my $prop = $self->{'server'}; |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
66
|
$prop->{'log_handle'} = IO::Handle->new; |
36
|
1
|
|
|
|
|
146
|
$prop->{'log_handle'}->fdopen(fileno(STDERR), "w"); |
37
|
1
|
|
|
|
|
102
|
$prop->{'no_client_stdout'} = 1; |
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
|
|
20
|
$self->SUPER::post_configure(@_); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
1
|
|
|
sub _tie_client_stdout {} # the client should not print directly |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub process_request { |
45
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
0
|
|
46
|
local $SIG{'ALRM'} = sub { die "Server Timeout\n" }; |
|
0
|
|
|
|
|
0
|
|
48
|
1
|
|
|
|
|
5
|
my $ok = eval { |
49
|
1
|
|
|
|
|
9
|
alarm($self->timeout_header); |
50
|
1
|
|
|
|
|
20
|
$self->process_headers; |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
7
|
alarm($self->timeout_idle); |
53
|
1
|
|
|
|
|
4
|
my $env = \%ENV; |
54
|
1
|
|
|
|
|
8
|
$env->{'psgi.version'} = [1, 0]; |
55
|
1
|
50
|
33
|
|
|
11
|
$env->{'psgi.url_scheme'} = ($ENV{'HTTPS'} && $ENV{'HTTPS'} eq 'on') ? 'https' : 'http'; |
56
|
1
|
|
|
|
|
5
|
$env->{'psgi.input'} = $self->{'server'}->{'client'}; |
57
|
1
|
|
|
|
|
12
|
$env->{'psgi.errors'} = $self->{'server'}->{'log_handle'}; |
58
|
1
|
|
|
|
|
7
|
$env->{'psgi.multithread'} = 1; |
59
|
1
|
|
|
|
|
5
|
$env->{'psgi.multiprocess'} = 1; |
60
|
1
|
|
|
|
|
4
|
$env->{'psgi.nonblocking'} = 1; # need to make this false if we aren't of a forking type server |
61
|
1
|
|
|
|
|
4
|
$env->{'psgi.streaming'} = 1; |
62
|
1
|
|
|
|
|
12
|
local %ENV; |
63
|
1
|
|
|
|
|
18
|
$self->process_psgi_request($env); |
64
|
1
|
|
|
|
|
10
|
alarm(0); |
65
|
1
|
|
|
|
|
20
|
1; |
66
|
|
|
|
|
|
|
}; |
67
|
1
|
|
|
|
|
8
|
alarm(0); |
68
|
|
|
|
|
|
|
|
69
|
1
|
50
|
|
|
|
29
|
if (! $ok) { |
70
|
0
|
|
0
|
|
|
0
|
my $err = "$@" || "Something happened"; |
71
|
0
|
|
|
|
|
0
|
$self->send_500($err); |
72
|
0
|
|
|
|
|
0
|
die $err; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub process_psgi_request { |
77
|
1
|
|
|
1
|
1
|
4
|
my ($self, $env) = @_; |
78
|
1
|
|
|
|
|
7
|
my $app = $self->find_psgi_handler($env); |
79
|
1
|
|
|
|
|
5
|
my $resp = $app->($env); |
80
|
|
|
|
|
|
|
return $resp->(sub { |
81
|
0
|
|
|
0
|
|
0
|
my $resp = shift; |
82
|
0
|
|
|
|
|
0
|
$self->print_psgi_headers($resp->[0], $resp->[1]); |
83
|
0
|
0
|
|
|
|
0
|
return $self->{'server'}->{'client'} if @$resp == 2; |
84
|
0
|
|
|
|
|
0
|
return $self->print_psgi_body($resp->[2]); |
85
|
1
|
50
|
|
|
|
8
|
}) if ref($resp) eq 'CODE'; |
86
|
1
|
|
|
|
|
19
|
$self->print_psgi_headers($resp->[0], $resp->[1]); |
87
|
1
|
|
|
|
|
24
|
$self->print_psgi_body($resp->[2]); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
1
|
50
|
|
1
|
1
|
6
|
sub find_psgi_handler { shift->app || \&psgi_echo_handler } |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub app { |
93
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
94
|
1
|
50
|
|
|
|
5
|
$self->{'server'}->{'app'} = shift if @_; |
95
|
1
|
|
|
|
|
3
|
my $app = $self->{'server'}->{'app'}; |
96
|
1
|
50
|
33
|
|
|
15
|
if (!ref($app) && $app) { |
97
|
0
|
|
0
|
|
|
0
|
$app = $self->{'server'}->{'app'} = eval { require CGI::Compile; CGI::Compile->compile($app) } |
98
|
|
|
|
|
|
|
|| die "Failed to compile app with CGI::Compile"; |
99
|
|
|
|
|
|
|
} |
100
|
1
|
|
|
|
|
8
|
return $app; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub print_psgi_headers { |
104
|
1
|
|
|
1
|
0
|
5
|
my ($self, $status, $headers) = @_; |
105
|
1
|
|
|
|
|
31
|
$self->send_status($status); |
106
|
1
|
|
|
|
|
3
|
my $request_info = $self->{'request_info'}; |
107
|
1
|
|
|
|
|
4
|
my $out = ''; |
108
|
1
|
50
|
|
|
|
3
|
for my $i (0 .. @{ $headers || [] } / 2 - 1) { |
|
1
|
|
|
|
|
12
|
|
109
|
1
|
|
|
|
|
7
|
my $key = "\u\L$headers->[$i*2]"; |
110
|
1
|
|
|
|
|
4
|
my $val = $headers->[$i*2 + 1]; |
111
|
1
|
|
|
|
|
4
|
$key =~ y/_/-/; |
112
|
1
|
|
|
|
|
5
|
$out .= "$key: $val\015\012"; |
113
|
1
|
|
|
|
|
2
|
push @{ $request_info->{'response_headers'} }, [$key, $val]; |
|
1
|
|
|
|
|
5
|
|
114
|
|
|
|
|
|
|
} |
115
|
1
|
|
|
|
|
2
|
$out .= "\015\012"; |
116
|
1
|
|
|
|
|
4
|
$request_info->{'response_header_size'} += length $out; |
117
|
1
|
|
|
|
|
6
|
$self->{'server'}->{'client'}->print($out); |
118
|
1
|
|
|
|
|
60
|
$request_info->{'headers_sent'} = 1; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub print_psgi_body { |
122
|
1
|
|
|
1
|
0
|
3
|
my ($self, $body) = @_; |
123
|
1
|
|
|
|
|
5
|
my $client = $self->{'server'}->{'client'}; |
124
|
1
|
|
|
|
|
2
|
my $request_info = $self->{'request_info'}; |
125
|
1
|
50
|
0
|
|
|
6
|
if (ref $body eq 'ARRAY') { |
|
|
0
|
|
|
|
|
|
126
|
1
|
|
|
|
|
4
|
for my $chunk (@$body) { |
127
|
1
|
|
|
|
|
5
|
$client->print($chunk); |
128
|
1
|
|
|
|
|
70
|
$request_info->{'response_size'} += length $chunk; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} elsif (blessed($body) && $body->can('getline')) { |
131
|
0
|
|
|
|
|
0
|
while (defined(my $chunk = $body->getline)) { |
132
|
0
|
|
|
|
|
0
|
$client->print($chunk); |
133
|
0
|
|
|
|
|
0
|
$request_info->{'response_size'} += length $chunk; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} else { |
136
|
0
|
|
|
|
|
0
|
while (defined(my $chunk = <$body>)) { |
137
|
0
|
|
|
|
|
0
|
$client->print($chunk); |
138
|
0
|
|
|
|
|
0
|
$request_info->{'response_size'} += length $chunk; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub psgi_echo_handler { |
144
|
1
|
|
|
1
|
0
|
2
|
my $env = shift; |
145
|
1
|
|
|
|
|
11
|
my $txt = qq{\n}; |
146
|
1
|
50
|
|
|
|
4
|
if (eval { require Data::Dumper }) { |
|
1
|
|
|
|
|
976
|
|
147
|
1
|
|
|
|
|
8458
|
local $Data::Dumper::Sortkeys = 1; |
148
|
1
|
|
|
|
|
4
|
my $form = {}; |
149
|
1
|
50
|
|
|
|
4
|
if (eval { require CGI::PSGI }) { my $q = CGI::PSGI->new($env); $form->{$_} = $q->param($_) for $q->param; } |
|
1
|
|
|
|
|
255
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
150
|
1
|
|
|
|
|
28
|
$txt .= "".Data::Dumper->Dump([$env, $form], ['env', 'form'])." "; |
151
|
|
|
|
|
|
|
} |
152
|
1
|
|
|
|
|
196
|
return [200, ['Content-type', 'text/html'], [$txt]]; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
0
|
1
|
|
sub exec_cgi { die "Not implemented" } |
156
|
0
|
|
|
0
|
1
|
|
sub exec_trusted_perl { die "Not implemented" } |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
1; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
__END__ |