]> git.uio.no Git - usit-rt.git/blob - lib/RT/PlackRunner.pm
Upgrade to 4.2.2
[usit-rt.git] / lib / RT / PlackRunner.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 use warnings;
50 use strict;
51
52 package RT::PlackRunner;
53
54 use base 'Plack::Runner';
55
56 sub new {
57     my $class = shift;
58     return $class->SUPER::new( default_middleware => 0, @_ );
59 }
60
61 sub parse_options {
62     my $self = shift;
63     my @args = @_;
64     # handle "rt-server 8888" for back-compat, but complain about it
65     if (@args && $args[0] =~ m/^\d+$/) {
66         warn "Deprecated: please run $0 --port $ARGV[0] instead\n";
67         unshift @args, '--port';
68     }
69
70     $self->SUPER::parse_options(@args);
71
72     $self->{app}    ||= $self->app;
73     $self->{server} ||= $self->loader->guess;
74
75     my %args = @{$self->{options}};
76     if ($self->{server} eq "FCGI") {
77         # We deal with the possible failure modes of this in ->run
78     } elsif ($args{port}) {
79         $self->{explicit_port} = 1;
80         my $old_app = $self->{app};
81         $self->{app} = sub {
82             my $env = shift;
83             $env->{'rt.explicit_port'} = $args{port};
84             $old_app->($env, @_);
85         };
86     } else {
87         $self->set_options(port => (RT->Config->Get('WebPort') || '8080'));
88     }
89 }
90
91 # Don't assume port 5000 with no port or socket supplied; this allows
92 # the WebPort default to kick in (above), and also to provide useful
93 # error messages when starting FCGI without any options.
94 sub mangle_host_port_socket {
95     my $self = shift;
96     my ($host, $port, $socket, @listen) = @_;
97     return $self->SUPER::mangle_host_port_socket(@_)
98         if @listen or $port or $socket;
99     return host => $host, port => $port, socket => $socket, listen => \@listen;
100 }
101
102 sub app {
103     require RT::Interface::Web::Handler;
104     my $app = RT::Interface::Web::Handler->PSGIApp;
105
106     if ($ENV{RT_TESTING}) {
107         my $screen_logger = $RT::Logger->remove('screen');
108         require Log::Dispatch::Perl;
109         $RT::Logger->add(
110             Log::Dispatch::Perl->new(
111                 name      => 'rttest',
112                 min_level => $screen_logger->min_level,
113                 action    => {
114                     error    => 'warn',
115                     critical => 'warn'
116                 }
117             )
118         );
119         require Plack::Middleware::Test::StashWarnings;
120         $app = Plack::Middleware::Test::StashWarnings->wrap($app);
121     }
122
123     return $app;
124 }
125
126 sub run {
127     my $self = shift;
128
129     my %args = @{$self->{options}};
130
131     # Plack::Handler::FCGI has its own catch for this, but doesn't
132     # notice that listen is an empty list, and we can also provide a
133     # better error message.
134     if ($self->{server} eq "FCGI" and not -S STDIN and not @{$args{listen}}) {
135         print STDERR "STDIN is not a socket, and no --listen, --socket, or --port provided\n";
136         exit 1;
137     }
138
139     eval { $self->SUPER::run(@_) };
140     my $err = $@;
141     exit 0 unless $err;
142
143     if ( $err =~ /listen/ ) {
144         print STDERR <<EOF;
145 WARNING: RT couldn't start up a web server on port $args{port}.
146 This is often the case if the port is already in use or you're running @{[$0]}
147 as someone other than your system's "root" user.  You may also specify a
148 temporary port with: $0 --port <port>
149 EOF
150
151         if ($self->{explicit_port}) {
152             print STDERR
153                 "Please check your system configuration or choose another port\n\n";
154         }
155         exit 1;
156     } else {
157         die
158             "Something went wrong while trying to run RT's standalone web server:\n\t"
159                 . $err;
160     }
161 }
162
163 1;