Finally drop CGI itself.
[MyWeb.git] / Web.pm
1 # $Id$
2 # Common functions for HTML/XHTML output generation
3 # Copyright (C) 2003-2005 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; exactly version 2 of June 1991 is required
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19 package My::Web;
20 require 5.6.0;  # at least 'use warnings;' but we need some 5.6.0+ modules anyway
21 our $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
22 our $CVS_ID=q$Id$;
23 use strict;
24 use warnings;
25
26 use Exporter;
27 sub Wrequire($);
28 sub Wuse($@);
29 our $W;
30 our @EXPORT=qw(
31                 &Wrequire &Wuse
32                 &path_web &path_abs_disk
33                 &uri_escaped
34                 &a_href &a_href_cc
35                 &vskip
36                 &img &centerimg &rightimg
37                 $W
38                 &input_hidden_persistents
39                 &escapeHTML
40                 );
41 our @ISA=qw(Tie::Handle Exporter);
42
43 my %packages_used_hash; # $packages_used_hash{$W->{"__PACKAGE__"}}{"_done"}=1;
44 my %packages_used_array;
45
46 BEGIN
47 {
48         use Carp qw(cluck confess);
49         $W->{"__My::Web_init"}=1;
50
51         sub Wrequire ($)
52         {
53         my($file)=@_;
54
55 #               print STDERR "Wrequire $file\n";
56                 $file=~s#/#::#g;
57                 $file=~s/[.]pm$//;
58                 my $class=$file;
59                 $file=~s#::#/#g;
60                 $file.=".pm";
61                 my %callers;
62                 for (my $depth=0;defined caller($depth);$depth++) {
63                         $callers{caller($depth)}=1;
64                         }
65                 my $selfpkg=__PACKAGE__;
66                 $callers{$selfpkg}=1;
67                 for my $target ($class,__PACKAGE__) {
68                         for my $caller (keys(%callers)) {
69                                 next if $caller eq $target;
70                                 next if $packages_used_hash{$caller}{$target}++;
71                                 cluck "Appending to the '_done' package list: caller=$caller,target=$target"
72                                                 if $packages_used_hash{$caller}{"_done"};
73                                 push @{$packages_used_array{$caller}},$target;
74                                 }
75                         }
76                 eval { CORE::require "$file"; } or confess $@;
77                 1;      # Otherwise 'require' would already file above.
78         }
79
80         sub Wuse ($@)
81         {
82         my($file,@list)=@_;
83
84 #               print STDERR "Wuse $file\n";
85                 Wrequire $file;
86                 local $Exporter::ExportLevel=$Exporter::ExportLevel+1;
87                 $file->import(@list);
88                 1;
89         }
90
91         sub import
92         {
93         my($class,@rest)=@_;
94
95                 local $Exporter::ExportLevel=$Exporter::ExportLevel+1;
96                 Wrequire("$class");
97                 return $class->SUPER::import(@rest);
98         }
99 }
100
101 use WebConfig;  # see also below: Wuse 'WebConfig';
102 require Image::Size;    # for &imgsize
103 use File::Basename;     # &basename
104 use Carp qw(cluck confess);
105 use URI::Escape;
106 require HTTP::BrowserDetect;
107 require HTTP::Negotiate;
108 our $have_Geo_IP; BEGIN { $have_Geo_IP=eval { require Geo::IP; 1; }; }
109 # Do not: use ModPerl::Util qw(exit);
110 # to prevent in mod_perl2: "exit" is not exported by the ModPerl::Util module
111 # I do not know why.
112 use POSIX qw(strftime);
113 use Tie::Handle;
114 use Apache2::Const qw(HTTP_MOVED_TEMPORARILY OK HTTP_OK);
115 use URI;
116 use URI::QueryParam;
117 use Cwd;
118 require HTTP::Date;
119 require Storable;
120 require Digest::MD5;
121 require Data::Compare;
122 use Data::Dumper;
123 require Encode;
124 use Apache2::Filter;
125 use Apache2::Connection;
126 require MIME::Base64;
127 use Apache2::ServerUtil;
128 require MIME::Types;
129 require MIME::Parser;
130
131
132 #our $W;
133
134 sub init ($%)
135 {
136 my($class,%args)=@_;
137
138         print STDERR "$class->init ".Apache2::RequestUtil->request()->unparsed_uri()."\n";
139
140         # We need to track package dependencies, so we need to call it from &init.
141         # We cannot do it in BEGIN { } block
142         # as it would not be tracked for each of the toplevel users later.
143         Wuse 'WebConfig';
144         Wrequire 'My::Hash';
145
146         # $W={} can get somehow created very easily.
147         # Do not: cluck "W not empty:\n".Dumper($W) if keys(%$W);
148         # to prevent (of $W->{"headers_in"}): TODO: Enumeration may not be expected.
149         cluck "W not empty; __PACKAGE__ was: ".$W->{"__PACKAGE__"} if keys(%$W);
150         $W=My::Hash->new({},"My::Hash::Sub","My::Hash::Push");
151         bless $W,$class;
152         %$W=(
153                 "__PACKAGE__"=>scalar(caller()),
154                 %WebConfig,
155                 %args,  # override %WebConfig settings
156                 );
157
158         # {"__PACKAGE__"} is mandatory for mod_perl-2.0;
159         # $Apache2::Registry::curstash is no longer supported.
160         do { cluck "No $_" if !$W->{$_}; } for "__PACKAGE__";
161         exit_hook_start();
162
163         do { $W->{$_}=0  if !defined $W->{$_}; } for "detect_ent";
164         do { $W->{$_}=0  if !defined $W->{$_}; } for "detect_js";
165         do { $W->{$_}=1  if !defined $W->{$_}; } for "have_css";        # AFAIK it does not hurt anyone.
166         do { $W->{$_}=0  if !defined $W->{$_}; } for "css_inherit";
167         do { $W->{$_}=1  if !defined $W->{$_}; } for "footer";
168         do { $W->{$_}=1  if !defined $W->{$_}; } for "footer_delimit";
169         do { $W->{$_}=1  if !defined $W->{$_}; } for "footer_ids";
170         do { $W->{$_}=1  if !defined $W->{$_}; } for "indexme";
171         do { $W->{$_}="" if !defined $W->{$_}; } for "head";
172         do { $W->{$_}="" if !defined $W->{$_}; } for "body_attr";
173         do { $W->{$_}="en-US" if !defined $W->{$_}; } for "language";
174
175         my $footer_any=0;
176         for (qw(footer_ids)) {
177                 $W->{$_}=0 if !$W->{"footer"};
178                 $footer_any=1 if $W->{$_};
179                 }
180         $W->{"footer"}=0 if !$footer_any;
181         $W->{"footer_delimit"}=0 if !$W->{"footer"};
182
183         $W->{"r"}=Apache2::RequestUtil->request();
184
185         $W->{"r"}->push_handlers("PerlCleanupHandler"=>\&cleanup);
186
187         $W->{"web_hostname"}||=$W->{"r"}->hostname();
188
189         tie *STDOUT,$W->{"r"};
190         select *STDOUT;
191         $|=1;
192
193         $W->{"headers_in"}=$W->{"r"}->headers_in();
194         Wrequire 'My::Hash::Merge';
195         $W->{"headers_in"}=My::Hash::Merge->new(
196                         $W->{"headers_in"},
197                         My::Hash::Sub->new({
198                                 "_remote_ip"=>sub { return $W->{"r"}->connection()->remote_ip(); },
199                                 }),
200                         );
201         Wrequire 'My::Hash::Readonly';
202         $W->{"headers_in"}=My::Hash::Readonly->new($W->{"headers_in"});
203         
204         if ($W->{"r"}->method() eq "GET" || $W->{"r"}->method() eq "HEAD") {
205                 for (\$W->{"http_safe"}) {
206                         # Do not: # Extend the current ETag system instead if you would need it:
207                         #         cluck "Explicitely NOT HTTP-Safe for method \"".$W->{"r"}->method()."\"?!?"
208                         #                       if defined($$_) && !$$_;
209                         # as sometimes it just does not make sense to cache it.
210                         $$_=1 if !defined $$_;
211                         }
212                 }
213         else {
214                 for (\$W->{"http_safe"}) {
215                         cluck "Undefined HTTP-Safe-ty for method \"".$W->{"r"}->method()."\"!"
216                                         if !defined($$_);
217                         $$_=0 if !defined $$_;
218                         }
219                 }
220         if ($W->{"http_safe"}) {
221                 Wrequire 'My::Hash::RecordKeys';
222                 $W->{"headers_in_RecordKeys"}=My::Hash::RecordKeys->new($W->{"headers_in"});
223                 $W->{"headers_in"}=$W->{"headers_in_RecordKeys"};
224                 }
225
226         {
227                 local $_=$W->{"r"}->args() || "";
228                 if ($W->{"detect_ent"}) {
229                                  if (/[&]amp;have_ent/)
230                                 { $W->{"have_ent"}=0; }
231                         elsif (    /[&]have_ent/)
232                                 { $W->{"have_ent"}=1; }
233                         else
234                                 { delete $W->{"have_ent"}; }
235                         if (!defined $W->{"have_ent"} && $W->{"r"}->method() eq "GET") {
236                                 $W->{"head"}.='<meta http-equiv="Refresh" content="0; URL='
237                                                 .escapeHTML("http://".$W->{"web_hostname"}."/".($W->{"r"}->uri()=~m#^/*(.*)$#)[0]
238                                                                 ."?".($_ || "detect_ent_glue=1").'&have_ent=detect')
239                                                 .'" />'."\n";
240                                 }
241                         }
242                 s/([&])amp;/$1/g;
243                 $W->{"r"}->args($_);
244                 }
245
246         $W->{"args"}=URI->new("?".$W->{"r"}->args())->query_form_hash();
247         $W->merge_post_args() if $W->{"r"}->method() eq "POST";
248         # Prepare '$args' first to (FIXME: Why?) prevent: Not a reference
249         my $args=$W->{"args"};
250         $W->{"args_orig"}=Storable::dclone($args);
251
252         $W->{"browser"}=sub {
253                 # Lazy-evaluation, we may not need the "User-Agent" header at all.
254                 return our $r||=HTTP::BrowserDetect->new($W->{"headers_in"}{"User-Agent"});
255                 };
256
257         if (!defined $W->{"have_style"}) {
258                 $W->{"have_style"}=sub {
259                         # Lazy-evaluation, we may not need the "User-Agent" header at all.
260                         return our $r||=(!$W->{"browser"}->netscape() || ($W->{"browser"}->major() && $W->{"browser"}->major()>4) ? 1 : 0);
261                         };
262                 }
263
264         $W->{"have_js"}=($W->{"args"}{"have_js"} ? 1 : 0);
265         if ($W->{"detect_js"} && !$W->{"have_js"}) {
266                 # Do not: <script />
267                 # as at least Lynx inhibits any further HTML output.
268                 # Do not: text/javascript
269                 # as it does not look as registered, at least according to: MIME::Types $VERSION 1.15
270                 # "application/javascript" so far standardized till 2005-12-08 by:
271                 #       http://www.ietf.org/internet-drafts/draft-hoehrmann-script-types-03.txt
272                 $W->{"head"}.='<script type="application/javascript" src="'.uri_escaped(path_web('/My/HaveJS.pm')).'"></script>'."\n";
273                 }
274
275         do { _args_check(%$_) if $_; } for ($W->{"args_check"});
276
277         $W->{"_init_done"}=1;
278         return $W;
279 }
280
281 sub merge_post_args($)
282 {
283 my($class)=@_;
284
285         my @post_args=$class->read_post_args();
286         while (@post_args) {
287                 my $name=shift @post_args;
288                 my $data=shift @post_args;
289                 my $ref=\$W->{"args"}{$name};
290                    if (!defined $$ref)       { $$ref=$data; }
291                 elsif (!ref $$ref)           { $$ref=[$$ref,$data]; }
292                 elsif ("ARRAY" eq ref $$ref) { push @$$ref,$data; }
293                 else {
294                         cluck "Ignoring POST argument \"$name\", orig is weird:\n",Dumper($$ref);
295                         }
296                 }
297         return;
298 }
299
300 # Do not: use CGI;
301 # as CGI parsing of POST vs. QUERY_STRING data, multiple-valued keys etc.
302 # is too dense and causes weird problems, together with mod_perl etc.
303 sub read_post_args($)
304 {
305 my($class)=@_;
306
307         local $_=$class->http_headers_in_for("Content-type")->content_type();
308         return $class->read_multipart_form_data() if $_ eq "multipart/form-data";
309         return $class->read_application_x_www_form_urlencoded() if $_ eq "application/x-www-form-urlencoded";
310         cluck "Unknown POST data body, ignored: $_";
311         return;
312 }
313
314 sub read_application_x_www_form_urlencoded($)
315 {
316 my($class)=@_;
317
318         my $body="";
319         for (;;) {
320                 my $got=$W->{"r"}->read(my($buf),0x1000);
321                 # Do not: cluck "Error reading POST data: $!" if !defined $got;
322                 # as it should be done using: APR::Error exceptions
323                 last if !$got;
324                 $body.=$buf;
325                 }
326         return URI->new("?".$body)->query_form();
327 }
328
329 sub read_multipart_form_data($)
330 {
331 my($class)=@_;
332
333         my $parser=MIME::Parser->new();
334         # FIXME: No unlink()s done!
335         $parser->output_under("/tmp");
336
337         local *R_FH;
338         tie *R_FH,$W->{"r"};
339         local *FH;
340         tie *FH,"My::Web::ReadMerged",
341                         join("",map(($_.": ".$W->{"headers_in"}{$_}."\n"),qw(
342                                         Content-type
343                                         )))."\n",
344                         \*R_FH;
345         my $body=$parser->parse(\*FH);
346         cluck "No multipart POST request body?" if !$body->is_multipart();
347
348         return map((
349                         $_->head()->mime_attr("content-disposition.name")
350                         =>
351                         join("",@{$_->body()})
352                         ),$body->parts());
353
354         # TODO: Globalize, make it IO::* compatible, split to the merging part + IO::Scalar.
355         package My::Web::ReadMerged;
356
357         require Tie::Handle;
358         require Exporter;
359         our @ISA=qw(Tie::Handle Exporter);
360         use Carp qw(cluck confess);
361
362         sub READLINE($)
363         {
364         my($self)=@_;
365
366                 confess "Slurp not yet implemented" if !defined $/;
367                 # Apache2::RequestIO does not support 'READLINE'!
368                 for (;;) {
369                         if (defined $self->{"data"} && $self->{"data"}=~s{^.*\Q$/\E}{}) {
370                                 $self->{"offset"}+=length $&;
371                                 return $&;
372                                 }
373                         my $fh_orig=$self->{"fh_orig"};
374                         if (!$fh_orig) {
375                                 my $r=$self->{"data"};
376                                 delete $self->{"data"};
377                                 $self->{"offset"}+=length $r if defined $r;
378                                 return $r;
379                                 }
380                         my $got=read $fh_orig,my($buf),0x1000;
381                         cluck "Error reading POST data: $!" if !defined $got;
382                         delete $self->{"fh_orig"} if !$got;
383                         cluck "INTERNAL: fh_orig should not exist here" if !defined $self->{"data"};
384                         $self->{"data"}.=$buf;
385                         }
386         }
387
388         sub TELL($)
389         {
390         my($self)=@_;
391
392                 return $self->{"offset"};
393         }
394
395         sub TIEHANDLE($$$)
396         {
397         my($class,$data,$fh_orig)=@_;
398
399                 my $self=bless {},$class;
400                 $self->{"data"}=$data;
401                 $self->{"offset"}=0;
402                 $self->{"fh_orig"}=$fh_orig;
403                 return $self;
404         }
405 }
406
407 sub cleanup($)
408 {
409 my($apache_request)=@_;
410
411         cluck "CORE::GLOBAL::exit hook not ran" if !$W->{"_exit_done"};
412         cluck "packages not finalized" if !$packages_used_hash{$W->{"__PACKAGE__"}}{"_done"};
413         cache_finish();
414         # Sanity protection.
415         $W=undef();
416         exit_hook_stop();
417         return OK;
418 }
419
420 # PerlResponseHandler is RUN_FIRST and &ModPerl::Util::exit returns OK, so no (sane) go.
421 # PerlLogHandler is already too late to be able to produce any output.
422 my $exit_orig;
423 sub exit_hook
424 {
425         cluck "Missing ->init while in exit_hook()" if !$W->{"_init_done"};
426         # &footer will call us recursively!
427         footer() if !$W->{"_exit_done"}++;
428         return &{$exit_orig}(@_);
429 }
430 sub exit_hook_start
431 {
432         do { cluck "exit_hook_start() twice?"; return; } if defined $exit_orig;
433         $exit_orig=\&CORE::GLOBAL::exit;
434         # Prevent: Subroutine CORE::GLOBAL::exit redefined
435         no warnings 'redefine';
436         *CORE::GLOBAL::exit=\&exit_hook;
437 }
438 sub exit_hook_stop
439 {
440         do { cluck "exit_hook_stop() without exit_hook_start()?"; return; }
441                         if \&exit_hook ne \&CORE::GLOBAL::exit;
442         do { cluck "INTERNAL: exit_orig uninitialized"; return; }
443                         if !$exit_orig;
444         # Prevent: Subroutine CORE::GLOBAL::exit redefined
445         no warnings 'redefine';
446         *CORE::GLOBAL::exit=$exit_orig;
447         $exit_orig=undef();
448 }
449
450 # Be aware other parts of code (non-My::Web) will NOT use this function!
451 # Do not: Wprint $W->{"heading"},"undef"=>1;
452 # as we would need to undef() it to turn it off and it would get defaulted in such case.
453 # Do not: exists $W->{"heading"}
454 # as we use a lot of 'for $W->{"heading"}' which instantiates it with the value: undef()
455 sub Wprint($%)
456 {
457 my($text,%args)=@_;
458
459         cluck "undef Wprint" if !defined $text && !$args{"undef"};
460         delete $args{"undef"};
461         cluck join(" ","Invalid arguments:",keys(%args)) if keys(%args);
462         return if !defined $text;
463         # Do not: cluck "utf-8 untested" if Encode::is_utf8($text);
464         # as it is valid here.
465         $W->{"r"}->puts($text);
466 }
467
468 sub request_check(;$)
469 {
470 my($self)=@_;
471
472         # Use &eval to prevent: Global $r object is not available. Set:\n\tPerlOptions +GlobalRequest\nin ...
473         confess "Calling sensitive dynamic code from a static code" if !eval { Apache2::RequestUtil->request(); };
474         # Do not: confess "Calling sensitive dynamic code without My::Web::init" if !$W->{"__PACKAGE__"};
475         # as it is valid at least while preparing arguments to call: &project::Lib::init
476 }
477
478 # Do not: use CGI;
479 # as it is too much backward compatible regarding the charset encodings etc.
480 # and the resulting code is too dense with no additional functionality for the recent content.
481 sub escapeHTML($)
482 {
483 my($text)=@_;
484
485         local $_=$text;
486         s{&}{&amp;}gso;
487         s{<}{&lt;}gso;
488         s{>}{&gt;}gso;
489         s{"}{&quot;}gso;
490         return $_;
491 }
492
493 # /home/user/www/webdir
494 sub dir_top_abs_disk()
495 {
496         our $dir_top_abs_disk;
497         if (!$dir_top_abs_disk) {
498                 my $selfpkg_relpath=__PACKAGE__;
499                 $selfpkg_relpath=~s{::}{/}g;
500                 $selfpkg_relpath.=".pm";
501                 my $selfpkg_abspath=$INC{$selfpkg_relpath} or do {
502                         cluck "Unable to find self package $selfpkg_relpath";
503                         return;
504                         };
505                 $selfpkg_abspath=~s{/*\Q$selfpkg_relpath\E$}{} or do {
506                         cluck "Unable to strip myself \"$selfpkg_relpath\" from the abspath: $selfpkg_abspath";
507                         return;
508                         };
509                 cluck "INC{myself} is relative?: $selfpkg_abspath" if $selfpkg_abspath!~m{^/};
510                 $dir_top_abs_disk=$selfpkg_abspath;
511                 }
512         return $dir_top_abs_disk;
513 }
514
515 sub unparsed_uri()
516 {
517         request_check();
518         if (!$W->{"unparsed_uri"}) {
519                 # Do not: $W->{"r"}
520                 # as we may be called before &init from: &My::Project::init
521                 my $r=Apache2::RequestUtil->request();
522                 cluck "Calling ".'&unparsed_uri'." from a static code, going to fail" if !$r;
523                 my $uri_string=$r->unparsed_uri() or cluck "Valid 'r' missing unparsed_uri()?";
524                 my $uri=URI->new_abs($uri_string,"http://".$W->{"web_hostname"}."/");
525                 $W->{"unparsed_uri"}=$uri;
526                 }
527         return $W->{"unparsed_uri"};
528 }
529
530 sub in_to_uri_abs($)
531 {
532 my($in)=@_;
533
534         # Otherwise we may have been already processed and thus legally relativized.
535         # FIXME data: Currently disabled, all the data are too violating such rule.
536         if (0 && !ref $in) {
537                 my $uri_check=URI->new($in);
538                 $uri_check->scheme() || $in=~m{^\Q./\E} || $in=~m{^/}
539                                 or cluck "Use './' or '/' prefix for all the local references: $in";
540                 }
541         my $uri=URI->new_abs($in,unparsed_uri());
542         $uri=$uri->canonical();
543         return $uri;
544 }
545
546 # $args{"uri_as_in"}=1 to permit passing URI objects as: $in
547 # $args{"abs"}=1;
548 sub path_web($%)
549 {
550 my($in,%args)=@_;
551
552         cluck if !$args{"uri_as_in"} && ref $in;
553         my $uri=in_to_uri_abs($in);
554         if (uri_is_local($uri)) {
555                 # Prefer the $uri values over "args_persistent" values.
556                 # &query_form_hash comes from: URI::QueryParam
557                 $uri->query_form_hash({
558                                 map({
559                                         my $key=$_;
560                                         my $val=$W->{"args"}{$key};
561                                         (!defined $val ? () : ($key=>$val));
562                                         } keys(%{$W->{"args_persistent"}})),
563                                 %{$uri->query_form_hash()},
564                                 });
565                 }
566         return $uri->abs(unparsed_uri()) if $W->{"args"}{"Wabs"} || $args{"abs"};
567         return $uri->rel(unparsed_uri());
568 }
569
570 sub path_abs_disk_register($)
571 {
572 my($path_abs_disk)=@_;
573
574         $W->{"path_abs_disk_register"}{$path_abs_disk}=1;
575 }
576
577 # $args{"uri_as_in"}=1 to permit passing URI objects as: $in
578 sub path_abs_disk($%)
579 {
580 my($in,%args)=@_;
581
582         cluck if !$args{"uri_as_in"} && ref $in;
583         my $uri=in_to_uri_abs($in);
584         cluck if !uri_is_local($uri);
585         my $path=$uri->path();
586         cluck "URI compatibility: ->path() not w/leading slash of URI \"$uri\"; path: $path" if $path!~m{^/};
587         my $r=dir_top_abs_disk().$path;
588         path_abs_disk_register $r if !defined $args{"register"} || $args{"register"};
589         return $r;
590 }
591
592 sub fatal (;$);
593
594 sub _args_check (%)
595 {
596 my(%tmpl)=@_;
597
598         while (my($name,$regex)=each(%tmpl)) {
599                 my $name_html="Parameter <span class=\"quote\">".escapeHTML($name)."</span>";
600                 $W->{"args"}{$name}="" if !defined $W->{"args"}{$name};
601                 $W->{"args"}{$name}=[ $W->{"args"}{$name} ] if !ref $W->{"args"}{$name} && ref $regex;
602                 fatal "$name_html passed as multivar although singlevar expected"
603                                 if ref $W->{"args"}{$name} && !ref $regex;
604                 $regex=$regex->[0] if ref $regex;
605                 for my $val (!ref $W->{"args"}{$name} ? $W->{"args"}{$name} : @{$W->{"args"}{$name}}) {
606                         $val="" if !defined $val;
607                         fatal "$name_html <span class=\"quote\">".escapeHTML($val)."</span>"
608                                                         ." does not match the required regex <span class=\"quote\">".escapeHTML($regex)."</span> "
609                                         if $regex ne "" && $val!~/$regex/;
610                         }
611                 }
612 }
613
614 sub vskip (;$)
615 {
616 my($height)=@_;
617
618         return '<p'.(!defined $height ? "" : ' style="height: '.$height.';"').'>&nbsp;</p>'."\n";
619 }
620
621 sub fatal (;$)
622 {
623 my($msg)=@_;
624
625         $msg="UNKNOWN" if !$msg;
626         cluck "FATAL: $msg";
627
628         # Do not send it unconditionally.
629         # The intial duplicated '<?xml...' crashes Gecko parser.
630         $W->{"heading_done"}=0 if $W->{"header_only"};
631         # Do not send it unconditionally.
632         # Prevents warn: Headers already sent
633         if (!$W->{"heading_done"}) {
634                 $W->{"indexme"}=0;      # For the case no heading was sent yet.
635                 $W->{"header_only"}=0;  # assurance for &heading
636                 My::Web->heading();
637                 }
638         Wprint "\n".vskip("3ex")."<hr /><h1 class=\"error\">FATAL ERROR: $msg!</h1>\n"
639                         ."<p>You can report this problem's details to"
640                         ." ".a_href("mailto:".$W->{"admin_mail"},"admin of this website").".</p>\n";
641         exit;
642 }
643
644 sub footer_packages_used_comments()
645 {
646         my $packages_used=$packages_used_array{$W->{"__PACKAGE__"}};
647         for my $package (@$packages_used) {
648                 my $cvs_id=(eval('$'.$package."::CVS_ID")
649 #                               || $package     # debug
650                                 );
651                 Wprint '<!-- '.$package.' - $'.$cvs_id.'$ -->'."\n" if $cvs_id;
652                 }
653 }
654
655 sub footer()
656 {
657         cluck 'Explicit &footer call is deprecated, !_exit_dne' if !$W->{"_exit_done"};
658         exit if $W->{"footer_done"}++;  # deadlock prevention:
659         &{$_}() for reverse @{$W->{"footer_sub_push"}};
660         if ($W->{"header_only"}) {
661                 $packages_used_hash{$W->{"__PACKAGE__"}}{"_done"}=1;
662                 exit;
663                 }
664
665         Wprint vskip if $W->{"footer_delimit"};
666         &{$_}() for reverse @{$W->{"footing_delimit_sub_push"}};
667         Wprint "<hr />\n" if $W->{"footer"};
668
669         # Never update the package list while we examine it!
670         $packages_used_hash{$W->{"__PACKAGE__"}}{"_done"}=1;
671
672         my $packages_used=$packages_used_array{$W->{"__PACKAGE__"}};
673         if ($W->{"footer_ids"}) {
674                 Wprint '<p class="cvs-id">';
675                 Wprint join("<br />\n",map({ my $package=$_;
676                         my $cvs_id=(eval('$'.$package."::CVS_ID")
677 #                                       || $package     # debug
678                                         );
679                         if (!$cvs_id) {
680                                 ();
681                                 }
682                         else {
683                                 $cvs_id='$'.$cvs_id.'$';        # Eaten by 'q' operator.
684                                 my @cvs_id_split=split / +/,$cvs_id;
685                                 if (@cvs_id_split==8) {
686                                         my $file=$package;
687                                         $file=~s#::#/#g;
688                                         my $ext;
689                                         my @tried;
690                                         for (qw(.pm)) {
691                                                 $ext=$_;
692                                                 my $path_abs_disk=path_abs_disk("/$file$ext");
693                                                 push @tried,$path_abs_disk;
694                                                 last if -r $path_abs_disk;
695                                                 cluck "Class file $file not found; tried: ".join(" ",@tried) if !$ext;
696                                                 }
697                                         $file.=$ext;
698                                         my $viewcvs;
699                                         if ((my $file_cvs=$file)=~s{^My/}{}) {
700                                                 $viewcvs=$W->{"viewcvs_My"}.$file_cvs;
701                                                 }
702                                         else {
703                                                 $viewcvs=$W->{"viewcvs"}.$file;
704                                                 }
705                                         $cvs_id_split[2]=""
706                                                         .a_href((map({ my $s=$_; $s=~s#/viewcvs/#$&~checkout~/#; $s; } $viewcvs))[0]."?rev=".$cvs_id_split[2],
707                                                                         $cvs_id_split[2]);
708                                         $cvs_id_split[1]=a_href($viewcvs,($package!~/^Apache2::/ ? $package : $cvs_id_split[1]));
709                                         $cvs_id_split[5]=&{$W->{"cvs_id_author_sub"}}($cvs_id_split[5]);
710                                         }
711                                 join " ",@cvs_id_split;
712                                 }
713                         } @$packages_used));
714                 Wprint "</p>\n";
715                 }
716
717         footer_packages_used_comments();
718
719         do { Wprint $_ if $_; } for $W->{"footing"};
720
721         Wprint "</body></html>\n";
722         exit;
723 }
724
725 # Existing entries are overwritten.
726 sub header(%)
727 {
728 my(%pairs)=@_;
729
730         while (my($key,$val)=each(%pairs)) {
731                 do { cluck "Headers already sent"; next; } if $W->{"heading_done"};
732                 $W->{"r"}->headers_out()->set($key,$val);
733                 }
734 }
735
736 sub size_display ($)
737 {
738 my($size)=@_;
739
740            if ($size<4096)
741                 {}
742         elsif ($size<1024*1024)
743                 { $size=sprintf "%.1fK",$size/1024; }
744         else
745                 { $size=sprintf "%.1fM",$size/1024/1024; }
746         $size.="B";
747         return $size;
748 }
749
750 sub uri_is_local($)
751 {
752 my($in)=@_;
753
754         my $uri_rel=in_to_uri_abs($in)->rel(unparsed_uri());
755         # Do not: defined $uri_rel->("userinfo"|"host"|"port")();
756         # as they fail to be called for schemes not supporting them.
757         return 0 if $uri_rel->scheme();
758         return 0 if $uri_rel->authority();
759         return 1;
760 }
761
762 # &path_web still may be required for &uri_escaped !
763 sub uri_escaped($)
764 {
765 my($uri)=@_;
766
767         cluck if !ref $uri;
768         my $urient=escapeHTML($uri);
769         return $uri    if $uri eq $urient;
770         request_check();
771         return $urient if uri_is_local $uri;
772         return $uri    if defined $W->{"have_ent"} && !$W->{"have_ent"};        # non-ent client
773         return $urient if $W->{"have_ent"};     # ent client
774         # Unknown client, &escapeHTML should not be needed here:
775         return escapeHTML(path_web('/My/Redirect.pm?location='.uri_escape($uri->abs(unparsed_uri()))));
776 }
777
778 our $a_href_inhibited;
779 sub a_href($;$%)
780 {
781 my($in,$contents,%args)=@_;
782
783         request_check();
784         do { $$_=1 if !defined $$_; } for (\$args{"size"});
785         if (!defined $contents) {
786                 $contents=$in;
787                 $contents=File::Basename::basename($contents) if $args{"basename"};
788                 $contents=escapeHTML($contents);
789                 }
790         $contents=~s#<a\b[^>]*>##gi;
791         $contents=~s#</a>##gi;
792         return $contents if $a_href_inhibited;
793
794         my $path_web=path_web $in,%args;
795         my $r="";
796         $r.='<a href="';
797         $r.=uri_escaped $path_web;
798         $r.='"';
799         do { $r.=" $_" if $_; } for ($args{"attr"});
800         $r.='>'.$contents.'</a>';
801         if ($args{"size"} && uri_is_local($in) && ($args{"size"}>=2 || $in=~/[.](?:gz|Z|rpm|zip|deb|lha)/)) {   # Downloadable?
802                 my $path_abs_disk=path_abs_disk $in,%args;
803                 cluck "File not readable: $path_abs_disk" if !-r $path_abs_disk;
804                 $r.='&nbsp;('.size_display((stat($path_abs_disk))[7]).')';
805                 }
806         return $r;
807 }
808
809 sub a_href_inhibit($$;@)
810 {
811 my($self,$sub,@sub_args)=@_;
812
813         local $a_href_inhibited=1;
814         return &{$sub}(@sub_args);
815 }
816
817 sub input_hidden_persistents()
818 {
819         request_check();
820         return join("",map({
821                 my $key=$_;
822                 my $val=$W->{"args"}{$key};
823                 (!defined $val ? () : '<input type="hidden"'
824                                 .' name="'.escapeHTML($key).'"'
825                                 .' value="'.escapeHTML($val).'"'
826                                 .' />'."\n");
827                 } (keys(%{$W->{"args_persistent"}}))));
828 }
829
830 sub http_moved($$;$)
831 {
832 my($self,$url,$status)=@_;
833
834         $url=path_web($url,"abs"=>1);
835         $status||=HTTP_MOVED_TEMPORARILY;
836         $W->{"r"}->status($status);
837         $W->{"r"}->headers_out()->{"Location"}=$url;
838         $W->{"header_only"}=1;
839         $W->{"content_type"}=0;
840         $W->{"charset"}=0;
841         My::Web->heading();
842         exit;
843         die "NOTREACHED";
844 }
845
846 sub remote_ip ()
847 {
848         # Do not: PerlModule                 Apache2::ForwardedFor
849         #         PerlPostReadRequestHandler Apache2::ForwardedFor
850         # As 'Apache2::ForwardedFor' takes the first of $ENV{"HTTP_X_FORWARDED_FOR"}
851         # while the contents is '127.0.0.1, 213.220.195.171' if client has its own proxy.
852         # We must take the last item ourselves.
853         # Be VERY sure you always retrieve all the headers unconditionally to hit: My::Hash::RecordKeys
854         my $x_forwarded_for=$W->{"headers_in"}{"X-Forwarded-For"};
855         $x_forwarded_for=~s/^.*,\s*// if $x_forwarded_for;
856         my $remote_ip=$W->{"headers_in"}{"_remote_ip"};
857         my $r;
858         $r||=$x_forwarded_for;
859         $r||=$remote_ip;
860         return $r;
861 }
862
863 # $url={"JP"=>"http://specific",...};
864 # $url={""=>"http://default",...};
865 sub a_href_cc($$;%)
866 {
867 my($url,$contents,%args)=@_;
868
869         # A bit ineffective but we must process all the possibilities to get stable 'headers_in' hits!
870         my %map=map(($_=>a_href($url->{$_},$contents,%args)),keys(%$url));
871         my $cc;
872         $cc||=Geo::IP->new()->country_code_by_addr(remote_ip()) if $have_Geo_IP;
873         $cc||="";
874         my $r=$map{$cc};
875         return $r if $r;
876         return $contents;
877 }
878
879 sub make ($)
880 {
881 my($cmd)=@_;
882
883         # FIXME: &alarm, --timeout is now infinite.
884         # FIXME: Try to remove bash(1).
885         # FIXME: Use: @PATH_FLOCK@
886         my @argv=('flock',dir_top_abs_disk(),'bash','-c',$cmd.' >&2');
887         print STDERR join(" ","SPAWN:",@argv)."\n";
888         system @argv;
889 }
890
891 sub make_file($$)
892 {
893 my($self,$file)=@_;
894
895         cluck "Pathname not absolute: $file" if $file!~m{^/};
896         return if -f $file;
897         # TODO: Somehow quickly check dependencies?
898         return make('make -s --no-print-directory'
899                                         .' -C '."'".File::Basename::dirname($file)."' '".File::Basename::basename($file)."'");
900 }
901
902 sub img_size ($$)
903 {
904 my($width,$height)=@_;
905
906         cluck if !defined $width || !defined $height;
907         return ($W->{"have_style"} ? "style=\"border:0;width:${width}px;height:${height}px\"" : "border=\"0\"")
908                         ." width=\"$width\" height=\"$height\"";
909 }
910
911 sub negotiate_variant (%)
912 {
913 my(%args)=@_;
914
915         my @fields=("id","qs","content-type","encoding","charset","lang","size");
916         return [ map(($args{$_}),@fields) ];
917 }
918
919 # Returns: 'HTTP::Headers' instance.
920 sub http_headers_in_for($@)
921 {
922 my($self,@headers)=@_;
923
924         # Limit these entries to generate proper 'Vary' header.
925         return HTTP::Headers->new(map(($_=>$W->{"headers_in"}{$_}),@headers));
926 }
927
928 # Input: $self is required!
929 # Input: Put the fallback variant as the first one.
930 # Returns: always only scalar!
931 sub Negotiate_choose($$)
932 {
933 my($self,$variants)=@_;
934
935         my $best=HTTP::Negotiate::choose($variants,
936                         # Do not: $W->{"r"}
937                         # to prevent: Can't locate object method "scan" via package "Apache2::RequestRec" at HTTP/Negotiate.pm line 84.
938                         # Do not: $W->{"r"}->headers_in()
939                         # to prevent: Can't locate object method "scan" via package "APR::Table" at HTTP/Negotiate.pm line 84.
940                         # Do not: HTTP::Headers->new($W->{"r"}->headers_in());
941                         # to prevent empty result or even: Odd number of elements in anonymous hash
942                         $self->http_headers_in_for(qw(
943                                         Accept
944                                         Accept-Charset
945                                         Accept-Encoding
946                                         Accept-Language
947                                         )));
948         $best||=$variants->[0][0];      # $variants->[0]{"id"}; &HTTP::Negotiate::choose failed?
949         return $best;
950 }
951
952 my @img_variants=(
953                 { "id"=>"png","qs"=>0.9,"content-type"=>"image/png" },
954                 { "id"=>"gif","qs"=>0.7,"content-type"=>"image/gif" },
955                 );
956 my $img_variants_re='[.](?:'.join('|',"jpeg",map(($_->{"id"}),@img_variants)).')$';
957
958 # Returns: ($path_web,$path_abs_disk)
959 # URI path segments support ignored here. Where it is used? (';' path segment options)
960 sub _img_src($%)
961 {
962 my($in,%args)=@_;
963
964         cluck if !uri_is_local $in;
965         my $uri=in_to_uri_abs $in;
966         my $path_abs_disk=path_abs_disk $uri,%args,"uri_as_in"=>1,"register"=>0;
967
968         # Known image extension?
969         return path_web($uri,%args,"uri_as_in"=>1),$path_abs_disk if $uri->path()=~m#$img_variants_re#o;
970
971         my @nego_variants;
972         for my $var (@img_variants) {
973                 my $path_abs_disk_variant=$path_abs_disk.".".$var->{"id"};
974                 path_abs_disk_register($path_abs_disk_variant);
975                 __PACKAGE__->make_file($path_abs_disk_variant);
976                 push @nego_variants,negotiate_variant(
977                                 %$var,
978                                 "size"=>(stat $path_abs_disk_variant)[7],
979                                 );
980                 }
981         my $ext=__PACKAGE__->Negotiate_choose(\@nego_variants);
982
983         $uri->path($uri->path().".$ext");
984         return path_web($uri,%args,"uri_as_in"=>1),path_abs_disk($uri,%args,"uri_as_in"=>1);
985 }
986
987 # $args{"attr"}
988 sub img ($$%)
989 {
990 my($in,$alt,%args)=@_;
991
992         request_check();
993         my($path_web,$path_abs_disk)=_img_src($in,%args);
994         my($width,$height)=Image::Size::imgsize($path_abs_disk);
995         $alt=~s/<[^>]*>//g;
996         $alt=escapeHTML($alt);
997         my $content="<img src=\"".uri_escaped($path_web)."\" alt=\"$alt\" title=\"$alt\" ".img_size($width,$height)
998                         .(!$args{"attr"} ? "" : " ".$args{"attr"})." />";
999         do { return a_href((_img_src($_))[0],$content,"uri_as_in"=>1) if $_; } for $args{"a_href_img"};
1000         do { return a_href $_,$content if $_; } for $args{"a_href"};
1001         return $content;
1002 }
1003
1004 sub centerimg
1005 {
1006         my $r="";
1007         $r.='<table border="0" width="100%"><tr>'."\n";
1008         @_=( [@_] ) if !ref $_[0];
1009         for (@_) {
1010                 $r.="\t".'<td align="center">'.&{\&img}(@$_).'</td>'."\n";
1011                 }
1012         $r.='</tr></table>'."\n";
1013         return $r;
1014 }
1015
1016 sub rightimg
1017 {
1018 my($text,@args_img)=@_;
1019
1020         # FIXME: Workaround bug of 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)':
1021         #        <col width="@{[ (!$W->{"browser"}->ie() ? "1*" : "90%" ) ]}" />
1022         #        <col width="@{[ (!$W->{"browser"}->ie() ? "0*" : "10%" ) ]}" />
1023         # causes whole invisible projects in: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.10) Gecko/20050719 Galeon/1.3.21
1024         return <<"HERE";
1025 <table border="0" width="100%">
1026         <tr>
1027                 <td align="left">
1028                         @{[ $text ]}
1029                 </td>
1030                 <td align="right">
1031                         @{[ &{\&img}(@args_img) ]}
1032                 </td>
1033         </tr>
1034 </table>
1035 HERE
1036 }
1037
1038 sub readfile($$)
1039 {
1040 my($class,$filename)=@_;
1041
1042         local *F;
1043         open F,$filename or cluck "Cannot open \"$filename\": $!";
1044         my $F=do { local $/=undef(); <F>; };
1045         close F or cluck "Cannot close \"$filename\": $!";
1046         return $F;
1047 }
1048
1049 sub _no_cache($)
1050 {
1051 my($self)=@_;
1052
1053         header("Expires"=>HTTP::Date::time2str(1000000000));    # date in the past
1054         header("Last-Modified"=>HTTP::Date::time2str());        # always modified
1055         header("Cache-Control"=>join(", ",
1056                         "no-cache",
1057                         "no-store",
1058                         "must-revalidate",
1059                         "max-age=0",
1060                         "pre-check=0",  # MSIE
1061                         "post-check=0", # MSIE
1062                         ));     # HTTP/1.1
1063         header("Pragma"=>"no-cache");   # HTTP/1.0
1064         header("Vary"=>"*");    # content may ba based on unpredictable sources
1065 }
1066
1067 sub headers_in_filtered(@)
1068 {
1069 my(@keys)=@_;
1070
1071         return map(($_=>$W->{"headers_in"}{$_}),@keys);
1072 }
1073
1074 our %uri_args_frozen_to_headers_in_keys;
1075 our %uri_args_headers_in_frozen_to_headers_out;
1076
1077 sub uri_args_headers_in_frozen_get($)
1078 {
1079 my($headers_in_keys_arrayref)=@_;
1080
1081         my %uri_args_headers_in_hash=(
1082                 "uri_args_frozen"=>$W->{"uri_args_frozen"},
1083                 "headers_in"=>{ headers_in_filtered(@$headers_in_keys_arrayref) },
1084                 );
1085         return do { local $Storable::canonical=1; Storable::freeze(\%uri_args_headers_in_hash); };
1086 }
1087
1088 sub cache_output_filter($)
1089 {
1090 my($f)=@_;
1091
1092         while ($f->read(my $text,0x400)) {
1093                 cluck "utf-8 untested" if Encode::is_utf8($text);       # Possible here at all?
1094                 $f->print($text);
1095                 $W->{"digest-md5"}->add($text);
1096                 }
1097         return OK;
1098 }
1099
1100 sub cache_start()
1101 {
1102         if (!$W->{"http_safe"}) {
1103                 __PACKAGE__->_no_cache();
1104                 return;
1105                 }
1106
1107         {
1108                 # &Wrequire it here even if it will not be later used; to be stable!
1109                 Wrequire 'My::Hash::RestrictTo';
1110                 my %uri_args_hash=(
1111                         "method"=>$W->{"r"}->method(),
1112                         "uri"=>"http://".$W->{"web_hostname"}."/".$W->{"r"}->uri(),
1113                         "args"=>$W->{"args_orig"},
1114                         );
1115                 $W->{"uri_args_frozen"}=do { local $Storable::canonical=1; Storable::freeze(\%uri_args_hash); };
1116                 last if !(my $headers_in_keys_arrayref=$uri_args_frozen_to_headers_in_keys{$W->{"uri_args_frozen"}});
1117
1118                 # Protection to be sure we are stable:
1119                 $W->{"headers_in"}=My::Hash::RestrictTo->new($W->{"headers_in"},@$headers_in_keys_arrayref);
1120
1121                 $W->{"uri_args_headers_in_frozen"}=uri_args_headers_in_frozen_get($headers_in_keys_arrayref);
1122                 last if !(my $headers_out_hashref=$uri_args_headers_in_frozen_to_headers_out{$W->{"uri_args_headers_in_frozen"}});
1123                 header(%$headers_out_hashref);
1124                 my $status;
1125                 {
1126                         # &meets_conditions will always deny the attempt if !2xx status().
1127                         # At least ap_read_request() sets: r->status=HTTP_REQUEST_TIME_OUT;     /* Until we get a request */
1128                         my $status_old=$W->{"r"}->status();
1129                         $W->{"r"}->status(HTTP_OK);
1130                         # Update httpd's 'r->mtime' as the header "Last-Modified" is just not enough for ap_meets_conditions():
1131                         # &update_mtime() argument is really in _secs_, not in _msecs_ as the docs claim.
1132                         # Be aware '*1000000' would overflow Perl integer anyway.
1133                         # &set_last_modified would also override the "Last-Modified" headers_out!
1134                         # &mtime may exist but somehow does not work.
1135                         $W->{"r"}->update_mtime(HTTP::Date::str2time($headers_out_hashref->{"Last-Modified"}));
1136                         $status=$W->{"r"}->meets_conditions();
1137                         $W->{"r"}->status($status_old);
1138                         }
1139                 last if OK==$status;
1140                 $W->{"r"}->status($status);
1141                 $W->{"header_only"}=1;  # Inhibit &footer output.
1142                 exit;
1143                 die "NOTREACHED";
1144                 }
1145
1146         $W->{"digest-md5"}=Digest::MD5->new();
1147         $W->{"cache_active"}=1;
1148         $W->{"r"}->add_output_filter(\&cache_output_filter);
1149 }
1150
1151 sub cache_finish_last_modified()
1152 {
1153         cluck "Not yet done now? W __PACKAGE__: ".$W->{"__PACKAGE__"}
1154                         if !$packages_used_hash{$W->{"__PACKAGE__"}}{"_done"};
1155         for my $package_orig (@{$packages_used_array{$W->{"__PACKAGE__"}}}) {
1156                 local $_=$package_orig.".pm";
1157                 s{::}{/}g;
1158                 path_abs_disk "/$_","register"=>1;
1159                 }
1160         my $mtime_newest;
1161         for my $path_abs_disk (keys(%{$W->{"path_abs_disk_register"}})) {
1162                 my $mtime=(stat $path_abs_disk)[9];
1163                 do { cluck "No mtime for: $path_abs_disk"; next; } if !$mtime;
1164                 $mtime_newest=$mtime if !$mtime_newest || $mtime_newest<$mtime;
1165                 }
1166         cluck "No mtime_newest found for the current W __PACKAGE__: ".$W->{"__PACKAGE__"}
1167                         if !$mtime_newest;
1168         return HTTP::Date::time2str($mtime_newest);
1169 }
1170
1171
1172 sub cache_finish()
1173 {
1174         # Do not: return if !$W->{"uri_args_frozen"};
1175         # as we may have just gave 304 and 'exit;' without starting the caching.
1176         return if !$W->{"cache_active"};
1177
1178         # Headers may not be complete in this case; not sure, just trying.
1179         return if $W->{"r"}->connection()->aborted();
1180
1181         # Fill-in/check: %uri_args_frozen_to_headers_in_keys
1182         my $headers_in_keys_stored_arrayref_ref=\$uri_args_frozen_to_headers_in_keys{$W->{"uri_args_frozen"}};
1183         my @headers_in_keys=tied(%{$W->{"headers_in_RecordKeys"}})->accessed();
1184         if (!$$headers_in_keys_stored_arrayref_ref
1185                         || !Data::Compare::Compare(\@headers_in_keys,$$headers_in_keys_stored_arrayref_ref)) {
1186                 cluck "Non-matching generated 'headers_in_keys' per 'uri_args_frozen' key:\n"
1187                                                 .Dumper(\@headers_in_keys,$$headers_in_keys_stored_arrayref_ref)
1188                                 if $$headers_in_keys_stored_arrayref_ref;
1189                 # Build or possibly prevent such further warn dupes:
1190                 $$headers_in_keys_stored_arrayref_ref=\@headers_in_keys;
1191                 # Build or regenerate as obsoleted now:
1192                 $W->{"uri_args_headers_in_frozen"}=uri_args_headers_in_frozen_get(\@headers_in_keys);
1193                 }
1194
1195         # Prepare 'headers_out' for the future reusal:
1196         my %headers_out;
1197         # Do not: $W->{"digest-md5"}->b64digest();
1198         # as it will not provide the trailing filling '='s.
1199         # RFC 1864 is not clear if they should be there but its sample provides them.
1200         # Do not try to provide canonical "\r\n" form of newlines as is said by RFC 1864.
1201         # RFC 2068 (HTTP/1.1) section 14.16 says the newlines should NOT be converted for HTTP.
1202         # ',""' to avoid breaking the headers by its default "\n".
1203         $headers_out{"Content-MD5"}=MIME::Base64::encode_base64($W->{"digest-md5"}->digest(),"");
1204         # In fact we could also use MD5 for ETag as if we know ETag we also know MD5.
1205         # But this way we do not need to calculate MD5 and we still can provide such ETag. So.
1206         # $W->{"r"}->set_etag() ?
1207         $headers_out{"ETag"}='"'.Digest::MD5::md5_base64($W->{"uri_args_headers_in_frozen"}).'"';
1208         # $W->{"r"}->set_content_length() ?
1209         $headers_out{"Content-Length"}=$W->{"r"}->bytes_sent();
1210         my %Vary=map(($_=>1),(@headers_in_keys));
1211         for (keys(%Vary)) {
1212                 next if !/^_/;
1213                 $Vary{"*"}=1;
1214                 delete $Vary{$_};
1215                 }
1216         %Vary=("*"=>1) if $Vary{"*"};
1217         $headers_out{"Vary"}=join(", ",sort keys(%Vary)) if keys(%Vary);
1218         # $W->{"r"}->set_last_modified() ?
1219         $headers_out{"Last-Modified"}=cache_finish_last_modified();
1220
1221         # Fill-in/check: %uri_args_headers_in_frozen_to_headers_out
1222         my $headers_out_stored_hashref_ref=\$uri_args_headers_in_frozen_to_headers_out{$W->{"uri_args_headers_in_frozen"}};
1223         if (!$$headers_out_stored_hashref_ref
1224                         || !Data::Compare::Compare(\%headers_out,$$headers_out_stored_hashref_ref)) {
1225                 cluck "Non-matching generated 'headers_out' per 'uri_args_headers_in_frozen' key:\n"
1226                                                 .Dumper(\%headers_out,$$headers_out_stored_hashref_ref)
1227                                 if $$headers_out_stored_hashref_ref;
1228                 # Build or possibly prevent such further warn dupes:
1229                 $$headers_out_stored_hashref_ref=\%headers_out;
1230                 }
1231
1232 ###print STDERR Dumper(\%uri_args_frozen_to_headers_in_keys,\%uri_args_headers_in_frozen_to_headers_out);
1233 }
1234
1235 sub heading()
1236 {
1237 my($class)=@_;
1238
1239         if (!$W->{"header_only"}) {
1240                 header("Content-Style-Type"=>"text/css");
1241                 # Do not: text/javascript
1242                 # as it does not look as registered, at least according to: MIME::Types $VERSION 1.15
1243                 # "application/javascript" so far standardized till 2005-12-08 by:
1244                 #       http://www.ietf.org/internet-drafts/draft-hoehrmann-script-types-03.txt
1245                 header("Content-Script-Type"=>"application/javascript");
1246                 # $W->{"r"}->content_languages() ?
1247                 do { header("Content-Language"=>$_) if $_; } for $W->{"language"};
1248                 }
1249         # TODO: Support also: private
1250         header("Cache-Control"=>"public");      # HTTP/1.1
1251
1252         # Use $W->{"charset"}=0 to disable charset.
1253         $W->{"charset"}="us-ascii"
1254                         if !defined $W->{"charset"} && (!defined($W->{"content_type"}) || $W->{"content_type"});
1255
1256         # Workaround bug
1257         #   https://bugzilla.mozilla.org/show_bug.cgi?id=120556
1258         # of at least
1259         #   Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8b) Gecko/20050217
1260         # http://validator.w3.org/ does not send ANY "Accept" headers!
1261         if (!defined $W->{"content_type"}) {
1262                 # Be _stable_ for "headers_in".
1263                 my $accept=$W->{"headers_in"}{"Accept"};
1264                 my $user_agent=$W->{"headers_in"}{"User-Agent"}||"";
1265                 $W->{"content_type"}="application/xhtml+xml"
1266                                 if !$accept && $user_agent=~m{^W3C_Validator/}i;
1267                 # Be _stable_:
1268                 my $negotiated=$class->Negotiate_choose([
1269                                 # Put the fallback variant as the first one.
1270                                 # Rate both variants the same to prefer "text/html" for undecided clients.
1271                                 # At least
1272                                 #   Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8b) Gecko/20050217
1273                                 # prefers "application/xhtml+xml" over "text/html" itself:
1274                                 #   text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
1275                                 negotiate_variant(
1276                                                 "id"=>"text/html",
1277                                                 "content-type"=>"text/html",
1278                                                 "qs"=>0.6,
1279                                                 (!$W->{"charset"} ? () : "charset"=>$W->{"charset"}),
1280                                                 "lang"=>$W->{"language"},
1281                                                 ),
1282                                 negotiate_variant(
1283                                                 "id"=>"application/xhtml+xml",
1284                                                 "content-type"=>"application/xhtml+xml",
1285                                                 "qs"=>0.6,
1286                                                 (!$W->{"charset"} ? () : "charset"=>$W->{"charset"}),
1287                                                 "lang"=>$W->{"language"},
1288                                                 ),
1289                                 # application/xml ?
1290                                 # text/xml ?
1291                                 ]);
1292                 $W->{"content_type"}=$negotiated if !defined $W->{"content_type"};
1293                 }
1294         # mod_perl doc: If you set this header via the headers_out table directly, it
1295         #               will be ignored by Apache. So do not do that.
1296         my $type;
1297         if ($W->{"content_type"}) {
1298                 $type=MIME::Types->new()->type($W->{"content_type"});
1299                 cluck "MIME::Types type '".$W->{"content_type"}."' not known" if !$type;
1300                 }
1301         cluck "charset='".$W->{"charset"}."' does not match content-type='".$W->{"content_type"}."'"
1302                         if ($W->{"charset"} ? 1 : 0) != (!$type ? 0 : $type->isAscii());
1303         $W->{"r"}->content_type($W->{"content_type"}.(!$W->{"charset"} ? "" : "; charset=".$W->{"charset"}))
1304                         if $W->{"content_type"};
1305
1306         cache_start();
1307         # We still can append headers before we put out some text.
1308         # FIXME: It is not clean to still append them without overwriting.
1309         return if $W->{"heading_done"};
1310         Wprint '<?xml version="1.0" encoding="'.$W->{"charset"}.'"?>'."\n"
1311                         if (!$W->{"header_only"} || $W->{"header_only"} eq "xml") && (0
1312                                         || $W->{"content_type"}=~m{^application/\w+[+]xml$}
1313                                         || $W->{"content_type"} eq "text/vnd.wap.wml");
1314         return if $W->{"header_only"};
1315         # Split 'heading_done' for the proper handling of: /project/Rel.pm
1316         $W->{"heading_done"}++;
1317
1318         Wprint '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n";
1319         Wprint '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$W->{"language"}.'">'."\n";
1320         my $title=$W->{"title_prefix"}.join("",map({ ': '.$_; } ($W->{"title"} || ())));
1321         # Do not: cluck if $title=~/[<>]/;
1322         # as it is not solved just by: &a_href_inhibit
1323         # as sometimes titles use also: <i>...</i>
1324         $title=~s#<[^>]*>##g;
1325         Wprint "<head>";
1326         Wprint "<title>$title</title>\n";
1327         if ($W->{"have_css"}) {
1328                 # Everything can get overriden later.
1329                 for my $css ("/My/Web.css",@{$W->{"css_push"}}) {
1330                         Wprint <<"HERE";
1331 <link rel="stylesheet" type="text/css" href="@{[ uri_escaped(path_web $css) ]}" />
1332 HERE
1333                         }
1334                 if ($W->{"css_inherit"}) {
1335                         # Do not: <script />
1336                         # as at least Lynx inhibits any further HTML output.
1337                         # Do not: text/javascript
1338                         # as it does not look as registered, at least according to: MIME::Types $VERSION 1.15
1339                         # "application/javascript" so far standardized till 2005-12-08 by:
1340                         #       http://www.ietf.org/internet-drafts/draft-hoehrmann-script-types-03.txt
1341                         Wprint <<"HERE";
1342 <script type="application/javascript" src="@{[ uri_escaped(path_web('/My/css_inherit.js')) ]}"></script>
1343 HERE
1344                         }
1345                 }
1346         Wprint '<meta name="robots" content="'.($W->{"indexme"} ? "" : "no" ).'index,follow" />'."\n";
1347         Wprint $W->{"head"};
1348         for my $type (qw(prev next index contents start up)) {
1349                 do { Wprint '<link rel="'.$type.'" href="'.uri_escaped(path_web $_).'" />'."\n" if $_; }
1350                                 for ($W->{"rel_$type"});
1351                 }
1352         Wprint "</head><body";
1353 #       Wprint ' bgcolor="black" text="white" link="aqua" vlink="teal"'
1354 #                       if $W->{"browser"}->netscape() && (!$W->{"browser"}->major() || $W->{"browser"}->major()<=4);
1355         Wprint $W->{"body_attr"};
1356         Wprint ">\n";
1357
1358         do { Wprint $_ if $_; } for $W->{"heading"};
1359 }
1360
1361 BEGIN {
1362         delete $W->{"__My::Web_init"};
1363         }
1364
1365 1;