Slide 1

Slide 1 text

Perl WAF Overview, with mod_perl גࣜձࣾfonfun ඌܗ మ࣍ (OGATA Tetsuji) Twitter: @xtetsuji 2012/05/12 Hokkaido.pm#7

Slide 2

Slide 2 text

*Preface for readers* • ͜ͷεϥΠυ͸ 2012/05/13ʹߦΘΕͨ Hokkaido.pm#7ʹͯʮඈͼೖΓLTʯΛ ߦͬͨࡍʹٳܜ࣌ؒதͷ10෼Ͱ࡞ͬͨ ଈ੮εϥΠυʹɺ࣮ࡍͷτʔΫ಺༰Λ ৫Γ·ͥͯ࠶ฤूͨ͠΋ͷͰ͢

Slide 3

Slide 3 text

ࣗݾ঺հ

Slide 4

Slide 4 text

ࣗݾ঺հ • ඌܗ మ࣍ (OGATA Tetsuji) • Twitter: @xtetsuji • Blog: http://post.tetsuji.jp/ • ग़਎͸๺ւಓՏ౦܊Իߋொ(ଳ޿ࢢͷྡ) • େֶͰ্ژͯ͠ݱࡏ͸౦ژͷձࣾʹۈ຿

Slide 5

Slide 5 text

ࣗݾ঺հ • Hokkaido.pm 3ճ໨ͷࢀՃ • Hokkaido.pm#5ʮmod_perlԹނ஌৽ʯ • Hokkaido.pm#6ʮmod_perl Hacks PHPʯ • ϞμϯPerlʹ৐Ε͍ͯͳ͍30୅ • mod_perl Hacker…ͳͷ͔ʁ

Slide 6

Slide 6 text

ॴଐ঺հ • גࣜձࣾfonfun(ϑΥϯϑΝϯ) http://www.fonfun.co.jp/ • ओྗ੡඼ɿϦϞʔτϝʔϧ http://rmail.jp/

Slide 7

Slide 7 text

Perl WAF, ancient and modern

Slide 8

Slide 8 text

Perl WAF, ancient • raw (not WAF?) • CGI::Application • ...etc

Slide 9

Slide 9 text

Perl WAF, modern • Catalyst • Jifty • ...etc

Slide 10

Slide 10 text

Perl WAF, post-modern • Dancer • Mojolicious • Amon2 • Kossy • ...etc

Slide 11

Slide 11 text

What is WAF? • ʮMVC෼཭ʯΛଅਐ͢Δ΋ͷʁ • ҧ͏ • Catalystͷࣦഊྫʁ • ModelΛWAFʹີணͤ͞Δͱɺ֎ଆͷ ؅ཧπʔϧ౳͔Β࢖͍ͮΒ͘ͳΔ

Slide 12

Slide 12 text

What is WAF? • ࢲ͸͜͏ࢥ͏… HTTP Request/Responseͷந৅Խ

Slide 13

Slide 13 text

What is WAF? •HTTP Request/Responseͷந৅Խ • Web Server͝ͱͷࠩҟΛٵऩ • Catalyst::Engine:: ͱ͔ͷόουϊ΢ϋ΢ •PSGI/Plackͷ࣮݁΁

Slide 14

Slide 14 text

ͦΜͳࡉ͔͍ࣄΑΓ mod_perl2

Slide 15

Slide 15 text

ΈΜͳେ޷͖ mod_perl2

Slide 16

Slide 16 text

Latest Perl WAF fashion • Sinatra (WAF, powered by Ruby) Like • Perl post-modern WAFs almost have Sinatratic syntax • Lightweight • Controller oriented

Slide 17

Slide 17 text

WAF by mod_perl2 • mod_perl1 ͸ׂѪɺmod_perl2 Λ࢖͏ • mod_perl2 ͷResponseHandlerΛ Sinatra Like ʹ͢Ε͹ WAF ͬͯݴ͍͍ͬͯΜ δϟϚΠΧʁ

Slide 18

Slide 18 text

