AppServer/Plugin/CustomError.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 66 CC 3 SCAR 16.7
CC
3
Cov
0%
_errorpage
12.0
init
2.0
BEGIN
2.0
CRAP 5.3
1 package HTTP::AppServer::Plugin::CustomError;
2 # Plugin for HTTP::AppServer that defines custom error pages
3 # for all HTTP error codes.
4 # 2010 by Tom Kirchner
5  
6 use 5.010000;
7 use strict;
8 use warnings;
9 use HTTP::AppServer::Plugin;
10 use base qw(HTTP::AppServer::Plugin);
11  
12 our $VERSION = '0.01';
13  
14 # all the mimetypes of files handled by the plugin
15 my $HTTPErrors = {
16 # <code> => [<info>, <html>],
17
18 # success
19 '200' => ['Ok',''],
20 '201' => ['Created',''],
21 '202' => ['Accepted',''],
22 '203' => ['Partial Information',''],
23 '204' => ['No Response',''],
24  
25 # error
26 '400' => ['Bad Request',''],
27 '401' => ['Unauthorized',''],
28 '402' => ['Payment required',''],
29 '403' => ['Forbidden',''],
30 '404' => ['Not found',''],
31 '500' => ['Internal Error',''],
32 '501' => ['Not implemented',''],
33 '502' => ['Service temporarily overloaded',''],
34 '503' => ['Gateway timeout',''],
35  
36 # redirection
37 '301' => ['Moved',''], # + Header: "URL: <url>"
38 '302' => ['Found',''], # + ...
39 '303' => ['Method',''], # + Header : "Method: <GET/POST> <url>"
40 '304' => ['Not modified',''],
41 };
42  
43 # called by the server when the plugin is installed
44 # to determine which routes are handled by the plugin
45 sub init
46 {
47 my ($class, $server, %options) = @_;
48  
49 # install properties in server
50 $server->set('errorpage', \&_errorpage);
51  
52 # the plugin does not install any URL handlers
53 return ();
54 }
55  
56 sub _errorpage
57 {
58 my ($server, $code) = @_;
59 $code = '500' if !defined $code || !exists $HTTPErrors->{$code};
60
61 my ($info, $html) = @{$HTTPErrors->{$code}};
62
63 # some nice default html
64 $html =
65 '<html><head><title>'.$code.' '.$info.'</title><style type="text/css">'.
66 'body { padding: 40pt; }'.
67 'body, h1, h2, p { color: #333; font-family: Arial, sans-serif; margin: 0; }'.
68 'div { width: 200px; background: #eee; padding: 2em; }'.
69 '</style></head><body><div><h1>'.$code.'</h1><h2>'.$info.'</h2></div></body></html>'
70 unless length $html;
71
72 print "HTTP/1.0 ".$code." ".$info."\r\n";
73 print "Content-type: text/html\r\n\r\n";
74 print $html;
75 }
76  
77 1;
78 __END__