Mediator.pm

filter: Statementstmt 39.5% 17 / 43 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod 100% 3 / 3 totaltotal 34.7% 25 / 72 CC 11 SCAR 31.5
CC
11
Cov
28%
new
42.0
translate
30.0
new_translator
6.0
CRAP 23.3
« MitM.pm ↑ Summary
1 package Net::Mediator;
2  
3 =head1 NAME
4  
5 Package::Package - Name-of-package - short description of package
6  
7 =head1 VERSION
8  
9 Version 0.01
10  
11 =cut
12  
13 1 our $VERSION = '0.01';
14  
15 =head1 SYNOPSIS
16  
17 Package::Package
18  
19 long
20  
21 description
22  
23 of package
24  
25 =head3 Usage
26  
27 For example
28  
29 use Package::Package;
30 my $Package = Package::Package->new();
31 $package->do_something();
32  
33 Is about the least code needed to do something useful
34  
35 =head3 Usage
36  
37 For example:
38  
39 use Package::Package;
40 my $Package = Package::Package->new();
41 $package->do_something();
42 $package->do_something_else();
43 $package->do_something_more();
44 $package->do_something_other();
45  
46 Introduces all the main options
47  
48 =head1 SUBROUTINES/METHODS
49  
50 =cut
51  
52 # #######
53 # Globals
54 # #######
55  
56 1 ▶use 5.002;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 #use Socket;
59 #use FileHandle;
60 #use IO::Handle;
61 1 ▶use Net::MitM;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 @ISA=qw(Net::MitM);
64  
65 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 =head2 new( local_port_num, remote_ip_address, remote_port_num, forward_translate_callback, back_translate_callback )
68  
69 Creates a new mediator
70  
71 =head4 Parameters
72  
73 =over
74  
75 =item * local_port_num - the port number to listen on
76  
77 =item * remote_ip_address, remote_port_number - the server to connect to
78  
79 =item * forward/back translate_callback - a function to do translations
80  
81 =item * Returns - the mediator object
82  
83 =back
84  
85 =head4 Usage
86  
87 For example:
88  
89 use Net::Mediator;
90 my %forward_translations = (
91 'a=(?<a>.+?),b=(?<b>.+?),c=(?<c>.+?);' => '<a>$+{a}</a><b>$+{b}</b><c>$+{c}</c>',
92 );
93 my %back_translations = (
94 '<(.)>(.*?)</\1><(.)>(.*?)</\3><(.)>(.*?)</\5>' => '$1=$2,$3=$4,$5=$6;'
95 );
96 my $forward_translator = Net::Mediator->new_translator(\%forward_translations);
97 my $back_translator = Net::Mediator->new_translator(\%back_translations);
98 my $forward_translate = sub{$translator->translate(@_)};
99 my $back_translate = sub{$translator->translate(@_)};
100 my $mediator = Net::Mediator->new($remote_host,$remote_port,$local_port,$forward_translate,$back_translate);
101 $mediator->go();
102  
103 =cut
104  
105 0 ▶sub new(@) {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
106 0 my $class=shift;
107 0 ▶ my $local_port_num = shift or croak "local port number missing";
Branch: 1 branch
BranchTrueFalseTotal
my $local_port_num = shift() or croak("local port number missing") 0 0 0
108 0 ▶ my $remote_ip_address = shift or croak "remote hostname/ip address missing";
Branch: 1 branch
BranchTrueFalseTotal
my $remote_ip_address = shift() or croak("remote hostname/ip address missing") 0 0 0
109 0 ▶ my $remote_port_num = shift or croak "remote port number missing";
Branch: 1 branch
BranchTrueFalseTotal
my $remote_port_num = shift() or croak("remote port number missing") 0 0 0
110 0 ▶ my $forward_translator = shift or croak "forward translator missing";
Branch: 1 branch
BranchTrueFalseTotal
my $forward_translator = shift() or croak("forward translator missing") 0 0 0
111 0 ▶ my $back_translator = shift or croak "back translator missing";
Branch: 1 branch
BranchTrueFalseTotal
my $back_translator = shift() or croak("back translator missing") 0 0 0
112 0 my $mitm=Net::MitM->new($remote_ip_address, $remote_port_num, $local_port_num);
113 0 $mitm->client_to_server_callback($forward_translator,callback_behaviour=>'modify');
114 0 $mitm->server_to_client_callback($back_translator,callback_behaviour=>'modify');
115 0 $mitm->name("Mediator");
116 0 return $mitm; # The mediator is an instance of a MitM, not an instance of some child class
117 }
118  
119  
120 =head2 new_translator( translations )
121  
122 Creates a new translator for the given translations.
123  
124  
125 It should not normally be necessary to call these methods directly.
126  
127 =head4 Parameters
128  
129 =over
130  
131 =item * translations - a hash table of from/to translations. Each 'from' should be a string which will be used as a regular expression. Each 'to' should be a string, which may rely on the regular expression having matched.
132  
133 =item * Returns - a new translator. The translator only has one method, translate().
134  
135 =back
136  
137 =head4 Usage
138  
139 For example:
140  
141 my %translations = { 'hello' => 'hi there', '(?<key>.+?)=(?<value>.+?)' => '<$+{key}>$+{value}</$+key>' };
142 my $translator = Net::Mediator->new_translator(\%translations);
143 say $translator->translate('hello'); # prints "hello"
144 say $translator->translate('a=b'); # prints "<a>b</a>"
145  
146 Translations are tested in effectively random order, and translation stops after the first match. If there are no matches, undef is returned.
147  
148 =cut
149  
150 #TODO put this in its own package
151  
152 sub new_translator(\%)
153 0 ▶{
Subroutine new_translator: not called CC=2 SCAR=17.9
Pod documented
154 0 my $class=shift;
155 0 my $translations=shift; # TODO should check that translations is a hash table
156 0 my %this;
157 0 ▶ while(my ($from,$to) = each %{$translations}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$translations;}) 0 0 0
158 0 $this{qr{$from}ms} = $to;
159 }
160 0 return bless(\%this, $class);
161 }
162  
163 sub translate($)
164 0 ▶{
Subroutine translate: not called CC=5 SCAR=34.0
Pod documented
165 0 ▶ my $this=shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $this = shift() or die 0 0 0
166 0 ▶ my $in=shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $in = shift() or die 0 0 0
167 #while(my ($from,$to) = each %{$this}) {
168 0 foreach my $from (keys %{$this}) {
169 0 ▶ if($in =~ $from){
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /$from/) 0 0 0
170 0 my $to=$this->{$from};
171 0 return eval qq{qq{$to}}; # expand $to into a string
172 }
173 }
174 0 return undef;
175 }
176  
177 =head1 Exports
178  
179 Package does not export any functions or variables.
180  
181 =head1 AUTHOR
182  
183 Ben AVELING, C<< <ben dot aveling at optusnet dot com dot au> >>
184  
185 =head1 BUGS
186  
187 Please report any bugs or feature requests to C<bug-Net-MitM at rt.cpan.org>, or through
188 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-MitM>. I will be notified, and then you'll
189 automatically be notified of progress on your bug as I make changes.
190  
191 =head1 SUPPORT
192  
193 You can find documentation for this module with the perldoc command.
194  
195 perldoc Package::Package
196  
197 You can also look for information at:
198  
199 =over
200  
201 =item * RT: CPAN's request tracker (report bugs here)
202  
203 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-MitM>
204  
205 =item * AnnoCPAN: Annotated CPAN documentation
206  
207 L<http://annocpan.org/dist/Net-MitM>
208  
209 =item * CPAN Ratings
210  
211 L<http://cpanratings.perl.org/d/Net-MitM>
212  
213 =item * Search CPAN
214  
215 L<http://search.cpan.org/dist/Net-MitM/>
216  
217 =back
218  
219 =head1 ACKNOWLEDGEMENTS
220  
221 I'd like to acknowledge someone I haven't acknowledge in any other package I've yet written.
222  
223 =head1 ALTERNATIVES
224  
225 Another Package, if any.
226  
227 =head1 LICENSE AND COPYRIGHT
228  
229 Copyleft 2013 Ben AVELING.
230  
231 This program is free software; you can redistribute it and/or modify it
232 under the terms of the the Artistic License (2.0). You may obtain a
233 copy of the full license at:
234  
235 L<http://www.perlfoundation.org/artistic_license_2_0>
236  
237 Any use, modification, and distribution of the Standard or Modified
238 Versions is governed by this Artistic License. By using, modifying or
239 distributing the Package, you accept this license. Do not use, modify,
240 or distribute the Package, if you do not accept this license.
241  
242 If your Modified Version has been derived from a Modified Version made
243 by someone other than you, you are nevertheless required to ensure that
244 your Modified Version complies with the requirements of this license.
245  
246 This license does not grant you the right to use any trademark, service
247 mark, tradename, or logo of the Copyright Holder.
248  
249 This license includes the non-exclusive, worldwide, free-of-charge
250 patent license to make, have made, have, hold and cherish,
251 use, offer to use, sell, offer to sell, import and
252 otherwise transfer the Package with respect to any patent claims
253 licensable by the Copyright Holder that are necessarily infringed by the
254 Package. If you institute patent litigation (including a cross-claim or
255 counterclaim) against any party alleging that the Package constitutes
256 direct or contributory patent infringement, then this Artistic License
257 to you shall terminate on the date that such litigation is filed.
258  
259 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
260 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
261 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
262 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
263 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
264 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
265 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
266 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. SO THERE.
267  
268 =cut
269  
270 1 1; # End of package Net::MitM::Mediator;
« MitM.pm ↑ Summary