blib/lib/IO/Stream/Proxy/HTTPS.pm

filter: Statementstmt 38.2% 26 / 68 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 8 / 12 Podpod 33.3% 1 / 3 totaltotal 30.1% 35 / 116 CC 14 SCAR 34.3
CC
14
Cov
27%
EVENT
72.0
new
30.0
PREPARE
12.0
CRAP 30.9
↑ Summary
1 package IO::Stream::Proxy::HTTPS;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use version; our $VERSION = qv('1.0.5'); # update POD & Changes & README
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # update DEPENDENCIES in POD & Makefile.PL & README
10 4 ▶use IO::Stream::const;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use constant HTTP_OK => 200;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub new {
17 0 ▶ my ($class, $opt) = @_;
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
18 croak '{host}+{port} required'
19 if !defined $opt->{host}
20 || !defined $opt->{port}
21 0 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"host"} and defined $opt->{"port"} 0 0 0
Condition0%0/3
defined $opt->{"host"} and defined $opt->{"port"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt->{"host"} and defined $opt->{"port"}
defined $opt->{"host"} defined $opt->{"port"}
Truth table0%0/3
A and B
A= defined $opt->{"host"}B= defined $opt->{"port"}
ABresult
0X0
100
111
22 croak '{user}+{pass} required'
23 0 ▶ if $opt->{user} xor $opt->{pass};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"user"} xor $opt->{"pass"} 0 0 0
Condition0%0/4
$opt->{"user"} xor $opt->{"pass"}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$opt->{"user"} xor $opt->{"pass"}
$opt->{"user"} $opt->{"pass"}
Truth table0%0/4
A xor B
A= $opt->{"user"}B= $opt->{"pass"}
ABresult
110
101
011
000
24 my $self = bless {
25 host => undef,
26 port => undef,
27 user => undef,
28 pass => undef,
29 0 %{$opt},
30 out_buf => q{}, # modified on: OUT
31 out_pos => undef, # modified on: OUT
32 out_bytes => 0, # modified on: OUT
33 in_buf => q{}, # modified on: IN
34 in_bytes => 0, # modified on: IN
35 ip => undef, # modified on: RESOLVED
36 is_eof => undef, # modified on: EOF
37 _want_write => undef,
38 }, $class;
39 0 return $self;
40 }
41  
42 sub PREPARE {
43 0 ▶ my ($self, $fh, $host, $port) = @_;
Subroutine PREPARE: not called CC=3 SCAR=24.8
Pod undocumented
44 0 ▶ croak '{fh} already connected'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 0 0
45 if !defined $host;
46 0 $self->{out_buf} = "CONNECT ${host}:${port} HTTP/1.0\r\n";
47 0 ▶ if (defined $self->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"user"}) 0 0 0
48 $self->{out_buf} .= 'Proxy-Authorization: Basic '
49 0 . encode_base64($self->{user}.q{:}.$self->{pass}, q{})
50 . "\r\n"
51 ;
52 }
53 0 $self->{out_buf} .= "\r\n";
54 0 $self->{_slave}->PREPARE($fh, $self->{host}, $self->{port});
55 0 $self->{_slave}->WRITE();
56 0 return;
57 }
58  
59 sub WRITE {
60 0 ▶ my ($self) = @_;
Subroutine WRITE: not called CC=1 SCAR=6.9
61 0 $self->{_want_write} = 1;
62 0 return;
63 }
64  
65 sub EVENT {
66 0 ▶ my ($self, $e, $err) = @_;
Subroutine EVENT: not called CC=8 SCAR=42.8
Pod undocumented
67 0 my $m = $self->{_master};
68 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
69 0 $m->EVENT(0, $err);
70 }
71 0 ▶ if ($e & IN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e & 4) 0 0 0
72 0 ▶ if ($self->{in_buf} =~ s{\A(HTTP/\d[.]\d\s(\d+)\s.*?)\r?\n\r?\n}{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_buf"} =~ s[\A(HTTP/\d[.]\d\s(\d+)\s.*?)\r?\n\r?\n][]msx) 0 0 0
73 0 my ($reply, $status) = ($1, $2);
74 0 ▶ if ($status == HTTP_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 200) { } 0 0 0
75 0 $e = CONNECTED;
76 0 ▶ if (my $l = length $self->{in_buf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = length $self->{"in_buf"}) 0 0 0
77 0 $e |= IN;
78 0 $m->{in_buf} .= $self->{in_buf};
79 0 $m->{in_bytes} += $l;
80 }
81 0 $m->EVENT($e);
82 0 $self->{_slave}->{_master} = $m;
83 0 weaken($self->{_slave}->{_master});
84 0 $m->{_slave} = $self->{_slave};
85 0 ▶ if ($self->{_want_write}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_want_write"}) 0 0 0
86 0 $self->{_slave}->WRITE();
87 }
88 }
89 else {
90 0 $m->EVENT(0, 'https proxy: '.$reply);
91 }
92 }
93 }
94 0 ▶ if ($e & EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e & 16) 0 0 0
95 0 $m->{is_eof} = $self->{is_eof};
96 0 $m->EVENT(0, 'https proxy: unexpected EOF');
97 }
98 0 return;
99 }
100  
101  
102 4 1; # Magic true value required at end of module
103 __END__
104  
105 =head1 NAME
106  
107 IO::Stream::Proxy::HTTPS - HTTPS proxy plugin for IO::Stream
108  
109  
110 =head1 VERSION
111  
112 This document describes IO::Stream::Proxy::HTTPS version 1.0.5
113  
114  
115 =head1 SYNOPSIS
116  
117 use IO::Stream;
118 use IO::Stream::Proxy::HTTPS;
119  
120 IO::Stream->new({
121 ...
122 plugin => [
123 ...
124 proxy => IO::Stream::Proxy::HTTPS->new({
125 host => 'my.proxy.com',
126 port => 3128,
127 user => 'me',
128 pass => 'mypass',
129 }),
130 ...
131 ],
132 });
133  
134  
135 =head1 DESCRIPTION
136  
137 This module is plugin for L<IO::Stream> which allow you to route stream
138 through HTTPS (also called CONNECT) proxy.
139  
140 You may use several IO::Stream::Proxy::HTTPS plugins for single IO::Stream
141 object, effectively creating proxy chain (first proxy plugin will define
142 last proxy in a chain).
143  
144 =head2 EVENTS
145  
146 When using this plugin event RESOLVED will never be delivered to user because
147 target {host} which user provide to IO::Stream will never be resolved on
148 user side (it will be resolved by HTTPS proxy).
149  
150 Event CONNECTED will be generated after HTTPS proxy successfully connects to
151 target {host} (and not when socket will connect to HTTPS proxy itself).
152  
153 =head1 INTERFACE
154  
155 =over
156  
157 =item new({ host=>$host, port=>$port })
158  
159 =item new({ host=>$host, port=>$port, user=>$user, pass=>$pass })
160  
161 Connect to proxy $host:$port, optionally using basic authorization.
162  
163 =back
164  
165  
166 =head1 DIAGNOSTICS
167  
168 =over
169  
170 =item C<< {host}+{port} required >>
171  
172 You must provide both {host} and {port} to IO::Stream::Proxy::HTTPS->new().
173  
174 =item C<< {user}+{pass} required >>
175  
176 You have provided either {user} or {pass} to IO::Stream::Proxy::HTTPS->new()
177 while you have to provide either both or none of them.
178  
179 =item C<< {fh} already connected >>
180  
181 You have provided {fh} to IO::Stream->new(), but this is not supported by
182 this plugin. Either don't use this plugin or provide {host}+{port} to
183 IO::Stream->new() instead.
184  
185 =back
186  
187  
188 =head1 CONFIGURATION AND ENVIRONMENT
189  
190 IO::Stream::Proxy::HTTPS requires no configuration files or environment variables.
191  
192  
193 =head1 DEPENDENCIES
194  
195 L<IO::Stream>.
196  
197  
198 =head1 INCOMPATIBILITIES
199  
200 None reported.
201  
202  
203 =head1 BUGS AND LIMITATIONS
204  
205 No bugs have been reported.
206  
207 Please report any bugs or feature requests to author, or
208 C<bug-io-stream-proxy-https@rt.cpan.org>, or through the web interface at
209 L<http://rt.cpan.org>.
210  
211  
212 =head1 AUTHOR
213  
214 Alex Efros C<< <powerman-asdf@ya.ru> >>
215  
216  
217 =head1 LICENSE AND COPYRIGHT
218  
219 Copyright (c) 2008, Alex Efros C<< <powerman-asdf@ya.ru> >>. All rights reserved.
220  
221 This module is free software; you can redistribute it and/or
222 modify it under the same terms as Perl itself. See L<perlartistic>.
223  
224  
225 =head1 DISCLAIMER OF WARRANTY
226  
227 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
228 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
229 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
230 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
231 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
232 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
233 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
234 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
235 NECESSARY SERVICING, REPAIR, OR CORRECTION.
236  
237 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
238 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
239 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
240 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
241 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
242 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
243 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
244 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
245 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
246 SUCH DAMAGES.
↑ Summary