blib/lib/Waft.pm

filter: Statementstmt 84.6% 11 / 13 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 85.0% 17 / 20 CC 2 SCAR 3.4
CC
2
Cov
80%
BEGIN
2.1
BEGIN
1.3
BEGIN
1.0
CRAP 1.4
↑ Summary
1 package Waft;
2  
3 23 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶BEGIN { eval { require warnings } ? 'warnings'->import : ( $^W = 1 ) }
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require warnings } } ? : 23 0 23
7  
8 0 ▶use CGI qw( -no_debug );
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Fcntl qw( :DEFAULT );
10 use Symbol;
11 require File::Spec;
12  
13 $VERSION = '0.99_90';
14 $VERSION = eval $VERSION;
15  
16 $Waft::Backword_compatible_version = $VERSION < 1.0 ? 1.0 : $VERSION;
17 @Waft::Allow_template_file_exts = qw( .html .css .js .txt );
18 $Waft::Cache = 1;
19 $Waft::Correct_NEXT_DISTINCT = 1;
20  
21 sub import {
22 my ($class, @mixins) = @_;
23  
24 if ( defined $mixins[0] and $mixins[0] eq 'with' ) {
25 shift @mixins;
26 }
27  
28 return if @mixins == 0;
29  
30 my $caller = caller;
31 my @bases;
32  
33 BASE:
34 for my $base ( @mixins, $class ) {
35 if ( $base =~ /\A :: /xms ) {
36 $base = 'Waft' . $base;
37 }
38  
39 next BASE if $caller->isa($base);
40  
41 eval qq{ require $base };
42  
43 if ( $@ ) {
44 CORE::die($@) if $@ !~ /\ACan't locate .*? at \(eval /;
45  
46 if ( not do { no strict 'refs'; %{ "${base}::" } } ) {
47 require Carp;
48  
49 Carp::croak($@);
50 }
51 }
52  
53 push @bases, $base;
54 }
55  
56 no strict 'refs';
57 push @{ "${caller}::ISA" }, @bases;
58  
59 return;
60 }
61  
62 {
63 my %Backword_compatible_version_of;
64  
65 sub set_waft_backword_compatible_version {
66 my ($class, $backword_compatible_version) = @_;
67  
68 $class->die('This is class method') if $class->blessed;
69  
70 $Backword_compatible_version_of{$class}
71 = $backword_compatible_version;
72  
73 return;
74 }
75  
76 sub BCV {
77 my ($self) = @_;
78  
79 my $class = $self->blessed || $self;
80  
81 my $backword_compatible_version
82 = $Backword_compatible_version_of{$class}
83 || $Waft::Backword_compatible_version;
84  
85 return $backword_compatible_version;
86 }
87 }
88  
89 sub get_waft_backword_compatible_version { shift->BCV(@_) }
90  
91 eval q{ use Scalar::Util qw( blessed refaddr ); 1 } or do {
92 *blessed = *blessed = sub {
93 my ($self) = @_;
94  
95 my $blessed = ref $self;
96  
97 return $blessed;
98 };
99  
100 *refaddr = *refaddr = sub {
101 my ($self) = @_;
102  
103 my $blessed_class = ref $self
104 or return;
105  
106 bless $self, __PACKAGE__;
107 my $refaddr = "$self";
108  
109 bless $self, $blessed_class;
110  
111 return $refaddr;
112 };
113 };
114  
115 sub die {
116 my ($self, @args) = @_;
117  
118 $self->dont_trust_me( sub { CORE::die(@_) }, @args ) if $self->BCV < 1.0;
119  
120 $self->dont_trust_me( sub { CORE::die(q{Error: }, @_) }, @args )
121 if not defined wantarray;
122  
123 $self->dont_trust_me( sub { CORE::warn(q{Error: }, @_) }, @args );
124  
125 return 'internal_server_error', @args if not $self->responded;
126 return @args;
127 }
128  
129 sub dont_trust_me {
130 my ($self, $coderef, @args) = @_;
131  
132 my $class = $self->blessed || $self;
133  
134 my $back;
135 CALLER:
136 while ( my @caller = caller $back++ ) {
137 my ($package, $filename, $line) = @caller;
138  
139 next CALLER if $package ne $class and $self->isa($package);
140  
141 if ( not grep { defined and length >= 1 } @args ) {
142 push @args, q{something's wrong};
143 }
144  
145 push @args, " at $filename line $line.\n";
146  
147 last CALLER;
148 }
149  
150 return $coderef->(@args);
151 }
152  
153 sub use_utf8 {
154 my ($class) = @_;
155  
156 $class->set_using_utf8(1);
157  
158 return;
159 }
160  
161 {
162 my %Using_utf8;
163  
164 sub set_using_utf8 {
165 my ($class, $using_utf8) = @_;
166  
167 $class->die('This is class method') if $class->blessed;
168  
169 return if $using_utf8 and not $class->can_use_utf8;
170  
171 $Using_utf8{$class} = $using_utf8;
172  
173 return;
174 }
175  
176 sub get_using_utf8 {
177 my ($self) = @_;
178  
179 if ($self->BCV < 0.53) {
180 return $self->stash->{use_utf8} if $self->blessed;
181 }
182  
183 my $class = $self->blessed || $self;
184  
185 my $using_utf8 = $Using_utf8{$class};
186  
187 return $using_utf8;
188 }
189 }
190  
191 sub can_use_utf8 {
192 my ($self) = @_;
193  
194 eval { require 5.008001 };
195 return 1 if not $@;
196 $self->warn($@);
197  
198 return;
199 }
200  
201 sub warn {
202 my ($self, @args) = @_;
203  
204 if ($self->BCV < 1.0) {
205 $self->dont_trust_me( sub { CORE::warn(@_) }, @args );
206  
207 return;
208 }
209  
210 $self->dont_trust_me( sub { CORE::warn(q{Warning: }, @_) }, @args );
211  
212 return;
213 }
214  
215 {
216 my %Allow_template_file_exts_arrayref_of;
217  
218 sub set_allow_template_file_exts {
219 my ($class, @allow_template_file_exts) = @_;
220  
221 $class->die('This is class method') if $class->blessed;
222  
223 $Allow_template_file_exts_arrayref_of{$class}
224 = \@allow_template_file_exts;
225  
226 return;
227 }
228  
229 sub get_allow_template_file_exts {
230 my $class = $_[1] || $_[0];
231  
232 return @{ $Allow_template_file_exts_arrayref_of{$class} }
233 if exists $Allow_template_file_exts_arrayref_of{$class};
234  
235 my $get_allowed_exts = do {
236 no strict 'refs';
237 *{ "${class}::allow_template_file_exts" }{CODE};
238 };
239  
240 my @allow_template_file_exts
241 = $get_allowed_exts ? $get_allowed_exts->($class)
242 : @Waft::Allow_template_file_exts;
243  
244 $Allow_template_file_exts_arrayref_of{$class}
245 = \@allow_template_file_exts;
246  
247 return @allow_template_file_exts;
248 }
249 }
250  
251 {
252 my %Default_content_type_of;
253  
254 sub set_default_content_type {
255 my ($class, $default_content_type) = @_;
256  
257 $class->die('This is class method') if $class->blessed;
258  
259 $Default_content_type_of{$class} = $default_content_type;
260  
261 return;
262 }
263  
264 sub get_default_content_type {
265 my ($self) = @_;
266  
267 my $class = $self->blessed || $self;
268  
269 my $default_content_type = $Default_content_type_of{$class}
270 || 'text/html';
271  
272 return $default_content_type;
273 }
274 }
275  
276 sub waft {
277 my ($self, @args) = @_;
278  
279 if ($self->BCV < 0.53) {
280 if ( not $self->blessed ) {
281 ($self, @args) = $self->new(@args);
282 }
283  
284 $self->init_base_url;
285 $self->init_binmode;
286 $self->_load_query_param;
287 }
288  
289 if ( not $self->blessed ) {
290 $self = $self->new->initialize;
291 }
292  
293 my @return_values = $self->controller(@args);
294  
295 return wantarray ? ($self, @return_values) : $self;
296 }
297  
298 sub new {
299 my ($class) = @_;
300  
301 $class->die('This is class method') if $class->blessed;
302  
303 my $self;
304 tie %$self, 'Waft::Object';
305 bless $self, $class;
306  
307 if ($class->BCV < 1.0) {
308 $class->define_subs_for_under_0_99x;
309 }
310  
311 if ($class->BCV < 0.53) {
312 ( undef, my @args ) = @_;
313  
314 $class->define_subs_for_under_0_52x;
315  
316 my $self;
317 tie %$self, 'Waft::Object';
318 bless $self, $class;
319  
320 my ($option_hashref, @return_values);
321  
322 if (ref $args[0] eq 'HASH') {
323 ($option_hashref, @return_values) = @args;
324 }
325 else {
326 $option_hashref = { @args };
327 }
328  
329 $option_hashref->{content_type} ||= $self->get_default_content_type;
330 $option_hashref->{headers} ||= [];
331  
332 my $stash = $self->stash;
333  
334 %$stash = %$option_hashref;
335  
336 if ($stash->{use_utf8}) {
337 $self->can_use_utf8; # carp in this method if cannot 'use utf8'
338 }
339  
340 return wantarray ? ($self, @return_values) : $self;
341 }
342  
343 return $self;
344 }
345  
346 sub initialize {
347 my ($self) = @_;
348  
349 $self->initialize_base_url;
350 $self->initialize_page;
351 $self->initialize_values;
352 $self->initialize_action;
353 $self->initialize_response_headers;
354 $self->initialize_binmode;
355  
356 return $self;
357 }
358  
359 sub initialize_base_url {
360 my ($self) = @_;
361  
362 my $base_url = $self->make_base_url;
363 $self->set_base_url($base_url);
364  
365 return;
366 }
367  
368 sub make_base_url {
369 my ($self) = @_;
370  
371 my $updir = $ENV{PATH_INFO} || q{};
372 my $updir_count = $updir =~ s{ /[^/]* }{../}gx;
373  
374 my $url;
375  
376 if ( defined $ENV{REQUEST_URI}
377 and $ENV{REQUEST_URI} =~ /\A ([^?]+) /xms
378 ) {
379 $url = $1;
380  
381 for (1 .. $updir_count) {
382 $url =~ s{ /[^/]* \z}{}x;
383 }
384 }
385 else {
386 $url = $ENV{SCRIPT_NAME} || $self->get_script_basename;
387 }
388  
389 my $base_url = $url =~ m{ ([^/]+) \z}xms ? "$updir$1"
390 : './';
391  
392 return $base_url;
393 }
394  
395 sub get_script_basename {
396 my ($self) = @_;
397  
398 return $FindBin::Script if eval { FindBin::again(); 1 };
399  
400 delete $INC{'FindBin.pm'};
401 require FindBin;
402  
403 return $FindBin::Script;
404 }
405  
406 sub set_base_url {
407 my ($self, $base_url) = @_;
408  
409 if ($self->BCV < 0.53) {
410 $self->stash->{url} = $base_url;
411 }
412  
413 $self->stash->{base_url} = $base_url;
414  
415 return;
416 }
417  
418 {
419 my %Stash;
420  
421 sub stash { $Stash{ $_[0]->refaddr or $_[0] }{ $_[1] or caller } ||= {} }
422  
423 sub DESTROY {
424 my ($self) = @_;
425  
426 my $ident = $self->refaddr;
427 delete $Stash{$ident};
428  
429 return;
430 }
431 }
432  
433 sub initialize_page {
434 my ($self) = @_;
435  
436 my $page = $self->is_submitted ? $self->cgi->param('s')
437 : $self->cgi->param('p');
438  
439 if ( $self->get_using_utf8 and defined $page ) {
440 utf8::encode($page);
441 }
442  
443 $page = $self->fix_and_validate_page($page);
444 $self->set_page( defined $page ? $page : 'default.html' );
445  
446 return;
447 }
448  
449 sub is_submitted {
450 my ($self) = @_;
451  
452 my $is_submitted = defined $self->cgi->param('s');
453  
454 return $is_submitted;
455 }
456  
457 sub cgi {
458 my ($self) = @_;
459  
460 my $query = ( $self->stash->{query} ||= $self->create_query_obj );
461  
462 return $query;
463 }
464  
465 sub create_query_obj {
466 my ($self) = @_;
467  
468 my $query = CGI->new;
469  
470 if ($self->get_using_utf8) {
471 eval qq{\n# line } . __LINE__ . q{ "} . __FILE__ . qq{"\n} . q{
472 use CGI 3.21 qw( -utf8 ); # -utf8 pragma is for 3.31 or later
473 };
474  
475 if ($@) {
476 $self->warn($@);
477 }
478 elsif ($query->VERSION < 3.31) {
479 $query->charset('utf-8');
480 }
481 }
482  
483 return $query;
484 }
485  
486 sub fix_and_validate_page {
487 my ($self, $page) = @_;
488  
489 return if not defined $page;
490  
491 $page =~ m{\A
492 (?! .* [/\\]{2,} )
493 (?! .* (?<! [^/\\] ) \.\.? (?! [^/\\] ) )
494 (?! .* :: )
495 (.+) \z}xms;
496 my $untainted_page = $1;
497  
498 return $untainted_page
499 if defined $untainted_page
500 and not File::Spec->file_name_is_absolute($untainted_page)
501 and not $untainted_page eq 'CURRENT'
502 and not $untainted_page eq 'TEMPLATE'
503 and not $self->to_page_id($untainted_page) =~ / __indirect \z/xms;
504  
505 $self->warn(qq{Invalid requested page "$page"});
506  
507 return;
508 }
509  
510 sub to_page_id {
511 my (undef, $page) = @_;
512  
513 my $page_id = $page;
514 $page_id =~ s{ \.[^/:\\]* \z}{}xms;
515 $page_id =~ tr/0-9A-Za-z_/_/c;
516  
517 return $page_id;
518 }
519  
520 sub set_page {
521 my ($self, $page) = @_;
522  
523 $self->stash->{page} = $page;
524  
525 return;
526 }
527  
528 sub initialize_values {
529 my ($self, $joined_values) = @_;
530  
531 $self->clear_values;
532  
533 $joined_values ||= $self->cgi->param('v');
534  
535 return if not defined $joined_values;
536  
537 my @key_values_pairs = split /\x20/, $joined_values, -1;
538  
539 KEY_VALUES_PAIR:
540 for my $key_values_pair (@key_values_pairs) {
541 my ($key, @values) = split /-/, $key_values_pair, -1;
542  
543 $key = $self->unescape_space_percent_hyphen($key);
544 @values = $self->unescape_space_percent_hyphen(@values);
545  
546 if ($key eq 'ALL_VALUES') {
547 $self->warn(q{Invalid init value 'ALL_VALUES'});
548  
549 next KEY_VALUES_PAIR;
550 }
551  
552 $self->set_values( $key => @values );
553 }
554  
555 return;
556 }
557  
558 sub clear_values {
559 my ($self) = @_;
560  
561 %{ $self->value_hashref } = ();
562  
563 return;
564 }
565  
566 sub value_hashref { tied %{ $_[0] } }
567  
568 sub unescape_space_percent_hyphen {
569 my (undef, @values) = @_;
570  
571 for my $value (@values) {
572 $value =~ s/ %(2[05d]) / pack 'H2', $1 /egxms;
573 }
574  
575 return wantarray ? @values : $values[0];
576 }
577  
578 sub set_values {
579 my ($self, $key, @values) = @_;
580  
581 @{ $self->value_hashref->{$key} } = @values;
582  
583 return;
584 }
585  
586 sub initialize_action {
587 my ($self) = @_;
588  
589 my $action = $self->find_first_action;
590 $self->set_action( defined $action ? $action : 'direct' );
591  
592 return;
593 }
594  
595 sub find_first_action {
596 my ($self) = @_;
597  
598 return if not $self->is_submitted;
599  
600 my $page_id = $self->to_page_id($self->get_page);
601 my $global_action;
602  
603 my @param_names = $self->cgi->param;
604 PARAM_NAME:
605 for my $param_name ( @param_names ) {
606 my $action_id = $self->to_action_id($param_name);
607  
608 if ($self->BCV < 0.53) {
609 next PARAM_NAME if $action_id =~ /\A global_ /xms;
610 }
611  
612 next PARAM_NAME if $action_id =~ /(?: \A | _ ) direct \z/xms
613 or $action_id =~ /(?: \A | _ ) indirect \z/xms
614 or $action_id =~ /\A global__ /xms;
615  
616 return $param_name if $self->can("__${page_id}__$action_id");
617  
618 next PARAM_NAME if defined $global_action;
619  
620 if ($self->BCV < 0.53) {
621 if ( $self->can("global_$action_id") ) {
622 $global_action = "global_$param_name";
623 }
624  
625 next PARAM_NAME;
626 }
627  
628 if ( $self->can("global__$action_id") ) {
629 $global_action = "global__$param_name";
630 }
631  
632 next PARAM_NAME;
633 }
634  
635 return $global_action if defined $global_action;
636  
637 return 'submit' if $self->can("__${page_id}__submit");
638  
639 if ($self->BCV < 0.53) {
640 return 'global_submit' if $self->can('global_submit');
641 }
642  
643 return 'global__submit' if $self->can('global__submit');
644  
645 $self->warn('Requested parameters do not match with defined action');
646  
647 return;
648 }
649  
650 sub get_page { $_[0]->stash->{page} }
651  
652 sub page { shift->get_page(@_) }
653  
654 sub to_action_id {
655 my (undef, $action) = @_;
656  
657 my $action_id = $action;
658 $action_id =~ s/ \. .* \z//xms;
659  
660 return $action_id;
661 }
662  
663 sub set_action {
664 my ($self, $action) = @_;
665  
666 $self->stash->{action} = $action;
667  
668 return;
669 }
670  
671 sub initialize_response_headers {
672 my ($self) = @_;
673  
674 $self->set_response_headers( () );
675  
676 return;
677 }
678  
679 sub initialize_binmode {
680 my ($self) = @_;
681  
682 if ( $self->get_using_utf8 ) {
683 eval q{ binmode select, ':utf8' };
684 }
685 else {
686 no strict 'refs';
687 binmode select;
688 }
689  
690 return;
691 }
692  
693 sub set_response_headers {
694 my ($self, @response_headers) = @_;
695  
696 if ($self->BCV < 0.53) {
697 $self->stash->{headers} = \@response_headers;
698  
699 return;
700 }
701  
702 $self->stash->{response_headers} = \@response_headers;
703  
704 return;
705 }
706  
707 sub controller {
708 my ($self, @relays) = @_;
709  
710 local $NEXT::SEEN if $NEXT::SEEN and $Waft::Correct_NEXT_DISTINCT;
711  
712 if ( my $coderef = $self->can('begin') ) {
713 @relays = $self->call_method($coderef, @relays);
714 }
715  
716 my $stash = $self->stash;
717 my $forward_count;
718 METHOD:
719 while ( not $stash->{responded} ) {
720 if ( my $coderef = $self->can('before') ) {
721 @relays = $self->call_method($coderef, @relays);
722  
723 last METHOD if $stash->{responded};
724 }
725  
726 if ( my $coderef = $self->find_action_method ) {
727 @relays = $self->call_method($coderef, @relays);
728  
729 last METHOD if $stash->{responded};
730  
731 if ($self->BCV < 0.53) {
732 if ( $self->to_action_id($self->get_action) eq 'template' ) {
733 @relays = $self->call_template('CURRENT', @relays);
734  
735 last METHOD if $stash->{responded};
736 }
737 }
738  
739 next METHOD;
740 }
741 else {
742 $self->set_action('template');
743 }
744  
745 @relays = $self->call_template('CURRENT', @relays);
746  
747 last METHOD if $stash->{responded};
748 }
749 continue {
750 $self->die('Methods called too many times in controller')
751 if ++$forward_count >= 5;
752 }
753  
754 if ( $self->can('end') ) {
755 my @return_values = $self->end(@relays);
756  
757 if ( @return_values ) {
758 @relays = @return_values;
759 }
760 }
761  
762 return wantarray ? @relays : $relays[0];
763 }
764  
765 sub call_method {
766 my ($self, $method_coderef, @args) = @_;
767  
768 my @return_values = $self->$method_coderef(@args);
769  
770 return wantarray ? @return_values : $return_values[0]
771 if $self->stash->{responded};
772  
773 require B;
774 my $method_name = B::svref_2object($method_coderef)->GV->NAME;
775  
776 if ( $method_name eq 'begin' || $method_name eq 'before'
777 and @return_values == 0
778 ) {
779 my $next = { page => 'CURRENT', action => undef };
780 @return_values = ($next, @args);
781 }
782  
783 my $next = shift @return_values;
784 my ($next_page, $next_action)
785 = ref $next eq 'ARRAY' ? @$next
786 : ref $next eq 'HASH' ? ($next->{page}, $next->{action})
787 : ($next, undef);
788  
789 if ( not defined $next_page ) {
790 $next_page = $method_name eq 'begin' ? 'CURRENT'
791 : $method_name eq 'before' ? 'CURRENT'
792 : 'TEMPLATE';
793 }
794  
795 if ( not defined $next_action ) {
796 $next_action = $next_page eq 'TEMPLATE' ? 'template'
797 : 'indirect';
798 }
799  
800 if ($next_page eq 'CURRENT' or $next_page eq 'TEMPLATE') {
801 # don't change page
802 }
803 else {
804 $self->set_page($next_page);
805 }
806  
807 if ( $next_page eq 'CURRENT'
808 and $method_name eq 'begin' || $method_name eq 'before'
809 ) {
810 # don't change action
811 }
812 else {
813 $self->set_action($next_action);
814 }
815  
816 return @return_values;
817 }
818  
819 sub find_action_method {
820 my ($self) = @_;
821  
822 my $page_id = $self->to_page_id($self->get_page);
823 my $action_id = $self->to_action_id($self->get_action);
824  
825 if ($self->BCV < 0.53) {
826 if ($action_id eq 'direct') {
827 return $self->can("__${page_id}__direct")
828 || $self->can("__${page_id}")
829 || $self->can('global_direct');
830 }
831 elsif ($action_id eq 'indirect') {
832 return $self->can("__${page_id}__indirect")
833 || $self->can("__${page_id}")
834 || $self->can('global_indirect');
835 }
836 elsif ( $action_id =~ /\A global_ /xms ) {
837 return $self->can($action_id);
838 }
839 }
840  
841 if ($action_id eq 'direct') {
842 return $self->can("__${page_id}__direct")
843 || $self->can("__${page_id}")
844 || $self->can('global__direct');
845 }
846 elsif ($action_id eq 'indirect') {
847 return $self->can("__${page_id}__indirect")
848 || $self->can("__${page_id}")
849 || $self->can('global__indirect');
850 }
851 elsif ( $action_id =~ /\A global__ /xms ) {
852 return $self->can($action_id);
853 }
854  
855 return $self->can("__${page_id}__$action_id");
856 }
857  
858 sub get_action { $_[0]->stash->{action} }
859  
860 sub action { shift->get_action(@_) }
861  
862 sub call_template {
863 my ($self, $page, @args) = @_;
864  
865 if ($self->BCV < 0.53) {
866 $page =~ s/ .+ :: //xms;
867 }
868  
869 if ($page eq 'CURRENT' or $page eq 'TEMPLATE') {
870 $page = $self->get_page;
871 }
872  
873 my ($template_file, $template_class) = $self->get_template_file($page);
874  
875 if ( not defined $template_file ) {
876 $self->warn(qq{Requested page "$page" is not found});
877  
878 my $goto_not_found_coderef = sub { shift; 'not_found.html', @_ };
879  
880 return $self->call_method($goto_not_found_coderef, @args);
881 }
882  
883 my $template_coderef
884 = $self->compile_template_file($template_file, $template_class);
885  
886 return $self->call_method($template_coderef, @args);
887 }
888  
889 sub include { shift->call_template(@_) }
890  
891 sub get_template_file {
892 my ($self, $page) = @_;
893  
894 if ($page eq 'CURRENT' or $page eq 'TEMPLATE') {
895 $page = $self->get_page;
896 }
897  
898 if ( File::Spec->file_name_is_absolute($page) ) {
899 return if not -f $page;
900  
901 my $template_file = $page;
902 my $template_class = $self->blessed || $self;
903  
904 return $template_file, $template_class;
905 }
906  
907 return $self->find_template_file($page);
908 }
909  
910 {
911 my %Cached_template_file;
912  
913 sub find_template_file {
914 my ($self, $page) = @_;
915  
916 my $class = $self->blessed || $self;
917  
918 return @{ $Cached_template_file{$class, $page} }
919 if $Waft::Cache and exists $Cached_template_file{$class, $page};
920  
921 my ($template_file, $template_class)
922 = $self->recursive_find_template_file($page, $class);
923  
924 return if not defined $template_file;
925  
926 $Cached_template_file{$class, $page}
927 = [$template_file, $template_class];
928  
929 return $template_file, $template_class;
930 }
931 }
932  
933 sub recursive_find_template_file {
934 my ($self, $page, $class, $seen) = @_;
935  
936 return if $seen->{$class}++;
937  
938 my $class_path = $class;
939 $class_path =~ s{ :: }{/}gxms;
940  
941 my $module_file = "$class_path.pm";
942 my @lib_dirs
943 = ! defined $INC{$module_file} ? @INC
944 : $INC{$module_file} =~ m{\A (.+) /\Q$module_file\E \z}xms ? ($1)
945 : @INC;
946  
947 my @finding_files;
948 push @finding_files, "$class_path.template/$page";
949 if ( $self->is_allowed_to_use_template_file_ext($page, $class) ) {
950 push @finding_files, "$class_path/$page";
951 }
952  
953 for my $lib_dir ( @lib_dirs ) {
954 for my $finding_file ( @finding_files ) {
955 my $template_file = "$lib_dir/$finding_file";
956  
957 return $template_file, $class if -f $template_file;
958 }
959 }
960  
961 my @super_classes = do { no strict 'refs'; @{ "${class}::ISA" } };
962 for my $super_class ( @super_classes ) {
963 my ($template_file, $template_class)
964 = $self->recursive_find_template_file($page, $super_class, $seen);
965  
966 return $template_file, $template_class if defined $template_file;
967 }
968  
969 return;
970 }
971  
972 sub is_allowed_to_use_template_file_ext {
973 my ($self, $page, $class) = @_;
974  
975 return if $self->BCV < 0.53;
976  
977 my @allow_template_file_exts
978 = $self->get_allow_template_file_exts($class);
979  
980 EXT:
981 for my $allow_template_file_ext ( @allow_template_file_exts ) {
982 if (length $allow_template_file_ext == 0) {
983 return 1 if $page !~ / \. /xms;
984  
985 next EXT;
986 }
987  
988 return 1 if $page =~ / \Q$allow_template_file_ext\E \z/xms;
989 }
990  
991 return;
992 }
993  
994 {
995 my %Cached_template_coderef;
996  
997 sub compile_template_file {
998 my ($self, $template_file, $template_class) = @_;
999  
1000 my @stat = stat $template_file;
1001 if ( not @stat ) {
1002 $self->warn(qq{Failed to stat template file "$template_file"});
1003  
1004 my $goto_internal_server_error_coderef
1005 = sub { shift; 'internal_server_error.html', @_ };
1006  
1007 return $goto_internal_server_error_coderef;
1008 }
1009 my $modified_time = $stat[9];
1010  
1011 my $template_name = "${template_class}::$template_file";
1012 my $template_id = "$template_name-$modified_time";
1013  
1014 return $Cached_template_coderef{$template_id}
1015 if $Waft::Cache and exists $Cached_template_coderef{$template_id};
1016  
1017 my $old_template_id_regexp = qr/\A \Q$template_name\E - \d{14} \z/xms;
1018 CACHED_TEMPLATE:
1019 for my $cached_template_id ( keys %Cached_template_coderef ) {
1020 next CACHED_TEMPLATE
1021 if $cached_template_id !~ $old_template_id_regexp;
1022 delete $Cached_template_coderef{$cached_template_id};
1023 }
1024  
1025 my $template_scalarref = $self->read_template_file($template_file);
1026 if ( not $template_scalarref ) {
1027 $self->warn(qq{Failed to read template file "$template_file"});
1028  
1029 my $goto_forbidden_coderef = sub { shift; 'forbidden.html', @_ };
1030  
1031 return $goto_forbidden_coderef;
1032 }
1033  
1034 my $template_coderef = $self->compile_template(
1035 $template_scalarref, $template_file, $template_class
1036 );
1037  
1038 $Cached_template_coderef{$template_id} = $template_coderef;
1039  
1040 return $template_coderef;
1041 }
1042 }
1043  
1044 sub read_template_file {
1045 my ($self, $template_file) = @_;
1046  
1047 sysopen my $file_handle = gensym, $template_file, O_RDONLY
1048 or return;
1049  
1050 binmode $file_handle;
1051  
1052 my ($untainted_template) = do { local $/; <$file_handle> =~ / (.*) /xms };
1053  
1054 close $file_handle;
1055  
1056 return \$untainted_template;
1057 }
1058  
1059 sub compile_template {
1060 my ($self, $template, $template_file, $template_class) = @_;
1061  
1062 if (ref $template eq 'SCALAR') {
1063 $template = $$template;
1064 }
1065  
1066 $template =~ s{ (?<= <form \b ) (.+?) (?= </form> ) }
1067 { $self->insert_output_waft_tags_method($1) }egixms;
1068  
1069 $template =~ / ( \x0D\x0A | [\x0A\x0D] ) /xms;
1070 my $break = $1 || "\n";
1071  
1072 $template = "%>$template<%";
1073  
1074 $template =~ s{ (?<= %> ) (?! <% ) (.+?) (?= <% ) }
1075 { $self->convert_text_part($1, $break) }egxms;
1076  
1077 $template
1078 =~ s{<% (?! \s*[\x0A\x0D]
1079 =[A-Za-z]
1080 )
1081 \s* j(?:sstr)? \s* = (.*?)
1082 %>}{\$__self->output( \$__self->jsstr_filter($1) );}gxms;
1083  
1084 $template
1085 =~ s{<% (?! \s*[\x0A\x0D]
1086 =[A-Za-z]
1087 )
1088 \s* p(?:lain)? \s* = (.*?)
1089 %>}{\$__self->output($1);}gxms;
1090  
1091 $template
1092 =~ s{<% (?! \s*[\x0A\x0D]
1093 =[A-Za-z]
1094 )
1095 \s* t(?:ext)? \s* = (.*?)
1096 %>}{\$__self->output( \$__self->text_filter($1) );}gxms;
1097  
1098 $template
1099 =~ s{<% (?! \s*[\x0A\x0D]
1100 =[A-Za-z]
1101 )
1102 \s* (?: w(?:ord)? \s* )? = (.*?)
1103 %>}{\$__self->output( \$__self->word_filter($1) );}gxms;
1104  
1105 $template =~ s/ %> | <% //gxms;
1106  
1107 $template = 'return sub {'
1108 . ( $self->BCV < 1.0 ? 'local $Waft::Self = $_[0];' : q{} )
1109 . 'my $__self = $_[0];'
1110 . $template
1111 . '}';
1112  
1113 if ( defined $template_class ) {
1114 $template = "package $template_class;" . $template;
1115 }
1116  
1117 if ( defined $template_file ) {
1118 $template = qq{$break# line 1 "$template_file"$break} . $template;
1119 }
1120  
1121 my $coderef = $self->compile(\$template);
1122  
1123 $self->die($@) if $@;
1124  
1125 return $coderef;
1126 }
1127  
1128 sub insert_output_waft_tags_method {
1129 my ($self, $form_block) = @_;
1130  
1131 return $form_block if $form_block =~ m{ \b (?:
1132 output_waft_tags
1133 | (?: (?i) waft(?: \s+ | _ ) tag s? )
1134 | form_elements # deprecated
1135 ) \b }xms;
1136  
1137 $form_block =~ s{ (?= < (?: input | select | textarea | label ) \b ) }
1138 {<% \$__self->output_waft_tags('ALL_VALUES'); %>}ixms;
1139  
1140 return $form_block;
1141 }
1142  
1143 sub output_waft_tags {
1144 my ($self, @keys_arrayref_or_key_value_pairs) = @_;
1145  
1146 $self->output( $self->get_waft_tags(@keys_arrayref_or_key_value_pairs) );
1147  
1148 return;
1149 }
1150  
1151 sub get_waft_tags {
1152 my ($self, @keys_arrayref_or_key_value_pairs) = @_;
1153  
1154 my $joined_values = $self->join_values(@keys_arrayref_or_key_value_pairs);
1155 my $waft_tags = q{<input name="s" type="hidden" value="}
1156 . $self->html_escape($self->get_page)
1157 . q{" /><input name="v" type="hidden" value="}
1158 . $self->html_escape($joined_values)
1159 . q{" />};
1160  
1161 return $waft_tags;
1162 }
1163  
1164 sub join_values {
1165 my ($self, @keys_arrayref_or_key_value_pairs) = @_;
1166  
1167 my %joined_values;
1168  
1169 my $value_hashref = $self->value_hashref;
1170 KEYS_ARRAYREF_OR_KEY:
1171 while ( @keys_arrayref_or_key_value_pairs ) {
1172 my $keys_arrayref_or_key = shift @keys_arrayref_or_key_value_pairs;
1173  
1174 if ( defined $keys_arrayref_or_key
1175 and $keys_arrayref_or_key eq 'ALL_VALUES'
1176 ) {
1177 $keys_arrayref_or_key = [ keys %$value_hashref ];
1178 }
1179  
1180 if (ref $keys_arrayref_or_key eq 'ARRAY') {
1181 KEY:
1182 for my $key ( @$keys_arrayref_or_key ) {
1183 if ( not defined $key ) {
1184 $self->warn('Use of uninitialized value');
1185 $key = q{};
1186 }
1187  
1188 next KEY if not exists $value_hashref->{$key};
1189  
1190 my @values = $self->get_values($key);
1191  
1192 VALUE:
1193 for my $value ( @values ) {
1194 next VALUE if defined $value;
1195 $self->warn('Use of uninitialized value');
1196 $value = q{};
1197 }
1198  
1199 @values = $self->escape_space_percent_hyphen(@values);
1200  
1201 $joined_values{$key} = join q{}, map { "-$_" } @values;
1202 }
1203  
1204 next KEYS_ARRAYREF_OR_KEY;
1205 }
1206  
1207 my $key;
1208  
1209 if ( defined $keys_arrayref_or_key ) {
1210 $key = $keys_arrayref_or_key;
1211 }
1212 else {
1213 $self->warn('Use of uninitialized value');
1214 $key = q{};
1215 }
1216  
1217 my @values;
1218  
1219 if ( @keys_arrayref_or_key_value_pairs ) {
1220 my $value_or_values_arrayref
1221 = shift @keys_arrayref_or_key_value_pairs;
1222  
1223 if ( not defined $value_or_values_arrayref ) {
1224 $self->warn('Use of uninitialized value');
1225 @values = (q{});
1226 }
1227 elsif (ref $value_or_values_arrayref eq 'ARRAY') {
1228 @values = @$value_or_values_arrayref;
1229  
1230 VALUE:
1231 for my $value ( @values ) {
1232 next VALUE if defined $value;
1233 $self->warn('Use of uninitialized value');
1234 $value = q{};
1235 }
1236 }
1237 else {
1238 @values = ($value_or_values_arrayref);
1239 }
1240 }
1241 else {
1242 $self->warn('Odd number of elements in arguments');
1243 @values = (q{});
1244 }
1245  
1246 @values = $self->escape_space_percent_hyphen(@values);
1247  
1248 $joined_values{$key} = join q{}, map { "-$_" } @values;
1249  
1250 next KEYS_ARRAYREF_OR_KEY;
1251 }
1252  
1253 my $joined_values
1254 = join q{ }, map { $self->escape_space_percent_hyphen($_)
1255 . $joined_values{$_}
1256 } sort keys %joined_values;
1257  
1258 return $joined_values;
1259 }
1260  
1261 {
1262 my @EMPTY;
1263  
1264 sub get_values {
1265 my ($self, $key, @i) = @_;
1266  
1267 return @{ $self->value_hashref->{$key} || \@EMPTY }[@i] if @i;
1268  
1269 return @{ $self->value_hashref->{$key} || \@EMPTY };
1270 }
1271 }
1272  
1273 sub escape_space_percent_hyphen {
1274 my (undef, @values) = @_;
1275  
1276 for my $value (@values) {
1277 $value =~ s/ ( [ %-] ) / '%' . unpack('H2', $1) /egxms;
1278 }
1279  
1280 return wantarray ? @values : $values[0];
1281 }
1282  
1283 sub convert_text_part {
1284 my (undef, $text_part, $break) = @_;
1285  
1286 if ($text_part =~ / ([^\x0A\x0D]*) ( [\x0A\x0D] .* ) /xms) {
1287 my ($first_line, $after_first_break) = ($1, $2);
1288  
1289 if (length $first_line > 0) {
1290 $first_line =~ s/ ( ['\\] ) /\\$1/gxms;
1291 $first_line = q{$__self->output('} . $first_line . q{');};
1292 }
1293  
1294 $after_first_break =~ s/ ( ["\$\@\\] ) /\\$1/gxms;
1295  
1296 my $breaks = $break x (
1297 $after_first_break =~ s/ \x0D\x0A /\\x0D\\x0A/gxms
1298 + $after_first_break =~ s/ \x0A /\\x0A/gxms
1299 + $after_first_break =~ s/ \x0D /\\x0D/gxms
1300 - 1
1301 );
1302  
1303 return $first_line . $break
1304 . qq{\$__self->output("$after_first_break");$breaks};
1305 }
1306  
1307 $text_part =~ s/ ( ['\\] ) /\\$1/gxms;
1308  
1309 return q{$__self->output('} . $text_part . q{');};
1310 }
1311  
1312 {
1313 package Waft::compile;
1314  
1315 no strict;
1316 BEGIN { 'warnings'->unimport if eval { require warnings } }
1317  
1318 sub Waft::compile { eval ${ $_[1] } }
1319 }
1320  
1321 {
1322 my $OUTPUT_CONTENT_CODEREF;
1323  
1324 sub output {
1325 ( $_[0]->stash->{output} ||= do {
1326 my ($self) = @_;
1327  
1328 $self->output_response_headers;
1329 $self->stash->{responded} = 1;
1330  
1331 $OUTPUT_CONTENT_CODEREF;
1332 } )->(@_);
1333 }
1334  
1335 $OUTPUT_CONTENT_CODEREF = sub { shift; print @_ if @_; return };
1336 }
1337  
1338 sub output_response_headers {
1339 my ($self) = @_;
1340  
1341 my ($http_status, $content_type);
1342  
1343 RESPONSE_HEADER:
1344 for my $response_header ( $self->get_response_headers ) {
1345 print "$response_header\x0D\x0A";
1346  
1347 if ( $response_header =~ /\A Status: \s* (.*) /ixms ) {
1348 $http_status = $1;
1349 }
1350  
1351 if ( $response_header =~ /\A Content-Type: \s* (.*) /ixms ) {
1352 $content_type = $1;
1353 }
1354 }
1355  
1356 if ($self->BCV < 0.53) {
1357 if ( not defined $content_type ) {
1358 $content_type = $self->stash->{content_type};
1359 print "Content-Type: $content_type\x0D\x0A";
1360 }
1361  
1362 print "\x0D\x0A";
1363  
1364 return;
1365 }
1366  
1367 if ( not defined $content_type ) {
1368 $content_type = $self->get_default_content_type;
1369 print "Content-Type: $content_type\x0D\x0A";
1370 }
1371  
1372 print "\x0D\x0A";
1373  
1374 @{ $self->stash }{ qw( http_status content_type ) }
1375 = ($http_status, $content_type);
1376  
1377 return;
1378 }
1379  
1380 sub get_response_headers {
1381 my ($self) = @_;
1382  
1383 return @{ $self->stash->{headers} } if $self->BCV < 0.53;
1384  
1385 return @{ $self->stash->{response_headers} }
1386 }
1387  
1388 sub responded { $_[0]->stash->{responded} }
1389  
1390 {
1391 my $BUFFER_CONTENT_CODEREF;
1392  
1393 sub get_content {
1394 my ($self, $coderef, @args) = @_;
1395  
1396 my $stash = $self->stash;
1397  
1398 push @{ $stash->{contents} }, undef;
1399  
1400 local $stash->{output} = $BUFFER_CONTENT_CODEREF
1401 if @{ $stash->{contents} } == 1;
1402 my @return_values = $self->$coderef(@args);
1403  
1404 return pop @{ $stash->{contents} }, @return_values if wantarray;
1405 return pop @{ $stash->{contents} };
1406 }
1407  
1408 $BUFFER_CONTENT_CODEREF = sub {
1409 shift->stash->{contents}[-1] .= join q{}, @_ if @_ > 1; return;
1410 };
1411 }
1412  
1413 sub jsstr_filter { shift->jsstr_escape(@_) }
1414  
1415 sub jsstr_escape {
1416 my ($self, @values) = @_;
1417  
1418 VALUE:
1419 for my $value (@values) {
1420 if ( not defined $value ) {
1421 $self->warn('Use of uninitialized value');
1422  
1423 next VALUE;
1424 }
1425  
1426 $value =~ s{ (["'/\\]) }{\\$1}gxms;
1427 $value =~ s/ \x0A /\\n/gxms;
1428 $value =~ s/ \x0D /\\r/gxms;
1429 $value =~ s/ < /\\x3C/gxms;
1430 $value =~ s/ > /\\x3E/gxms;
1431 }
1432  
1433 return wantarray ? @values : $values[0];
1434 }
1435  
1436 sub text_filter {
1437 my ($self, @values) = @_;
1438  
1439 VALUE:
1440 for my $value ( @values ) {
1441 if ( not defined $value ) {
1442 $self->warn('Use of uninitialized value');
1443  
1444 next VALUE;
1445 }
1446  
1447 $value = $self->expand_tabs($value);
1448 $value = $self->html_escape($value);
1449 $value =~ s{ ( \x0D\x0A | [\x0A\x0D] ) }{<br />$1}gxms;
1450 $value =~ s{\A \x20 }{&nbsp;}gxms;
1451 $value =~ s{ (\s) \x20 }{$1&nbsp;}gxms;
1452 }
1453  
1454 return wantarray ? @values : $values[0];
1455 }
1456  
1457 sub expand_tabs {
1458 my ($self, @values) = @_;
1459  
1460 VALUE:
1461 for my $value (@values) {
1462 if ( not defined $value ) {
1463 $self->warn('Use of uninitialized value');
1464  
1465 next VALUE;
1466 }
1467  
1468 $value =~ s{( [^\x0A\x0D]+ )}{
1469 my $line = $1;
1470  
1471 while ( $line =~ / \t /gxms ) {
1472 my $offset = pos($line) - 1;
1473 substr( $line, $offset, 1 ) = q{ } x ( 8 - $offset % 8 );
1474 }
1475  
1476 $line;
1477 }egxms;
1478 }
1479  
1480 return wantarray ? @values : $values[0];
1481 }
1482  
1483 sub html_escape {
1484 my ($self, @values) = @_;
1485  
1486 VALUE:
1487 for my $value (@values) {
1488 if ( not defined $value ) {
1489 $self->warn('Use of uninitialized value');
1490  
1491 next VALUE;
1492 }
1493  
1494 $value =~ s/ & /&amp;/gxms;
1495 $value =~ s/ " /&quot;/gxms;
1496 $value =~ s/ ' /&#39;/gxms;
1497 $value =~ s/ < /&lt;/gxms;
1498 $value =~ s/ > /&gt;/gxms;
1499 }
1500  
1501 return wantarray ? @values : $values[0];
1502 }
1503  
1504 sub word_filter { shift->html_escape(@_) }
1505  
1506 {
1507 my (%Start, %Progress, $FIND_NEXT_CODEREF);
1508  
1509 sub next {
1510 my ($self) = @_;
1511  
1512 my ($back, $subroutine);
1513 1 while ( ( $subroutine = ( caller ++$back )[3] ) eq '(eval)' );
1514 my ($caller, $method) = $subroutine =~ / (.+) :: (.+) /xms;
1515  
1516 my $ident = $self->refaddr || $self;
1517  
1518 local $Start{ $ident, $method } = $caller
1519 if not $Start{ $ident, $method }
1520 or ( caller $back + 1 )[3] ne ( caller 0 )[3];
1521 local $Progress{ $ident, $method, $Start{ $ident, $method } }
1522 = $Progress{ $ident, $method, $Start{ $ident, $method } };
1523  
1524 my $next_coderef = $self->$FIND_NEXT_CODEREF(
1525 $method
1526 , $Start{ $ident, $method }
1527 , $Progress{ $ident, $method, $Start{ $ident, $method } }++
1528 );
1529  
1530 return if not $next_coderef;
1531  
1532 return $next_coderef->(@_);
1533 }
1534  
1535 my %Cached_next_coderefs;
1536  
1537 $FIND_NEXT_CODEREF = sub {
1538 my ($self, $method, $start, $progress) = @_;
1539  
1540 my $class = $self->blessed || $self;
1541  
1542 return $Cached_next_coderefs{$class, $method, $start}->[$progress]
1543 if $Waft::Cache
1544 and exists $Cached_next_coderefs{$class, $method, $start};
1545  
1546 my @next_classes;
1547  
1548 my @classes = ($class);
1549 while ( my $class = shift @classes ) {
1550 push @next_classes, $class;
1551  
1552 no strict 'refs';
1553 unshift @classes, @{ "${class}::ISA" };
1554 }
1555  
1556 while ( $start ne shift @next_classes ) {
1557 return if @next_classes == 0;
1558 }
1559  
1560 my @next_coderefs = do {
1561 no strict 'refs';
1562 grep { $_ } map { *{ "${_}::$method" }{CODE} } @next_classes;
1563 };
1564  
1565 $Cached_next_coderefs{$class, $method, $start} = \@next_coderefs;
1566  
1567 return $next_coderefs[$progress];
1568 };
1569 }
1570  
1571 sub get_page_id {
1572 my ($self, $page) = @_;
1573  
1574 if ( not defined $page ) {
1575 $page = $self->get_page;
1576 }
1577  
1578 my $page_id = $self->to_page_id($page);
1579  
1580 return $page_id;
1581 }
1582  
1583 sub page_id { shift->get_page_id(@_) }
1584  
1585 sub set_value {
1586 my ($self, $key, $value) = @_;
1587  
1588 $self->set_values($key, $value);
1589  
1590 return;
1591 }
1592  
1593 sub get_value {
1594 my ($self, $key, @i) = @_;
1595  
1596 return( ( $self->get_values($key, @i) )[0] );
1597 }
1598  
1599 sub http_status {
1600 return $_[0]->stash->{http_status} if @_ == 1;
1601 my ($self, $http_status) = @_;
1602  
1603 $self->set_response_header("Status: $http_status");
1604  
1605 return;
1606 }
1607  
1608 sub content_type {
1609 return $_[0]->stash->{content_type} if @_ == 1;
1610 my ($self, $content_type) = @_;
1611  
1612 $self->set_response_header("Content-Type: $content_type");
1613  
1614 return;
1615 }
1616  
1617 sub set_response_header {
1618 my ($self, $response_header) = @_;
1619  
1620 if ( $self->stash->{responded} ) {
1621 $self->warn('Too late to set response header');
1622  
1623 return;
1624 }
1625  
1626 if ( $response_header =~ /\A ([^:]+) /xms ) {
1627 my $field = $1;
1628 $self->unset_response_header($field);
1629 }
1630  
1631 $self->add_response_header($response_header);
1632  
1633 return;
1634 }
1635  
1636 sub unset_response_header {
1637 my ($self, $response_header_field) = @_;
1638  
1639 my $stash = $self->stash;
1640  
1641 if ( $stash->{responded} ) {
1642 $self->warn('Too late to unset response header');
1643  
1644 return;
1645 }
1646  
1647 @{ $stash->{response_headers} }
1648 = grep { not /\A \Q$response_header_field\E: /ixms }
1649 @{ $stash->{response_headers} };
1650  
1651 return;
1652 }
1653  
1654 sub add_response_header {
1655 my ($self, $response_header) = @_;
1656  
1657 if ($_[0]->BCV < 0.53) {
1658 ( undef, my @response_header_blocks ) = @_;
1659  
1660 my $stash = $self->stash;
1661 for my $response_header_block ( @response_header_blocks ) {
1662 my @response_header_lines
1663 = grep { length > 0 }
1664 split /[\x0A\x0D]+/, $response_header_block;
1665 push @{ $stash->{headers} }, @response_header_lines;
1666 }
1667  
1668 return;
1669 }
1670  
1671 my $stash = $self->stash;
1672  
1673 if ( $stash->{responded} ) {
1674 $self->warn('Too late to add response header');
1675  
1676 return;
1677 }
1678  
1679 $response_header =~ s/ [\x0A\x0D]+ //gxms;
1680 push @{ $stash->{response_headers} }, $response_header;
1681  
1682 return;
1683 }
1684  
1685 sub header { shift->add_response_header(@_) }
1686  
1687 sub add_header { shift->header(@_) }
1688  
1689 sub make_url {
1690 my ($self, $page, @keys_arrayref_or_key_value_pairs) = @_;
1691  
1692 my $query_string
1693 = $self->make_query_string($page, @keys_arrayref_or_key_value_pairs);
1694  
1695 return $self->get_base_url if length $query_string == 0;
1696  
1697 return $self->get_base_url . '?' . $query_string;
1698 }
1699  
1700 sub url { shift->make_url(@_) }
1701  
1702 sub make_absolute_url {
1703 my ($self, @args) = @_;
1704  
1705 my $protocol = $self->cgi->protocol;
1706  
1707 my $base_url = "$protocol://";
1708  
1709 if ( defined $ENV{HTTP_HOST} ) {
1710 $base_url .= $ENV{HTTP_HOST};
1711 }
1712 else {
1713 $base_url .= $ENV{SERVER_NAME};
1714  
1715 if ( $protocol eq 'http' and $ENV{SERVER_PORT} != 80
1716 or $protocol eq 'https' and $ENV{SERVER_PORT} != 443
1717 ) {
1718 $base_url .= ":$ENV{SERVER_PORT}";
1719 }
1720 }
1721  
1722 if ( defined $ENV{REQUEST_URI}
1723 and $ENV{REQUEST_URI} =~ /\A ([^?]+) /xms
1724 ) {
1725 $base_url .= $1;
1726 }
1727 else {
1728 $base_url .= $ENV{SCRIPT_NAME};
1729 }
1730  
1731 local $self->stash->{url} = $base_url if $self->BCV < 0.53;
1732 local $self->stash->{base_url} = $base_url;
1733  
1734 return $self->make_url(@args);
1735 }
1736  
1737 sub absolute_url { shift->make_absolute_url(@_) }
1738  
1739 sub make_query_string {
1740 my ($self, $page, @keys_arrayref_or_key_value_pairs) = @_;
1741  
1742 if (ref $page eq 'ARRAY') {
1743 $page = $page->[0];
1744 }
1745  
1746 $page = ! defined $page ? 'default.html'
1747 : $page eq 'CURRENT' ? $self->get_page
1748 : $page;
1749  
1750 my @query_string;
1751  
1752 if ($page ne 'default.html') {
1753 push @query_string,
1754 join( '=', ( $self->url_encode( 'p' => $page ) ) );
1755 }
1756  
1757 my $joined_values = $self->join_values(@keys_arrayref_or_key_value_pairs);
1758 if ( $joined_values ) {
1759 push @query_string,
1760 join( '=', ( $self->url_encode('v' => $joined_values) ) );
1761 }
1762  
1763 my $query_string = join '&', @query_string;
1764  
1765 return $query_string;
1766 }
1767  
1768 sub url_encode {
1769 my ($self, @values) = @_;
1770  
1771 my $using_utf8 = $self->get_using_utf8;
1772  
1773 VALUE:
1774 for my $value ( @values ) {
1775 if ( not defined $value ) {
1776 $self->warn('Use of uninitialized value');
1777  
1778 next VALUE;
1779 }
1780  
1781 if ( $using_utf8 ) {
1782 utf8::encode($value);
1783 }
1784  
1785 $value =~ s/ ( [^ .\w-] ) / '%' . unpack('H2', $1) /egxms;
1786 $value =~ tr/ /+/;
1787 }
1788  
1789 return wantarray ? @values : $values[0];
1790 }
1791  
1792 sub get_base_url {
1793 my ($self) = @_;
1794  
1795 return $Waft::Base_url if defined $Waft::Base_url and $self->BCV < 1.0;
1796  
1797 return $self->stash->{url} if $self->BCV < 0.53;
1798  
1799 return $self->stash->{base_url};
1800 }
1801  
1802 sub __forbidden__indirect {
1803 my ($self, @args) = @_;
1804  
1805 $self->http_status('403 Forbidden');
1806 $self->content_type('text/html; charset=ISO8859-1');
1807  
1808 my $escaped_request_uri = $self->html_escape($ENV{REQUEST_URI});
1809  
1810 $self->output(qq{<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n});
1811 $self->output(qq{<html><head>\n});
1812 $self->output(qq{<title>403 Forbidden</title>\n});
1813 $self->output(qq{</head><body>\n});
1814 $self->output(qq{<h1>Forbidden</h1>\n});
1815 $self->output( q{<p>You don't have permission to access});
1816 $self->output(qq{ $escaped_request_uri\non this server.</p>\n});
1817 $self->output(qq{</body></html>\n});
1818  
1819 return @args;
1820 }
1821  
1822 sub __not_found__indirect {
1823 my ($self, @args) = @_;
1824  
1825 $self->http_status('404 Not Found');
1826 $self->content_type('text/html; charset=ISO8859-1');
1827  
1828 my $escaped_request_uri = $self->html_escape($ENV{REQUEST_URI});
1829  
1830 $self->output(qq{<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n});
1831 $self->output(qq{<html><head>\n});
1832 $self->output(qq{<title>404 Not Found</title>\n});
1833 $self->output(qq{</head><body>\n});
1834 $self->output(qq{<h1>Not Found</h1>\n});
1835 $self->output(qq{<p>The requested URL $escaped_request_uri});
1836 $self->output(qq{ was not found on this server.</p>\n});
1837 $self->output(qq{</body></html>\n});
1838  
1839 return @args;
1840 }
1841  
1842 sub __internal_server_error__indirect {
1843 my ($self, @args) = @_;
1844  
1845 $self->http_status('500 Internal Server Error');
1846 $self->content_type('text/html; charset=ISO8859-1');
1847  
1848 my $escaped_server_admin = $self->html_escape($ENV{SERVER_ADMIN});
1849  
1850 $self->output(qq{<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n});
1851 $self->output(qq{<html><head>\n});
1852 $self->output(qq{<title>500 Internal Server Error</title>\n});
1853 $self->output(qq{</head><body>\n});
1854 $self->output(qq{<h1>Internal Server Error</h1>\n});
1855 $self->output(qq{<p>The server encountered an internal error or\n});
1856 $self->output(qq{misconfiguration and was unable to complete\n});
1857 $self->output(qq{your request.</p>\n});
1858 $self->output(qq{<p>Please contact the server administrator,\n});
1859 $self->output(qq{ $escaped_server_admin});
1860 $self->output(qq{ and inform them of the time the error occurred,\n});
1861 $self->output(qq{and anything you might have done that may have\n});
1862 $self->output(qq{caused the error.</p>\n});
1863 $self->output( q{<p>More information about this error may be });
1864 $self->output(qq{available\n});
1865 $self->output(qq{in the server error log.</p>\n});
1866 $self->output(qq{</body></html>\n});
1867  
1868 return @args;
1869 }
1870  
1871 {
1872 my $Defined_subs_for_under_0_99x;
1873  
1874 sub define_subs_for_under_0_99x {
1875  
1876 return if $Defined_subs_for_under_0_99x;
1877  
1878 *croak = *croak = sub { shift->die(@_) };
1879 *carp = *carp = sub { shift->warn(@_) };
1880  
1881 *init_base_url = *init_base_url
1882 = sub { shift->initialize_base_url(@_) };
1883 *init_page = *init_page
1884 = sub { shift->initialize_page(@_) };
1885 *init_values = *init_values
1886 = sub { shift->initialize_values(@_) };
1887 *init_action = *init_action
1888 = sub { shift->initialize_action(@_) };
1889 *init_response_headers = *init_response_headers
1890 = sub { shift->initialize_response_headers(@_) };
1891 *init_binmode = *init_binmode
1892 = sub { shift->initialize_binmode(@_) };
1893  
1894 *is_blessed = *is_blessed = sub { shift->blessed(@_) };
1895  
1896 *ident = *ident = sub { shift->refaddr(@_) };
1897  
1898 *keys_arrayref = *keys_arrayref
1899 = sub { [ keys %{ $_[0]->value_hashref } ] };
1900  
1901 *exists_key = *exists_key
1902 = sub { exists $_[0]->value_hashref->{ $_[1] } };
1903  
1904 *expand = *expand = sub { Waft->expand_tabs(@_) };
1905  
1906 $Defined_subs_for_under_0_99x = 1;
1907  
1908 return;
1909 }
1910  
1911 my $Defined_subs_for_under_0_52x;
1912  
1913 sub define_subs_for_under_0_52x {
1914  
1915 return if $Defined_subs_for_under_0_52x;
1916  
1917 *array = *array = sub {
1918 my ($self, $key, @values) = @_;
1919  
1920 if ( @values ) {
1921 my @old_values = $self->get_values($key);
1922  
1923 $self->set_values($key, @values);
1924  
1925 return @old_values;
1926 }
1927  
1928 return $self->get_values($key);
1929 };
1930  
1931 *arrayref = *arrayref = sub {
1932 my ($self, $key, $arrayref) = @_;
1933  
1934 return $self->value_hashref->{$key} = $arrayref
1935 if ref $arrayref eq 'ARRAY';
1936  
1937 return $self->value_hashref->{$key} ||= $arrayref;
1938 };
1939  
1940 eval q{ sub begin { return } };
1941 eval q{ sub before { return } };
1942  
1943 *end = *end = sub { return };
1944  
1945 *form_elements = *form_elements = sub {
1946 my ($self, @args) = @_;
1947  
1948 if (@args == 1
1949 and defined $args[0]
1950 and $args[0] eq 'ALL' || $args[0] eq 'ALLVALUES'
1951 ) {
1952 $args[0] = 'ALL_VALUES';
1953 }
1954  
1955 $self->output_waft_tags(@args);
1956  
1957 return;
1958 };
1959  
1960 *query = *query = \&cgi;
1961  
1962 *waft_tags = *waft_tags = \&get_waft_tags;
1963  
1964 *_join_values = *_join_values = \&join_values;
1965  
1966 *_load_query_param = *_load_query_param = sub {
1967 my ($self) = @_;
1968  
1969 $self->init_page;
1970 $self->init_action;
1971 $self->init_values;
1972  
1973 return;
1974 };
1975  
1976 *__DEFAULT = *__DEFAULT = sub {
1977 my ($self, @args) = @_;
1978  
1979 return { page => 'default.html', action => $self->action }, @args;
1980 };
1981  
1982 $Defined_subs_for_under_0_52x = 1;
1983  
1984 return;
1985 }
1986 }
1987  
1988 package Waft::Object;
1989  
1990 sub TIEHASH {
1991  
1992 bless {};
1993 }
1994  
1995 sub STORE {
1996 if (ref $_[2] eq 'ARRAY') {
1997 @{ $_[0]{ defined $_[1] ? $_[1] : warn_and_null() } } = @{$_[2]};
1998 }
1999 else {
2000 @{ $_[0]{ defined $_[1] ? $_[1] : warn_and_null() } } = ($_[2]);
2001 }
2002 }
2003  
2004 sub warn_and_null {
2005 require Carp;
2006 Carp::carp('Use of uninitialized value');
2007 q{};
2008 }
2009  
2010 sub FETCH {
2011 my $arrayref = $_[0]{ defined $_[1] ? $_[1] : warn_and_null() }
2012 or return;
2013  
2014 $arrayref->[0];
2015 }
2016  
2017 sub FIRSTKEY { keys %{$_[0]}; each %{$_[0]} }
2018  
2019 sub NEXTKEY { each %{$_[0]} }
2020  
2021 sub EXISTS { exists $_[0]{ defined $_[1] ? $_[1] : warn_and_null() } }
2022  
2023 sub DELETE { delete $_[0]{ defined $_[1] ? $_[1] : warn_and_null() } }
2024  
2025 sub CLEAR { %{$_[0]} = () }
2026  
2027 1;
2028 __END__
↑ Summary