File Coverage

blib/lib/AnyEvent/Connector/Proxy/http.pm
Criterion Covered Total %
statement 45 53 84.9
branch 5 6 83.3
condition n/a
subroutine 11 12 91.6
pod 0 5 0.0
total 61 76 80.2


line stmt bran cond sub pod time code
1             package AnyEvent::Connector::Proxy::http;
2 4     4   29 use strict;
  4         7  
  4         167  
3 4     4   21 use warnings;
  4         6  
  4         248  
4 4     4   2547 use AnyEvent::Handle;
  4         30192  
  4         2526  
5              
6             sub new {
7 19     19 0 100 my ($class, $uri) = @_;
8 19         246 my $self = bless {
9             uri => $uri
10             }, $class;
11 19         98 return $self;
12             }
13              
14             sub uri_string {
15 37     37 0 84 my ($self) = @_;
16 37         164 return $self->{uri}->as_string;
17             }
18              
19             sub host {
20 4     4 0 10 my ($self) = @_;
21 4         30 return $self->{uri}->host;
22             }
23              
24             sub port {
25 4     4 0 289 my ($self) = @_;
26 4         20 return $self->{uri}->port;
27             }
28              
29             sub establish_proxy {
30 3     3 0 12 my ($self, $fh, $target_host, $target_port, $cb) = @_;
31 3         6 my $ah;
32             my $header_reader;
33             $ah = AnyEvent::Handle->new(
34             fh => $fh,
35             on_error => sub {
36             ## TODO: how should we report the error detail?
37 1     1   785 undef $ah;
38 1         9 undef $header_reader;
39 1         6 $cb->(0);
40             },
41             on_eof => sub {
42 0     0   0 undef $ah;
43 0         0 undef $header_reader;
44 0         0 $cb->(0);
45             },
46 3         92 );
47 3         421 $ah->push_write(
48             "CONNECT $target_host:$target_port HTTP/1.1\r\n" .
49             "Host: $target_host:$target_port\r\n\r\n"
50             );
51             $header_reader = sub {
52 2     2   175 my ($h, $line) = @_;
53 2 100       12 if($line eq "") {
54 1         6 $ah->destroy();
55 1         39 undef $ah;
56 1         3 undef $header_reader;
57 1         4 $cb->(1);
58 1         299 return;
59             }
60 1         5 $ah->push_read(line => $header_reader);
61 3         411 };
62             $ah->push_read(line => sub {
63 2     2   1562 my ($h, $line) = @_;
64 2 100       47 if($line !~ qr{^HTTP/1\S* +(\d{3})}) {
65 1         7 $ah->destroy();
66 1         36 undef $ah;
67 1         6 undef $header_reader;
68 1         5 $cb->(0);
69 1         20 return;
70             }
71 1         7 my $status = $1;
72 1 50       20 if(int($status / 100) != 2) {
73 0         0 $ah->destroy();
74 0         0 undef $ah;
75 0         0 undef $header_reader;
76 0         0 $cb->(0);
77 0         0 return;
78             }
79 1         11 $ah->push_read(line => $header_reader);
80 3         29 });
81             }
82              
83              
84             1;
85              
86             __END__