FakeCGI/Apache1.pm untested

filter: Statementstmt 0.0% 0 / 332 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 107 Podpod 0.0% 0 / 78 totaltotal 0.0% 0 / 678 CC 51 SCAR 21.3
CC
51
Cov
0%
_cgi_request_args
30.0
args
30.0
add
30.0
CRAP 8.4
1 use strict;
2 use warnings;
3  
4 package Dancer::Plugin::FakeCGI::Apache1;
5  
6 BEGIN {
7 foreach (
8 'Apache.pm', 'Apache/Constants.pm', 'Apache/Request.pm', 'Apache/Log.pm',
9 'Apache/Table.pm', 'Apache/Status.pm', 'mod_perl.pm'
10 ) {
11 $INC{$_} = $INC{'Dancer/Plugin/FakeCGI/Apache.pm'};
12 }
13 }
14  
15 =head1 NAME
16  
17 Dancer::Plugin::FakeCGI::Apache1 - Simply emulation mod_perl version 1 for CGI
18  
19 =head1 CONTRIBUTING
20  
21 Thanks for developer B<Nigel Wetters Gourlay> from C<HTML::Mason> with his C<Apache::Emulator>
22  
23 =cut
24  
25 require Dancer;
26 use vars qw{$AUTOLOAD};
27  
28 use Carp;
29  
30 our $VERSION = "0.2";
31  
32 our $CGI_obj = undef; # Global variable for CGI->new handle
33  
34 sub new {
35 my $class = shift;
36 my $self = {};
37 bless $self, $class;
38 return $self;
39 }
40  
41 sub AUTOLOAD {
42 my $self = shift;
43  
44 my $name = $AUTOLOAD;
45 $name =~ s/.*://; # strip fully-qualified portion
46  
47 # In CGI method which we don't want emulated is given parametter get from ENV
48 unless ($CGI_obj) {
49 croak "Can't defined FakeCGI enviroment in Dancer for function '$name' with args: @_";
50  
51 #} elsif ($name eq 'auth_type') {
52 } elsif ($name eq 'request_method') {
53 return Dancer->request->method();
54  
55 #} elsif ($name eq 'query_string') {
56 #} elsif ($name eq 'server_protocol') {
57 # return Dancer->request->protocol();
58 #} elsif ($name eq 'server_name') {
59 #} elsif ($name eq 'script_name') { # get from CGI
60 # return Dancer->request->script_name();
61 #} elsif ($name eq 'path_info') {
62 # return Dancer->request->path();
63 #} elsif ($name eq 'content_type') {
64 #} elsif ($name eq 'http') {
65 } elsif ($CGI_obj->can($name)) {
66 return $CGI_obj->$name(@_);
67 } else {
68 croak "Can't defined function '$name' with args: @_";
69 }
70  
71 }
72  
73 # cgi_request_args ($cgi, $method)
74 #
75 # This function expects to receive a C<CGI.pm> object and the request
76 # method (GET, POST, etc). Given these two things, it will return a
77 # hash in list context or a hashref in scalar context. The hash(ref)
78 # will contain all the arguments passed via the CGI request. The keys
79 # will be argument names and the values will be either scalars or array references.
80 sub _cgi_request_args {
81 my ($q, $method) = @_;
82  
83 my %args;
84  
85 # Checking that there really is no query string when the method is
86 # not POST is important because otherwise ->url_param returns a
87 # parameter named 'keywords' with a value of () (empty array).
88 # This is apparently a feature related to <ISINDEX> queries or
89 # something (see the CGI.pm) docs. It makes my head hurt. - dave
90 my @methods = $method ne 'POST' || !$ENV{QUERY_STRING} ? ('param') : ('param', 'url_param');
91  
92 foreach my $key (map { $q->$_() } @methods) {
93 next if exists $args{$key};
94 my @values = map { $q->$_($key) } @methods;
95 $args{$key} = @values == 1 ? $values[0] : \@values;
96 }
97  
98 return wantarray ? %args : \%args;
99 }
100  
101 DESTROY {
102 my $self = shift;
103 }
104  
105 package Apache;
106  
107 use vars qw{$AUTOLOAD};
108  
109 sub AUTOLOAD {
110 my $self = shift;
111  
112 my $name = $AUTOLOAD;
113 $name =~ s/.*://; # strip fully-qualified portion
114  
115 return Apache->new() if ($name eq 'request');
116 Dancer->debug("Can't defined function '$name' with args: @_");
117 }
118  
119 sub register_cleanup {
120 my ($self, $code) = @_;
121 push @{$$self{'HANDLERS'}{'PerlCleanupHandler'}}, $code;
122 }
123  
124 # Faked method for Apache->read is a built-in function, and so can do magic.
125 sub read() {
126 my $self = shift;
127 my $buf = \$_[0]; # Must be setted as scalarref
128 shift;
129  
130 my ($len, $offset) = @_;
131 no strict 'refs';
132 $$buf = substr(Dancer->request->body(), $offset, $len);
133 return length($$buf);
134 }
135  
136 # Destroy
137 DESTROY {
138 my $self = shift;
139 foreach my $code (@{$$self{'HANDLERS'}{'PerlCleanupHandler'}}) {
140 no strict 'refs';
141 &$code();
142 }
143 }
144  
145 ##################################################################################
146  
147 sub new {
148 my $class = shift;
149 my %p = @_;
150  
151 return bless {
152  
153 #query => $p{cgi} || CGI->new,
154 query => Dancer::Plugin::FakeCGI::Apache1->new,
155 headers_out => Apache::Table->new,
156 err_headers_out => Apache::Table->new,
157 pnotes => {},
158 }, $class;
159 }
160  
161 # CGI request are _always_ main, and there is never a previous or a next
162 # internal request.
163 sub main { }
164 sub prev { }
165 sub next { }
166 sub is_main { 1 }
167 sub is_initial_req { 1 }
168  
169 # What to do with this?
170 # sub allowed {}
171  
172 sub method {
173 $_[0]->{query}->request_method;
174 }
175  
176 # There mut be a mapping for this.
177 # sub method_number {}
178  
179 # Can CGI.pm tell us this?
180 # sub bytes_sent {0}
181  
182 # The request line sent by the client." Poached from Apache::Emulator.
183 sub the_request {
184 my $self = shift;
185 $self->{the_request} ||= join ' ', $self->method,
186 ( $self->{query}->query_string
187 ? $self->uri . '?' . $self->{query}->query_string
188 : $self->uri
189 ),
190 $self->{query}->server_protocol;
191 }
192  
193 # Is CGI ever a proxy request?
194 # sub proxy_req {}
195  
196 sub header_only { $_[0]->method eq 'HEAD' }
197  
198 sub protocol { $ENV{SERVER_PROTOCOL} || 'HTTP/1.0' }
199  
200 sub hostname { $_[0]->{query}->server_name }
201  
202 # Fake it by just giving the current time.
203 sub request_time { time }
204  
205 sub uri {
206 my $self = shift;
207  
208 $self->{uri} ||= $self->{query}->script_name . $self->path_info || '';
209 }
210  
211 # Is this available in CGI?
212 # sub filename {}
213  
214 # "The $r->location method will return the path of the
215 # <Location> section from which the current "Perl*Handler"
216 # is being called." This is irrelevant, I think.
217 # sub location {}
218  
219 sub path_info { $_[0]->{query}->path_info }
220  
221 sub args {
222 my $self = shift;
223  
224 my %all_params = Dancer->request->params;
225 delete($all_params{"splat"}); # Delete 'splat' from params. Dancer put to this if use 'splat' function
226  
227 if (@_) {
228  
229 # Assign args here.
230 }
231  
232 return unless keys %all_params;
233  
234 # Redirected when is only method 'GET' and not existed CONTENT_TYPE
235 # we must return params as string with separator & or ;
236 if ($ENV{'REQUEST_METHOD'} eq 'GET' && !$ENV{'CONTENT_TYPE'}) {
237 my @a = ();
238 while (my ($k, $d) = each %all_params) {
239 push(@a, $k . "=" . ($d || ""));
240 }
241 return join("&", @a);
242 }
243  
244 return %all_params if wantarray;
245 return \%all_params;
246  
247 #return $self->{query}->Vars unless wantarray;
248  
249 # Do more here to return key => arg values.
250 }
251  
252 sub headers_in {
253 my $self = shift;
254  
255 # Create the headers table if necessary. Decided how to build it based on
256 # information here:
257 # http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html#6.1
258 #
259 # Try to get as much info as possible from CGI.pm, which has
260 # workarounds for things like the IIS PATH_INFO bug.
261 #
262 $self->{headers_in} ||= Apache::Table->new(
263 'Authorization' => $self->{query}->auth_type, # No credentials though.
264  
265 #'Cookie' => $ENV{HTTP_COOKIE} || $ENV{COOKIE},
266 'Content-Length' => $ENV{CONTENT_LENGTH},
267 'Content-Type' => (
268 $self->{query}->can('content_type')
269 ? $self->{query}->content_type
270 : $ENV{CONTENT_TYPE}
271 ),
272  
273 # Convert HTTP environment variables back into their header names.
274 map {
275 my $k = ucfirst lc;
276 $k =~ s/_(.)/-\u$1/g;
277 ($k => $self->{query}->http($_))
278 } grep {
279 s/^HTTP_//
280 } keys %ENV
281 );
282  
283 # Give 'em the hash list of the hash table.
284 return wantarray ? %{$self->{headers_in}} : $self->{headers_in};
285 }
286  
287 sub header_in {
288 my ($self, $header) = (shift, shift);
289 my $h = $self->headers_in;
290 return @_ ? $h->set($header, shift) : $h->get($header);
291 }
292  
293 # The $r->content method will return the entity body
294 # read from the client, but only if the request content
295 # type is "application/x-www-form-urlencoded". When
296 # called in a scalar context, the entire string is
297 # returned. When called in a list context, a list of
298 # parsed key => value pairs are returned. *NOTE*: you
299 # can only ask for this once, as the entire body is read
300 # from the client.
301 # Not sure what to do with this one.
302 # sub content {}
303  
304 # I think this may be irrelevant under CGI.
305 # sub read {}
306  
307 # Use LWP?
308 sub get_remote_host { }
309 sub get_remote_logname { }
310  
311 sub http_header {
312 my $self = shift;
313 my $h = $self->headers_out;
314 my $e = $self->err_headers_out;
315 my $method = exists $h->{Location}
316 || exists $e->{Location} ? 'redirect' : 'header';
317  
318 #return $self->{query}->$method(tied(%$h)->cgi_headers, tied(%$e)->cgi_headers);
319 return "";
320 }
321  
322 sub send_http_header {
323 my $self = shift;
324  
325 print STDOUT $self->http_header;
326  
327 $self->{http_header_sent} = 1;
328 }
329  
330 sub http_header_sent { shift->{http_header_sent} }
331  
332 # How do we know this under CGI?
333 # sub get_basic_auth_pw {}
334 # sub note_basic_auth_failure {}
335  
336 # I think that this just has to be empty.
337 sub handler { }
338  
339 sub notes {
340 my ($self, $key) = (shift, shift);
341 $self->{notes} ||= Apache::Table->new;
342 return wantarray ? %{$self->{notes}} : $self->{notes}
343 unless defined $key;
344 return $self->{notes}{$key} = "$_[0]" if @_;
345 return $self->{notes}{$key};
346 }
347  
348 sub pnotes {
349 my ($self, $key) = (shift, shift);
350 return wantarray ? %{$self->{pnotes}} : $self->{pnotes}
351 unless defined $key;
352 return $self->{pnotes}{$key} = $_[0] if @_;
353 return $self->{pnotes}{$key};
354 }
355  
356 sub subprocess_env {
357 my ($self, $key) = (shift, shift);
358 unless (defined $key) {
359 $self->{subprocess_env} = Apache::Table->new(%ENV);
360 return wantarray
361 ? %{$self->{subprocess_env}}
362 : $self->{subprocess_env};
363  
364 }
365 $self->{subprocess_env} ||= Apache::Table->new(%ENV);
366 return $self->{subprocess_env}{$key} = "$_[0]" if @_;
367 return $self->{subprocess_env}{$key};
368 }
369  
370 sub content_type {
371 shift->header_out('Content-Type', @_);
372 }
373  
374 sub content_encoding {
375 shift->header_out('Content-Encoding', @_);
376 }
377  
378 sub content_languages {
379 my ($self, $langs) = @_;
380 return unless $langs;
381 my $h = shift->headers_out;
382 for my $l (@$langs) {
383 $h->add('Content-Language', $l);
384 }
385 }
386  
387 sub status {
388 shift->header_out('Status', @_);
389 }
390  
391 sub status_line {
392  
393 # What to do here? Should it be managed differently than status?
394 my $self = shift;
395 if (@_) {
396 my $status = shift =~ /^(\d+)/;
397 return $self->header_out('Status', $status);
398 }
399 return $self->header_out('Status');
400 }
401  
402 sub headers_out {
403 my $self = shift;
404 return wantarray ? %{$self->{headers_out}} : $self->{headers_out};
405 }
406  
407 sub header_out {
408 my ($self, $header) = (shift, shift);
409 my $h = $self->headers_out;
410 return @_ ? $h->set($header, shift) : $h->get($header);
411 }
412  
413 sub err_headers_out {
414 my $self = shift;
415 return wantarray ? %{$self->{err_headers_out}} : $self->{err_headers_out};
416 }
417  
418 sub err_header_out {
419 my ($self, $err_header) = (shift, shift);
420 my $h = $self->err_headers_out;
421 return @_ ? $h->set($err_header, shift) : $h->get($err_header);
422 }
423  
424 sub no_cache {
425 my $self = shift;
426 $self->header_out(Pragma => 'no-cache');
427 $self->header_out('Cache-Control' => 'no-cache');
428 }
429  
430 sub print {
431 print @_;
432 }
433  
434 sub send_fd {
435 my ($self, $fd) = @_;
436 local $_;
437  
438 print STDOUT while defined($_ = <$fd>);
439 }
440  
441 #sub print {
442 # my $self = shift;
443 # foreach my $arg (@_) {
444 # $arg = $$arg if ref($arg) eq 'SCALAR';
445 # }
446 # CORE::print @_;
447 #}
448 #
449 #*CORE::GLOBAL::print = \&print;
450  
451 #sub send_fd {
452 # my ($self, $fh) = @_;
453 # my $buf;
454 # while (CORE::read($fh,$buf,16384) > 0) {
455 # CORE::print $buf;
456 # }
457 #}
458  
459 sub rflush { flush STDOUT; flush STDERR; }
460  
461 # Should this perhaps throw an exception?
462 # sub internal_redirect {}
463 # sub internal_redirect_handler {}
464  
465 # Do something with ErrorDocument?
466 # sub custom_response {}
467  
468 # I think we'ev made this essentially the same thing.
469 BEGIN {
470 local $^W;
471 *send_cgi_header = \&send_http_header;
472 }
473  
474 # Does CGI support logging?
475 # sub log_reason {}
476 # sub log_error {}
477 sub warn {
478 shift;
479 Dancer->warn(@_);
480 }
481  
482 sub params {
483 my $self = shift;
484  
485 return Dancer::Plugin::FakeCGI::Apache1::_cgi_request_args($self->query, $self->query->request_method);
486 }
487  
488 package Apache::Constants;
489  
490 use vars qw (%EXPORT_TAGS @EXPORT_OK $EXPORT @ISA);
491 require Exporter;
492 @ISA = qw(Exporter);
493  
494 my @common = qw(OK
495 DECLINED
496 DONE
497 NOT_FOUND
498 FORBIDDEN
499 AUTH_REQUIRED
500 SERVER_ERROR);
501  
502 sub OK { 0 }
503 sub DECLINED { -1 }
504 sub DONE { -2 }
505 sub NOT_FOUND { 404 }
506 sub FORBIDDEN { 403 }
507 sub AUTH_REQUIRED { 401 }
508 sub SERVER_ERROR { 500 }
509  
510 my (@methods) = qw(M_CONNECT
511 M_DELETE
512 M_GET
513 M_INVALID
514 M_OPTIONS
515 M_POST
516 M_PUT
517 M_TRACE
518 M_PATCH
519 M_PROPFIND
520 M_PROPPATCH
521 M_MKCOL
522 M_COPY
523 M_MOVE
524 M_LOCK
525 M_UNLOCK
526 METHODS);
527  
528 my (@options) = qw(OPT_NONE OPT_INDEXES OPT_INCLUDES
529 OPT_SYM_LINKS OPT_EXECCGI OPT_UNSET OPT_INCNOEXEC
530 OPT_SYM_OWNER OPT_MULTI OPT_ALL);
531  
532 my (@server) = qw(MODULE_MAGIC_NUMBER
533 SERVER_VERSION SERVER_BUILT);
534  
535 my (@response) = qw(DOCUMENT_FOLLOWS
536 MOVED
537 REDIRECT
538 USE_LOCAL_COPY
539 BAD_REQUEST
540 BAD_GATEWAY
541 RESPONSE_CODES
542 NOT_IMPLEMENTED
543 NOT_AUTHORITATIVE
544 CONTINUE);
545  
546 #define DOCUMENT_FOLLOWS HTTP_OK
547 #define PARTIAL_CONTENT HTTP_PARTIAL_CONTENT
548 #define MULTIPLE_CHOICES HTTP_MULTIPLE_CHOICES
549 #define MOVED HTTP_MOVED_PERMANENTLY
550 #define REDIRECT HTTP_MOVED_TEMPORARILY
551 #define USE_LOCAL_COPY HTTP_NOT_MODIFIED
552 #define BAD_REQUEST HTTP_BAD_REQUEST
553 #define AUTH_REQUIRED HTTP_UNAUTHORIZED
554 #define FORBIDDEN HTTP_FORBIDDEN
555 #define NOT_FOUND HTTP_NOT_FOUND
556 #define METHOD_NOT_ALLOWED HTTP_METHOD_NOT_ALLOWED
557 #define NOT_ACCEPTABLE HTTP_NOT_ACCEPTABLE
558 #define LENGTH_REQUIRED HTTP_LENGTH_REQUIRED
559 #define PRECONDITION_FAILED HTTP_PRECONDITION_FAILED
560 #define SERVER_ERROR HTTP_INTERNAL_SERVER_ERROR
561 #define NOT_IMPLEMENTED HTTP_NOT_IMPLEMENTED
562 #define BAD_GATEWAY HTTP_BAD_GATEWAY
563 #define VARIANT_ALSO_VARIES HTTP_VARIANT_ALSO_VARIES
564  
565 my (@satisfy) = qw(SATISFY_ALL SATISFY_ANY SATISFY_NOSPEC);
566  
567 my (@remotehost) = qw(REMOTE_HOST
568 REMOTE_NAME
569 REMOTE_NOLOOKUP
570 REMOTE_DOUBLE_REV);
571  
572 use constant REMOTE_HOST => 0;
573 use constant REMOTE_NAME => 1;
574 use constant REMOTE_NOLOOKUP => 2;
575 use constant REMOTE_DOUBLE_REV => 3;
576  
577 my (@http) = qw(HTTP_OK
578 HTTP_MOVED_TEMPORARILY
579 HTTP_MOVED_PERMANENTLY
580 HTTP_METHOD_NOT_ALLOWED
581 HTTP_NOT_MODIFIED
582 HTTP_UNAUTHORIZED
583 HTTP_FORBIDDEN
584 HTTP_NOT_FOUND
585 HTTP_BAD_REQUEST
586 HTTP_INTERNAL_SERVER_ERROR
587 HTTP_NOT_ACCEPTABLE
588 HTTP_NO_CONTENT
589 HTTP_PRECONDITION_FAILED
590 HTTP_SERVICE_UNAVAILABLE
591 HTTP_VARIANT_ALSO_VARIES);
592  
593 use constant HTTP_OK => 200;
594 use constant HTTP_MOVED_TEMPORARILY => 302;
595 use constant HTTP_MOVED_PERMANENTLY => 301;
596 use constant HTTP_METHOD_NOT_ALLOWED => 405;
597 use constant HTTP_NOT_MODIFIED => 304;
598 use constant HTTP_UNAUTHORIZED => 401;
599 use constant HTTP_FORBIDDEN => 403;
600 use constant HTTP_NOT_FOUND => 404;
601 use constant HTTP_BAD_REQUEST => 400;
602 use constant HTTP_INTERNAL_SERVER_ERROR => 500;
603 use constant HTTP_NOT_ACCEPTABLE => 406;
604 use constant HTTP_NO_CONTENT => 204;
605 use constant HTTP_PRECONDITION_FAILED => 412;
606 use constant HTTP_SERVICE_UNAVAILABLE => 503;
607 use constant HTTP_VARIANT_ALSO_VARIES => 506;
608  
609 my (@config) = qw(DECLINE_CMD);
610 my (@types) = qw(DIR_MAGIC_TYPE);
611 my (@override) = qw(
612 OR_NONE
613 OR_LIMIT
614 OR_OPTIONS
615 OR_FILEINFO
616 OR_AUTHCFG
617 OR_INDEXES
618 OR_UNSET
619 OR_ALL
620 ACCESS_CONF
621 RSRC_CONF);
622 my (@args_how) = qw(
623 RAW_ARGS
624 TAKE1
625 TAKE2
626 ITERATE
627 ITERATE2
628 FLAG
629 NO_ARGS
630 TAKE12
631 TAKE3
632 TAKE23
633 TAKE123);
634  
635 my $rc = [@common, @response];
636  
637 %EXPORT_TAGS = (
638 common => \@common,
639 config => \@config,
640 response => $rc,
641 http => \@http,
642 options => \@options,
643 methods => \@methods,
644 remotehost => \@remotehost,
645 satisfy => \@satisfy,
646 server => \@server,
647 types => \@types,
648 args_how => \@args_how,
649 override => \@override,
650  
651 #deprecated
652 response_codes => $rc,
653 );
654  
655 @EXPORT_OK = (@response, @http, @options, @methods, @remotehost, @satisfy, @server, @config, @types, @args_how, @override,);
656  
657 *EXPORT = \@common;
658  
659 package Apache::TableHash;
660  
661 sub TIEHASH {
662 my $class = shift;
663 return bless {}, ref $class || $class;
664 }
665  
666 sub _canonical_key {
667 my $key = lc shift;
668  
669 # CGI really wants a - before each header
670 return substr($key, 0, 1) eq '-' ? $key : "-$key";
671 }
672  
673 sub STORE {
674 my ($self, $key, $value) = @_;
675 $self->{_canonical_key $key} = [$key => ref $value ? "$value" : $value];
676 }
677  
678 sub add {
679 my ($self, $key) = (shift, shift);
680 return unless defined $_[0];
681 my $value = ref $_[0] ? "$_[0]" : $_[0];
682 my $ckey = _canonical_key $key;
683 if (exists $self->{$ckey}) {
684 if (ref $self->{$ckey}[1]) {
685 push @{$self->{$ckey}[1]}, $value;
686 } else {
687 $self->{$ckey}[1] = [$self->{$ckey}[1], $value];
688 }
689 } else {
690 $self->{$ckey} = [$key => $value];
691 }
692 }
693  
694 sub DELETE {
695 my ($self, $key) = @_;
696 my $ret = delete $self->{_canonical_key $key};
697 return $ret->[1];
698 }
699  
700 sub FETCH {
701 my ($self, $key) = @_;
702  
703 # Grab the values first so that we don't autovivicate the key.
704 my $val = $self->{_canonical_key $key} or return;
705 if (my $ref = ref $val->[1]) {
706 return unless $val->[1][0];
707  
708 # Return the first value only.
709 return $val->[1][0];
710 }
711 return $val->[1];
712 }
713  
714 sub get {
715 my ($self, $key) = @_;
716 my $ckey = _canonical_key $key;
717 return unless exists $self->{$ckey};
718 return $self->{$ckey}[1] unless ref $self->{$ckey}[1];
719 return wantarray ? @{$self->{$ckey}[1]} : $self->{$ckey}[1][0];
720 }
721  
722 sub CLEAR {
723 %{shift()} = ();
724 }
725  
726 sub EXISTS {
727 my ($self, $key) = @_;
728 return exists $self->{_canonical_key $key};
729 }
730  
731 sub FIRSTKEY {
732 my $self = shift;
733  
734 # Reset perl's iterator.
735 keys %$self;
736  
737 # Get the first key via perl's iterator.
738 my $first_key = each %$self;
739 return undef unless defined $first_key;
740 return $self->{$first_key}[0];
741 }
742  
743 sub NEXTKEY {
744 my ($self, $nextkey) = @_;
745  
746 # Get the next key via perl's iterator.
747 my $next_key = each %$self;
748 return undef unless defined $next_key;
749 return $self->{$next_key}[0];
750 }
751  
752 sub cgi_headers {
753 my $self = shift;
754 map { $_ => $self->{$_}[1] } keys %$self;
755 }
756  
757 package Apache::Table;
758  
759 sub new {
760 my $class = shift;
761 my $self = {};
762 tie %{$self}, 'Apache::TableHash';
763 %$self = @_ if @_;
764 return bless $self, ref $class || $class;
765 }
766  
767 sub set {
768 my ($self, $header, $value) = @_;
769 defined $value ? $self->{$header} = $value : delete $self->{$header};
770 }
771  
772 sub unset {
773 my $self = shift;
774 delete $self->{shift()};
775 }
776  
777 sub add {
778 tied(%{shift()})->add(@_);
779 }
780  
781 sub clear {
782 %{shift()} = ();
783 }
784  
785 sub get {
786 tied(%{shift()})->get(@_);
787 }
788  
789 sub merge {
790 my ($self, $key, $value) = @_;
791 if (defined $self->{$key}) {
792 $self->{$key} .= ',' . $value;
793 } else {
794 $self->{$key} = "$value";
795 }
796 }
797  
798 sub do {
799 my ($self, $code) = @_;
800 while (my ($k, $val) = each %$self) {
801 for my $v (ref $val ? @$val : $val) {
802 return unless $code->($k => $v);
803 }
804 }
805 }
806  
807 1;
808 __END__