Re: Apache2

John Williams <[email protected]>
Newsgroups gmane.comp.web.mason.devel
Message-ID <[email protected]>
On Tue, 27 Jul 2004, Dave Rolsky wrote:
>  if ( $mod_perl::VERSION >= 1.99 ) {
>     ...
>  } else {
>     ...
>  }
>
> The reason I dislike that is the two blocks of the branch generally are
> doing the same thing, but they invoke slightly different methods or
> classes to do what they want.

I don't think it's too bad.  There are already 3 mod_per::VERSION checks
in ApacheHandler.pm (1 if, 2 ?:).  I'm adding 6 more so far.

1 big if is unavoidable to conditionally require the Apache module or the
corresponding Apache2 modules.

There are 3 places where I avoid calling send_http_headers, but all of
them add the version check on to existing conditionals, so it is
"if (something and version)" and no else.

Finally 2 in ?: style, which are kinda ugly.  Suggestions for better style
appreciated.  This one avoids Apache->request:

@@ -581,7 +596,7 @@
     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
+        my $req = $r || ($mod_perl::VERSION < 1.99 ? Apache->request : undef) )  # DocumentRoot is only available inside requests
     {
        $defaults{comp_root} = $req->document_root;
     }

And this one avoids Apache::Request->instance.  The code here is already
ugly, but the comments say there is a good reason for it.  I could avoid
this by doing:
  *Apache::Request::instance = \&Apache::Request::new;
as recommended by the libapreq2 docs
<http://search.cpan.org/~joesuf/libapreq2-2.03_04-dev/glue/perl/xsbuilder/Apache/Request/Request_pod#instance>.

@@ -884,8 +901,10 @@
     # use multiple variables to avoid this, which is annoying.
     return
         $r_sub->( $self->args_method eq 'mod_perl' ?
-                  Apache::Request->instance( $_[0] ) :
-                  $_[0]
+                      ($mod_perl::VERSION >= 1.99 ?
+                       Apache::Request->new( $_[0] ) :
+                       Apache::Request->instance( $_[0] ) )
+               : $_[0]
                 );
 }


The current diff for ApacheHandler.pm is attached.  I have all ah.t tests
passing, except the Apache::Filter test, which is currently being skipped
because it's version number is lower in Apache2.  The last test in
live-cgi.t is also failing, but I don't know why yet.

~ John Williams
ApacheHandler.patch (text/plain, 4.3 KB)
Index: lib/HTML/Mason/ApacheHandler.pm
===================================================================
RCS file: /cvsroot/mason/mason/dist/lib/HTML/Mason/ApacheHandler.pm,v
retrieving revision 1.309
diff -u -r1.309 ApacheHandler.pm
--- lib/HTML/Mason/ApacheHandler.pm	8 Feb 2004 05:19:38 -0000	1.309
+++ lib/HTML/Mason/ApacheHandler.pm	28 Jul 2004 05:38:36 -0000
@@ -5,6 +5,7 @@
 # under the same terms as Perl itself.
 
 use strict;
+use mod_perl 1.24;
 
 #----------------------------------------------------------------------
 #
@@ -12,8 +13,6 @@
 #
 package HTML::Mason::Request::ApacheHandler;
 
-use Apache::Constants qw( REDIRECT );
-
 use HTML::Mason::Request;
 use Class::Container;
 use Params::Validate qw(BOOLEAN);
@@ -26,6 +25,7 @@
 use constant OK         => 0;
 use constant DECLINED   => -1;
 use constant NOT_FOUND  => 404;
+use constant REDIRECT	=> 302;
 
 BEGIN
 {
@@ -73,8 +73,7 @@
 sub flush_buffer
 {
     my ($self) = @_;
-    $self->SUPER::flush_buffer;
-    $self->apache_req->rflush;
+    $self->SUPER::flush_buffer and $self->apache_req->rflush;
 }
 
 sub cgi_object
@@ -141,6 +140,7 @@
     # headers, this will typically only apply after $m->abort.
     # On an error code, leave it to Apache to send the headers.
     if (!$self->is_subrequest
+	and $mod_perl::VERSION < 1.99
 	and $self->auto_send_headers
 	and !HTML::Mason::ApacheHandler::http_header_sent($r)
 	and (!$retval or $retval==200)) {
@@ -175,7 +175,7 @@
 
     $r->method('GET');
     $r->headers_in->unset('Content-length');
-    $r->err_header_out( Location => $url );
+    $r->err_headers_out->{Location} = $url;
     $self->clear_and_abort($status || REDIRECT);
 }
 
@@ -249,8 +249,23 @@
 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 );
+BEGIN {
+	if ($mod_perl::VERSION >= 1.99) {
+		require Apache2;
+		Apache2->import();
+		require Apache::RequestRec;
+		require Apache::RequestIO;
+		require Apache::Log;
+	} else {
+		require Apache;
+		Apache->import();
+	}
+}
+
+use constant OK         => 0;
+use constant DECLINED   => -1;
+use constant NOT_FOUND  => 404;
+use constant REDIRECT	=> 302;
 
 # This is the version that introduced PerlAddVar
 use mod_perl 1.24;
@@ -269,7 +284,7 @@
 
 use vars qw($VERSION);
 
-$VERSION = 1.69;
+$VERSION = 1.70;
 
 use base qw(HTML::Mason::Handler);
 
@@ -581,7 +596,7 @@
     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
+	 my $req = $r || ($mod_perl::VERSION < 1.99 ? Apache->request : undef) )  # DocumentRoot is only available inside requests
     {
 	$defaults{comp_root} = $req->document_root;
     }
@@ -846,7 +861,9 @@
 		       isa_mason_exception($err, 'Decline') ? $err->declined_value :
 		       rethrow_exception $err );
 	unless ($retval and $retval != 200) {
-	    $r->send_http_header;
+	    if ($mod_perl::VERSION < 1.99) {
+		$r->send_http_header();
+	    }
 	}
 	return $retval;
     }
@@ -884,8 +901,10 @@
     # use multiple variables to avoid this, which is annoying.
     return
         $r_sub->( $self->args_method eq 'mod_perl' ?
-                  Apache::Request->instance( $_[0] ) :
-                  $_[0]
+                      ($mod_perl::VERSION >= 1.99 ?
+                       Apache::Request->new( $_[0] ) :
+                       Apache::Request->instance( $_[0] ) )
+		: $_[0]
                 );
 }
 
@@ -960,7 +979,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"} }
 
 sub _set_mason_req_out_method
 {
@@ -978,7 +997,7 @@
 	# Send headers if they have not been sent by us or by user.
         # We use instance here because if we store $m we get a
         # circular reference and a big memory leak.
-	if (!$sent_headers and HTML::Mason::Request->instance->auto_send_headers) {
+	if (!$sent_headers and $mod_perl::VERSION < 1.99 and HTML::Mason::Request->instance->auto_send_headers) {
 	    unless (http_header_sent($r)) {
 		$r->send_http_header();
 	    }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.