1 | package Mojo::Server::PSGI;
|
---|
2 | use Mojo::Base 'Mojo::Server';
|
---|
3 |
|
---|
4 | sub run {
|
---|
5 | my ($self, $env) = @_;
|
---|
6 |
|
---|
7 | my $tx = $self->build_tx;
|
---|
8 | my $req = $tx->req->parse($env);
|
---|
9 | $tx->local_port($env->{SERVER_PORT})->remote_address($env->{REMOTE_ADDR});
|
---|
10 |
|
---|
11 | # Request body (may block if we try to read too much)
|
---|
12 | my $len = $env->{CONTENT_LENGTH};
|
---|
13 | until ($req->is_finished) {
|
---|
14 | my $chunk = ($len && $len < 131072) ? $len : 131072;
|
---|
15 | last unless my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0);
|
---|
16 | $req->parse($buffer);
|
---|
17 | last if ($len -= $read) <= 0;
|
---|
18 | }
|
---|
19 |
|
---|
20 | $self->emit(request => $tx);
|
---|
21 |
|
---|
22 | # Response headers
|
---|
23 | my $res = $tx->res->fix_headers;
|
---|
24 | my $hash = $res->headers->to_hash(1);
|
---|
25 | my @headers;
|
---|
26 | for my $name (keys %$hash) { push @headers, $name, $_ for @{$hash->{$name}} }
|
---|
27 |
|
---|
28 | # PSGI response
|
---|
29 | my $io = Mojo::Server::PSGI::_IO->new(tx => $tx, empty => $tx->is_empty);
|
---|
30 | return [$res->code // 404, \@headers, $io];
|
---|
31 | }
|
---|
32 |
|
---|
33 | sub to_psgi_app {
|
---|
34 | my $self = shift;
|
---|
35 |
|
---|
36 | # Preload application and wrap it
|
---|
37 | $self->app->server($self);
|
---|
38 | return sub { $self->run(@_) }
|
---|
39 | }
|
---|
40 |
|
---|
41 | package Mojo::Server::PSGI::_IO;
|
---|
42 | use Mojo::Base -base;
|
---|
43 |
|
---|
44 | # Finish transaction
|
---|
45 | sub close { shift->{tx}->closed }
|
---|
46 |
|
---|
47 | sub getline {
|
---|
48 | my $self = shift;
|
---|
49 |
|
---|
50 | # Empty
|
---|
51 | return undef if $self->{empty};
|
---|
52 |
|
---|
53 | # No content yet, try again later
|
---|
54 | my $chunk = $self->{tx}->res->get_body_chunk($self->{offset} //= 0);
|
---|
55 | return '' unless defined $chunk;
|
---|
56 |
|
---|
57 | # End of content
|
---|
58 | return undef unless length $chunk;
|
---|
59 |
|
---|
60 | $self->{offset} += length $chunk;
|
---|
61 | return $chunk;
|
---|
62 | }
|
---|
63 |
|
---|
64 | 1;
|
---|
65 |
|
---|
66 | =encoding utf8
|
---|
67 |
|
---|
68 | =head1 NAME
|
---|
69 |
|
---|
70 | Mojo::Server::PSGI - PSGI server
|
---|
71 |
|
---|
72 | =head1 SYNOPSIS
|
---|
73 |
|
---|
74 | use Mojo::Server::PSGI;
|
---|
75 |
|
---|
76 | my $psgi = Mojo::Server::PSGI->new;
|
---|
77 | $psgi->unsubscribe('request')->on(request => sub {
|
---|
78 | my ($psgi, $tx) = @_;
|
---|
79 |
|
---|
80 | # Request
|
---|
81 | my $method = $tx->req->method;
|
---|
82 | my $path = $tx->req->url->path;
|
---|
83 |
|
---|
84 | # Response
|
---|
85 | $tx->res->code(200);
|
---|
86 | $tx->res->headers->content_type('text/plain');
|
---|
87 | $tx->res->body("$method request for $path!");
|
---|
88 |
|
---|
89 | # Resume transaction
|
---|
90 | $tx->resume;
|
---|
91 | });
|
---|
92 | my $app = $psgi->to_psgi_app;
|
---|
93 |
|
---|
94 | =head1 DESCRIPTION
|
---|
95 |
|
---|
96 | L<Mojo::Server::PSGI> allows L<Mojolicious> applications to run on all L<PSGI>
|
---|
97 | compatible servers.
|
---|
98 |
|
---|
99 | See L<Mojolicious::Guides::Cookbook/"DEPLOYMENT"> for more.
|
---|
100 |
|
---|
101 | =head1 EVENTS
|
---|
102 |
|
---|
103 | L<Mojo::Server::PSGI> inherits all events from L<Mojo::Server>.
|
---|
104 |
|
---|
105 | =head1 ATTRIBUTES
|
---|
106 |
|
---|
107 | L<Mojo::Server::PSGI> inherits all attributes from L<Mojo::Server>.
|
---|
108 |
|
---|
109 | =head1 METHODS
|
---|
110 |
|
---|
111 | L<Mojo::Server::PSGI> inherits all methods from L<Mojo::Server> and implements
|
---|
112 | the following new ones.
|
---|
113 |
|
---|
114 | =head2 run
|
---|
115 |
|
---|
116 | my $res = $psgi->run($env);
|
---|
117 |
|
---|
118 | Run L<PSGI>.
|
---|
119 |
|
---|
120 | =head2 to_psgi_app
|
---|
121 |
|
---|
122 | my $app = $psgi->to_psgi_app;
|
---|
123 |
|
---|
124 | Turn L<Mojolicious> application into L<PSGI> application.
|
---|
125 |
|
---|
126 | =head1 SEE ALSO
|
---|
127 |
|
---|
128 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
129 |
|
---|
130 | =cut
|
---|