RWDE/Web/CGIadapter.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 275 CC 20 SCAR 31.5
CC
20
Cov
0%
print_header
90.0
add_header
30.0
get_formdata
12.0
CRAP 23.3
1 ## @file
2 # (Enter your file info here)
3 #
4 # @copy 2007 MailerMailer LLC
5 # $Id: CGIadapter.pm 446 2008-05-07 17:45:44Z damjan $
6  
7 use vars qw($VERSION);
8 $VERSION = sprintf "%d", q$Revision: 567 $ =~ /(\d+)/;
9  
10 ## @class RWDE::Web::CGIadapter
11 # (Enter RWDE::Web::CGIadapter info here)
12 package RWDE::Web::CGIadapter;
13  
14 use strict;
15 use warnings;
16  
17 use Error qw(:try);
18 use RWDE::Exceptions;
19  
20 use RWDE::CCR;
21  
22 use base qw(RWDE::Logging);
23  
24 ## @method object new()
25 # create the blessed object and call the subclass's initialize() method
26 # @return
27 sub new {
28 my ($proto, $params) = @_;
29  
30 my $class = ref($proto) || $proto;
31 my $self = { accumulator => {} };
32  
33 bless $self, $class;
34  
35 $self->initialize($params);
36  
37 return $self;
38 }
39  
40 ## @method object initialize()
41 # perform any initializations necessary for this type of connection.
42 # MUST be overridden in subclass.
43 # @return
44 sub initialize {
45 my ($self, $params) = @_;
46  
47 throw RWDE::DevelException({ info => 'initialize() method not overridden in '.ref($self) });
48  
49 return;
50 }
51  
52  
53 ## @method object get_req()
54 # (Enter get_req info here)
55 # @return
56 sub get_req {
57 my ($self, $params) = @_;
58  
59 return $self->{req};
60 }
61  
62 ## @method object is_https()
63 # (Enter is_https info here)
64 # @return
65 sub is_https {
66 my ($self, $params) = @_;
67  
68 return ($self->get_req->protocol() eq 'https');
69 }
70  
71 ## @method void check_https()
72 # (Enter check_https info here)
73 sub check_https {
74 my ($self, $params) = @_;
75  
76 unless ($self->is_https()) {
77 throw RWDE::DevelException({ info => 'Attempt to access secure information on a non-ssl connection.' });
78 }
79  
80 return ();
81 }
82  
83 ## @method object get_uri()
84 # (Enter get_uri info here)
85 # @return
86 sub get_uri {
87 my ($self, $params) = @_;
88  
89 my $script_name = substr($self->get_req->script_name(), 1);
90  
91 #Get script returns the get part of the request if there is a // in it, we only want the script name
92 my ($uri, $get_request) = split /\?/, $script_name;
93  
94 # get the uri requested
95 return $uri;
96 }
97  
98 ## @method object get_url()
99 # (Enter get_url info here)
100 # @return
101 sub get_url {
102 my ($self, $params) = @_;
103  
104 # get the uri requested
105 return $self->get_req->url();
106 }
107  
108 ## @method object get_class()
109 # (Enter get_class info here)
110 # @return
111 sub get_class {
112 my ($self, $params) = @_;
113  
114 my $uri = $self->get_uri();
115  
116 my $cpath = (split /\./, $uri)[0]; #the command path
117  
118 my @cparts = split /\//, $cpath;
119  
120 my $class = join('::', @cparts);
121  
122 $class = 'Command::' . $class;
123  
124 #replace dashes for underscores
125 $class =~ s/-/_/g;
126  
127 #sanitize: remove everything that is not allowed in the package name
128 # $class =~ /([a-zA-Z0-9_:]+)/;
129  
130 return ($class);
131 }
132  
133 ## @method object get_formdata()
134 # (Enter get_formdata info here)
135 # @return
136 sub get_formdata {
137 my ($self, $params) = @_;
138  
139 my $formdata = {};
140  
141 foreach my $f ($self->get_req->param()) {
142  
143 #each param is an array, but there most likely is just one value
144 my @ar = $self->get_req->param($f);
145  
146 #if this parameter had an array of values
147 if (scalar @ar > 1) {
148  
149 #pass as reference
150 $$formdata{$f} = \@ar;
151 }
152  
153 #otherwise...
154 else {
155  
156 #store single value under hash key
157 if (defined $ar[0] && $ar[0] ne '--' && $ar[0] ne '' && !($ar[0] =~ m/^\s*$/)) {
158 $$formdata{$f} = $ar[0]; #store form data in hash
159 }
160 }
161 }
162 return $formdata;
163 }
164  
165 ## @method void set_cookie($data, $insecure, $name, $uri)
166 # (Enter set_cookie info here)
167 # @param name (Enter explanation for param here)
168 # @param insecure (Enter explanation for param here)
169 # @param data (Enter explanation for param here)
170 # @param uri (Enter explanation for param here)
171 sub set_cookie {
172 my ($self, $params) = @_;
173  
174 my $uri = $$params{uri};
175 my $name = $$params{name};
176 my $data = $$params{data};
177 my $cookie;
178  
179 if (!$$params{insecure}) {
180 $self->check_https();
181  
182 $cookie = $self->get_req->cookie(
183 -name => $name,
184 -value => $data,
185 -expires => '+24h',
186 -secure => 1
187 );
188 }
189 else {
190 $cookie = $self->get_req->cookie(
191 -name => $name,
192 -value => $data,
193 -expires => '+24h',
194 );
195 }
196  
197 $self->forward({ cookie => $cookie, uri => $$params{uri} });
198  
199 return ();
200 }
201  
202 ## @method void add_header(header,$value)
203 # Add a header to the accumulator. "known" headers are 'type',
204 # 'status', 'expires', and 'cookie' as per CGI.pm's header() method.
205 # Only cookie can be set multiple times for multiple cookies.
206 # Headers are delivered when print_header() method is called.
207 # @param header name of header
208 # @param value value of header
209 # @return
210 sub add_header {
211 my ($self, $params) = @_;
212  
213 if ($self->{header}) {
214 throw RWDE::DevelException({ info => 'Header was already sent to the client.' });
215 }
216  
217 my $header = '-'.lc($$params{header});
218 my $value = $$params{value};
219  
220 my $ac = $self->{accumulator};
221  
222 if ($header eq '-cookie') {
223 $ac->{$header} = [] unless exists $ac->{$header};
224 push @{$ac->{$header}},$value;
225 } elsif (exists $ac->{$header}) {
226 throw RWDE::DevelException({ info => "Header $header was already set." });
227 } else {
228 $ac->{$header} = $value;
229 }
230  
231 return;
232 }
233  
234 ## @method void add_cookie_header($cookie)
235 # Add the cookie created using the params passed. see CGI.pm for cookie()
236 # params. print_header() method will actually output the cookies.
237 # @param cookie hashref passed directly to CGI.pm's cookie() method
238 # @return true for success, undef if too late to add headers
239 sub add_cookie_header {
240 my ($self, $params) = @_;
241  
242 if ($self->{header}) {
243 return; # too late to add headers
244 }
245  
246 my $cookie = $self->get_req->cookie(%$params);
247  
248 $self->add_header({header => 'cookie', value => $cookie});
249 }
250  
251 ## @method object get_cookies()
252 # @return hash ref of key/values for all cookies passed from browser
253 sub get_cookies {
254 my ($self) = @_;
255  
256 my $cgi = $self->get_req();
257  
258 my %cookies = map { $_ => $cgi->cookie($_) } $cgi->cookie();
259  
260 return \%cookies;
261 }
262  
263 ## @method void print_header($filename, $mimetype, $pagetype)
264 # (Enter print_header info here)
265 # @param filename (Enter explanation for param here)
266 # @param pagetype (Enter explanation for param here)
267 # @param mimetype (Enter explanation for param here)
268 sub print_header {
269 my ($self, $params) = @_;
270  
271 if ($self->{header}) {
272 return ();
273 }
274  
275 my $pagetype = $$params{pagetype};
276  
277 unless (defined $pagetype) {
278
279 #once all pagetypes are well defined, we want to throw exception
280 #meanwhile just return, as we have no action to perform - avoids uninitialized
281 #comparison warns
282 return;
283 #throw RWDE::DevelException({ info => 'Pagetype is not defined' });
284 }
285  
286  
287 #if this is a standard webserver type
288 if ($pagetype eq 'rwp') {
289 $self->add_header({header => 'type', value => 'text/html; charset=utf-8'});
290 $self->add_header({header => 'pragma', value => 'no-cache'});
291 $self->{header} = 'true';
292 }
293  
294 elsif ($pagetype eq 'ttml') {
295 $self->add_header({header => 'type', value => 'text/html; charset=utf-8'});
296 $self->add_header({header => 'pragma', value => 'no-cache'});
297 $self->{header} = 'true';
298 }
299  
300 elsif ($pagetype eq 'rss') {
301 $self->add_header({header => 'type', value => 'text/xml; charset=utf-8'});
302 $self->add_header({header => 'pragma', value => 'no-cache'});
303 $self->{header} = 'true';
304 }
305  
306 elsif ($pagetype eq 'bin') {
307  
308 # Ignore header printout request for .bin's
309 # if all required elements are not set we'll
310 # let the command print it out later.
311 # This avoids the problem of special casing all
312 # standard header printouts.
313 if (not defined $$params{filename}) {
314 return ();
315 }
316  
317 $self->add_header({header => 'type', value => $$params{mimetype}});
318 $self->add_header({header => 'attachment', value => $$params{filename}});
319 $self->{header} = 'true';
320 }
321  
322 print $self->get_req->header($self->{accumulator}) if $self->{header};
323  
324 return ();
325 }
326  
327 ## @method void forward($cookie, $uri)
328 # (Enter forward info here)
329 # @param cookie (Enter explanation for param here)
330 # @param uri (Enter explanation for param here)
331 sub forward {
332 my ($self, $params) = @_;
333  
334 if ($self->{header}) {
335 throw RWDE::DevelException({ info => 'Header was already sent to the client, redirect won\'t work' });
336 }
337  
338 my $uri = $$params{uri};
339 my $cookie = $$params{cookie};
340 print $self->get_req->redirect(
341 -uri => $uri,
342 -nph => 0,
343 -cookie => $cookie,
344 -status => '303 See Other'
345 );
346  
347 $self->{header} = 'true';
348 $self->{forward} = 'true';
349  
350 # 301 - Moved Permanently
351 # 302 - Found
352 # 303 - See Other
353 return ();
354 }
355  
356 sub not_found {
357 my ($self, $params) = @_;
358 $self->{header} = 'true';
359 print $self->get_req->header(-status => '404 Not Found');
360 return();
361 }
362  
363  
364 ## @method void auth_required()
365 # (Enter auth_required info here)
366 sub auth_required {
367 my ($self, $params) = @_;
368  
369 if ($self->{header}) {
370 throw RWDE::DevelException({ info => 'Header was already sent to the client, redirect won\'t work' });
371 }
372  
373 print $self->get_req->header(
374 -status => '401 Authorization Required',
375 -WWW_authenticate => 'Basic realm="DiscussThis Archives"',
376 );
377  
378 $self->{header} = 'true';
379  
380 # 401 - Authorization Required
381 return ();
382 }
383  
384 ## @method object get_record()
385 # (Enter get_record info here)
386 # @return
387 sub get_record {
388 my ($self, $params) = @_;
389  
390 my @proof;
391 push(@proof, "Time: " . RWDE::Time->now, "Request: " . $self->get_req->script_name(), "IP: " . $self->get_req->remote_host(), "Agent: " . ($self->get_req->user_agent() || 'N/A'));
392  
393 my $record .= join("\n", @proof);
394  
395 return $record;
396 }
397  
398 ## @method object get_remote_ip()
399 # (Enter get_remote_ip info here)
400 # @return
401 sub get_remote_ip {
402 my ($self, $params) = @_;
403 return $self->get_req->remote_host();
404 }
405  
406 ## @method object get_referer()
407 # (Enter get_referer info here)
408 # @return
409 sub get_referer {
410 my ($self, $params) = @_;
411  
412 return $self->get_req->referer();
413 }
414  
415 ## @method object get_contenttype()
416 # (Enter get_contenttype info here)
417 # @return
418 sub get_contenttype {
419 my ($self, $params) = @_;
420  
421 my $file = $self->get_req->param('upload');
422  
423 my $info = $self->get_req->uploadInfo($file);
424  
425 return $info->{'Content-Type'};
426 }
427  
428 1;