| 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 | $ENV{DOCUMENT_ROOT} = $r->document_root; | ||
| 22 | $ENV{REMOTE_ADDR} = $r->connection->get_remote_host(); | ||
| 23 | $ENV{HTTP_HOST} = $r->hostname; | ||
| 24 | |||
| 25 | my $context = ASP4::HTTPContext->new(); | ||
| 26 | $r->pool->cleanup_register(sub { $context->DESTROY }); | ||
| 27 | |||
| 28 | if( ($r->headers_in->{'content-type'}||'') =~ m/multipart\/form\-data/ ) | ||
| 29 | { | ||
| 30 | $context->{r} = $r; | ||
| 31 | if( $@ ) | ||
| 32 | { | ||
| 33 | warn $@; | ||
| 34 | $r->status( 500 ); | ||
| 35 | return $r->status; | ||
| 36 | }# end if() | ||
| 37 | |||
| 38 | my $handler_class = eval { | ||
| 39 | $context->config->web->handler_resolver->new()->resolve_request_handler( $r->uri ) | ||
| 40 | }; | ||
| 41 | if( $@ ) | ||
| 42 | { | ||
| 43 | warn $@; | ||
| 44 | $r->status( 500 ); | ||
| 45 | return $r->status; | ||
| 46 | }# end if() | ||
| 47 | |||
| 48 | return 404 unless $handler_class; | ||
| 49 | |||
| 50 | eval { | ||
| 51 | my $cgi = CGI->new( $r ); | ||
| 52 | my %args = map { my ($k,$v) = split /\=/, $_; ( $k => $v ) } split /&/, $ENV{QUERY_STRING}; | ||
| 53 | map { $cgi->param($_ => $args{$_}) } keys %args; | ||
| 54 | $context->setup_request( $r, $cgi); | ||
| 55 | $context->execute; | ||
| 56 | }; | ||
| 57 | if( $@ ) | ||
| 58 | { | ||
| 59 | if( $@ =~ m/Software\scaused\sconnection\sabort/ ) | ||
| 60 | { | ||
| 61 | return 0; | ||
| 62 | }# end if() | ||
| 63 | warn $@; | ||
| 64 | $r->status( 500 ); | ||
| 65 | }# end if() | ||
| 66 | return $r->status =~ m/^2/ ? 0 : $r->status == 500 ? 0 : $r->status; | ||
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | my $cgi = CGI->new( $r ); | ||
| 71 | eval { | ||
| 72 | $context->setup_request( $r, $cgi ); | ||
| 73 | $context->execute; | ||
| 74 | }; | ||
| 75 | if( $@ =~ m/Software\scaused\sconnection\sabort/ ) | ||
| 76 | { | ||
| 77 | return 0; | ||
| 78 | }# end if() | ||
| 79 | warn $@ if $@; | ||
| 80 | |||
| 81 | |||
| 82 | if( $context->response->Status == 200 ) | ||
| 83 | { | ||
| 84 | $r->status( 200 ); | ||
| 85 | if( $context->did_end && $context->did_send_headers ) | ||
| 86 | { | ||
| 87 | $r->rflush(); | ||
| 88 | }# end if() | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | else | ||
| 92 | { | ||
| 93 | $r->status( $context->response->Status ); | ||
| 94 | if( $context->did_end && $context->did_send_headers ) | ||
| 95 | { | ||
| 96 | $r->rflush(); | ||
| 97 | } | ||
| 98 | else | ||
| 99 | { | ||
| 100 | # Make sure we send our headers now, since we haven't done so already: | ||
| 101 | $context->send_headers(); | ||
| 102 | $context->did_end(1); | ||
| 103 | $r->rflush(); | ||
| 104 | }# end if() | ||
| 105 | return $context->response->Status == 500 ? 0 : $context->response->Status; | ||
| 106 | }# end if() | ||
| 107 | }# end if() | ||
| 108 | |||
| 109 | }# end handler() | ||
| 110 | |||
| 111 | 1;# return true: | ||
| 112 | |||
| 113 | =pod | ||
| 114 | |||
| 115 | =head1 NAME | ||
| 116 | |||
| 117 | ASP4::ModPerl - mod_perl2 PerlResponseHandler for ASP4 | ||
| 118 | |||
| 119 | =head1 SYNOPSIS | ||
| 120 | |||
| 121 | In your httpd.conf | ||
| 122 | |||
| 123 | # Load up some important modules: | ||
| 124 | PerlModule DBI | ||
| 125 | PerlModule DBD::mysql | ||
| 126 | PerlModule ASP4::ModPerl | ||
| 127 | |||
| 128 | <VirtualHost *:80> | ||
| 129 | |||
| 130 | ServerName mysite.com | ||
| 131 | ServerAlias www.mysite.com | ||
| 132 | DocumentRoot /usr/local/projects/mysite.com/htdocs | ||
| 133 | |||
| 134 | # Set the directory index: | ||
| 135 | DirectoryIndex index.asp | ||
| 136 | |||
| 137 | # All *.asp files are handled by ASP4::ModPerl | ||
| 138 | <Files ~ (\.asp$)> | ||
| 139 | SetHandler perl-script | ||
| 140 | PerlResponseHandler ASP4::ModPerl | ||
| 141 | </Files> | ||
| 142 | |||
| 143 | # !IMPORTANT! Prevent anyone from viewing your GlobalASA.pm | ||
| 144 | <Files ~ (\.pm$)> | ||
| 145 | Order allow,deny | ||
| 146 | Deny from all | ||
| 147 | </Files> | ||
| 148 | |||
| 149 | # All requests to /handlers/* will be handled by their respective handler: | ||
| 150 | <Location /handlers> | ||
| 151 | SetHandler perl-script | ||
| 152 | PerlResponseHandler ASP4::ModPerl | ||
| 153 | </Location> | ||
| 154 | |||
| 155 | </VirtualHost> | ||
| 156 | |||
| 157 | =head1 DESCRIPTION | ||
| 158 | |||
| 159 | C<ASP4::ModPerl> provides a mod_perl2 PerlResponseHandler interface to | ||
| 160 | L<ASP4::HTTPContext>. | ||
| 161 | |||
| 162 | Under normal circumstances, all you have to do is configure it and forget about it. | ||
| 163 | |||
| 164 | =head1 BUGS | ||
| 165 | |||
| 166 | It's possible that some bugs have found their way into this release. | ||
| 167 | |||
| 168 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports. | ||
| 169 | |||
| 170 | =head1 HOMEPAGE | ||
| 171 | |||
| 172 | Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples | ||
| 173 | of ASP4 in action. | ||
| 174 | |||
| 175 | =head1 AUTHOR | ||
| 176 | |||
| 177 | John Drago L<mailto:jdrago_999@yahoo.com> | ||
| 178 | |||
| 179 | =head1 COPYRIGHT AND LICENSE | ||
| 180 | |||
| 181 | Copyright 2007 John Drago, All rights reserved. | ||
| 182 | |||
| 183 | This software is free software. It may be used and distributed under the | ||
| 184 | same terms as Perl itself. | ||
| 185 | |||
| 186 | =cut | ||
| 187 |