| 1 | package Perlbal::Plugin::Addheader; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Perlbal::Plugin::Addheader - Add Headers to Perlbal webserver/reverse_proxy responses | ||||||||||
| 9 | |||||||||||
| 10 | =head1 VERSION | ||||||||||
| 11 | |||||||||||
| 12 | Version 0.01 | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 1 | our $VERSION = '0.02'; | |||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | =head1 Description | ||||||||||
| 20 | |||||||||||
| 21 | This module allows you to add/change headers to/from perlbal responses. | ||||||||||
| 22 | |||||||||||
| 23 | You can configure headers to be added/changed based on each service declared, although the service role has to be set to web_server or reverse_proxy. | ||||||||||
| 24 | |||||||||||
| 25 | For each header you want to add/change, you have to specify the header content, this header content can be a set of characters or Perl code that will be evaluated for each response. | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||
| 31 | |||||||||||
| 32 | This module provides a Perlbal plugin wich can be loaded and used as follows | ||||||||||
| 33 | |||||||||||
| 34 | Load Addheader | ||||||||||
| 35 | |||||||||||
| 36 | #ADDHEADER <service_name> <header_name> <header_content> | ||||||||||
| 37 | ADDHEADER static Server This is My Webserver | ||||||||||
| 38 | |||||||||||
| 39 | CREATE SERVICE static | ||||||||||
| 40 | SET ROLE = web_server | ||||||||||
| 41 | SET docroot /server/static | ||||||||||
| 42 | SET plugins = Addheader | ||||||||||
| 43 | ENABLE static | ||||||||||
| 44 | |||||||||||
| 45 | In this case for each response served by the C<Service static>, the header C<Server> will be changed to C<This is my Webserver>. | ||||||||||
| 46 | |||||||||||
| 47 | In cases where you need a dynamic value to be server as header content, you can put Perl code as the header content, surrounding the header content with C<[%> and C<%]>. | ||||||||||
| 48 | |||||||||||
| 49 | ADDHEADER static Expires [% {use HTTP::Date;HTTP::Date::time2str(time() + 2592000)} %] | ||||||||||
| 50 | |||||||||||
| 51 | In this case, for each response, the header C<Expires> will be added, ant the content will be the time in exactly 30 days from the time the response has been sent . | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | =head1 AUTHOR | ||||||||||
| 58 | |||||||||||
| 59 | Bruno Martins, C<< <bruno.martins at co.sapo.pt> >> | ||||||||||
| 60 | |||||||||||
| 61 | =head1 BUGS | ||||||||||
| 62 | |||||||||||
| 63 | Please report any bugs or feature requests to C<bug-perlbal-plugin-addheader at rt.cpan.org>, or through | ||||||||||
| 64 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Perlbal-Plugin-Addheader>. I will be notified, and then you'll | ||||||||||
| 65 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 66 | |||||||||||
| 67 | |||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | =head1 SUPPORT | ||||||||||
| 71 | |||||||||||
| 72 | You can find documentation for this module with the perldoc command. | ||||||||||
| 73 | |||||||||||
| 74 | perldoc Perlbal::Plugin::Addheader | ||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | You can also look for information at: | ||||||||||
| 78 | |||||||||||
| 79 | =over 4 | ||||||||||
| 80 | |||||||||||
| 81 | =item * RT: CPAN's request tracker | ||||||||||
| 82 | |||||||||||
| 83 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perlbal-Plugin-Addheader> | ||||||||||
| 84 | |||||||||||
| 85 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 86 | |||||||||||
| 87 | L<http://annocpan.org/dist/Perlbal-Plugin-Addheader> | ||||||||||
| 88 | |||||||||||
| 89 | =item * CPAN Ratings | ||||||||||
| 90 | |||||||||||
| 91 | L<http://cpanratings.perl.org/d/Perlbal-Plugin-Addheader> | ||||||||||
| 92 | |||||||||||
| 93 | =item * Search CPAN | ||||||||||
| 94 | |||||||||||
| 95 | L<http://search.cpan.org/dist/Perlbal-Plugin-Addheader/> | ||||||||||
| 96 | |||||||||||
| 97 | =back | ||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | =head1 TODO | ||||||||||
| 101 | |||||||||||
| 102 | Allow add/change response headers on all services (non role dependent) | ||||||||||
| 103 | |||||||||||
| 104 | Allow add/change response headers on all services at a time (one line configuration) | ||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 109 | |||||||||||
| 110 | Copyright 2009 Bruno Martins C<< <bruno.martins at co.sapo.pt> >> and SAPO C<http://www.sapo.pt>, all rights reserved. | ||||||||||
| 111 | |||||||||||
| 112 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 113 | under the same terms as Perl itself. | ||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | |||||||||||
| 118 | 1 | my $added_headers; | |||||||||
| 119 | |||||||||||
| 120 | 0 | ▶ | sub register { | ||||||||
|
Subroutine
register: not called CC=1 SCAR=6.0
Pod
undocumented
| |||||||||||
| 121 | 0 | my ($class, $svc) = @_; | |||||||||
| 122 | 2 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 123 | 0 | ▶ | $svc->register_hook('Addheader','modify_response_headers', sub { | ||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||
| 124 | |||||||||||
| 125 | 0 | my Perlbal::HTTPHeaders $res = $_[0]->{res_headers}; | |||||||||
| 126 | 0 | my $service_name = $_[0]->{service}{'name'}; | |||||||||
| 127 | 0 | ▶ | if (defined $added_headers->{$service_name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | foreach my $header (@{$added_headers->{$service_name}}) { | |||||||||
| 129 | 0 | my $header_content= $header->{'header_content'}; | |||||||||
| 130 | 0 | ▶ | if ($header_content =~/^\[\%.*\%]$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | 0 | $header_content =~s/^\[\%//; | |||||||||
| 132 | 0 | $header_content =~s/\%\]$//; | |||||||||
| 133 | 0 | $header_content = eval($header_content); | |||||||||
| 134 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | print "Error on eval for header '$header->{'header_name'}'\n"; | |||||||||
| 136 | 0 | next; | |||||||||
| 137 | } | ||||||||||
| 138 | } | ||||||||||
| 139 | 0 | $res->header($header->{'header_name'}, $header_content); | |||||||||
| 140 | } | ||||||||||
| 141 | } | ||||||||||
| 142 | 0 | return 0; | |||||||||
| 143 | 0 | }); | |||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | $svc->register_hook('Addheader','backend_response_received', sub { | ||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||
| 146 | |||||||||||
| 147 | 0 | my Perlbal::HTTPHeaders $res = $_[0]->{res_headers}; | |||||||||
| 148 | 0 | my $service_name = $_[0]->{service}{'name'}; | |||||||||
| 149 | 0 | ▶ | if (defined $added_headers->{$service_name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | 0 | foreach my $header (@{$added_headers->{$service_name}}) { | |||||||||
| 151 | 0 | my $header_content= $header->{'header_content'}; | |||||||||
| 152 | 0 | ▶ | if ($header_content =~/^\[\%.*\%]$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | 0 | $header_content =~s/^\[\%//; | |||||||||
| 154 | 0 | $header_content =~s/\%\]$//; | |||||||||
| 155 | 0 | $header_content = eval($header_content); | |||||||||
| 156 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | print "Error on eval for header '$header->{'header_name'}'\n"; | |||||||||
| 158 | 0 | next; | |||||||||
| 159 | } | ||||||||||
| 160 | } | ||||||||||
| 161 | 0 | $res->header($header->{'header_name'}, $header_content); | |||||||||
| 162 | } | ||||||||||
| 163 | } | ||||||||||
| 164 | 0 | return 0; | |||||||||
| 165 | 0 | }); | |||||||||
| 166 | |||||||||||
| 167 | |||||||||||
| 168 | 0 | return 0; | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | 0 | ▶ | sub unregister { | ||||||||
|
Subroutine
unregister: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 172 | 0 | my ($class, $svc) = @_; | |||||||||
| 173 | 0 | $svc->unregister_hooks('Addheader'); | |||||||||
| 174 | 0 | return 1; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | |||||||||||
| 178 | 0 | ▶ | sub load { | ||||||||
|
Subroutine
load: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 179 | |||||||||||
| 180 | 0 | ▶ | Perlbal::register_global_hook('manage_command.addheader', sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 181 | 0 | my $command_regexp = qr/^addheader\s+(\w+)\s+([^\s]+)\s+(.*?)$/i; | |||||||||
| 182 | 0 | my $mc = shift->parse($command_regexp, | |||||||||
| 183 | "usage: ADDHEADER <SERVICE> <HEADER_NAME> <HEADER_CONTENT>"); | ||||||||||
| 184 | 0 | my ($service, $header_name, $header_content) = $mc->args; | |||||||||
| 185 | |||||||||||
| 186 | # Get the original line, since perlbal puts everything to lower case before parsing | ||||||||||
| 187 | 0 | my @args = ($mc->orig =~/$command_regexp/); | |||||||||
| 188 | 0 | $header_content = pop @args; | |||||||||
| 189 | |||||||||||
| 190 | 0 | push @{$added_headers->{$service}},{'header_name' => $header_name, 'header_content' => $header_content}; | |||||||||
| 191 | 0 | }); | |||||||||
| 192 | 0 | return 1; | |||||||||
| 193 | } | ||||||||||
| 194 | 0 | ▶ | sub unload { return 1; } | ||||||||
|
Subroutine
unload: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 195 | |||||||||||
| 196 | |||||||||||
| 197 | |||||||||||
| 198 | 1 | 1; # End of Perlbal::Plugin::Addheader | |||||||||