| blib/lib/Perlbal/Plugin/Addheader.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 9 | 56 | 16.0 |
| branch | 0 | 12 | 0.0 |
| condition | n/a | ||
| subroutine | 3 | 10 | 30.0 |
| pod | 0 | 4 | 0.0 |
| total | 12 | 82 | 14.6 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Perlbal::Plugin::Addheader; | ||||||
| 2 | |||||||
| 3 | 1 | 1 | 411778 | use warnings; | |||
| 1 | 4 | ||||||
| 1 | 6769 | ||||||
| 4 | 1 | 1 | 32 | use strict; | |||
| 1 | 3 | ||||||
| 1 | 508 | ||||||
| 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 | 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 |
||||||
| 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 |
||||||
| 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 |
||||||
| 52 | |||||||
| 53 | |||||||
| 54 | |||||||
| 55 | |||||||
| 56 | |||||||
| 57 | =head1 AUTHOR | ||||||
| 58 | |||||||
| 59 | Bruno Martins, C<< |
||||||
| 60 | |||||||
| 61 | =head1 BUGS | ||||||
| 62 | |||||||
| 63 | Please report any bugs or feature requests to C |
||||||
| 64 | the web interface at L |
||||||
| 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 |
||||||
| 84 | |||||||
| 85 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||
| 86 | |||||||
| 87 | L |
||||||
| 88 | |||||||
| 89 | =item * CPAN Ratings | ||||||
| 90 | |||||||
| 91 | L |
||||||
| 92 | |||||||
| 93 | =item * Search CPAN | ||||||
| 94 | |||||||
| 95 | L |
||||||
| 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<< |
||||||
| 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 | my $added_headers; | ||||||
| 119 | |||||||
| 120 | sub register { | ||||||
| 121 | 0 | 0 | 0 | my ($class, $svc) = @_; | |||
| 122 | 1 | 1 | 8439 | use Data::Dumper; | |||
| 1 | 21070 | ||||||
| 1 | 598 | ||||||
| 123 | $svc->register_hook('Addheader','modify_response_headers', sub { | ||||||
| 124 | |||||||
| 125 | 0 | 0 | my Perlbal::HTTPHeaders $res = $_[0]->{res_headers}; | ||||
| 126 | 0 | my $service_name = $_[0]->{service}{'name'}; | |||||
| 127 | 0 | 0 | if (defined $added_headers->{$service_name}) { | ||||
| 128 | 0 | foreach my $header (@{$added_headers->{$service_name}}) { | |||||
| 0 | |||||||
| 129 | 0 | my $header_content= $header->{'header_content'}; | |||||
| 130 | 0 | 0 | if ($header_content =~/^\[\%.*\%]$/) { | ||||
| 131 | 0 | $header_content =~s/^\[\%//; | |||||
| 132 | 0 | $header_content =~s/\%\]$//; | |||||
| 133 | 0 | $header_content = eval($header_content); | |||||
| 134 | 0 | 0 | if ($@) { | ||||
| 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 | $svc->register_hook('Addheader','backend_response_received', sub { | ||||||
| 146 | |||||||
| 147 | 0 | 0 | my Perlbal::HTTPHeaders $res = $_[0]->{res_headers}; | ||||
| 148 | 0 | my $service_name = $_[0]->{service}{'name'}; | |||||
| 149 | 0 | 0 | if (defined $added_headers->{$service_name}) { | ||||
| 150 | 0 | foreach my $header (@{$added_headers->{$service_name}}) { | |||||
| 0 | |||||||
| 151 | 0 | my $header_content= $header->{'header_content'}; | |||||
| 152 | 0 | 0 | if ($header_content =~/^\[\%.*\%]$/) { | ||||
| 153 | 0 | $header_content =~s/^\[\%//; | |||||
| 154 | 0 | $header_content =~s/\%\]$//; | |||||
| 155 | 0 | $header_content = eval($header_content); | |||||
| 156 | 0 | 0 | if ($@) { | ||||
| 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 | sub unregister { | ||||||
| 172 | 0 | 0 | 0 | my ($class, $svc) = @_; | |||
| 173 | 0 | $svc->unregister_hooks('Addheader'); | |||||
| 174 | 0 | return 1; | |||||
| 175 | } | ||||||
| 176 | |||||||
| 177 | |||||||
| 178 | sub load { | ||||||
| 179 | |||||||
| 180 | Perlbal::register_global_hook('manage_command.addheader', sub { | ||||||
| 181 | 0 | 0 | my $command_regexp = qr/^addheader\s+(\w+)\s+([^\s]+)\s+(.*?)$/i; | ||||
| 182 | 0 | my $mc = shift->parse($command_regexp, | |||||
| 183 | "usage: ADDHEADER |
||||||
| 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}; | |||||
| 0 | |||||||
| 191 | 0 | 0 | 0 | }); | |||
| 192 | 0 | return 1; | |||||
| 193 | } | ||||||
| 194 | 0 | 0 | 0 | sub unload { return 1; } | |||
| 195 | |||||||
| 196 | |||||||
| 197 | |||||||
| 198 | 1; # End of Perlbal::Plugin::Addheader |