Moxy::Plugin::Hosts - /etc/hosts file emulator


Moxy documentation Contained in the Moxy distribution.

Index


Code Index:

NAME

Top

Moxy::Plugin::Hosts - /etc/hosts file emulator

SYNOPSIS

Top

    - module: Hosts
      config:
        hosts:
          coderepos.org: 192.168.0.1

AUTHOR

Top

    Kazuhiro Osawa

SEE ALSO

Top

Moxy


Moxy documentation Contained in the Moxy distribution.

package Moxy::Plugin::Hosts;
use strict;
use warnings;
use base qw/Moxy::Plugin/;

sub request_filter :Hook {
    my ($self, $context, $args) = @_;

    my $hosts = $self->config->{config}->{hosts};
    my $uri   = $args->{request}->uri;
    if (my $ip = $hosts->{$uri->host}) {
        my $host = $uri->host;
        $context->log( debug => " $host -> $ip " );
        $uri->host($ip);
        $args->{request}->uri($uri);

        $args->{request}->header( Host => $host );
        $self->{original_host} = $host;
    }
}

sub response_filter :Hook {
    my ($self, $context, $args) = @_;

    my $uri = $args->{response}->request->uri;
    $uri->host($self->{original_host});
    $args->{response}->request->uri($uri);
}


1;
__END__