Web Apps in Perl - HTTP 101

5,623 views
5,471 views

Published on

An 15 minute introduction to web applications and writing them in Perl with CGI.pm and mod_perl v1 and v2

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
5,623
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
64
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide


























































  • Web Apps in Perl - HTTP 101

    1. 1. WEB APPS WITH PERL HTTP 101 HENDRIK VAN BELLEGHEM HENDRIK.VANBELLEGHEM@GMAIL.COM
    2. 2. UNDERSTANDING HTTP ONE PAGE, MANY PACKETS
    3. 3. UNDERSTANDING HTTP DOCUMENT REQUEST
    4. 4. UNDERSTANDING HTTP RESPONSE STATUS
    5. 5. UNDERSTANDING HTTP REQUEST DOMAIN
    6. 6. UNDERSTANDING HTTP RESPONSE SIZE
    7. 7. UNDERSTANDING HTTP RESPONSE TIME
    8. 8. UNDERSTANDING HTTP
    9. 9. UNDERSTANDING HTTP BROWSER SENDS
    10. 10. UNDERSTANDING HTTP WEBSERVER RESPONDS
    11. 11. DIY CGI SCRIPT - HELLO WORLD #!/usr/bin/perl print quot;Content-type: text/htmlnnquot;; print quot;Hello Worldquot;;
    12. 12. DIY
    13. 13. DIY CGI SCRIPT - INTERACTION #!/usr/bin/perl print quot;Content-type: text/htmlnnquot;; @pairs = split(/&/, $ENV{'QUERY_STRING'}); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(quot;Cquot;, hex($1))/eg; $value =~ s/n/ /g; $request{$name} = $value; } print quot;Hello quot;,$request{'name'};
    14. 14. DIY
    15. 15. DIY CGI SCRIPT - INTERACTION #!/usr/bin/perl MY EYES!!!! print quot;Content-type: text/htmlnnquot;; @pairs = split(/&/, $ENV{'QUERY_STRING'}); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(quot;Cquot;, hex($1))/eg; $value =~ s/n/ /g; $request{$name} = $value; } THE HORROR!!!! print quot;Hello quot;,$request{'name'};
    16. 16. CGI.PM CGI SCRIPT - HELLO WORLD #!/usr/bin/perl use CGI qw(header); print header; print “Hello world”;
    17. 17. CGI.PM CGI SCRIPT - INTERACTION #!/usr/bin/perl use CGI qw(header param); print header; my $name = param(‘name’); print “Hello $name”;
    18. 18. CGI.PM TRANSPARENT POST & GET FILE UPLOADS QUICK COOKIES HTML GENERATION (NOT MY FAVORITE) FUNCTION SETS: :CGI = PARAM , HEADER, ... DROP-IN DEVELOPMENT
    19. 19. MOD_PERL PERL AS AN APACHE MODULE INTERACT WITH EVERY STEP OF APACHE REQUEST CYCLE PERSISTENT COPY OF PERL IN MEMORY NO DROP-IN DEVELOPMENT EXCEPT FOR APACHE::RELOAD
    20. 20. MOD_PERL 1 16 CYCLES IN MOD_PERL 1 CONTENT HANDLER: PERLHANDLER LOG HANDLER: PERLLOGHANDLER AUTHENTICATION HANDLER
    21. 21. MOD_PERL 2 21 CYCLES IN MOD_PERL 2 CONTENT HANDLER: PERLRESPONSEHANDLER LOG HANDLER: PERLLOGHANDLER AUTHENTICATION HANDLER
    22. 22. MOD_PERL 1 CONTENT HANDLER - HELLO WORLD package Apache::HelloWorld; use Apache::Constants qw(:common); # Export OK sub handler # mod_perl uses handler method unless specified otherwise { print quot;Content-type: text/plainnnquot;; # MY EYES!! print quot;Hello Worldnquot;; return OK; # read as HTTP status 200 } 1; PerlModule Apache::HelloWorld <Location /HelloWorld> SetHandler perl-script PerlHandler Apache::HelloWorld </Location> http://localhost/HelloWorld/
    23. 23. MOD_PERL 1 CONTENT HANDLER - HELLO WORLD REVISED package Apache::HelloWorld; use Apache::Constants qw(:common); # Export OK sub handler # mod_perl uses handler method unless specified otherwise { my $r = shift; # 1st argument is instance of Apache's Request object $r->send_http_header('text/plain'); # Send HTTP header (similar to CGI's header) $r->print(quot;Hello Worldnquot;); return OK; # read as HTTP status 200 } 1; PerlModule Apache::HelloWorld <Location /HelloWorld> SetHandler perl-script PerlHandler Apache::HelloWorld </Location> http://localhost/HelloWorld/
    24. 24. MOD_PERL 1 CONTENT HANDLER - HELLO BOB! package Apache::HelloBob use Apache::Constants qw(:common); # Export OK sub handler # mod_perl uses handler method unless specified otherwise { my $r = shift; # 1st argument is instance of Apache's Request object my %query_string = $r->args; # GET data my %post_data = $r->content; # POST data my $name = $query_string{'name'}; $r->send_http_header('text/plain'); # Send HTTP header (similar to CGI's header) $r->print(quot;Hello $namenquot;); return OK; # read as HTTP status 200 } 1; http://localhost/HelloBob?name=Bob
    25. 25. APACHE::REQUEST (A::R) INHERITS FROM APACHE CLASS MIMICS CGI.PM METHODS SUPPORTS FILE UPLOADS SUPPORTS COOKIES - APACHE::COOKIE / APACHE2::COOKIE AVAILABLE FOR MOD_PERL V1 & V2
    26. 26. MOD_PERL 1 CONTENT HANDLER - HELLO BOB WITH A::R package Apache::HelloWorld; use Apache::Constants qw(:common); # Export OK use Apache::Request (); # isa Apache sub handler # mod_perl uses handler method unless specified otherwise { my $r = shift; my $apr = Apache::Request->new($r); # Pass Apaches Request object to A::R $apr->send_http_header('text/plain'); my $name = $apr->param(‘name’); # Send HTTP header (similar to CGI's header) $apr->print(quot;Hello $namenquot;); return OK; # read as HTTP status 200 } 1; http://localhost/HelloWorld?name=Bob
    27. 27. MOD_PERL 2 CONTENT HANDLER - HELLO WORLD package Apache2::HelloWorld; use Apache2::Const qw(:common); # Export OK sub handler # mod_perl2 uses handler method unless specified otherwise { print quot;Content-type: text/plainnnquot;; # MY EYES!! print quot;Hello Worldnquot;; return OK; # read as HTTP status 200 } 1; PerlModule Apache2::HelloWorld <Location /HelloWorld> SetHandler perl-script PerlResponseHandler Apache2::HelloWorld </Location> http://localhost/HelloWorld/
    28. 28. MOD_PERL 2 CONTENT HANDLER - HELLO WORLD REVISED package Apache2::HelloWorld; use Apache2::Const qw(:common); # Export OK sub handler # mod_perl2 uses handler method unless specified otherwise { my $r = shift; # 1st argument is instance of Apache's Request object $r->content_type('text/plain'); # Send HTTP header (similar to CGI's header) $r->print(quot;Hello Worldnquot;); return OK; # read as HTTP status 200 } 1; PerlModule Apache2::HelloWorld <Location /HelloWorld> SetHandler perl-script PerlResponseHandler Apache2::HelloWorld </Location> http://localhost/HelloWorld/
    29. 29. MOD_PERL 2 CONTENT HANDLER - HELLO BOB WITH A2::R package Apache2::HelloBob; use Apache2::Const qw(:common); # Export OK use Apache2::Request (); # isa Apache sub handler # mod_perl uses handler method unless specified otherwise { my $r = shift; my $apr = Apache2::Request->new($r); # Pass Apaches Request object to A2::R $apr->send_http_header('text/plain'); my $name = $apr->param(‘name’); # Send HTTP header (similar to CGI's header) $apr->print(quot;Hello $namenquot;); return OK; # read as HTTP status 200 } 1; http://localhost/HelloWorld?name=Bob

    ×