Tmojo/HttpArgParser.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 245 CC 31 SCAR 37.9
CC
31
Cov
0%
args
72.0
args_query
72.0
args_post
72.0
CRAP 44.4
1 ###########################################################################
2 # Copyright 2004 Lab-01 LLC <http://lab-01.com/>
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Tmojo(tm) is a trademark of Lab-01 LLC.
17 ###########################################################################
18  
19 package HTML::Tmojo::HttpArgParser;
20  
21 =head1 Description
22  
23 HttpArgParser is a package designed for use in
24 Mod Perl handlers and CGI scripts
25  
26 It provides an object, which, when instantiated, slurps
27 up all the arguments passed via the CGI or mod perl ENV,
28 including STDIN and MIME MULTIPART form data
29  
30 Only one object should be created per HTTP POST or GET--
31 if you create a second one, it won't get POST parameters
32  
33 Usage:
34  
35 my $http_obj = HTML::Tmojo::HttpArgParser->new();
36 my %args = $http_obj->args();
37  
38 or
39  
40 my $http_obj = HTML::Tmojo::HttpArgParser->new();
41 my %args = $http_obj->args(
42 foo => 'array',
43 bar => 'hash',
44 goo => 'ref',
45 );
46  
47 If you want some arguments to be interpreted as hashes
48 or arrays, you can specify that by passing a literal hash
49 of argument names and types.
50  
51 =cut
52  
53  
54  
55 use strict;
56 use Encode;
57  
58  
59 #######################################
60  
61 =head2 new:method
62  
63 my $http_obj = HTML::Tmojo::HttpArgParser->new()
64  
65 Parses all standard parameters in the HTTP query
66 string and the http POST (if there is one) and
67 stores them in a new object, then returns the new
68 object.
69  
70 =cut
71  
72 sub new:method {
73  
74 my ($class, @extra_settings) = @_;
75
76 # make an object
77
78 my %data = (
79 args => {},
80 query => {},
81 post => {},
82 max_mime_size => 300_000,
83 @extra_settings
84 );
85
86 my $self = bless (\%data, $class);
87
88 # get the query parameters
89
90 $self->read_get();
91
92 # get a post with either regular or MIME multipart input
93
94 if ($ENV{REQUEST_METHOD} eq 'POST') {
95
96 if ($ENV{CONTENT_TYPE} =~ m/multipart\/form-data\;\s+boundary=[-]+(\S+)/i) {
97 $self->read_mime_post($1);
98 } else {
99 $self->read_post($1);
100 }
101
102 }
103
104 return $self;
105  
106 }
107  
108 #######################################
109  
110 =head2 args:method
111  
112 Call this to retrieve the GET and POST arguments stored in the object.
113  
114 my %args = $HttpArgParser->args();
115  
116 or
117  
118 my %args = $http_obj->args(
119 foo => 'array',
120 bar => 'hash',
121 goo => 'ref',
122 );
123  
124 =cut
125  
126 sub args:method {
127  
128 my ($self, %arg_specs) = @_;
129
130 # Unless they asked for a hash, array, or ref, we
131 # always return a scalar. The actual contents of
132 # $self->{args}{foo} is always an array
133
134 my @result;
135 my %union_hash = map { $_ => 1 } (keys %arg_specs, keys %{$self->{args}});
136
137 foreach my $key (keys %union_hash) {
138
139 # return the values as a hash ref
140
141 if (lc($arg_specs{$key}) eq 'hash') {
142
143 if (defined($self->{args}{$key})) {
144 push @result, $key, { map { $$_ } @{$self->{args}{$key}} };
145 } else {
146 push @result, $key, undef;
147 }
148
149 # return the values as an array ref
150
151 } elsif (lc($arg_specs{$key}) eq 'array') {
152
153 if (defined($self->{args}{$key})) {
154 push @result, $key, [ map { $$_ } @{$self->{args}{$key}} ];
155 } else {
156 push @result, $key, undef;
157 }
158
159 # return a reference to the last value
160
161 } elsif (lc($arg_specs{$key}) eq 'ref') {
162
163 if (not defined($self->{args}{$key})) {
164 push @result, $key, \undef;
165 } else {
166 my $array_size = scalar(@{$self->{args}{$key}});
167 push @result, $key, $self->{args}{$key}[$array_size-1];
168  
169 }
170
171 # return the last value as a scalar
172
173 } else {
174
175 if (not defined($self->{args}{$key})) {
176 push @result, $key, undef;
177 } else {
178 my $array_size = scalar(@{$self->{args}{$key}});
179 push @result, $key, ${$self->{args}{$key}[$array_size-1]};
180 }
181
182 }
183
184 }
185
186 return @result;
187  
188 }
189  
190 #######################################
191  
192 =head2 args_query:method
193  
194 Call this to retrieve ONLY the QUERY arguments stored in the object
195 (this ignores the POST arguments)
196  
197 my %query_args = $HttpArgParser->args_query();
198  
199 or
200  
201 my %query_args = $HttpArgParser->args_query(
202 foo => 'array',
203 bar => 'hash',
204 goo => 'ref',
205 );
206  
207 =cut
208  
209 sub args_query:method {
210  
211 my ($self, %arg_specs) = @_;
212
213 # Unless they asked for a hash, array, or ref, we
214 # always return a scalar. The actual contents of
215 # $self->{query}{foo} is always an array
216
217 my @result;
218 my %union_hash = map { $_ => 1 } (keys %arg_specs, keys %{$self->{query}});
219
220 foreach my $key (keys %union_hash) {
221
222 # return the values as a hash ref
223
224 if (lc($arg_specs{$key}) eq 'hash') {
225
226 if (defined($self->{query}{$key})) {
227 push @result, $key, { map { $$_ } @{$self->{query}{$key}} };
228 } else {
229 push @result, $key, undef;
230 }
231
232 # return the values as an array ref
233
234 } elsif (lc($arg_specs{$key}) eq 'array') {
235
236 if (defined($self->{query}{$key})) {
237 push @result, $key, [ map { $$_ } @{$self->{query}{$key}} ];
238 } else {
239 push @result, $key, undef;
240 }
241
242 # return a reference to the last value
243
244 } elsif (lc($arg_specs{$key}) eq 'ref') {
245
246 if (not defined($self->{query}{$key})) {
247 push @result, $key, \undef;
248 } else {
249 my $array_size = scalar(@{$self->{query}{$key}});
250 push @result, $key, $self->{query}{$key}[$array_size-1];
251  
252 }
253
254 # return the last value as a scalar
255
256 } else {
257
258 if (not defined($self->{query}{$key})) {
259 push @result, $key, undef;
260 } else {
261 my $array_size = scalar(@{$self->{query}{$key}});
262 push @result, $key, ${$self->{query}{$key}[$array_size-1]};
263 }
264
265 }
266
267 }
268
269 return @result;
270  
271 }
272  
273  
274 #######################################
275  
276 =head2 args_post:method
277  
278 Call this to retrieve ONLY the QUERY arguments stored in the object
279 (this ignores the POST arguments)
280  
281 my %query_args = $http_obj->query_args();
282  
283 or
284  
285 my %query_args = $http_obj->query_args(
286 foo => 'array',
287 bar => 'hash',
288 goo => 'ref',
289 );
290  
291 =cut
292  
293 sub args_post:method {
294  
295 my ($self, %arg_specs) = @_;
296
297 # Unless they asked for a hash, array, or ref, we
298 # always return a scalar. The actual contents of
299 # $self->{post}{foo} is always an array
300
301 my @result;
302 my %union_hash = map { $_ => 1 } (keys %arg_specs, keys %{$self->{post}});
303
304 foreach my $key (keys %union_hash) {
305
306 # return the values as a hash ref
307
308 if (lc($arg_specs{$key}) eq 'hash') {
309
310 if (defined($self->{post}{$key})) {
311 push @result, $key, { map { $$_ } @{$self->{post}{$key}} };
312 } else {
313 push @result, $key, undef;
314 }
315
316 # return the values as an array ref
317
318 } elsif (lc($arg_specs{$key}) eq 'array') {
319
320 if (defined($self->{post}{$key})) {
321 push @result, $key, [ map { $$_ } @{$self->{post}{$key}} ];
322 } else {
323 push @result, $key, undef;
324 }
325
326 # return a reference to the last value
327
328 } elsif (lc($arg_specs{$key}) eq 'ref') {
329
330 if (not defined($self->{post}{$key})) {
331 push @result, $key, \undef;
332 } else {
333 my $array_size = scalar(@{$self->{post}{$key}});
334 push @result, $key, $self->{post}{$key}[$array_size-1];
335  
336 }
337
338 # return the last value as a scalar
339
340 } else {
341
342 if (not defined($self->{post}{$key})) {
343 push @result, $key, undef;
344 } else {
345 my $array_size = scalar(@{$self->{post}{$key}});
346 push @result, $key, ${$self->{post}{$key}[$array_size-1]};
347 }
348
349 }
350
351 }
352
353 return @result;
354  
355 }
356  
357  
358 #######################################
359  
360 =head2 arg_count:method
361  
362 You can call this method with an argument name to
363 find out how many total arguments of that name
364 were submitted:
365  
366 my $count = $http_obj->arg_count('password');
367  
368 =cut
369  
370 sub arg_count:method {
371  
372 my ($self, $arg) = @_;
373
374 unless (defined $self->{args}{$arg}) { return 0; }
375 return scalar(@{$self->{args}{$arg}});
376  
377 }
378  
379  
380  
381 #######################################
382  
383 =head2 arg_headers:method
384  
385 When a mime-multipart post was submitted, you can
386 call this method with an argument name to get back
387 all the headers that came with for that argument
388 parsed as a literal hash. Like this:
389  
390 my %file_headers = $http_obj->arg_headers('file');
391  
392 =cut
393  
394 sub arg_headers:method {
395  
396 my ($self, $arg) = @_;
397
398 unless (defined $self->{arg_headers}{$arg}) {
399 return;
400 }
401
402 return ( %{$self->{arg_headers}{$arg}} );
403  
404 }
405  
406  
407  
408 #######################################
409  
410 =head2 read_get:method
411  
412 Called internally, reads the arguments from the query string
413 and adds them to $self->{args}{...}
414  
415 =cut
416  
417 sub read_get:method {
418  
419 my $self = $_[0];
420  
421 # split up and parse the query
422
423 foreach my $pair (split /&/, $ENV{QUERY_STRING}) {
424 my ($name, $value) = map { $self->url_decode($_) } split(/=/, $pair);
425 unless ($value eq '') {
426 push @{$self->{args}{$name}}, \$value;
427 push @{$self->{query}{$name}}, \$value;
428 }
429 }
430  
431 }
432  
433  
434 #######################################
435  
436 =head2 read_post:method
437  
438 Called internally, reads the arguments from STDIN
439 and adds them to $self->{args}{...}
440  
441 =cut
442  
443 sub read_post:method {
444  
445 my $self = $_[0];
446
447 # split up and parse the POST
448
449 read STDIN, my $input, $ENV{CONTENT_LENGTH};
450  
451 foreach my $pair (split /&/, $input) {
452 my ($name, $value) = map { $self->url_decode($_) } split(/=/, $pair);
453 unless ($value eq '') {
454 push @{$self->{args}{$name}}, \$value;
455 push @{$self->{post}{$name}}, \$value;
456 }
457 }
458  
459 }
460  
461  
462 #######################################
463  
464 =head2 read_mime_post:method
465  
466 Called internally, reads the arguments from STDIN,
467 translates them from mime/multipart
468 and adds them to $self->{args}{...}
469  
470 =cut
471  
472 sub read_mime_post:method {
473  
474 my ($self, $mime_boundary) = @_;
475
476 my $boundary_regex = qr/[-]+${mime_boundary}[ \r\n\t]*/;
477
478 read STDIN, my $input, $ENV{CONTENT_LENGTH};
479
480 # chop up the mime parts
481
482 foreach my $chunk (split $boundary_regex, $input) {
483
484 # snag the attributes
485
486 my %headers;
487
488 for ($chunk) {
489  
490 s/^\s+//s;
491
492 while (s/^([-a-zA-Z0-9]+)\:\s*([^\n\r]+)(\r\n|\n)//) {
493 $headers{lc($1)} = $2;
494 }
495
496 s/^\s+//s;
497 s/\s+$//s;
498
499 }
500
501 if (length($chunk) > $self->{max_mime_size}) {
502 $chunk = substr($chunk, $self->{max_mime_size});
503 }
504
505 if ($headers{'content-disposition'} =~ m/form-data\;\s*name\=\"(\w+)\"/) {
506 push @{$self->{args}{$1}}, \$chunk;
507 push @{$self->{post}{$1}}, \$chunk;
508 $self->{arg_headers}{$1} = \%headers;
509 }
510 }
511  
512 }
513  
514  
515 #######################################
516  
517 =head2 url_decode
518  
519 decodes a urlencoded string, replacing '+' with ' '
520 and %XX with the provided hex value we don;t bother to export this
521 stupid function, BTW
522  
523 =cut
524  
525 sub url_decode {
526
527 my ($self, $string) = @_;
528
529 for ($string) {
530 $string =~ tr/+/ /;
531 $string =~ s/%([\da-f][\da-f])/chr(hex($1))/egi;
532 }
533
534 if ($self->{charset} ne '') {
535 return decode($self->{charset}, $string);
536 }
537 else {
538 return $string;
539 }
540 }
541  
542  
543 #it's a library!
544  
545 1;