File Coverage

blib/lib/HTTP/Proxy/HeaderFilter/standard.pm
Criterion Covered Total %
statement 40 49 81.6
branch 11 20 55.0
condition 12 14 85.7
subroutine 7 7 100.0
pod 1 1 100.0
total 71 91 78.0


line stmt bran cond sub pod time code
1             package HTTP::Proxy::HeaderFilter::standard;
2             $HTTP::Proxy::HeaderFilter::standard::VERSION = '0.304';
3 69     69   320 use strict;
  69         97  
  69         2418  
4 69     69   330 use HTTP::Proxy;
  69         89  
  69         1767  
5 69     69   34621 use HTTP::Headers::Util qw( split_header_words );
  69         56929  
  69         5454  
6 69     69   29946 use HTTP::Proxy::HeaderFilter;
  69         139  
  69         1940  
7 69     69   329 use vars qw( @ISA );
  69         82  
  69         37389  
8             @ISA = qw( HTTP::Proxy::HeaderFilter );
9              
10             # known hop-by-hop headers
11             my @hopbyhop =
12             qw( Connection Keep-Alive Proxy-Authenticate Proxy-Authorization
13             TE Trailers Transfer-Encoding Upgrade Proxy-Connection Public );
14              
15             # standard proxy header filter (RFC 2616)
16             sub filter {
17 152     152 1 10068 my ( $self, $headers, $message ) = @_;
18              
19             # the Via: header
20 152   100     643 my $via = $message->protocol() || '';
21 152 100 100     2590 if ( $self->proxy->via and $via =~ s!HTTP/!! ) {
22 143         540 $via .= " " . $self->proxy->via;
23 143   66     617 $headers->header(
24             Via => join ', ',
25             $message->headers->header('Via') || (), $via
26             );
27             }
28              
29             # the X-Forwarded-For header
30             $headers->push_header(
31 152 100 100     13853 X_Forwarded_For => $self->proxy->client_socket->peerhost )
32             if $message->isa( 'HTTP::Request' ) && $self->proxy->x_forwarded_for;
33              
34             # make a list of hop-by-hop headers
35 152         6968 my %h2h = map { (lc) => 1 } @hopbyhop;
  1520         5164  
36 152         1075 my $hop = HTTP::Headers->new();
37 152         1945 my $client = HTTP::Headers->new();
38 80         2712 $h2h{ lc $_->[0] } = 1
39 152         1267 for map { split_header_words($_) } $headers->header('Connection');
40              
41             # hop-by-hop headers are set aside
42             # as well as LWP::UserAgent Client-* headers
43             $headers->scan(
44             sub {
45 1251     1251   19756 my ( $k, $v ) = @_;
46 1251 100       3060 if ( $h2h{lc $k} ) {
47 166         526 $hop->push_header( $k => $v );
48 166         3746 $headers->remove_header($k);
49             }
50 1251 100       6296 if( $k =~ /^Client-/ ) {
51 143         535 $client->push_header( $k => $v );
52 143         5214 $headers->remove_header($k);
53             }
54             }
55 152         8760 );
56              
57             # set the hop-by-hop and client headers in the proxy
58             # only the end-to-end headers are left in the message
59 152         2818 $self->proxy->hop_headers($hop);
60 152         532 $self->proxy->client_headers($client);
61              
62             # handle Max-Forwards
63 152 50 66     1542 if ( $message->isa('HTTP::Request')
64             and defined $headers->header('Max-Forwards') ) {
65 0         0 my ( $max, $method ) =
66             ( $headers->header('Max-Forwards'), $message->method );
67 0 0       0 if ( $max == 0 ) {
    0          
68             # answer directly TRACE ou OPTIONS
69 0 0       0 if ( $method eq 'TRACE' ) {
    0          
70 0         0 my $response =
71             HTTP::Response->new( 200, 'OK',
72             HTTP::Headers->new( Content_Type => 'message/http'
73             , Content_Length => 0),
74             $message->as_string );
75 0         0 $self->proxy->response($response);
76             }
77             elsif ( $method eq 'OPTIONS' ) {
78 0         0 my $response = HTTP::Response->new(200);
79 0         0 $response->header( Allow => join ', ', @HTTP::Proxy::METHODS );
80 0         0 $self->proxy->response($response);
81             }
82             }
83             # The Max-Forwards header field MAY be ignored for all
84             # other methods defined by this specification (RFC 2616)
85             elsif ( $method =~ /^(?:TRACE|OPTIONS)/ ) {
86 0         0 $headers->header( 'Max-Forwards' => --$max );
87             }
88             }
89              
90             # no encoding accepted (gzip, compress, deflate)
91             # if we plan to do anything with the response body
92 152         521 $headers->remove_header( 'Accept-Encoding' )
93 152 100       2669 if @{ $self->proxy->{body}{response}{filters} };
94             }
95              
96             1;
97              
98             __END__