This file is indexed.

/usr/share/perl5/UR/Service/WebServer.pm is in libur-perl 0.440-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package UR::Service::WebServer;

use strict;
use warnings;

use UR;
use UR::Service::WebServer::Server;
use IO::File;
use IO::Socket::INET;
use Sys::Hostname;

class UR::Service::WebServer {
    has => [
        host => { is => 'String',
                    default_value => 'localhost',
                    doc => 'IP address to listen on' },
        port => { is => 'Integer',
                    default_value => undef,
                    doc => 'TCP port to listen on' },
    ],
    has_optional => [
        server  => { is => 'HTTP::Server::PSGI', calculate_from => ['__host','__port'], is_constant => 1,
                    calculate => q(
                        return UR::Service::WebServer::Server->new(
                            host => $__host,
                            port => $__port,
                            timeout => $self->timeout,
                            server_ready => sub { $self->announce() },
                        );
                    ), },
        timeout => { is => 'Integer',
                        default_value => undef,
                        doc => 'Timeout for read and write events' },
        idle_timeout => { is => 'Integer', default_value => undef,
                        doc => 'Exit the event loop after being idle for this many seconds' },
        cb => { is => 'CODE', doc => 'callback for handling requests' },
    ],
};

# Override port and host so they can auto-fill when needed
sub _port_host_override {
    my $self = shift;
    my $methodname = shift;
    my $method = '__'.$methodname;
    my $socket_method = 'sock'.$methodname;
    if (@_) {
        if ($self->{server}) {
            die "Cannot change $methodname after it has created the listen socket";
        }
        $self->$method(@_);

    } else {
      #  if (!defined($self->$method) && !defined($self->{server})) {
        unless (defined $self->$method) {
            unless (defined $self->{server}) {
                # not connected yet - start the server's listen socket and get its port
                $self->server->setup_listener();
            }
            $self->$method( $self->server->listen_sock->$socket_method() );
        }
    }
    return $self->$method;
}

sub port {
    my $self = shift;
    $self->_port_host_override('port', @_);
}

sub host {
    my $self = shift;
    $self->_port_host_override('host', @_);
}


sub announce {
    my $self = shift;

    my $sock = $self->server->listen_sock;
    my $host = ($sock->sockhost eq '0.0.0.0') ? Sys::Hostname::hostname() : gethostbyaddr($sock->sockaddr, AF_INET);
    $self->status_message(sprintf('Listening on http://%s:%d/', $host, $sock->sockport));
    return 1;
}

sub run {
    my $self = shift;

    my $cb = shift || $self->cb;

    unless ($cb) {
        $self->warning_message("No callback for run()... returning");
        return;
    }

    my $timeout = $self->idle_timeout || 0;
    local $SIG{'ALRM'} = sub { die "alarm\n" };
    eval {
        alarm($timeout);
        $self->server->run($cb);
    };
    alarm(0);
    die $@ unless $@ eq "alarm\n";
}

my %mime_types = (
    'js'    => 'application/javascript',
    'html'  => 'text/html',
    'css'   => 'text/css',
    '*'     => 'text/plain',
);
sub _mime_type_for_filename {
    my($self, $pathname) = @_;
    my($ext) = ($pathname =~ m/\.(\w+)$/);
    $ext ||= '*';
    return $mime_types{$ext} || $mime_types{'*'};
}
sub _file_opener_for_directory {
    my($self, $dir) = @_;
    return sub {
        (my $pathname = shift) =~ s#/?\.\.##g;  # Remove .. - don't want them escaping the given directory tree
        return IO::File->new( join('/', $dir, $pathname), 'r');
    };
    
}
sub file_handler_for_directory {
    my($self, $dir) = @_;

    my $opener = $self->_file_opener_for_directory($dir);

    return sub {
        my($env, $pathname) = @_;

        my $fh = $opener->($pathname);
        unless($fh) {
            return [ 404, [ 'Content-Type' => 'text/plain'], ['Not Found']];
        }
        my $type = $self->_mime_type_for_filename($pathname);
        if ($env->{'psgi.streaming'}) {
            return [ 200, ['Content-Type' => $type], $fh];
        } else {
            local $/;
            my $buffer = <$fh>;
            return [ 200, ['Content-Type' => $type], [$buffer]];
        }
    };
}

sub delete {
    my $self = shift;
    $self->server->listen_sock->close();
    $self->{server} = undef;
    $self->SUPER::delete(@_);
}

1;

=pod

=head1 NAME

UR::Service::WebServer - A PSGI-based web server

=head1 SYNOPSIS

  my $s = UR::Service::WebServer(port => 4321);
  $s->run( \&handle_request );

=head1 DESCRIPTION

Implements a simple, standalone web server based on HTTP::Server::PSGI.  The
event loop is entered by calling the run() method.

=head2 Properties

=over 4

=item host

The IP address to listen on for connections.  The default value is
'localhost'.  host can be changed any time before the server is created,
usually the first time run() is called.

=item port

The TCP port to listen on for connections.   The detault value is undef,
meaning that the system will pick an unused port.  port can be changed any
time before the server is created, usually the first time run() is called.

=item server

Holds a reference to an object that isa HTTP::Server::PSGI.  This will be
automatically created the first time run() is called.

=item cb

Holds a CODE reference used as the default request handler within run().

=back

=head2 Methods

=over 4

=item $self->announce()

This method is called when the PSGI server is ready to accept requests.
The base-class behavior is to print the listening URL on STDOUT.  Subclasses
can override it to implement their own behavior.

=item my $code = $self->file_handler_for_directory($path)

A helper method used for implementing server for files located in the
directory $path.  It returns a CODE ref that takes 2 arguments, $env (the
standard PSGI env hashref) and $pathname (a path relative to $path).  It
returns the standard tuple a PSGI server expects.

$pathname is pre-processed by removing all occurances of ".." to keep requests
within the provided $path.  If the requested file is not found, then it
returns a 404.

=item $self->run(<$cb>)

Enter the request loop.  If a callback is not provided to run(), then the
object's cb property is used instead.  If neither have a value, then run()
returns immediately.

For each request $cb is called with one argument, the standard PSGI env
hashref.

=back

=head1 SEE ALSO

L<UR::Service::UrlRouter>

=cut