NAME
BS::HTTPD - A simple lightweight event based web (application) server
VERSION
Version 0.01
SYNOPSIS
use BS::HTTPD;
my $httpd = BS::HTTPD->new (port => 9090);
$httpd->reg_cb (
_ => sub {
my ($httpd, $url, $headers) = @_;
$httpd->o ("<html><body><h1>Hello World!</h1>");
$httpd->o ("<a href=\"/test\">another test page</a>");
$httpd->o ("</body></html>");
() # !
},
_test => sub {
my ($httpd, $url, $headers) = @_;
$httpd->o ("<html><body><h1>Test page</h1>");
$httpd->o ("<a href=\"/\">Back to the main page</a>");
$httpd->o ("</body></html>");
() # !
},
);
DESCRIPTION
This module provides a simple HTTPD for serving simple web application interfaces. It's completly event based and independend from any event loop by using the AnyEvent module.
It's HTTP implementation is a bit hacky, so before using this module make sure it works for you and the expected deployment. Feel free to improve the HTTP support and send in patches!
I mainly wrote this module to provide a HTTP interface in BS. However, it doesn't depend on BS and it can be used to extend any application with a (simple) web interface.
The documentation is currently only the source code, but next versions of this module will be better documented hopefully. See also the samples/
directory in the BS::HTTPD distribution for basic starting points.
BS::HTTPD even comes with some basic AJAX framework/helper.
FEATURES
support for GET and POST requests
processing of
x-www-form-urlencoded
andmultipart/form-data
encoded form parametersajax helper and javascript output functions in BS::HTTPD::Appgets
support for chunked encoding output to the HTTP client
METHODS
The BS::HTTPD class inherits directly from BS::HTTPD::HTTPServer which inherits the event callback interface from BS::Event.
Event callbacks can be registered via the BS::Event API (see the documentation of BS::Event for details).
For a list of available events see below in the EVENTS section.
EVENTS
Every request goes to a specific URL. After a (GET or POST) request is received the URL is split at the '/' characters and joined again with '_' characters. After that the event with the name of the converted URL is invoked, this means that if you get a request to the url '/test/bla' the even _test_bla
is emitted, you can register a callback for that URL like this:
$httpd->reg_cb (
_test_bla => sub {
my ($httpd, $url, $headers) = @_;
# ...
[200, 'ok', { 'Content-Type' => 'text/html' }, '<h1>Test</h1>' }]
}
);
The first argument to such a callback is always the BS::HTTPD object itself. The second argument ($url
) is the URI::URL object of the request URL, the third argument ($headers
) are the HTTP headers as hashreference of array references.
Also every request also emits the request
event, with the same arguments and semantics, you can use this to implement your own request multiplexing.
The return value of these event callbacks are searched for array or hash references. The first callback that returned some response (a non-empty list) determines what the server will respond to the HTTP useragent.
If the return value was an array reference it's elements have the following semantics:
my ($code, $message, $header_hash, $content) =
[200, 'ok', { 'Content-Type' => 'text/html' }, '<h1>Test</h1>' }]
If the return value was a hash reference it the hash is first searched for the redirect
key and if that key does not exist for the content
key.
The value for the redirect
key should contain the URL that you want to redirect the request to.
The value for the content
key should contain an array reference with the first value being the content type and the second the content.
Here is an example:
$httpd->reg_cb (
_image_elmex => sub {
my ($httpd, $url, $headers) = @_;
open IMG, "$ENV{HOME}/media/images/elmex.png"
or return [404, 'not found', { 'Content-Type' => 'text/plain' }, 'not found'];
{ content => ['image/png', do { local $/; <IMG> }] }
}
);
Alternatively you can fill the response via the o
method which will append any strings it gets as argument to the response. The content type of a response constructed by o
will be text/html
.
REQUEST INPUT
If you would like to access the transmitted content you can call the request_input
method or use the parm
method to access the via multipart of urlencoded transmitted parameters.
If the request_input
method returns undef you know that only parameters have been passed by the request.
CACHING
Any response from the HTTP server will have Cache-Control
set to max-age=0
and also the Expires
header set to the Date
header. Meaning: Caching is disabled.
If you need caching or would like to have it you can send me a mail or even better: a patch :)
AUTHOR
Robin Redeker, <elmex at ta-sa.org>
BUGS
Please report any bugs or feature requests to bug-bs-httpd at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=BS-HTTPD. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc BS::HTTPD
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
COPYRIGHT & LICENSE
Copyright 2008 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.