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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
# A 99% clone of CGI::Application::Server that allows the server to pass
# parameters (e.g., CFG files).
package MyServer;
use strict;
use warnings;
use Carp qw( confess );
use CGI qw( param );
use Scalar::Util qw( blessed reftype );
use HTTP::Response;
use HTTP::Status;
our $VERSION = '0.062';
use base qw( HTTP::Server::Simple::CGI );
use HTTP::Server::Simple::Static;
# HTTP::Server::Simple methods
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{entry_points} = {};
$self->{document_root} = '.';
$self->{options} = {};
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 options {
my ($self, $options) = @_;
if (defined $options) {
(reftype($options) && reftype($options) eq 'HASH')
|| confess "The entry points map must be a HASH reference, not $options";
$self->{options} = $options;
}
$self->{options};
}
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($self->options->{$path})->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 SYNOPSIS
use CGI::Application::Server;
use MyCGIApp;
use MyCGIApp::Admin;
use MyCGI::App::Account::Dispatch;
use MyCGIApp::DefaultApp;
my $server = CGI::Application::Server->new();
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
=over 4
=item B<new ($port)>
This acts just like C<new> for L<HTTP::Server::Simple>, except it
will initialize instance slots that we use.
=item 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).
=item 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.
=item B<is_valid_entry_point ($uri)>
This attempts to match the C<$uri> to an entry point.
=item B<document_root (?$document_root)>
This is the server's document root where all static files will
be served from.
=back
=head1 CAVEATS
This is a subclass of L<HTTP::Server::Simple> and all of its caveats
apply here as well.
=head1 BUGS
All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.
=head1 CODE COVERAGE
I use L<Devel::Cover> to test the code coverage of my tests, below
is the L<Devel::Cover> report on this module's test suite.
---------------------------- ------ ------ ------ ------ ------ ------ ------
File stmt bran cond sub pod time total
---------------------------- ------ ------ ------ ------ ------ ------ ------
...CGI/Application/Server.pm 94.4 80.0 53.3 100.0 100.0 100.0 88.3
Total 94.4 80.0 53.3 100.0 100.0 100.0 88.3
---------------------------- ------ ------ ------ ------ ------ ------ ------
=head1 ACKNOWLEDGEMENTS
=over 4
=item The HTTP response handling was shamelessly stolen from L<HTTP::Request::AsCGI> by chansen
=back
=head1 AUTHOR
Stevan Little E<lt>stevan@iinteractive.comE<gt>
Rob Kinyon E<lt>rob.kinyon@iinteractive.comE<gt>
Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2006 by Infinity Interactive, Inc.
L<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|