| 1 | |||
| 2 | package Apache2::ASP::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 Apache2::ASP::HTTPContext (); | ||
| 13 | use CGI(); | ||
| 14 | use Apache2::ASP::UploadHook; | ||
| 15 | |||
| 16 | local $Apache2::ASP::HTTPContext::ClassName = 'Apache2::ASP::HTTPContext'; | ||
| 17 | |||
| 18 | |||
| 19 | #============================================================================== | ||
| 20 | sub handler : method | ||
| 21 | { | ||
| 22 | my ($class, $r) = @_; | ||
| 23 | |||
| 24 | my $context = Apache2::ASP::HTTPContext->new(); | ||
| 25 | |||
| 26 | if( uc($ENV{REQUEST_METHOD}) eq 'POST' && lc($ENV{CONTENT_TYPE}) =~ m@multipart/form-data@ ) | ||
| 27 | { | ||
| 28 | $context->_load_class( $context->config->web->handler_resolver ); | ||
| 29 | my $handler_class = $context->config->web->handler_resolver->new()->resolve_request_handler( $r->uri ); | ||
| 30 | $context->_load_class( $handler_class ); | ||
| 31 | unless( $ENV{QUERY_STRING} =~ m/mode\=[a-z0-9_]+/ ) | ||
| 32 | { | ||
| 33 | die "All UploadHandlers require a querystring parameter 'mode' to be specified when uploading!"; | ||
| 34 | }# end unless() | ||
| 35 | |||
| 36 | eval { | ||
| 37 | my $cgi = CGI->new( $r ); | ||
| 38 | my %args = map { my ($k,$v) = split /\=/, $_; ( $k => $v ) } split /&/, $ENV{QUERY_STRING}; | ||
| 39 | map { $cgi->param($_ => $args{$_}) } keys %args; | ||
| 40 | $context->setup_request( $r, $cgi); | ||
| 41 | $handler_class->init_asp_objects( $context ); | ||
| 42 | |||
| 43 | foreach my $field ( $cgi->param ) | ||
| 44 | { | ||
| 45 | my $ifh = $cgi->param($field); | ||
| 46 | next unless my $info = $cgi->uploadInfo( $ifh ); | ||
| 47 | my ($filename) = $info->{'Content-Disposition'} =~ m/filename\="?(.*?)"?$/; | ||
| 48 | |||
| 49 | $info->{filename_only} = $filename; | ||
| 50 | my $tmpfile = '/tmp/' . rand(); | ||
| 51 | open my $ofh, '>', $tmpfile | ||
| 52 | or die "Cannot open '$tmpfile' for writing: $!"; | ||
| 53 | my $buffer; | ||
| 54 | while( my $bytesread = read( $ifh , $buffer , 1024 ) ) | ||
| 55 | { | ||
| 56 | print $ofh $buffer; | ||
| 57 | }# end while() | ||
| 58 | close($ofh); | ||
| 59 | |||
| 60 | $ENV{filename} = $tmpfile; | ||
| 61 | $ENV{download_file} = $filename; | ||
| 62 | my $Upload = Apache2::ASP::UploadHookArgs->new( | ||
| 63 | upload => $info, | ||
| 64 | percent_complete => 100, | ||
| 65 | elapsed_time => 1, | ||
| 66 | total_expected_time => 1, | ||
| 67 | time_remaining => 0, | ||
| 68 | length_received => $ENV{CONTENT_LENGTH}, | ||
| 69 | data => undef, | ||
| 70 | %$info, | ||
| 71 | ); | ||
| 72 | $handler_class->upload_end( $context, $Upload ); | ||
| 73 | }# end foreach() | ||
| 74 | $context->execute; | ||
| 75 | }; | ||
| 76 | warn $@ if $@; | ||
| 77 | return $r->status =~ m/^2/ ? 0 : $r->status; | ||
| 78 | } | ||
| 79 | else | ||
| 80 | { | ||
| 81 | eval { | ||
| 82 | my $cgi = CGI->new( $r ); | ||
| 83 | $context->setup_request( $r, $cgi ); | ||
| 84 | $context->execute; | ||
| 85 | }; | ||
| 86 | warn $@ if $@; | ||
| 87 | |||
| 88 | return 500 if $@; | ||
| 89 | return $r->status =~ m/^2/ ? 0 : $r->status; | ||
| 90 | }# end if() | ||
| 91 | }# end handler() | ||
| 92 | |||
| 93 | 1;# return true: | ||
| 94 | |||
| 95 | =pod | ||
| 96 | |||
| 97 | =head1 NAME | ||
| 98 | |||
| 99 | Apache2::ASP::ModPerl - mod_perl2 PerlResponseHandler for Apache2::ASP | ||
| 100 | |||
| 101 | =head1 SYNOPSIS | ||
| 102 | |||
| 103 | In your httpd.conf | ||
| 104 | |||
| 105 | # Needed for file uploads to work properly: | ||
| 106 | LoadModule apreq_module modules/mod_apreq2.so | ||
| 107 | |||
| 108 | # Load up some important modules: | ||
| 109 | PerlModule DBI | ||
| 110 | PerlModule DBD::mysql | ||
| 111 | PerlModule Apache2::ASP::ModPerl | ||
| 112 | |||
| 113 | # Admin website: | ||
| 114 | <VirtualHost *:80> | ||
| 115 | |||
| 116 | ServerName mysite.com | ||
| 117 | ServerAlias www.mysite.com | ||
| 118 | DocumentRoot /usr/local/projects/mysite.com/htdocs | ||
| 119 | |||
| 120 | # Set the directory index: | ||
| 121 | DirectoryIndex index.asp | ||
| 122 | |||
| 123 | # All *.asp files are handled by Apache2::ASP::ModPerl | ||
| 124 | <Files ~ (\.asp$)> | ||
| 125 | SetHandler perl-script | ||
| 126 | PerlResponseHandler Apache2::ASP::ModPerl | ||
| 127 | </Files> | ||
| 128 | |||
| 129 | # !IMPORTANT! Prevent anyone from viewing your GlobalASA.pm | ||
| 130 | <Files ~ (\.pm$)> | ||
| 131 | Order allow,deny | ||
| 132 | Deny from all | ||
| 133 | </Files> | ||
| 134 | |||
| 135 | # All requests to /handlers/* will be handled by their respective handler: | ||
| 136 | <Location /handlers> | ||
| 137 | SetHandler perl-script | ||
| 138 | PerlResponseHandler Apache2::ASP::ModPerl | ||
| 139 | </Location> | ||
| 140 | |||
| 141 | </VirtualHost> | ||
| 142 | |||
| 143 | =head1 DESCRIPTION | ||
| 144 | |||
| 145 | C<Apache2::ASP::ModPerl> provides a mod_perl2 PerlResponseHandler interface to | ||
| 146 | L<Apache2::ASP::HTTPContext>. | ||
| 147 | |||
| 148 | Under normal circumstances, all you have to do is configure it and forget about it. | ||
| 149 | |||
| 150 | =head1 BUGS | ||
| 151 | |||
| 152 | It's possible that some bugs have found their way into this release. | ||
| 153 | |||
| 154 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports. | ||
| 155 | |||
| 156 | =head1 HOMEPAGE | ||
| 157 | |||
| 158 | Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples | ||
| 159 | of Apache2::ASP in action. | ||
| 160 | |||
| 161 | =head1 AUTHOR | ||
| 162 | |||
| 163 | John Drago L<mailto:jdrago_999@yahoo.com> | ||
| 164 | |||
| 165 | =head1 COPYRIGHT AND LICENSE | ||
| 166 | |||
| 167 | Copyright 2007 John Drago, All rights reserved. | ||
| 168 | |||
| 169 | This software is free software. It may be used and distributed under the | ||
| 170 | same terms as Perl itself. | ||
| 171 | |||
| 172 | =cut | ||
| 173 |