HTTP::Engineを入れてみる

よく分からないまま、とにかくCPANからinstallしてみた。
とりあえずHTTP::Engine - モダンなPerl入門 - モダンなPerl入門を参考に、何か書いてみる。

#!/usr/bin/perl
use strict;
use warnings;

use HTTP::Engine;

HTTP::Engine->new(
    interface => {
        module => 'ServerSimple',
        args   => {
            host => 'localhost',
            port =>  1982,
        },
        request_handler => sub {
            my $req = shift;
            return HTTP::Engine::Response->new(
                status => 200,
                body => 'Hello World!',
                );
        },
    },
    )->run;

で、ブラウザから"http://localhost:1982/"にアクセス。
おぉ、"Hello World!"が表示された!


request_handlerでリクエストを受け取っているようだ。
ちょっと書き換えてみる。

#!/usr/bin/perl
use strict;
use warnings;

use HTTP::Engine;
use Data::Dumper;

HTTP::Engine->new(
    interface => {
        module => 'ServerSimple',
        args   => {
            host => 'localhost',
            port =>  1982,
        },
        request_handler => sub {
            my $req = shift;
            my $body = "<html><body><pre>\n";
            $body .= Dumper($req);
            $body .= "</pre></body></html>\n";
            return HTTP::Engine::Response->new(
                status => 200,
                body   => $body,
                );
        },
    },
    )->run;

とやってみると、結果は

$VAR1 = bless( {
                 '_connection' => {
                                    'input_handle' => \*Symbol::GEN10,
                                    'env' => {},
                                    'output_handle' => \*Symbol::GEN10
                                  },
                 'headers' => bless( {
                                       'user-agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; ja-jp) AppleWebKit/525.26.2 (KHTML, like Gecko) Version/3.2 Safari/525.26.12',
                                       'connection' => 'keep-alive',
                                       'accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
                                       'accept-language' => 'ja-jp',
                                       'accept-encoding' => 'gzip, deflate',
                                       'host' => 'localhost:1982'
                                     }, 'HTTP::Headers' ),
                 'connection_info' => {
                                        'protocol' => 'HTTP/1.1',
                                        'request_uri' => '/',
                                        'user' => undef,
                                        'address' => '127.0.0.1',
                                        'method' => 'GET',
                                        'port' => 1982,
                                        '_https_info' => undef
                                      },
                 'request_builder' => bless( {
                                               'chunk_size' => 4096
                                             }, 'HTTP::Engine::RequestBuilder::NoEnv' ),
                 'uri' => bless( [
                                   bless( do{\(my $o = 'http://localhost:1982/')}, 'URI::http' ),
                                   'http://localhost:1982/'
                                 ], 'URI::WithBase' )
               }, 'HTTP::Engine::Request' );

おぉ、headerとかconnection_infoが取り出せる!