2. Attention for audience
• This slide is mainly written by Japanese, and
few English. In the future, I will write and
share this slide of English version, perhaps.
• I speach by Japanese language.
• If you do not known Japanese language,
please fun and feel from some Perl code
and few English description on this slide.
14. History of mod_perl
• mod_perl1 first public release: 1996/3/25
(see: http://perl.apache.org/dist/mod_perl-1.0-current/Changes)
• mod_perl2 first public release: 2002/4/6
(see: http://perl.apache.org/dist/mod_perl-2.0-current/Changes)
29. first mod_perl1 handler
package MyApache::Hello;
use strict;
use warnings;
use Apache::Constants qw(OK);
sub handler {
my $r = shift; # "Apache" object
$r->send_http_header('text/plain');
$r->print("Hello! mod_perl1.n");
return OK;
}
1;
30. first mod_perl2 handler
package MyApache2::Hello;
use strict;
use warnings;
use Apache2::RequestRec (); # for $r->content_type()
use Apache2::RequestIO (); # for $r->print()
use Apache2::Const -compile => qw(OK);
sub handler {
my $r = shift; # "Apache2::RequestRec" object
$r->content_type('text/plain');
$r->print("Hello! mod_perl2.n");
return Apache2::Const::OK;
}
1;
37. Apache1 URI Trans
package MyApache1::Trans;
use strict;
use warnings;
use Apache::Constants qw(DECLINED);
sub handler {
my $r = shift;
my $uri = $r->uri(); # e.g. "/path/to/foo.html"
### ... modify $uri ...
$r->uri($uri);
return DECLINED; # I tell a lie that I do nothing.
}
1;
38. Apache2 URI Trans
package MyApache2::Trans;
use strict;
use warnings;
use Apache2::RequestRec (); # for $r->uri()
use Apache2::Const -compile => qw(DECLINED);
sub handler {
my $r = shift;
my $uri = $r->uri(); # e.g. "/path/to/foo.html"
### ... modify $uri ...
$r->uri($uri); # set $uri
return Apache2::Const::DECLINED;
# I tell a lie that I do nothing.
}
1;
39. Apache{1,2} configs
# Apache1
# e.g. In <VirtualHost> Directive
PerlTransHandler MyApache1::Trans
# Apache2
# e.g. In <VirtualHost> Directive
PerlTransHandler MyApache2::Trans
40. mod_perl URI Trans
pros than mod_rewrite
• mod_rewriteに比べて可読性が高くなる
• DBを引いたりmemcachedにアクセスし
たりPerlで出来ることは何でもできる
41. mod_perl URI Trans
cons than mod_rewrite
• 複数のファイル管理が必要となる(*.pm)
• Perl/mod_perlが分からない担当者への
引き継ぎが難しくなる
• Directory Context(<Location>, .htaccess)
に書かれたmod_rewrite設定の場合は、
別途ケアが必要なケースもある
47. package MyApache2::Auth;
use strict;
use warnings;
# $r->headers_in() and $r->headers_out() returns APR::Table
use APR::Table ();
use Apache2::RequestRec ();
use Apache2::Const -compile => qw(OK REDIRECT);
sub handler {
my $r = shift;
my $cookie = $r->headers_in->get('Cookie'); # raw cookie
my $x_up_subno = $r->headers_in->get('X-UP-Subno');
my ($is_success, $location, $set_cookie);
###
### ... modify and analyze this session information ...
###
if ( $is_success ) {
$r->headers_out->set('Set-Cookie' => $set_cookie);
return Apache2::Const::OK;
}
else {
$r->headers_out->set(Location => $location);
$r->err_headers_out->set(Location => $location);
return Apache2::Const::REDIRECT;
}
} # e.g. In <VirtualHost> Directive
1;
PerlAccessHandler MyApache2::Auth
50. package MyApache2::FilterObfuscate;
use APR::Table ();
use Apache2::Filter ();
use Apache2::RequestRec ();
use Apache2::Const -compile => qw(OK);
my $READ_CHUNK_LENGTH = 2048;
sub handler {
my $f = shift; # $f is "Apache2::Filter" object
### If filter is chained, this filter is first?
unless ($f->ctx) {
$f->r->headers_out->unset('Content-Length');
$f->ctx(1);
}
while ($f->read(my $buffer, $READ_CHUNK_LENGTH)) {
$buffer =~ s/[rn]//g;
$f->print($buffer);
}
return Apache2::Const::OK;
}
1; # e.g. In <VirtualHost> Directive
<FilesMatch “.*(html?|php)$”>
PerlOutputFilterHandler MyApache2::FilterObfuscate
</FilesMatch>
51. PHP and Perl are friend
• Apacheの上でPHPとPerlは友達!
• 他にも各種フックを使ってmod_perlは
PHPを助けることができる
• 可能性は無限大
57. Queue server by
mod_perl2 thread
• 今回はスレッドの変数共有を利用した
mod_perl2の文字列Queueサーバを実演
• “POST /” でRequest BodyをQueueing、
• “GET /” でQueueをPickup
58. Queue server by
mod_perl2 thread (1)
package My::Queue;
use strict;
use warnings;
use threads;
use threads::shared;
my @queue :shared;
sub push_queue {
my $value = shift;
return 0 if !defined $value;
push @queue, $value;
return 1;
}
sub shift_queue {
return shift @queue
}
1;
59. Queue server by
mod_perl2 thread (2)
package MyApache2::ThreadQueue;
use strict;
use warnings;
use threads;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => qw(OK HTTP_METHOD_NOT_ALLOWED);
use My::Queue;
my $READ_CHUNK_LENGH = 2048;
###
### ... following sub handler { ... } ...
###
60. Queue server by
mod_perl2 thread (3)
### continued
sub handler {
my $r = shift;
if ( $r->method eq 'POST' ) {
my $value;
while ( $r->read(my $buf, $READ_CHUNK_LENGH) ) {
$value .= $buf;
}
My::Queue::push_queue($value);
$r->content_type('text/plain'); $r->print('Queued');
return Apache2::Const::OK;
}
elsif ( $r->method eq 'GET' ) {
my $retval = My::Queue::shift_queue();
$r->content_type('text/plain'); $r->print($retval);
return Apache2::Const::OK;
}
else {
return Apache2::Const::HTTP_METHOD_NOT_ALLOWED;
}
}
1;
61. worker MPM and
perl ithreads practice
• 複雑な事はさせてはいけない
• 複雑な事をさせる場合には十分試験を
• 大事なことなので(ry
• 前出のQueue serverは100KB程度の文字
列の入出力を10K/minさせても問題無し