| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # proxy::http Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Proxy::Http; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | port => [ qw(integer) ], | ||
| 20 | truncate_request => [ qw(integer) ], | ||
| 21 | truncate_response => [ qw(integer) ], | ||
| 22 | }, | ||
| 23 | attributes_default => { | ||
| 24 | port => 3128, | ||
| 25 | truncate_response => 512, | ||
| 26 | }, | ||
| 27 | commands => { | ||
| 28 | requests => [ ], | ||
| 29 | requests_responses => [ ], | ||
| 30 | }, | ||
| 31 | require_modules => { | ||
| 32 | 'HTTP::Proxy' => [ ], | ||
| 33 | 'HTTP::Proxy::HeaderFilter::simple' => [ ], | ||
| 34 | 'LWP::Protocol::connect' => [ ], | ||
| 35 | }, | ||
| 36 | }; | ||
| 37 | } | ||
| 38 | |||
| 39 | # | ||
| 40 | # XXX: see http://cpansearch.perl.org/src/MIKEM/Net-SSLeay-1.65/examples/https-proxy-snif.pl | ||
| 41 | # XXX: for HTTPS mitm | ||
| 42 | # | ||
| 43 | sub requests { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | my $proxy = HTTP::Proxy->new( | ||
| 47 | port => $self->port, | ||
| 48 | ); | ||
| 49 | |||
| 50 | $proxy->push_filter( | ||
| 51 | request => HTTP::Proxy::HeaderFilter::simple->new( | ||
| 52 | sub { | ||
| 53 | my ($self, $headers, $request) = @_; | ||
| 54 | my $string = $request->as_string; | ||
| 55 | if ($self->truncate_request) { | ||
| 56 | print substr($string, 0, $self->truncate_request); | ||
| 57 | print "\n[..]\n"; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | print $string; | ||
| 61 | } | ||
| 62 | }, | ||
| 63 | ), | ||
| 64 | ); | ||
| 65 | |||
| 66 | print "Listening on port: ".$self->port."\n"; | ||
| 67 | print "Ready to process browser requests, blocking state...\n"; | ||
| 68 | |||
| 69 | return $proxy->start; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub requests_responses { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | my $proxy = HTTP::Proxy->new( | ||
| 76 | port => $self->port, | ||
| 77 | ); | ||
| 78 | |||
| 79 | $proxy->push_filter( | ||
| 80 | request => HTTP::Proxy::HeaderFilter::simple->new( | ||
| 81 | sub { | ||
| 82 | my ($proxy, $headers, $request) = @_; | ||
| 83 | my $string = $request->as_string; | ||
| 84 | if ($self->truncate_request) { | ||
| 85 | print substr($string, 0, $self->truncate_request); | ||
| 86 | print "\n[..]\n"; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | print $string; | ||
| 90 | } | ||
| 91 | }, | ||
| 92 | ), | ||
| 93 | response => HTTP::Proxy::HeaderFilter::simple->new( | ||
| 94 | sub { | ||
| 95 | my ($proxy, $headers, $response) = @_; | ||
| 96 | my $string = $response->as_string; | ||
| 97 | if ($self->truncate_response) { | ||
| 98 | print substr($string, 0, $self->truncate_response); | ||
| 99 | print "\n[..]\n"; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | print $string; | ||
| 103 | } | ||
| 104 | }, | ||
| 105 | ), | ||
| 106 | ); | ||
| 107 | |||
| 108 | print "Listening on port: ".$self->port."\n"; | ||
| 109 | print "Ready to process browser requests, blocking state...\n"; | ||
| 110 | |||
| 111 | return $proxy->start; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | __END__ | ||