-
Notifications
You must be signed in to change notification settings - Fork 577
/
PSGI.pm
148 lines (107 loc) · 3.09 KB
/
PSGI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package Mojo::Server::PSGI;
use Mojo::Base 'Mojo::Server';
use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;
# "Things aren't as happy as they used to be down here at the unemployment
# office.
# Joblessness is no longer just for philosophy majors.
# Useful people are starting to feel the pinch."
sub run {
my ($self, $env) = @_;
# Environment
my $tx = $self->build_tx;
my $req = $tx->req;
$req->parse($env);
# Store connection information
$tx->remote_address($env->{REMOTE_ADDR});
$tx->local_port($env->{SERVER_PORT});
# Request body
my $len = $env->{CONTENT_LENGTH};
while (!$req->is_finished) {
my $chunk = ($len && $len < CHUNK_SIZE) ? $len : CHUNK_SIZE;
my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0);
last unless $read;
$req->parse($buffer);
$len -= $read;
last if $len <= 0;
}
# Handle
$self->emit(request => $tx);
# Response headers
my $res = $tx->res;
$res->fix_headers;
my $headers = $res->content->headers;
my @headers;
for my $name (@{$headers->names}) {
for my $values ($headers->header($name)) {
push @headers, $name => $_ for @$values;
}
}
# PSGI response
return [$res->code || 404,
\@headers, Mojo::Server::PSGI::_IO->new(tx => $tx)];
}
sub to_psgi {
(my $self = shift)->app;
return sub { $self->run(@_) }
}
# "Wow! Homer must have got one of those robot cars!
# *Car crashes in background*
# Yeah, one of those AMERICAN robot cars."
package Mojo::Server::PSGI::_IO;
use Mojo::Base -base;
sub close { shift->{tx}->server_close }
sub getline {
my $self = shift;
# Blocking read
my $res = $self->{tx}->res;
while (1) {
my $chunk = $res->get_body_chunk($self->{offset} //= 0);
# No content yet, try again
sleep 1 and next unless defined $chunk;
# End of content
return unless length $chunk;
# Content
$self->{offset} += length $chunk;
return $chunk;
}
}
1;
__END__
=head1 NAME
Mojo::Server::PSGI - PSGI server
=head1 SYNOPSIS
use Mojo::Server::PSGI;
my $psgi = Mojo::Server::PSGI->new;
$psgi->unsubscribe('request');
$psgi->on(request => sub {
my ($psgi, $tx) = @_;
# Request
my $method = $tx->req->method;
my $path = $tx->req->url->path;
# Response
$tx->res->code(200);
$tx->res->headers->content_type('text/plain');
$tx->res->body("$method request for $path!");
# Resume transaction
$tx->resume;
});
$psgi->to_psgi;
=head1 DESCRIPTION
L<Mojo::Server::PSGI> allows L<Mojo> applications to run on all PSGI
compatible servers.
See L<Mojolicious::Guides::Cookbook> for deployment recipes.
=head1 EVENTS
L<Mojo::Server::PSGI> inherits all events from L<Mojo::Server>.
=head1 METHODS
L<Mojo::Server::PSGI> inherits all methods from L<Mojo::Server> and
implements the following new ones.
=head2 C<run>
my $res = $psgi->run($env);
Run L<PSGI>.
=head2 C<to_psgi>
my $app = $psgi->to_psgi;
Turn L<Mojo> application into L<PSGI> application. Note that this method is
EXPERIMENTAL and might change without warning!
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut