| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | Froody::Server - baseclass for Froody::Server | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | A Froody server. use as: | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use warnings; | ||||||||||||||||||||||||||||
| 12 | use strict; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use Froody::Server; | ||||||||||||||||||||||||||||
| 15 | Froody::Server->dispatch(); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | ..in a CGI script that is a Froody endpoint. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | This server accepts a CGI request as the Froody request, and will dispatch | ||||||||||||||||||||||||||||
| 20 | the method, and return the XML of the response as the result of the HTTP | ||||||||||||||||||||||||||||
| 21 | request. If the dispatcher throws an error, we catch it and wrap it in | ||||||||||||||||||||||||||||
| 22 | a L<Froody::Response> object that represents the error. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | You can pass a namespace to dispatch into to the L<dispatch()> call, to | ||||||||||||||||||||||||||||
| 25 | override the default L<Froody::Dispatch> namespace. This is strongly | ||||||||||||||||||||||||||||
| 26 | recommended, or your code will be fairly useless. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | package Froody::Server; | ||||||||||||||||||||||||||||
| 31 | 9 | ▶ | use base qw( Froody::Base ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 9 | ▶ | use CGI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 9 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 9 | ▶ | use Params::Validate qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 9 | ▶ | use Froody::Dispatch; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 9 | ▶ | use Froody::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 9 | ▶ | use Froody::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 9 | my $logger = get_logger("froody.server"); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 9 | ▶ | use Froody::Renderer::json; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # XXX: move down to autoload when doing autodetect | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | eval q{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 49 | use Apache::Constants; | ||||||||||||||||||||||||||||
| 50 | use Froody::Request::Apache; | ||||||||||||||||||||||||||||
| 51 | use Froody::Server::Apache; | ||||||||||||||||||||||||||||
| 52 | }; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 9 | ▶ | use Froody::Server::CGI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 METHODS | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =over 4 | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item dispatch() | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Detects the environment that the Froody server is running under, assembles | ||||||||||||||||||||||||||||
| 63 | a request, and dispatches it. Replies to the request with the XML response. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub dispatch { | ||||||||||||||||||||||||||
|
Subroutine
dispatch: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $server = $class->server_class->new; | |||||||||||||||||||||||||||
| 71 | 0 | my $request = $server->request_class->new; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | my $response = $class->dispatcher->dispatch( | ||||||||||||||||||||||||||
|
Condition0%0/2
$request->params || {}
MC/DC0%0/1
$request->params || {}
$request->params
Truth table0%0/2
A
A= $request->params
| |||||||||||||||||||||||||||||
| 73 | method => $request->method, | ||||||||||||||||||||||||||||
| 74 | params => $request->params || {}, | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # lookup what type of output the request asked for then send | ||||||||||||||||||||||||||||
| 78 | # the correct header and response rendered for that type | ||||||||||||||||||||||||||||
| 79 | 0 | my $type = $request->type; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | $server->send_header($response, $class->content_type_for_type($type)); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $render_method = "render_$type"; | |||||||||||||||||||||||||||
| 84 | 0 | my $bytes = $response->$render_method; | |||||||||||||||||||||||||||
| 85 | 0 | $server->send_body($bytes); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item dispatcher() | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Returns a dispatcher object set up for this server with the correct | ||||||||||||||||||||||||||||
| 91 | request, response and error classes. I find this useful for getting | ||||||||||||||||||||||||||||
| 92 | a dispatcher object for testing against when I have a server class with | ||||||||||||||||||||||||||||
| 93 | lots of customization in. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 9 | our $D_CACHE; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub dispatcher { | ||||||||||||||||||||||||||
|
Subroutine
dispatcher: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | unless ($D_CACHE->{ ref($class) || $class }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 101 | 0 | my $server = $class->server_class->new; | |||||||||||||||||||||||||||
| 102 | 0 | my $dispatcher = $class->dispatch_class->new; | |||||||||||||||||||||||||||
| 103 | 0 | $dispatcher->response_class($server->response_class) | |||||||||||||||||||||||||||
| 104 | ->error_class($server->error_class); | ||||||||||||||||||||||||||||
| 105 | 0 | $dispatcher->error_style('response'); | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | $D_CACHE->{ ref($class) || $class } = $dispatcher; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | return $D_CACHE->{ ref($class) || $class }; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =item send_header($response, $content_type) | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Implemented by subclasses. Should send the headers to the client | ||||||||||||||||||||||||||||
| 114 | using the content type passed. Should call the $response's cookies | ||||||||||||||||||||||||||||
| 115 | method and dtrt with that. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =item send_body($bytes) | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Implmented by subclasses. Sends the body of the connection. $bytes should | ||||||||||||||||||||||||||||
| 120 | be a rendered response. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =item handler | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Handler for apache. Passes the request off to C<dispatch()> and returns | ||||||||||||||||||||||||||||
| 125 | C<&Apache::OK> if it succeeds. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub handler : method { | ||||||||||||||||||||||||||
|
Subroutine
handler: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 0 | my ($class, $r) = @_; | |||||||||||||||||||||||||||
| 131 | 0 | $class->dispatch(); | |||||||||||||||||||||||||||
| 132 | 0 | return &Apache::OK; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item content_type_for_type($type) / content_type_for_type($type, $header); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Gets and sets the header for a type of server. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 9 | my $h4t = { | |||||||||||||||||||||||||||
| 142 | xml => "text/xml; charset=utf-8", | ||||||||||||||||||||||||||||
| 143 | json => 'text/json', | ||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub content_type_for_type { | ||||||||||||||||||||||||||
|
Subroutine
content_type_for_type: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 148 | 0 | my $type = shift; | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | unless (@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | { return $h4t->{ $type } or Froody::Error->throw('froody.type.notknown', "unknown type '$type'"); } | |||||||||||||||||||||||||||
| 151 | 0 | $h4t->{ $type } = shift; | |||||||||||||||||||||||||||
| 152 | 0 | return $class; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =back | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 Subclassing | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | There's serveral methods you might want to override in subclasses of | ||||||||||||||||||||||||||||
| 161 | these. In particular these methods define what helper classes this | ||||||||||||||||||||||||||||
| 162 | uses: | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =over | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item server_class | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item dispatch_class | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =item request_class | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item error_class | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =item response_class | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | sub server_class { | ||||||||||||||||||||||||||
|
Subroutine
server_class: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | return 'Froody::Server::Apache' if $ENV{MOD_PERL}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | return 'Froody::Server::CGI'; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub dispatch_class { "Froody::Dispatch" } | ||||||||||||||||||||||||||
|
Subroutine
dispatch_class: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub request_class { "Froody::Request" } | ||||||||||||||||||||||||||
|
Subroutine
request_class: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub response_class { "Froody::Response" } | ||||||||||||||||||||||||||
|
Subroutine
response_class: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub error_class { "Froody::Response::Error" } | ||||||||||||||||||||||||||
|
Subroutine
error_class: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =back | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 BUGS | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | None known. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Please report any bugs you find via the CPAN RT system. | ||||||||||||||||||||||||||||
| 198 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody> | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Copyright Fotango 2005. All rights reserved. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Please see the main L<Froody> documentation for details of who has worked | ||||||||||||||||||||||||||||
| 205 | on this project. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 208 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | L<Froody>, L<Froody::Dispatch>, | ||||||||||||||||||||||||||||
| 213 | L<Froody::Server::Standalone>, L<Froody::Server::CGI>, L<Froody::Server::Apache> | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =cut | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 9 | 1; | |||||||||||||||||||||||||||