/usr/share/perl5/CGI/Application/Server.pm is in libcgi-application-server-perl 0.063-2.
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | use strict;
use warnings;
package CGI::Application::Server;
{
$CGI::Application::Server::VERSION = '0.063';
}
# ABSTRACT: a simple HTTP server for developing with CGI::Application
use Carp 0.01 qw( confess );
use CGI qw( param );
use Scalar::Util 1.18 qw( blessed reftype );
use HTTP::Response;
use HTTP::Status;
use base qw( HTTP::Server::Simple::CGI );
use HTTP::Server::Simple 0.18;
use HTTP::Server::Simple::Static 0.02;
# HTTP::Server::Simple methods
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{entry_points} = {};
$self->{document_root} = '.';
return $self;
}
# accessors
sub document_root {
my ($self, $document_root) = @_;
if (defined $document_root) {
(-d $document_root)
|| confess "The server root ($document_root) is not found";
$self->{document_root} = $document_root;
}
$self->{document_root};
}
sub entry_points {
my ($self, $entry_points) = @_;
if (defined $entry_points) {
(reftype($entry_points) && reftype($entry_points) eq 'HASH')
|| confess "The entry points map must be a HASH reference, not $entry_points";
$self->{entry_points} = $entry_points;
}
$self->{entry_points};
}
# check request
sub is_valid_entry_point {
my ($self, $uri) = @_;
# Remove all parameters
$uri =~ s/\?.*//;
while ( $uri ) {
# Check to see if this is an exact match
if (exists $self->{entry_points}{$uri}) {
return ($uri, $self->{entry_points}{$uri});
}
# Remove the rightmost path element
$uri =~ s/\/[^\/]*$//;
}
# Check to see if there's an entry for '/'
if (exists $self->{entry_points}{'/'}) {
return ($uri, $self->{entry_points}{'/'});
}
# Didn't find anything. Oh, well.
return;
}
sub handle_request {
my ($self, $cgi) = @_;
if (my ($path, $target) = $self->is_valid_entry_point($ENV{REQUEST_URI})) {
# warn "$ENV{REQUEST_URI} ($target)\n";
# warn "\t$_ => " . param( $_ ) . "\n" for param();
local $ENV{CGI_APP_RETURN_ONLY} = 1;
(local $ENV{PATH_INFO} = $ENV{PATH_INFO}) =~ s/\A\Q$path//;
if (-d $target && -x $target) {
return $self->serve_static($cgi, $target);
}
elsif ($target->isa('CGI::Application::Dispatch')) {
return $self->_serve_response($target->dispatch);
} elsif ($target->isa('CGI::Application')) {
if (!defined blessed $target) {
return $self->_serve_response($target->new->run);
} else {
$target->query($cgi);
return $self->_serve_response($target->run);
}
}
else {
confess "Target must be a CGI::Application or CGI::Application::Dispatch subclass or the name of a directory that exists and is readable.\n";
}
} else {
return $self->serve_static($cgi, $self->document_root);
}
}
sub _serve_response {
my ( $self, $stdout ) = @_;
my $response = $self->_build_response( $stdout );
print $response->as_string();
return 1; # Like ...Simple::Static::serve_static does
}
# Shamelessly stolen from HTTP::Request::AsCGI by chansen
sub _build_response {
my ( $self, $stdout ) = @_;
$stdout =~ s{(.*?\x0d?\x0a\x0d?\x0a)}{}xsm;
my $headers = $1;
unless ( defined $headers ) {
$headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a";
}
unless ( $headers =~ /^HTTP/ ) {
$headers = "HTTP/1.1 200 OK\x0d\x0a" . $headers;
}
my $response = HTTP::Response->parse($headers);
$response->date( time() ) unless $response->date;
my $message = $response->message;
my $status = $response->header('Status');
$response->header( Connection => 'close' );
if ( $message && $message =~ /^(.+)\x0d$/ ) {
$response->message($1);
}
if ( $status && $status =~ /^(\d\d\d)\s?(.+)?$/ ) {
my $code = $1;
$message = $2 || HTTP::Status::status_message($code);
$response->code($code);
$response->message($message);
}
my $length = length $stdout;
if ( $response->code == 500 && !$length ) {
$response->content( $response->error_as_HTML );
$response->content_type('text/html');
return $response;
}
$response->add_content($stdout);
$response->content_length($length);
return $response;
}
1;
__END__
=pod
=head1 NAME
CGI::Application::Server - a simple HTTP server for developing with CGI::Application
=head1 VERSION
version 0.063
=head1 SYNOPSIS
use CGI::Application::Server;
use MyCGIApp;
use MyCGIApp::Admin;
use MyCGI::App::Account::Dispatch;
use MyCGIApp::DefaultApp;
my $server = CGI::Application::Server->new();
# this CGI::Application object will stay persistent, might not be safe to use
# in this way - your mileage may vary
# http://www.mail-archive.com/cgiapp@lists.erlbaum.net/msg08997.html
my $object = MyOtherCGIApp->new(PARAMS => { foo => 1, bar => 2 });
$server->document_root('./htdocs');
$server->entry_points({
'/' => 'MyCGIApp::DefaultApp',
'/index.cgi' => 'MyCGIApp',
'/admin' => 'MyCGIApp::Admin',
'/account' => 'MyCGIApp::Account::Dispatch',
'/users' => $object,
'/static' => '/usr/local/htdocs',
});
$server->run();
=head1 DESCRIPTION
This is a simple HTTP server for for use during development with
L<CGI::Application>. At this moment, it serves our needs in a
very basic way. The plan is to release early and release often,
and add features when we need them. That said, we welcome any
and all patches, tests and feature requests (the ones with which
are accompanied by failing tests will get priority).
=head1 METHODS
=head2 B<new ($port)>
This acts just like C<new> for L<HTTP::Server::Simple>, except it
will initialize instance slots that we use.
=head2 B<handle_request>
This will check the request uri and dispatch appropriately, either
to an entry point, or serve a static file (html, jpeg, gif, etc).
=head2 B<entry_points (?$entry_points)>
This accepts a HASH reference in C<$entry_points>, which maps server entry
points (uri) to L<CGI::Application> or L<CGI::Application::Dispatch> class
names or objects or to directories from which static content will be served
by HTTP::Server::Simple::Static. See the L<SYNOPSIS> above for examples.
=head2 B<is_valid_entry_point ($uri)>
This attempts to match the C<$uri> to an entry point.
=head2 B<document_root (?$document_root)>
This is the server's document root where all static files will
be served from.
=head1 CAVEATS
This is a subclass of L<HTTP::Server::Simple> and all of its caveats
apply here as well.
=head1 ACKNOWLEDGEMENTS
The HTTP response handling was shamelessly stolen from L<HTTP::Request::AsCGI>
by chansen
=head1 AUTHORS
=over 4
=item *
Stevan Little <stevan@iinteractive.com>
=item *
Rob Kinyon <rob.kinyon@iinteractive.com>
=item *
Ricardo SIGNES <rjbs@cpan.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|