WAF by mod_perl2 • ʮWeb Server͝ͱͷࠩҟΛٵऩʯͱ͔ ͖ͬ͞ݴ͍ͬͯͨࣄ͸ແࢹʂ •Apache2/mod_perl2΂ͬͨΓ • mod_perl2ͷ $r (Apache2::RequestRec)͸ Request/ResponseΛྑ͘ந৅Խ͍ͯ͠Δ

Slide 19

Slide 19 text

mod_perl2 handler’s simple “Hello world”

Slide 20

Slide 20 text

package MyApache2::Hello; use strict; use warnings; use Apache2::RequestRec; use Apache2::RequestIO; use Apache2::Const -compile => qw(OK); sub handler { my $r = shift; $r->content_type("text/plain"); $r->print("Hello, world"); return Apache2::Const::OK; } 1; __END__ PerlResponseHandler MyApache2::Hello

Slide 21

Slide 21 text

͜ΕΛSinatra Likeʹ ͢Ε͹͍͍

Slide 22

Slide 22 text

ͳΒ͹ͱॻ͍ͯΈͨ MyApache2::Sinatratic

Slide 23

Slide 23 text

package MyApache2::Sinatratic; use strict; use warnings; # $CALLBACK->{$handler_package}->{$http_method} = [ [$url, $handler], ... ]; my $CALLBACK = {}; sub import { my $pkg = shift; my @args = @_; my $callpkg = caller(0); for my $method (qw(get post put del)) { $CALLBACK->{$callpkg}->{$method} = []; } # sub handler definition require Apache2::RequestRec; require Apache2::RequestUtil; require APR::Table; no strict 'refs'; *{"$callpkg\::handler"} = \&import_handler; for my $method (qw(get post put del)) { *{"$callpkg\::$method"} = sub { my ($url, $handler) = @_; push @{$CALLBACK->{$callpkg}->{$method}}, [$url, $handler]; }; } *{"$callpkg\::default"} = sub { my $handler = shift; $CALLBACK->{$callpkg} ||= {}; $CALLBACK->{$callpkg}->{default} = $handler; }; }

Slide 24

Slide 24 text

֓ཁ͸͜Μͳײ͡

Slide 25

Slide 25 text

mod_perl2 handler’s Sinatratic “Hello world”

Slide 26

Slide 26 text

package MyApache2::Hello2; use strict; use warnings; use Apache2::RequestRec; use Apache2::RequestIO; use Apache2::Const -compile => qw(OK); use MyApache2::Sinatratic; get '/' => sub { my $r = shift; $r->content_type("text/plain"); $r->print("Hello, world"); return Apache2::Const::OK; }; 1; __END__ PerlResponseHandler MyApache2::Hello2

Slide 27

Slide 27 text

SinatraͬΆ͘ͳͬͨ! mod_perl2΋WAFͩ!

Slide 28

Slide 28 text

MyApache2::Sinatratic • …ͱ͍͏ͷ͸׬શͳΔδϣʔΫ • ଍Γͳ͍ػೳɺόάຬࡌ • ϓϨʔεϗϧμʹରԠͨͭ͠΋Γఔ౓

Slide 29

Slide 29 text

MyApache2::Sinatratic • ෺޷͖ͷͨΊʹGistʹ์ΓࠐΜͩ • ࢖͏ਓ͸͍ͳ͍ͱࢥ͏͚Ͳɺ࢖͓͏ͱ ࢥ͏༐ऀ͸CAVEATSΛख़ಡͯ͠ʂ • ༐ऀ͕ू·Ε͹ModPerl::Sinatratic(Ծশ) ϓϩδΣΫτ΋΍ͬͪΌ͏͔΋ʂ

Slide 30

Slide 30 text

MyApache2::Sinatratic • ॻ͍͍ͯͯࢥͬͨࣄ • Sinatra LikeͳWAFͬͯɺPlackແ͠Ͱ΋ ͜͏࡞Ε͹͍͍ΜͩͱษڧͰ͖ͨ • γϯϘϧςʔϒϧ͍͡Γָ͍̇͠ •I ὑ Perl!

Slide 31

Slide 31 text

͝ਗ਼ௌ ͋Γ͕ͱ͏͍͟͝·ͨ͠