/usr/share/perl5/Perlbal/Plugin/Cgilike.pm is in libperlbal-perl 1.80-3.
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | #!/usr/bin/perl
#
# Copyright 2007 Martin Atkins <mart@degeneration.co.uk> and Six Apart Ltd.
#
=head1 NAME
Perlbal::Plugin::Cgilike - Handle Perlbal requests with a Perl subroutine
=head1 DESCRIPTION
This module allows responses to be handled with a simple API that's similar in principle to
CGI, mod_perl response handlers, etc.
It does not, however, come anywhere close to conforming to the CGI "standard". It's actually
more like mod_perl in usage, though there are several differences.
Most notably, Perlbal is single-process and single-threaded, and handlers run inside the Perlbal
process and must therefore return quickly and not do any blocking operations.
As it currently stands, this is very bare-bones and has only really been used with basic GET
requests. It lacks a nice API for handling the body of a POST or PUT request.
It is not recommended to use this for extensive applications. Perlbal is first and foremost
a load balancer, so if you're doing something at all complicated you're probably better off
using something like Apache mod_perl and then putting Perlbal in front if it if necessary.
However, this plugin may prove useful for simple handlers or perhaps embedding a simple
HTTP service into another application that uses C<Danga::Socket>.
=head1 SYNOPSIS
This module provides a Perlbal plugin which can be loaded and used as follows.
LOAD cgilike
PERLREQUIRE = MyPackage
CREATE SERVICE cgilike
SET role = web_server
SET listen = 127.0.0.1:80
SET plugins = cgilike
PERLHANDLER = MyPackage::handler
ENABLE cgilike
With this plugin loaded into a particular service, the plugin will then be called for
all requests for that service.
Set cgilike.handler to the name of a subroutine that will handle requests. This subroutine
will receive an object which allows interaction with the Perlbal service.
package MyPackage
sub handler {
my ($r) = @_;
if ($r->uri eq '/') {
print "<p>Hello, world</p>";
return Perlbal::Plugin::Cgilike::HANDLED;
}
else {
return 404;
}
}
Return C<Perlbal::Plugin::Cgilike::HANDLED> to indicate that the request has been handled, or return some HTTP error code
to produce a predefined error message.
You may also return C<Perlbal::Plugin::Cgilike::DECLINED> if you do not wish to handle the request, in which case Perlbal
will be allowed to handle the request in whatever way it would have done without Cgilike loaded.
If your handler returns any non-success value, it B<MUST NOT> produce any output. If you
produce output before returning such a value, the response to the client is likely to be
utter nonsense.
You may also return C<Perlbal::Plugin::Cgilike::POSTPONE_RESPONSE>, which is equivalent to
returning zero except that the HTTP connection will be left open once you return. It is
your responsibility to later call C<$r-E<gt>end_response()> when you have completed
the response. This style is necessary when you need to perform some long operation
before you can return a response; you'll need to use some appropriate method to set
a callback to run when the operation completes and then do your response in the
callback. Once you've called C<end_response>, you must not call any further methods on C<$r>;
it's probably safest to just return immediately afterwards to avoid any mishaps.
=head1 API DOCUMENTATION
TODO: Write this
=head1 TODO
Currently there is no API for dealing with the body of a POST or PUT request. Ideally it'd be able
to do automatic decoding of application/x-www-form-urlencoded data, too.
The POSTPONE_RESPONSE functionality has not been tested extensively and is probably buggy.
=head1 COPYRIGHT AND LICENSE
Copyright 2007 Martin Atkins <mart@degeneration.co.uk> and Six Apart Ltd.
This module is part of the Perlbal distribution, and as such can be distributed under the same licence terms as the rest of Perlbal.
=cut
package Perlbal::Plugin::Cgilike;
use Perlbal;
use strict;
use Symbol;
use constant DECLINED => -2;
use constant HANDLED => 0;
use constant POSTPONE_RESPONSE => -1;
sub register {
my ($class, $svc) = @_;
$svc->register_hook('Cgilike', 'start_http_request', sub { Perlbal::Plugin::Cgilike::handle_request($svc, $_[0]); });
}
sub handle_request {
my Perlbal::Service $svc = shift;
my Perlbal::ClientProxy $pb = shift;
return 0 unless $pb->{req_headers};
# Create a new request object, and tie a filehandle to it
my $output_handle = Symbol::gensym();
my $req = tie(*{$output_handle}, 'Perlbal::Plugin::Cgilike::Request', $pb);
my $handler = $svc->{extra_config}->{_perlhandler};
if (! defined($handler)) {
return $pb->send_response(500, "No perlhandler is configured for this service");
}
# Our $output_handle is tied to the request object, which provides PRINT and PRINTF methods
# Set it as the default so that handlers can just use print and printf as normal.
my $oldfh = select($output_handle);
my $ret;
my $result = eval {
no strict;
$ret = &{$handler}($req);
1;
};
# Restore the old filehandle to avoid breaking anyone else
select($oldfh);
if ($result) {
if ($ret == 0 || $ret == POSTPONE_RESPONSE) {
if ($ret == 0) {
$req->end_response();
untie($req);
}
return 1;
}
elsif ($ret == DECLINED) {
return 0;
}
else {
return $pb->send_response($ret+0, $ret+0);
}
}
else {
return $pb->send_response(500, "Error in handler: ".$@);
}
return $pb->send_response(500, "I seem to have fallen into a place I shouldn't be.");
}
sub handle_perlrequire_command {
# This is defined with an equals because Perlbal lowercases all manage commands except
# after an equals, which means that having an equals here is the only way to actually
# get the correct case of the module name. Lame++.
my $mc = shift->parse(qr/^perlrequire\s*=\s*([\w:]+)$/, "usage: PERLREQUIRE=<module>");
my ($module) = $mc->args;
my $success = eval "require $module; 1;";
unless ($success) {
return $mc->err("Failed to load $module: $@")
}
return 1;
}
sub handle_perlhandler_command {
my $mc = shift->parse(qr/^perlhandler\s*=\s*([\w:]+)$/, "usage: PERLHANDLER=<package::subroutine>");
my ($subname) = $mc->args;
my $svcname;
unless ($svcname ||= $mc->{ctx}{last_created}) {
return $mc->err("No service name in context from CREATE SERVICE <name> or USE <service_name>");
}
my $svc = Perlbal->service($svcname);
return $mc->err("Non-existent service '$svcname'") unless $svc;
my $cfg = $svc->{extra_config}->{_perlhandler} = $subname;
return 1;
}
# called when we're no longer active on a service
sub unregister {
my ($class, $svc) = @_;
$svc->unregister_hooks('Cgilike');
return 1;
}
# called when we are loaded
sub load {
Perlbal::register_global_hook('manage_command.perlrequire', \&Perlbal::Plugin::Cgilike::handle_perlrequire_command);
Perlbal::register_global_hook('manage_command.perlhandler', \&Perlbal::Plugin::Cgilike::handle_perlhandler_command);
return 1;
}
# called for a global unload
sub unload {
return 1;
}
package Perlbal::Plugin::Cgilike::Request;
use URI;
sub new {
my ($class, $pb) = @_;
return bless {
pb => $pb,
header_sent => 0,
}, $class;
}
# This class can also provide a tied handle which supports PRINT and PRINTF (but not much else)
sub TIEHANDLE {
my ($class, $req_headers) = @_;
return $class->new($req_headers);
}
sub request_header {
return $_[0]->{pb}->{req_headers};
}
sub response_header {
my ($self, $k, $v) = @_;
if (defined($k)) {
my $hdrs = $self->response_header;
if (defined($v)) {
$hdrs->header($k => $v);
return $v;
}
else {
return $hdrs->header($k);
}
}
else {
if (defined($self->{response_headers})) {
return $self->{response_headers};
}
else {
return $self->{response_headers} = Perlbal::HTTPHeaders->new_response(200);
}
}
}
sub response_status_code {
my ($self, $value) = @_;
my $res = $self->response_header;
if (defined($value)) {
$res->code($value);
}
return $res->response_code;
}
sub uri {
my ($self) = @_;
return $self->{uri} ? $self->{uri} : $self->{uri} = URI->new($self->request_header->request_uri);
}
sub path {
my ($self) = @_;
return $self->uri->path;
}
sub path_segments {
my ($self) = @_;
my @segments = $self->uri->path_segments;
shift @segments; # Get rid of the empty segment at the start
return @segments;
}
sub query_string {
my ($self) = @_;
return $self->uri->query;
}
sub query_args {
my ($self) = @_;
return $self->uri->query_form;
}
sub method {
my ($self) = @_;
return $self->request_header->request_method;
}
sub send_response_header {
my ($self) = @_;
$self->response_header('Content-type' => 'text/html') unless $self->response_header('Content-type');
$self->{pb}->write($self->response_header->to_string_ref);
$self->{header_sent} = 1;
}
sub response_header_sent {
return $_[0]->{header_sent} ? 1 : 0;
}
sub PRINT {
my ($self, @stuff) = @_;
$self->print(@stuff);
}
sub PRINTF {
my ($self, $format, @stuff) = @_;
$self->print(sprintf($format, @stuff));
}
sub print {
my ($self, @stuff) = @_;
if (! $self->response_header_sent) {
$self->send_response_header();
}
$self->{pb}->write(join("", @stuff));
}
sub end_response {
my ($self) = @_;
$self->{pb}->write(sub { $self->{pb}->http_response_sent; });
}
1;
|