File: Server.pm

package info (click to toggle)
libmojolicious-perl 0.999926-1%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,672 kB
  • ctags: 949
  • sloc: perl: 17,391; makefile: 4
file content (209 lines) | stat: -rw-r--r-- 4,529 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Copyright (C) 2008-2010, Sebastian Riedel.

package Mojo::Server;

use strict;
use warnings;

use base 'Mojo::Base';

use Carp 'croak';
use Mojo::Loader;

__PACKAGE__->attr(
    app => sub {
        my $self = shift;

        # App in environment
        return $ENV{MOJO_APP} if ref $ENV{MOJO_APP};

        # Load
        if (my $e = Mojo::Loader->load($self->app_class)) {
            die $e if ref $e;
        }

        return $self->app_class->new;
    }
);
__PACKAGE__->attr(app_class =>
      sub { ref $ENV{MOJO_APP} || $ENV{MOJO_APP} || 'Mojo::HelloWorld' });
__PACKAGE__->attr(
    build_tx_cb => sub {
        sub {
            my $self = shift;

            # Reload
            if (my $reload = $self->reload) {
                local $ENV{MOJO_RELOAD} = $reload;
                if (my $e = Mojo::Loader->reload) { warn $e }
                delete $self->{app};

                # Reset if this reload was triggered by a USR1 signal
                $self->reload(0) if $reload == 2;
            }

            return $self->app->build_tx_cb->($self->app);
          }
    }
);
__PACKAGE__->attr(
    continue_handler_cb => sub {
        sub {
            my ($self, $tx) = @_;
            if ($self->app->can('continue_handler')) {
                $self->app->continue_handler($tx);

                # Close connection to prevent potential race condition
                unless ($tx->res->code == 100) {
                    $tx->keep_alive(0);
                    $tx->res->headers->connection('Close');
                }
            }
            else { $tx->res->code(100) }
        };
    }
);
__PACKAGE__->attr(
    handler_cb => sub {
        sub { shift->app->handler(shift) }
    }
);
__PACKAGE__->attr(reload => sub { $ENV{MOJO_RELOAD} || 0 });
__PACKAGE__->attr(
    websocket_handshake_cb => sub {
        sub {
            my $self = shift;
            return $self->app->websocket_handshake_cb->($self->app, @_);
          }
    }
);

# Are you saying you're never going to eat any animal again? What about bacon?
# No.
# Ham?
# No.
# Pork chops?
# Dad, those all come from the same animal.
# Heh heh heh. Ooh, yeah, right, Lisa. A wonderful, magical animal.
sub new {
    my $self = shift->SUPER::new(@_);

    # Reload on USR1 signal (does not work on win32)
    $SIG{USR1} = sub { $self->reload(2) }
      if $^O ne 'MSWin32';

    return $self;
}

sub run { croak 'Method "run" not implemented by subclass' }

1;
__END__

=head1 NAME

Mojo::Server - HTTP Server Base Class

=head1 SYNOPSIS

    use base 'Mojo::Server';

    sub run {
        my $self = shift;

        # Get a transaction
        my $tx = $self->build_tx_cb->($self);

        # Call the handler
        $tx = $self->handler_cb->($self);
    }

=head1 DESCRIPTION

L<Mojo::Server> is an abstract HTTP server base class.

=head1 ATTRIBUTES

L<Mojo::Server> implements the following attributes.

=head2 C<app>

    my $app = $server->app;
    $server = $server->app(MojoSubclass->new);

Application this server handles, defaults to a L<Mojo::HelloWorld> object.

=head2 C<app_class>

    my $app_class = $server->app_class;
    $server       = $server->app_class('MojoSubclass');

Class of the application this server handles, defaults to
L<Mojo::HelloWorld>.

=head2 C<build_tx_cb>

    my $btx = $server->build_tx_cb;
    $server = $server->build_tx_cb(sub {
        my $self = shift;
        return Mojo::Transaction::HTTP->new;
    });

Transaction builder callback.

=head2 C<continue_handler_cb>

    my $handler = $server->continue_handler_cb;
    $server     = $server->continue_handler_cb(sub {
        my ($self, $tx) = @_;
    });

Callback handling C<100 Continue> requests.

=head2 C<handler_cb>

    my $handler = $server->handler_cb;
    $server     = $server->handler_cb(sub {
        my ($self, $tx) = @_;
    });

Handler callback.

=head2 C<reload>

    my $reload = $server->reload;
    $server    = $server->reload(1);

Activate automatic reloading.

=head2 C<websocket_handshake_cb>

    my $handshake = $server->websocket_handshake_cb;
    $server       = $server->websocket_handshake_cb(sub {
        my ($self, $tx) = @_;
    });

WebSocket handshake callback.

=head1 METHODS

L<Mojo::Server> inherits all methods from L<Mojo::Base> and implements the
following new ones.

=head2 C<new>

    my $server = Mojo::Server->new;

Construct a new L<Mojo::Server> object.

=head2 C<run>

    $server->run;

Start server.

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.

=cut