| 1 | package HTTP::Engine::Interface::ModPerl; | ||
| 2 | my $CURRENT_ENGINE; | ||
| 3 | use HTTP::Engine::Interface | ||
| 4 | builder => 'CGI', | ||
| 5 | writer => { | ||
| 6 | output_header => sub { | ||
| 7 | my ($self, $req, $res) = @_; | ||
| 8 | my $r = $req->_connection->{apache_request} or die "missing apache request"; | ||
| 9 | $r->status( $res->status ); | ||
| 10 | my $content_type; | ||
| 11 | $res->headers->scan( | ||
| 12 | sub { | ||
| 13 | my ($key, $val) = @_; | ||
| 14 | $content_type = $val if lc $key eq 'content-type'; | ||
| 15 | $r->headers_out->add($key => $val); | ||
| 16 | } | ||
| 17 | ); | ||
| 18 | $r->content_type($content_type) if $content_type; | ||
| 19 | }, | ||
| 20 | write => sub { | ||
| 21 | my ($self, $buffer) = @_; | ||
| 22 | $CURRENT_ENGINE->interface->apache->print( $buffer ); | ||
| 23 | }, | ||
| 24 | } | ||
| 25 | ; | ||
| 26 | |||
| 27 | |||
| 28 | BEGIN | ||
| 29 | { | ||
| 30 | if (! exists $ENV{MOD_PERL_API_VERSION} || | ||
| 31 | $ENV{MOD_PERL_API_VERSION} != 2) | ||
| 32 | { | ||
| 33 | die "HTTP::Engine::Interface::ModPerl only supports mod_perl2"; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | use Apache2::Const -compile => qw(OK); | ||
| 38 | use Apache2::Connection; | ||
| 39 | use Apache2::RequestRec; | ||
| 40 | use Apache2::RequestIO (); | ||
| 41 | use Apache2::RequestUtil; | ||
| 42 | use Apache2::ServerRec; | ||
| 43 | use APR::Table; | ||
| 44 | use HTTP::Engine; | ||
| 45 | has 'apache' => ( | ||
| 46 | is => 'rw', | ||
| 47 | isa => 'Apache2::RequestRec', | ||
| 48 | weak_ref => 1, | ||
| 49 | ); | ||
| 50 | |||
| 51 | has 'context_key' => ( | ||
| 52 | is => 'rw', | ||
| 53 | isa => 'Str', | ||
| 54 | ); | ||
| 55 | |||
| 56 | my %HE; | ||
| 57 | |||
| 58 | sub handler : method | ||
| 59 | { | ||
| 60 | my $class = shift; | ||
| 61 | my $r = shift; | ||
| 62 | |||
| 63 | local %ENV = %ENV; | ||
| 64 | |||
| 65 | # ModPerl is currently the only environment where the inteface comes | ||
| 66 | # before the actual invocation of HTTP::Engine | ||
| 67 | |||
| 68 | my $context_key = $ENV{HTTP_ENGINE_CONTEXT_KEY} || join ':', $ENV{SERVER_NAME}, $ENV{SERVER_PORT}, $r->location; | ||
| 69 | my $engine = $HE{ $context_key }; | ||
| 70 | if (! $engine ) { | ||
| 71 | $engine = $class->create_engine($r, $context_key); | ||
| 72 | $HE{ $context_key } = $engine; | ||
| 73 | } | ||
| 74 | $CURRENT_ENGINE = $engine; | ||
| 75 | |||
| 76 | $engine->interface->apache( $r ); | ||
| 77 | $engine->interface->context_key( $context_key ); | ||
| 78 | |||
| 79 | |||
| 80 | $engine->interface->handle_request( | ||
| 81 | _connection => { | ||
| 82 | input_handle => $r, | ||
| 83 | output_handle => \*STDOUT, | ||
| 84 | env => \%ENV, | ||
| 85 | apache_request => $r, | ||
| 86 | }, | ||
| 87 | ); | ||
| 88 | |||
| 89 | return &Apache2::Const::OK; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub create_engine | ||
| 93 | { | ||
| 94 | my ($class, $r) = @_; | ||
| 95 | |||
| 96 | HTTP::Engine->new( | ||
| 97 | interface => HTTP::Engine::Interface::ModPerl->new( | ||
| 98 | request_handler => sub { HTTP::Engine::Response->new(status => 200) }, | ||
| 99 | ) | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub run { die "THIS IS DUMMY" } | ||
| 104 | |||
| 105 | __INTERFACE__ | ||
| 106 | |||
| 107 | __END__ |