Apache-SWIT

 view release on metacpan or  search on metacpan

lib/Apache/SWIT.pm  view on Meta::CPAN

=head1 NAME

Apache::SWIT - mod_perl based application server with integrated testing.

=head1 SYNOPSIS

	package MyHandler;
	use base 'Apache::SWIT';

	# overload render routine
	sub swit_render {
		my ($class, $r) = @_;
		return ({ hello => 'world' }, 'my_template.tt');
		# or return { hello => 'world' }; to rely on swit.yaml
		# based generation
	}

	# overload update routine, usually result of POST
	sub swit_update {
		my ($class, $r) = @_;
		# do some work ...
		# and redirect to another page
		return '/redirect/to/some/url';
	}

=head1 DISCLAIMER
	
This is pre-alpha quality software. Please use it on your own risk.

=head1 DESCRIPTION

This module serves as yet another mod_perl based application server.

It tries to capture several often occuring paradigms in mod_perl development.
It provides user with the tools to bootstrap a new project, write tests easily,
etc.

=head1 METHODS

=cut

use strict;
use warnings FATAL => 'all';

package Apache::SWIT;
use Carp;
use Data::Dumper;
use File::Slurp;

our $VERSION = 0.54;

sub swit_startup {}

=head2 $class->swit_send_http_header($r, $ct)

Sends HTTP default headers: session cookie and content type. C<$r> is apache
request and C<$ct> is optional content type (defaults to
C<text/html; charset=utf-8>.

=cut
sub swit_send_http_header {
	my ($class, $r, $ct) = @_;
	$r->pnotes('SWITSession')->end;
	$r->pnotes('SWITSession', undef);
	$r->content_type($ct || "text/html; charset=utf-8");
}

=head2 $class->swit_die($msg, $r, @data_to_dump)

Dies with first line of C<$msg> using Carp::croak and dumps request C<$r> and
C<@data_to_dump> with Data::Dumper into /tmp/swit_<time>.err file.

=cut
sub swit_die {
	my ($class, $msg, $r, @more) = @_;
	my $err = (split(/\n/, $msg))[0];
	my $f = "/tmp/swit_" . time . ".err";
	write_file($f, "$msg with request:\n" . $r->as_string . "and more:\n"
			. join("\n", map { Dumper($_) } @more));
	croak "In $f $err";
}

sub _raw_respond {
	my ($class, $r, $to) = @_;
	my $s = ref($to) ? $to->[0] : Apache2::Const::REDIRECT();
	if ($s eq 'INTERNAL') {
		$r->pnotes("PrevRequestSuppress", $to->[2]) if $to->[2];
		$r->headers_in->unset("Content-Length");
		$r->internal_redirect($r->uri . "/../" . $to->[1]);
		return Apache2::Const::OK();
	}
	$r->headers_out->add(Location => $to) unless ref($to);
	$class->swit_send_http_header($r, ref($to) ? $to->[2] : undef);
	$r->print($to->[1]) if (ref($to) && defined($to->[1]));
	return $s;
}

=head2 swit_post_max

Maximal size of POST request. Default is 1M. Overload it to return something
else.

=cut
sub swit_post_max { return '1000000'; }

sub swit_invalid_request {
	my ($class, $r, $err) = @_;
	die "Invalid request: $err";
}

=head2 $class->swit_update_handler($class, $r)

Entry point for an update handler. Calls $class->swit_update($apr) function with
C<Apache2::Request> parameter. The result of C<swit_update> henceforth is called
C<$to> is passed down.

If C<$to> is regular string then 302 status is produced with Location equal to
C<$to>.

If C<$to> is array reference and first item is a number then the status with
C<$to->[0]> is produced and $to->[1] is returned as response body. $to->[2]
may by used as content type.

I.e. [ 200, "Hello", "text/plain" ] will respond with C<200 OK> status and
C<Hello> as a body with C<text/plain> as content type.



( run in 5.113 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )