The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
--- /home/test/src/mason/HTML-Mason-1.26/lib/HTML/Mason/ApacheHandler.pm	2004-04-05 12:32:33.000000000 -1000
+++ /home/test/src/mason/MasonX-Apache2Handler-0.05/lib/MasonX/Apache2Handler.pm	2004-04-11 12:02:16.000000000 -1000
@@ -1,3 +1,17 @@
+#------------------------------------------------------------------------
+# Changes to HTML::Mason::ApacheHandler for Apache2/mod_perl 2.
+#
+# Beau E. Cox <beau@beaucox.com>
+# April 2004
+#
+# Changes (C)Copyright 2004 Beau E. Cox.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#------------------------------------------------------------------------
+
 # Copyright (c) 1998-2003 by Jonathan Swartz. All rights reserved.
 # This program is free software; you can redistribute it and/or modify it
 # under the same terms as Perl itself.
@@ -8,16 +22,16 @@
 #
 # APACHE-SPECIFIC REQUEST OBJECT
 #
-package HTML::Mason::Request::ApacheHandler;
+package MasonX::Request::Apache2Handler;
 
-use Apache::Constants qw( REDIRECT );
+use Apache::Const -compile => qw( REDIRECT );
 
-use HTML::Mason::Request;
+use MasonX::Request2;
 use Class::Container;
 use Params::Validate qw(BOOLEAN);
 Params::Validate::validation_options( on_fail => sub { param_error( join '', @_ ) } );
 
-use base qw(HTML::Mason::Request);
+use base qw(MasonX::Request2);
 
 use HTML::Mason::Exceptions( abbr => [qw(param_error error)] );
 
@@ -25,16 +39,19 @@
 use constant DECLINED   => -1;
 use constant NOT_FOUND  => 404;
 
-BEGIN
+if ( $mod_perl::VERSION < 1.99 )
 {
-    my $ap_req_class = $mod_perl::VERSION < 1.99 ? 'Apache' : 'Apache::RequestRec';
+    error "you must use mod_perl 2 (version >= 1.99)", __PACKAGE__, "\n";
+}
 
+BEGIN
+{
     __PACKAGE__->valid_params
-	( ah         => { isa => 'HTML::Mason::ApacheHandler',
-			  descr => 'An ApacheHandler to handle web requests',
+	( ah         => { isa => 'MasonX::Apache2Handler',
+			  descr => 'An Apache2Handler to handle web requests',
 			  public => 0 },
 
-	  apache_req => { isa => $ap_req_class, default => undef,
+	  apache_req => { isa => 'Apache::RequestRec', default => undef,
 			  descr => "An Apache request object",
 			  public => 0 },
 
@@ -71,8 +88,9 @@
 sub flush_buffer
 {
     my ($self) = @_;
-    $self->SUPER::flush_buffer;
-    $self->apache_req->rflush;
+    # Only call rflush if flush_buffer returns a true value.
+    # (return implemented in MasonX::Request2 and MasonX::Buffer2.)
+    $self->SUPER::flush_buffer and $self->apache_req->rflush;
 }
 
 sub cgi_object
@@ -148,11 +166,12 @@
     # if we are the top-level request. Since the out_method sends
     # headers, this will typically only apply after $m->abort.
     # On an error code, leave it to Apache to send the headers.
+    # not needed in mod_per2 (??)
     if (!$self->is_subrequest
 	and $self->auto_send_headers
-	and !HTML::Mason::ApacheHandler::http_header_sent($r)
+	and !MasonX::Apache2Handler::http_header_sent($r)
 	and (!$retval or $retval==200)) {
-	$r->send_http_header();
+	#$r->send_http_header();
     }
 
     return defined($retval) ? $retval : OK;
@@ -184,15 +203,15 @@
     $self->clear_buffer;
     $r->method('GET');
     $r->headers_in->unset('Content-length');
-    $r->err_header_out( Location => $url );
-    $self->abort($status || REDIRECT);
+    $r->err_headers_out->{ Location } = $url;
+    $self->abort($status || Apache::REDIRECT);
 }
 
 #----------------------------------------------------------------------
 #
 # APACHE-SPECIFIC FILE RESOLVER OBJECT
 #
-package HTML::Mason::Resolver::File::ApacheHandler;
+package MasonX::Resolver::File::Apache2Handler;
 
 use strict;
 
@@ -247,7 +266,7 @@
 #
 # APACHEHANDLER OBJECT
 #
-package HTML::Mason::ApacheHandler;
+package MasonX::Apache2Handler;
 
 use File::Path;
 use File::Spec;
@@ -258,21 +277,21 @@
 use Params::Validate qw(:all);
 Params::Validate::validation_options( on_fail => sub { param_error( join '', @_ ) } );
 
-use Apache;
-use Apache::Constants qw( OK DECLINED NOT_FOUND );
+use Apache::Const -compile => qw( OK DECLINED NOT_FOUND );
+use APR;
+use Apache::ServerUtil;
 
-# Require a reasonably modern mod_perl - should probably be later
-use mod_perl 1.22;
+# Require a mod_perl 2
+use mod_perl 1.99;
 
 if ( $mod_perl::VERSION < 1.99 )
 {
-    error "mod_perl must be compiled with PERL_METHOD_HANDLERS=1 (or EVERYTHING=1) to use ", __PACKAGE__, "\n"
-	unless Apache::perl_hook('MethodHandlers');
+    error "you must use mod_perl 2 (version >= 1.99)", __PACKAGE__, "\n";
 }
 
 use vars qw($VERSION);
 
-$VERSION = 1.69;
+$VERSION = 0.05;
 
 use Class::Container;
 use base qw(Class::Container);
@@ -341,7 +360,7 @@
 }
 
 use constant
-    HAS_TABLE_API => $mod_perl::VERSION >= 1.99 || Apache::perl_hook('TableApi');
+    HAS_TABLE_API => $mod_perl::VERSION >= 1.99;
 
 my %AH_BY_CONFIG;
 sub make_ah
@@ -569,23 +588,34 @@
     my %params = @_;
 
     my %defaults;
-    $defaults{request_class}  = 'HTML::Mason::Request::ApacheHandler'
+    $defaults{request_class}  = 'MasonX::Request::Apache2Handler'
         unless exists $params{request};
-    $defaults{resolver_class} = 'HTML::Mason::Resolver::File::ApacheHandler'
+    $defaults{resolver_class} = 'MasonX::Resolver::File::Apache2Handler'
         unless exists $params{resolver};
 
     my $allowed_params = $class->allowed_params(%defaults, %params);
 
-    if ( exists $allowed_params->{comp_root} and
-	 my $req = $r || Apache->request )  # DocumentRoot is only available inside requests
+     if ( exists $allowed_params->{comp_root} and
+	 my $req = $r )  # DocumentRoot is only available inside requests
     {
 	$defaults{comp_root} = $req->document_root;
     }
+=comment
+   if ( exists $allowed_params->{comp_root} ) {
+	if ( my $req = $r ) {
+	# DocumentRoot is only available inside requests
+	    $defaults{comp_root} = $req->document_root;
+	} else {
+	    $defaults{comp_root} =
+		Apache->server->dir_config( '_MasonDefaultDocumentRoot' );
+	}
+    }
+=cut
 
     if (exists $allowed_params->{data_dir} and not exists $params{data_dir})
     {
 	# constructs path to <server root>/mason
-	my $def = $defaults{data_dir} = Apache->server_root_relative('mason');
+	my $def = $defaults{data_dir} = Apache->server->server_root_relative('mason');
 	param_error "Default data_dir (MasonDataDir) '$def' must be an absolute path"
 	    unless File::Spec->file_name_is_absolute($def);
 	  
@@ -624,7 +654,7 @@
     unless ( $self->interp->resolver->can('apache_request_to_comp_path') )
     {
 	error "The resolver class your Interp object uses does not implement " .
-              "the 'apache_request_to_comp_path' method.  This means that ApacheHandler " .
+              "the 'apache_request_to_comp_path' method.  This means that Apache2Handler " .
               "cannot resolve requests.  Are you using a handler.pl file created ".
 	      "before version 1.10?  Please see the handler.pl sample " .
               "that comes with the latest version of Mason.";
@@ -633,7 +663,9 @@
     # If we're running as superuser, change file ownership to http user & group
     if (!($> || $<) && $self->interp->files_written)
     {
-	chown Apache->server->uid, Apache->server->gid, $self->interp->files_written
+	chown getpwnam( Apache->server->dir_config( '_MasonUser' ) ),
+	getgrnam( Apache->server->dir_config( '_MasonGroup' ) ),
+	$self->interp->files_written
 	    or system_error( "Can't change ownership of files written by interp object: $!\n" );
     }
 
@@ -684,8 +716,10 @@
 	    }
 
 	    return ["<center><h2>" . $self->apache_status_title . "</h2></center>" ,
-		    $self->status_as_html(apache_req => $r),
-		    $self->interp->status_as_html(ah => $self, apache_req => $r)];
+		    $self->status_as_html
+		    (apache_req => $r),
+		    $self->interp->status_as_html
+		    (ah => $self, $r) ];
 	};
 	local $^W = 0; # to avoid subroutine redefined warnings
 	Apache::Status->menu_item($status_name, $self->apache_status_title, $statsub);
@@ -700,7 +734,7 @@
 	if $interp->compiler->can('add_allowed_globals');
 }
 
-# Generate HTML that describes ApacheHandler's current status.
+# Generate HTML that describes Apache2andler's current status.
 # This is used in things like Apache::Status reports.
 
 sub status_as_html {
@@ -709,7 +743,7 @@
     # Should I be scared about this?  =)
 
     my $comp_source = <<'EOF';
-<h3>ApacheHandler properties:</h3>
+<h3>Apache2Handler properties:</h3>
 <blockquote>
  <tt>
 <table width="75%">
@@ -762,7 +796,7 @@
 </blockquote>
 
 <%args>
- $ah       # The ApacheHandler we'll elucidate
+ $ah       # The Apache2Handler we'll elucidate
  %valid    # Contains default values for member data
 </%args>
 EOF
@@ -805,11 +839,9 @@
     # Apache::Request object to a variable currently containing a
     # plain Apache object, we leak memory.  This means we'd have to
     # use multiple variables to avoid this, which is annoying.
-    my $r =
-        $r_sub->( $self->args_method eq 'mod_perl' ?
-                  Apache::Request->instance( $_[0] ) :
-                  $_[0]
-                );
+
+    # for mod_perl2 just pickup Apache::RequestRec
+    my $r = $_[0];
 
     my $interp = $self->interp;
 
@@ -826,7 +858,7 @@
 
     if ($is_dir) {
 	if ($self->decline_dirs) {
-	    return DECLINED;
+	    return Apache::DECLINED;
 	} else {
 	    $r->content_type(undef);
 	}
@@ -880,7 +912,8 @@
 	my $retval = isa_mason_exception($err, 'Abort')   ? $err->aborted_value  :
 		     isa_mason_exception($err, 'Decline') ? $err->declined_value :
 		     rethrow_exception $err;
-        $r->send_http_header unless $retval and $retval != 200;
+	# not needed in mod_perl2 (??)
+        #$r->send_http_header unless $retval and $retval != 200;
 	return $retval;
     }
 
@@ -896,10 +929,11 @@
 	# Send headers if they have not been sent by us or by user.
         # We use instance here because if we store $request we get a
         # circular reference and a big memory leak.
-	if (!$sent_headers and HTML::Mason::Request->instance->auto_send_headers) {
-	    unless (http_header_sent($r)) {
-		$r->send_http_header();
-	    }
+	if (!$sent_headers and MasonX::Request2->instance->auto_send_headers) {
+	    # not needed in mod_perl2 (??) - just set content type
+	    #unless (http_header_sent($r)) {
+	    #   $r->send_http_header();
+	    #}
 	    $sent_headers = 1;
 	}
 
@@ -959,8 +993,10 @@
 #
 sub _mod_perl_args
 {
-    my ($self, $apr, $request) = @_;
+    my ($self, $r, $request) = @_;
 
+    # for mod_perl2, get back to Apache::Request from Apache::RequestRec
+    my $apr = Apache::Request->new( $r );
     my %args;
     foreach my $key ( $apr->param ) {
 	my @values = $apr->param($key);
@@ -973,7 +1009,7 @@
 #
 # Determines whether the http header has been sent.
 #
-sub http_header_sent { shift->header_out("Content-type") }
+sub http_header_sent { shift->headers_out->{"Content-type"} }
 
 # Utility function to prepare $r before returning NOT_FOUND.
 sub return_not_found
@@ -984,17 +1020,17 @@
 	$r->method('GET');
 	$r->headers_in->unset('Content-length');
     }
-    return NOT_FOUND;
+    return Apache::NOT_FOUND;
 }
 
 #
-# PerlHandler HTML::Mason::ApacheHandler
+# PerlHandler MasonX::Apache2Handler
 #
 BEGIN
 {
-    # A method handler is prototyped differently in mod_perl 1.x than in 2.x
-    my $handler_code = sprintf <<'EOF', $mod_perl::VERSION >= 1.99 ? ': method' : '($$)';
-sub handler %s
+    # A mod_perl2 method handler
+    my $handler_code = <<'EOF';
+sub handler : method
 {
     my ($package, $r) = @_;
 
@@ -1014,147 +1050,69 @@
 
 =head1 NAME
 
-HTML::Mason::ApacheHandler - Mason/mod_perl interface
+MasonX::Apache2Handler - experimental (alpha) Mason/mod_perl2 interface
 
 =head1 SYNOPSIS
 
-    use HTML::Mason::ApacheHandler;
-
-    my $ah = HTML::Mason::ApacheHandler->new (..name/value params..);
+    use MasonX::Apache2Handler;
+ 
+    my $ah = MasonX::Apache2Handler->new (..name/value params..);
     ...
     sub handler {
-        my $r = shift;
-        $ah->handle_request($r);
+     my $r = shift;
+     $ah->handle_request($r);
     }
 
 =head1 DESCRIPTION
 
-The ApacheHandler object links Mason to mod_perl, running components in
-response to HTTP requests. It is controlled primarily through
-parameters to the new() constructor.
-
-handle_request() is not a user method, but rather is called from the
-HTML::Mason::handler() routine in handler.pl.
-
-=head1 PARAMETERS TO THE new() CONSTRUCTOR
-
-=over
-
-=item apache_status_title
-
-Title that you want this ApacheHandler to appear as under
-Apache::Status.  Default is "HTML::Mason status".  This is useful if
-you create more than one ApacheHandler object and want them all
-visible via Apache::Status.
-
-=item args_method
+B<MasonX::Apache2Handler is highly experimental ( alpha ) and
+should only be used in a test environment.>
 
-Method to use for unpacking GET and POST arguments. The valid options
-are 'CGI' and 'mod_perl'; these indicate that a C<CGI.pm> or
-C<Apache::Request> object (respectively) will be created for the
-purposes of argument handling.
+MasonX::Apache2Handler is a clone of HTML::Mason::ApacheHandler
+changed to work under a pure mod_perl2 environment. The external
+interface is unchanged, see
+L<HTML::Mason::ApacheHandler|ApacheHandler>.
 
-'mod_perl' is the default and requires that you have installed the
-C<Apache::Request> package.
+The actual changes I made can be found in the distribution in
+B<diff/ApacheHandler.diff> ( made with 'diff -Naru' ... ).
 
-If args_method is 'mod_perl', the C<$r> global is upgraded to an
-Apache::Request object. This object inherits all Apache methods and
-adds a few of its own, dealing with parameters and file uploads.  See
-C<Apache::Request> for more information.
+A HOTWO for MasonX::Apache2Handler may be found at
+L<HOWTO Run Mason with mod_perl2|Mason-with-mod_perl2>.
 
-If the args_method is 'CGI', the Mason request object (C<$m>) will have a
-method called C<cgi_object> available.  This method returns the CGI
-object used for argument processing.
+=head1 PREREQUISITES
 
-While Mason will load C<Apache::Request> or C<CGI> as needed at runtime, it
-is recommended that you preload the relevant module either in your
-F<httpd.conf> or F<handler.pl> file, as this will save some memory.
+You must have the following packages installed:
 
-=item decline_dirs
+    mod_perl        => 1.9910
+    HTML::Mason'    => 1.25
+    libapreq2       => 2.02-dev
 
-True or false, default is true. Indicates whether Mason should decline
-directory requests, leaving Apache to serve up a directory index or a
-C<FORBIDDEN> error as appropriate. See the L<allowing directory requests|HTML::Mason::Admin/allowing directory requests> section of the administrator's manual
-for more information about handling directories with Mason.
+Please refer to the original packages' documentation
+for instructions.
 
-=item interp
-
-The interpreter object to associate with this compiler. By default a
-new object of the specified L<interp_class|HTML::Mason::Params/interp_class> will be created.
-
-=item interp_class
-
-The class to use when creating a interpreter. Defaults to
-L<HTML::Mason::Interp|HTML::Mason::Interp>.
-
-=back
-
-=head1 ACCESSOR METHODS
-
-All of the above properties, except interp_class, have standard accessor
-methods of the same name: no arguments retrieves the value, and one
-argument sets it, except for args_method, which is not settable.  For
-example:
-
-    my $ah = HTML::Mason::ApacheHandler->new;
-    my $decline_dirs = $ah->decline_dirs;
-    $ah->decline_dirs(1);
-
-=head1 OTHER METHODS
-
-The ApacheHandler object has a few other publically accessible methods
-that may be of interest to end users.
-
-=over 4
-
-=item handle_request ($r)
-
-This method takes an Apache object representing a request and
-translates that request into a form Mason can understand.  It's return
-value is an Apache status code.
-
-=item prepare_request ($r)
-
-This method takes an Apache object representing a request and returns
-a new Mason request object or an Apache status code.  If it is a
-request object you can manipulate that object as you like, and then
-call the request object's C<exec> method to have it generate output.
-
-If this method returns an Apache status code, that means that it could
-not create a Mason request object.
-
-This method is useful if you would like to have a chance to decline a
-request based on properties of the Mason request object or a component
-object.  For example:
-
-    my $req = $ah->prepare_request($r);
-    # $req must be an Apache status code if it's not an object
-    return $req unless ref($req);
-
-    return DECLINED
-        unless $req->request_comp->source_file =~ /\.html$/;
-
-    $req->exec;
+=head1 SEE ALSO
 
-=item request_args ($r)
+My documents, including:
+L<HOWTO Run Mason with mod_perl2|Mason-with-mod_perl2>,
+L<MasonX::Request::WithApache2Session|WithApache2Session>,
+L<MasonX::Request::WithMulti2Session|WithMulti2Session>,
 
-Given an Apache request object, this method returns a three item list.
-The first item is a hash reference containing the arguments passed by
-the client's request.
+Original Mason documents, including:
+L<HTML::Mason::ApacheHandler|ApacheHandler>,
+L<MasonX::Request::WithApacheSession|WithApacheSession>,
+L<MasonX::Request::WithMultiSession|WithMultiSession>.
 
-The second is an Apache request object.  This is returned for
-backwards compatibility from when this method was responsible for
-turning a plain Apache object into an Apache::Request object.
+Also see the Mason documentation at L<http://masonhq.com/docs/manual/>.
 
-The third item may be a CGI.pm object or C<undef>, depending on the
-value of the L<args_method|HTML::Mason::Params/args_method> parameter.
+=head1 AUTHOR
 
-=back
+Beau E. Cox <beau@beaucox.com> L<http://beaucox.com>.
 
-=head1 SEE ALSO
+The real authors (I just made mod_perl2 changes) are the Mason crew, including:
+Jonathan Swartz <swartz@pobox.com>,
+Dave Rolsky <autarch@urth.org>,
+Ken Williams <ken@mathforum.org>.
 
-L<HTML::Mason|HTML::Mason>,
-L<HTML::Mason::Admin|HTML::Mason::Admin>,
-L<HTML::Mason::Interp|HTML::Mason::Interp>
+Version 0.05 as of April, 2004.
 
 =cut