root/lang/perl/Moxy/branches/CC/lib/Moxy/Plugin/Filter/HTTPHeader.pm @ 9923

Revision 9923, 1.8 kB (checked in by tokuhirom, 7 years ago)

HTTPHeaders も Class::Component 化完了。

Line 
1package Moxy::Plugin::Filter::HTTPHeader;
2use strict;
3use warnings;
4use base qw/Moxy::Plugin/;
5use HTTP::MobileAgent;
6use URI;
7use URI::Escape;
8use CGI;
9
10sub r:Hook('request_filter') {
11    my ($self, $context, $args) = @_;
12
13    my $http_header = $context->storage->get(__PACKAGE__. $args->{user});
14
15    if ($http_header) {
16        for my $header (split /\n/, $http_header) {
17            next unless $header;
18
19            if ($header =~ /^([^:]+)\s*:\s*(.+)$/) {
20                $args->{request}->header($1 => $2);
21                $context->log(debug => "set header: '$1' => '$2'");
22            }
23        }
24    }
25}
26
27sub c:Hook('control_panel') {
28    my ($self, $context, $args) = @_;
29
30    # generate control panel html.
31    my %params = URI->new($args->{response}->request->uri)->query_form;
32
33    return $self->render_template(
34        $context,
35        'panel.tt' => {
36            params      => \%params,
37            current_uri => $args->{response}->request->uri,
38            headers     => $context->storage->get(__PACKAGE__ . $args->{user}),
39        }
40    );
41}
42
43# set.
44sub x:Hook('request_filter') {
45    my ($self, $context, $args) = @_;
46
47    if ($args->{request}->uri =~ m{^http://http-header\.moxy/(.+)}) {
48        my $back = uri_unescape($1);
49
50        # store settings
51        my $r = CGI->new($args->{request}->content);
52        $context->storage->set(__PACKAGE__ . $args->{user} => $r->param('moxy_http_header'));
53
54        # back
55        my $response = HTTP::Response->new( 302, "Moxy(@{[ __PACKAGE__ ]})" );
56        $response->header(Location => $back);
57        return $response;
58    }
59}
60
611;
62__END__
63
64=encoding utf8
65
66=head1 NAME
67
68Moxy::Plugin::Filter::HTTPHeader - HTTP Header を操作する
69
70=head1 SYNOPSIS
71
72  - module: HTTPHeader
73
74=head1 DESCRIPTION
75
76set some http headers.
77
78=head1 AUTHOR
79
80Tokuhiro Matsuno
81
82=head1 SEE ALSO
83
84L<Moxy>
85
Note: See TracBrowser for help on using the browser.