| 1 | # $Id: /mirror/gungho/lib/Gungho/Engine.pm 31637 2007-12-01T14:04:35.046822Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package Gungho::Engine; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Base); | ||
| 10 | |||
| 11 | __PACKAGE__->mk_virtual_methods($_) for qw(run stop); | ||
| 12 | |||
| 13 | sub finish_request | ||
| 14 | { | ||
| 15 | my ($self, $c, $request) = @_; | ||
| 16 | if (my $host = $request->notes('original_host')) { | ||
| 17 | # Put it back | ||
| 18 | $request->uri->host($host); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | sub handle_response | ||
| 23 | { | ||
| 24 | my ($self, $c, $request, $response) = @_; | ||
| 25 | $self->finish_request($c, $request); | ||
| 26 | $c->handle_response($request, $response); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub handle_dns_response | ||
| 30 | { | ||
| 31 | my ($self, $c, $request, $dns_response) = @_; | ||
| 32 | |||
| 33 | if (! $dns_response) { | ||
| 34 | return; | ||
| 35 | } | ||
| 36 | |||
| 37 | foreach my $answer ($dns_response->answer) { | ||
| 38 | next unless $answer->type eq 'A'; | ||
| 39 | return if $c->handle_dns_response($request, $answer, $dns_response); | ||
| 40 | } | ||
| 41 | |||
| 42 | $c->handle_response($request, $c->_http_error(500, "Failed to resolve host " . $request->uri->host, $request)), | ||
| 43 | } | ||
| 44 | |||
| 45 | 1; | ||
| 46 | |||
| 47 | __END__ |