| 1 | package Catalyst::View::Reproxy; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw/Catalyst::View/; | ||
| 7 | |||
| 8 | use Fcntl; | ||
| 9 | use File::MimeInfo qw//; | ||
| 10 | use File::MMagic; | ||
| 11 | use HTTP::Request; | ||
| 12 | use HTTP::Response; | ||
| 13 | use LWP::UserAgent; | ||
| 14 | use NEXT; | ||
| 15 | |||
| 16 | __PACKAGE__->mk_accessors(qw/mmagic/); | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Catalyst::View::Reproxy - Reproxing View for lighty and perlbal. | ||
| 21 | |||
| 22 | =head1 VERSION | ||
| 23 | |||
| 24 | Version 0.05 | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | our $VERSION = '0.05'; | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | In your view class | ||
| 33 | |||
| 34 | package MyApp::View::MyReproxy; | ||
| 35 | |||
| 36 | use base qw/Catalyst::View::Reproxy/; | ||
| 37 | |||
| 38 | __PACKAGE__->config( | ||
| 39 | perlbal => 1 | ||
| 40 | ); | ||
| 41 | |||
| 42 | In your controller class | ||
| 43 | |||
| 44 | sub index: Private { | ||
| 45 | my ($self, $c) = @_; | ||
| 46 | |||
| 47 | $c->forward('View::MyReproxy', {}); | ||
| 48 | } | ||
| 49 | |||
| 50 | =head1 METHODS | ||
| 51 | |||
| 52 | =head2 new($c, $arguments) | ||
| 53 | |||
| 54 | Constructor | ||
| 55 | |||
| 56 | =over 2 | ||
| 57 | |||
| 58 | =item config, arguments | ||
| 59 | |||
| 60 | =over 2 | ||
| 61 | |||
| 62 | =item lighttpd | ||
| 63 | |||
| 64 | If the frontend web server is lighttpd, the value would be 1. (default 0) | ||
| 65 | |||
| 66 | =item perlbal | ||
| 67 | |||
| 68 | If the frontend web server is perlbal, the value would be 1. (default 0) | ||
| 69 | |||
| 70 | =item mime_magic | ||
| 71 | |||
| 72 | Using mime magic. (depend on L<File::MMagic>) | ||
| 73 | |||
| 74 | =item mime_magic_file | ||
| 75 | |||
| 76 | Using external magic file (see L<File::MMagic>) | ||
| 77 | |||
| 78 | =item timeout | ||
| 79 | |||
| 80 | Emulating "X-REPROXY-URL" without using perlbal, | ||
| 81 | L<LWP::UserAgent>'s timeout setting. (see L<LWP::UserAgent>) | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | =back | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub new { | ||
| 90 | my ( $class, $c, $arguments ) = @_; | ||
| 91 | |||
| 92 | my $config = { | ||
| 93 | lighttpd => 0, | ||
| 94 | perlbal => 0, | ||
| 95 | mime_magic => 0, | ||
| 96 | mime_magic_file => undef, | ||
| 97 | timeout => 0 | ||
| 98 | }; | ||
| 99 | |||
| 100 | $config = | ||
| 101 | $class->merge_config_hashes( $config, $arguments, $class->config ); | ||
| 102 | $class->config($config); | ||
| 103 | |||
| 104 | my $self = $class->NEXT::new( $c, $config ); | ||
| 105 | |||
| 106 | if ( $config->{mime_magic} | ||
| 107 | && $config->{mime_magic_file} | ||
| 108 | && -e $config->{mime_magic_file} ) | ||
| 109 | { | ||
| 110 | $self->mmagic( File::MMagic->new( $config->{mime_magic_file} ) ); | ||
| 111 | } | ||
| 112 | |||
| 113 | $c->log->debug("$class config parameters"); | ||
| 114 | |||
| 115 | return $self; | ||
| 116 | } | ||
| 117 | |||
| 118 | =head2 process($c, $arguments) | ||
| 119 | |||
| 120 | Override L<Catalyst::View> process method. | ||
| 121 | |||
| 122 | Using 'X-REPROXY-FILE' or 'X-Sendfile' header, | ||
| 123 | |||
| 124 | sub index: Private { | ||
| 125 | my ($self, $c) = @_; | ||
| 126 | |||
| 127 | $c->forward('View::MyReproxy', [reproxy_file => '/path/to/file']) | ||
| 128 | } | ||
| 129 | |||
| 130 | Using 'X-REPROXY-URL', | ||
| 131 | |||
| 132 | sub index: Private { | ||
| 133 | my ($self, $c) = @_; | ||
| 134 | |||
| 135 | $c->view('MyReproxy')->process($c, | ||
| 136 | 'reproxy_file' => $c->path_to('DUMMY') | ||
| 137 | ); | ||
| 138 | } | ||
| 139 | |||
| 140 | The url or path can be substituted to $c->stash instead of arguments, | ||
| 141 | |||
| 142 | sub index: Private { | ||
| 143 | my ($self, $c) = @_; | ||
| 144 | |||
| 145 | $c->stash->{reproxy_file} = '/path/to/file'; | ||
| 146 | $c->forward('View::MyReproxy'); | ||
| 147 | } | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub process { | ||
| 152 | my ( $self, $c, %arguments ) = @_; | ||
| 153 | |||
| 154 | my $file = $arguments{file} | ||
| 155 | || $arguments{reproxy_file} | ||
| 156 | || $c->stash->{reproxy_file}; | ||
| 157 | my $url = $arguments{url} | ||
| 158 | || $arguments{reproxy_url} | ||
| 159 | || $c->stash->{reproxy_url}; | ||
| 160 | |||
| 161 | if ($file) { | ||
| 162 | $self->process_file( $c, $file, \%arguments ); | ||
| 163 | } | ||
| 164 | elsif ($url) { | ||
| 165 | $url = [ split( / /, $url ) ] unless ( ref $url eq "ARRAY" ); | ||
| 166 | $self->process_url( $c, $url, \%arguments ); | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | $c->response->status(403); | ||
| 170 | $c->log->error("No setting file or url"); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | =head2 process_file($c, $file, $arguments) | ||
| 175 | |||
| 176 | processing file. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | sub process_file { | ||
| 181 | my ( $self, $c, $file, $arguments ) = @_; | ||
| 182 | |||
| 183 | if ( $self->config->{perlbal} || $self->config->{lighttpd} ) { | ||
| 184 | if ( $self->config->{perlbal} ) { | ||
| 185 | $c->response->header( 'X-REPROXY-FILE', $file ); | ||
| 186 | |||
| 187 | my $expected_size = | ||
| 188 | ( -e $file ) ? ( -s $file ) : $c->stash->{reproxy_expected_size}; | ||
| 189 | |||
| 190 | if ( defined $expected_size ) { | ||
| 191 | $c->response->header( 'X-REPROXY-EXPECTED-SIZE', | ||
| 192 | $expected_size ); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | $c->response->header( 'X-Sendfile', $file ); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | unless ( -e $file ) { | ||
| 201 | $c->response->status(404); | ||
| 202 | $c->log->error("File not found"); | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | my $content; | ||
| 207 | my $content_length = -s $file; | ||
| 208 | |||
| 209 | sysopen( SENDFILE, $file, O_RDONLY ); | ||
| 210 | sysread( SENDFILE, $content, -s $file ); | ||
| 211 | |||
| 212 | unless ( $c->response->content_type ) { | ||
| 213 | if ( $self->mmagic ) { | ||
| 214 | $c->response->content_type( | ||
| 215 | $self->mmagic->checktype_contents($content) ); | ||
| 216 | } | ||
| 217 | else { | ||
| 218 | $c->response->content_type( | ||
| 219 | File::MimeInfo::mimetype(*SENDFILE) ); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | close(SENDFILE); | ||
| 224 | |||
| 225 | $c->response->content_length( -s $file ); | ||
| 226 | $c->response->body($content); | ||
| 227 | } | ||
| 228 | |||
| 229 | $self->process_extra_headers( $c, $arguments ); | ||
| 230 | } | ||
| 231 | |||
| 232 | =head2 process_url($c, $url, $arguments) | ||
| 233 | |||
| 234 | processing urls. | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | sub process_url { | ||
| 239 | my ( $self, $c, $url, $arguments ) = @_; | ||
| 240 | |||
| 241 | if ( $self->config->{perlbal} ) { | ||
| 242 | $c->response->header( 'X-REPROXY-URL', join( " ", @$url ) ); | ||
| 243 | |||
| 244 | my $expected_size = $c->stash->{reproxy_expected_size}; | ||
| 245 | |||
| 246 | if ( defined $expected_size ) { | ||
| 247 | $c->response->header( 'X-REPROXY_EXPECTED_SIZE', $expected_size ); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | my $rand_url = $url->[ int( rand( scalar @{$url} ) ) ]; | ||
| 252 | |||
| 253 | my $ua = LWP::UserAgent->new; | ||
| 254 | $ua->timeout( int $self->config->{timeout} ) | ||
| 255 | if ( $self->config->{timeout} ); | ||
| 256 | |||
| 257 | my $req = HTTP::Request->new( GET => $rand_url ); | ||
| 258 | $req->header( 'Accept' => '*' ); | ||
| 259 | my $res = $ua->request($req); | ||
| 260 | |||
| 261 | if ( $res->is_success ) { | ||
| 262 | my $content = $res->content; | ||
| 263 | |||
| 264 | unless ( $c->response->content_type ) { | ||
| 265 | if ( $self->mmagic ) { | ||
| 266 | $c->response->content_type( | ||
| 267 | $self->mmagic->checktype_contents($content) ); | ||
| 268 | } | ||
| 269 | else { | ||
| 270 | $c->response->content_type( $res->header('Content-Type') ); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | $c->response->content_length( $res->header('Content-Length') ); | ||
| 275 | $c->response->body( $res->content ); | ||
| 276 | } | ||
| 277 | else { | ||
| 278 | $c->response->status(403); | ||
| 279 | $c->log->error("Request $url is not success"); | ||
| 280 | return; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | $self->process_extra_headers( $c, $arguments ); | ||
| 285 | } | ||
| 286 | |||
| 287 | =head2 header_name($header) | ||
| 288 | |||
| 289 | Translating http header name. | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | sub header_name { | ||
| 294 | my ( $self, $header ) = @_; | ||
| 295 | |||
| 296 | my $header_name = $header; | ||
| 297 | $header_name =~ s/_/-/g; | ||
| 298 | $header_name = | ||
| 299 | join( "-" => map { ucfirst lc $_ } split( /-/, $header_name ) ); | ||
| 300 | |||
| 301 | return $header_name; | ||
| 302 | } | ||
| 303 | |||
| 304 | =head2 process_extra_headers($c, $arguments) | ||
| 305 | |||
| 306 | Setting extra http response headers. | ||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | sub process_extra_headers { | ||
| 311 | my ( $self, $c, $arguments ) = @_; | ||
| 312 | |||
| 313 | if ( $arguments->{extra_headers} ) { | ||
| 314 | foreach my $header ( keys %{ $arguments->{extra_headers} } ) { | ||
| 315 | $c->response->header( $self->header_name($header), | ||
| 316 | $arguments->{extra_headers}->{$header} ); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | =head1 AUTHOR | ||
| 322 | |||
| 323 | Toru Yamaguchi, C<< <zigorou at cpan.org> >> | ||
| 324 | |||
| 325 | =head1 BUGS | ||
| 326 | |||
| 327 | Please report any bugs or feature requests to | ||
| 328 | C<bug-catalyst-view-reproxy at rt.cpan.org>, or through the web interface at | ||
| 329 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-View-Reproxy>. | ||
| 330 | I will be notified, and then you'll automatically be notified of progress on | ||
| 331 | your bug as I make changes. | ||
| 332 | |||
| 333 | =head1 SUPPORT | ||
| 334 | |||
| 335 | You can find documentation for this module with the perldoc command. | ||
| 336 | |||
| 337 | perldoc Catalyst::View::Reproxy | ||
| 338 | |||
| 339 | You can also look for information at: | ||
| 340 | |||
| 341 | =over 4 | ||
| 342 | |||
| 343 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 344 | |||
| 345 | L<http://annocpan.org/dist/Catalyst-View-Reproxy> | ||
| 346 | |||
| 347 | =item * CPAN Ratings | ||
| 348 | |||
| 349 | L<http://cpanratings.perl.org/d/Catalyst-View-Reproxy> | ||
| 350 | |||
| 351 | =item * RT: CPAN's request tracker | ||
| 352 | |||
| 353 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-View-Reproxy> | ||
| 354 | |||
| 355 | =item * Search CPAN | ||
| 356 | |||
| 357 | L<http://search.cpan.org/dist/Catalyst-View-Reproxy> | ||
| 358 | |||
| 359 | =back | ||
| 360 | |||
| 361 | =head1 ACKNOWLEDGEMENTS | ||
| 362 | |||
| 363 | =head1 COPYRIGHT & LICENSE | ||
| 364 | |||
| 365 | Copyright 2006 Toru Yamaguchi, all rights reserved. | ||
| 366 | |||
| 367 | This program is free software; you can redistribute it and/or modify it | ||
| 368 | under the same terms as Perl itself. | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | 1; # End of Catalyst::View::Reproxy |