| 1 | |||
| 2 | package ASP4::ModPerl; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings 'all'; | ||
| 6 | use APR::Table (); | ||
| 7 | use APR::Socket (); | ||
| 8 | use Apache2::RequestRec (); | ||
| 9 | use Apache2::RequestIO (); | ||
| 10 | use Apache2::Connection (); | ||
| 11 | use Apache2::RequestUtil (); | ||
| 12 | use ASP4::HTTPContext (); | ||
| 13 | use CGI( ':cgi' ); | ||
| 14 | |||
| 15 | |||
| 16 | #============================================================================== | ||
| 17 | sub handler : method | ||
| 18 | { | ||
| 19 | my ($class, $r) = @_; | ||
| 20 | |||
| 21 | my $context = ASP4::HTTPContext->new(); | ||
| 22 | |||
| 23 | if( uc($r->method) eq 'POST' && $r->headers_in->{'content-type'} =~ m/multipart\/form\-data/ ) | ||
| 24 | { | ||
| 25 | $context->{r} = $r; | ||
| 26 | if( $@ ) | ||
| 27 | { | ||
| 28 | warn $@; | ||
| 29 | $r->status( 500 ); | ||
| 30 | return $r->status; | ||
| 31 | }# end if() | ||
| 32 | |||
| 33 | my $handler_class = eval { | ||
| 34 | $context->config->web->handler_resolver->new()->resolve_request_handler( $r->uri ) | ||
| 35 | }; | ||
| 36 | if( $@ ) | ||
| 37 | { | ||
| 38 | warn $@; | ||
| 39 | $r->status( 500 ); | ||
| 40 | return $r->status; | ||
| 41 | }# end if() | ||
| 42 | |||
| 43 | return 404 unless $handler_class; | ||
| 44 | |||
| 45 | eval { | ||
| 46 | my $cgi = CGI->new( $r ); | ||
| 47 | my %args = map { my ($k,$v) = split /\=/, $_; ( $k => $v ) } split /&/, $ENV{QUERY_STRING}; | ||
| 48 | map { $cgi->param($_ => $args{$_}) } keys %args; | ||
| 49 | $context->setup_request( $r, $cgi); | ||
| 50 | $context->execute; | ||
| 51 | }; | ||
| 52 | if( $@ ) | ||
| 53 | { | ||
| 54 | if( $@ =~ m/Software\scaused\sconnection\sabort/ ) | ||
| 55 | { | ||
| 56 | return 0; | ||
| 57 | }# end if() | ||
| 58 | warn $@; | ||
| 59 | $r->status( 500 ); | ||
| 60 | }# end if() | ||
| 61 | return $r->status =~ m/^2/ ? 0 : $r->status; | ||
| 62 | } | ||
| 63 | else | ||
| 64 | { | ||
| 65 | my $cgi = CGI->new( $r ); | ||
| 66 | eval { | ||
| 67 | $context->setup_request( $r, $cgi ); | ||
| 68 | $context->execute; | ||
| 69 | }; | ||
| 70 | if( $@ =~ m/Software\scaused\sconnection\sabort/ ) | ||
| 71 | { | ||
| 72 | return 0; | ||
| 73 | }# end if() | ||
| 74 | warn $@ if $@; | ||
| 75 | |||
| 76 | return 500 if $@; | ||
| 77 | return $r->status =~ m/^2/ ? 0 : $r->status; | ||
| 78 | }# end if() | ||
| 79 | |||
| 80 | }# end handler() | ||
| 81 | |||
| 82 | 1;# return true: | ||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | =head1 NAME | ||
| 87 | |||
| 88 | ASP4::ModPerl - mod_perl2 PerlResponseHandler for ASP4 | ||
| 89 | |||
| 90 | =head1 SYNOPSIS | ||
| 91 | |||
| 92 | In your httpd.conf | ||
| 93 | |||
| 94 | # Load up some important modules: | ||
| 95 | PerlModule DBI | ||
| 96 | PerlModule DBD::mysql | ||
| 97 | PerlModule ASP4::ModPerl | ||
| 98 | |||
| 99 | # Admin website: | ||
| 100 | <VirtualHost *:80> | ||
| 101 | |||
| 102 | ServerName mysite.com | ||
| 103 | ServerAlias www.mysite.com | ||
| 104 | DocumentRoot /usr/local/projects/mysite.com/htdocs | ||
| 105 | |||
| 106 | # Set the directory index: | ||
| 107 | DirectoryIndex index.asp | ||
| 108 | |||
| 109 | # All *.asp files are handled by ASP4::ModPerl | ||
| 110 | <Files ~ (\.asp$)> | ||
| 111 | SetHandler perl-script | ||
| 112 | PerlResponseHandler ASP4::ModPerl | ||
| 113 | </Files> | ||
| 114 | |||
| 115 | # !IMPORTANT! Prevent anyone from viewing your GlobalASA.pm | ||
| 116 | <Files ~ (\.pm$)> | ||
| 117 | Order allow,deny | ||
| 118 | Deny from all | ||
| 119 | </Files> | ||
| 120 | |||
| 121 | # All requests to /handlers/* will be handled by their respective handler: | ||
| 122 | <Location /handlers> | ||
| 123 | SetHandler perl-script | ||
| 124 | PerlResponseHandler ASP4::ModPerl | ||
| 125 | </Location> | ||
| 126 | |||
| 127 | </VirtualHost> | ||
| 128 | |||
| 129 | =head1 DESCRIPTION | ||
| 130 | |||
| 131 | C<ASP4::ModPerl> provides a mod_perl2 PerlResponseHandler interface to | ||
| 132 | L<ASP4::HTTPContext>. | ||
| 133 | |||
| 134 | Under normal circumstances, all you have to do is configure it and forget about it. | ||
| 135 | |||
| 136 | =head1 BUGS | ||
| 137 | |||
| 138 | It's possible that some bugs have found their way into this release. | ||
| 139 | |||
| 140 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports. | ||
| 141 | |||
| 142 | =head1 HOMEPAGE | ||
| 143 | |||
| 144 | Please visit the ASP4 homepage at L<http://www.devstack.com/> to see examples | ||
| 145 | of ASP4 in action. | ||
| 146 | |||
| 147 | =head1 AUTHOR | ||
| 148 | |||
| 149 | John Drago L<mailto:jdrago_999@yahoo.com> | ||
| 150 | |||
| 151 | =head1 COPYRIGHT AND LICENSE | ||
| 152 | |||
| 153 | Copyright 2007 John Drago, All rights reserved. | ||
| 154 | |||
| 155 | This software is free software. It may be used and distributed under the | ||
| 156 | same terms as Perl itself. | ||
| 157 | |||
| 158 | =cut | ||
| 159 |