Newer Older
130 lines | 2.758kb
copy gitweblite soruce code
root authored on 2012-11-23
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

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
11
  # Request body (may block if we try to read too much)
copy gitweblite soruce code
root authored on 2012-11-23
12
  my $len = $env->{CONTENT_LENGTH};
13
  until ($req->is_finished) {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
14
    my $chunk = ($len && $len < 131072) ? $len : 131072;
copy gitweblite soruce code
root authored on 2012-11-23
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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
20
  # Handle request
copy gitweblite soruce code
root authored on 2012-11-23
21
  $self->emit(request => $tx);
22

            
23
  # Response headers
24
  my $res     = $tx->res->fix_headers;
25
  my $headers = $res->content->headers;
26
  my @headers;
27
  for my $name (@{$headers->names}) {
28
    push @headers, $name => $_ for map {@$_} $headers->header($name);
29
  }
30

            
31
  # PSGI response
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
32
  my $io = Mojo::Server::PSGI::_IO->new(tx => $tx, empty => $tx->is_empty);
33
  return [$res->code || 404, \@headers, $io];
copy gitweblite soruce code
root authored on 2012-11-23
34
}
35

            
36
sub to_psgi_app {
37
  my $self = shift;
38

            
39
  # Preload application and wrap it
40
  $self->app;
41
  return sub { $self->run(@_) }
42
}
43

            
44
package Mojo::Server::PSGI::_IO;
45
use Mojo::Base -base;
46

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
47
# Finish transaction
copy gitweblite soruce code
root authored on 2012-11-23
48
sub close { shift->{tx}->server_close }
49

            
50
sub getline {
51
  my $self = shift;
52

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
53
  # Empty
54
  return undef if $self->{empty};
55

            
copy gitweblite soruce code
root authored on 2012-11-23
56
  # No content yet, try again later
57
  my $chunk = $self->{tx}->res->get_body_chunk($self->{offset} = defined $self->{offset} ? $self->{offset} : 0);
58
  return '' unless defined $chunk;
59

            
60
  # End of content
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
61
  return undef unless length $chunk;
copy gitweblite soruce code
root authored on 2012-11-23
62

            
63
  $self->{offset} += length $chunk;
64
  return $chunk;
65
}
66

            
67
1;
68

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
69
=encoding utf8
70

            
copy gitweblite soruce code
root authored on 2012-11-23
71
=head1 NAME
72

            
73
Mojo::Server::PSGI - PSGI server
74

            
75
=head1 SYNOPSIS
76

            
77
  use Mojo::Server::PSGI;
78

            
79
  my $psgi = Mojo::Server::PSGI->new;
80
  $psgi->unsubscribe('request');
81
  $psgi->on(request => sub {
82
    my ($psgi, $tx) = @_;
83

            
84
    # Request
85
    my $method = $tx->req->method;
86
    my $path   = $tx->req->url->path;
87

            
88
    # Response
89
    $tx->res->code(200);
90
    $tx->res->headers->content_type('text/plain');
91
    $tx->res->body("$method request for $path!");
92

            
93
    # Resume transaction
94
    $tx->resume;
95
  });
96
  my $app = $psgi->to_psgi_app;
97

            
98
=head1 DESCRIPTION
99

            
100
L<Mojo::Server::PSGI> allows L<Mojo> applications to run on all PSGI
101
compatible servers.
102

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
103
See L<Mojolicious::Guides::Cookbook> for more.
copy gitweblite soruce code
root authored on 2012-11-23
104

            
105
=head1 EVENTS
106

            
107
L<Mojo::Server::PSGI> inherits all events 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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
114
=head2 run
copy gitweblite soruce code
root authored on 2012-11-23
115

            
116
  my $res = $psgi->run($env);
117

            
118
Run L<PSGI>.
119

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
120
=head2 to_psgi_app
copy gitweblite soruce code
root authored on 2012-11-23
121

            
122
  my $app = $psgi->to_psgi_app;
123

            
124
Turn L<Mojo> application into L<PSGI> application.
125

            
126
=head1 SEE ALSO
127

            
128
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
129

            
130
=cut