Search/Inet.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 206 CC 10 SCAR 21.4
CC
10
Cov
0%
get_http
30.0
_open_socket_timeout
12.0
get_page
6.0
CRAP 8.5
1 package MMM::Text::Search::Inet;
2 #$Id: Inet.pm,v 1.9 1999/11/24 18:46:27 maxim Exp $
3  
4 package HTTPRequest;
5 use strict;
6 use IO::Socket::INET;
7 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $verbose_flag );
8 require Exporter;
9 @ISA = qw(Exporter );
10 @EXPORT = qw( GetHTTP );
11 $VERSION = '1.0';
12  
13 $verbose_flag = 1;
14 my $ERROR = undef;
15 my $AGENT = "Mozzarilla/1.0 [it] (CPM/80 1.0 Z81)";
16 sub DEBUG (@) { $verbose_flag && print STDERR @_, "\n" };
17  
18 sub new {
19 my ($pak,$opt) =@_;
20 my $req = {
21 Status => 0,
22 Content => '',
23 Header => { },
24 AutoRedirect => $opt->{AutoRedirect}
25 };
26 bless $req;
27 return $req;
28 }
29  
30  
31 sub reset { $_[0]->{_URL} = $_[0]->{URL} = $_[0]->{Status} = undef };
32  
33 sub get_page {
34 my ($self, $url ) = @_;
35 my $rc = $self->get_http($url);
36 return $rc unless $self->{AutoRedirect};
37 while ($self->{Status} == 301 || $self->{Status} == 302) {
38 $url = $self->{Header}->{location};
39 DEBUG("Redirected to $url...");
40 $rc = $self->get_http($url);
41 }
42 return $rc;
43 }
44  
45 sub set_url {
46 my ($self, $url) = @_;
47 $url =~ m|(\w+)://([^/]+)(:(\d+))?(.*)|;
48 my ($proto, $host,$port, $path) = ($1,$2,$4,$5);
49 $self->{_URL} ||= $url;
50 $path =~ s|[^/]+/\.\.||g;
51 $path =~ s|/\.||g;
52 $path =~ s|/+|/|g;
53 $path =~ s:^([^/]|$):/$1:;
54 $port ||= 80;
55 $url = "http://".$host.($port!=80?":$port":"").$path;
56 #DEBUG("set_url(): $url");
57 $self->{URL} = $url;
58 $self->{URL} =~ m|(.*)/|;
59 $self->{BaseURL} = $1;
60 $self->{Host} = $host;
61 $self->{Path} = $path,
62 $self->{Port} = $port
63 }
64  
65  
66  
67 sub get_http {
68 my ($self, $url ) = @_;
69 $self->{Status} = 0;
70 if ($url) {
71 $self->set_url($url);
72 }
73 my ($host,$port, $path) = @{$self}{qw/Host Port Path/} ;
74 DEBUG("Retrieving http://$host:$port$path...");
75 my $s = _open_socket_timeout( 20,
76 PeerAddr => $host,
77 PeerPort => $port, Proto => 'tcp' );
78 return undef unless $s;
79 print $s "GET $path HTTP/1.0\r\n";
80 print $s "Host: $host\r\n";
81 print $s "User-Agent: $AGENT\r\n";
82 print $s "\r\n";
83 my $line = _read_from_socket($s,"\n", 60);
84 return undef unless $line;
85 DEBUG($line);
86 my ($proto, $status, $msg) = split ' ', $line, 3;
87 $self->{Status} = int $status;
88 my $header = _read_from_socket($s,"\r\n\r\n",60);
89 return undef unless $header;
90 my %header;
91 for ( split /\r*\n/, $header) {
92 my ($k,$v) = split ":\s*", $_,2;
93 $header{lc $k} = $v;
94 }
95 $self->{Header} = \%header;
96 my $text;
97 while ( $line = _read_from_socket($s,"\n",60) ) {
98 $text .= $line;
99 }
100 $s->close();
101 $self->{Content} = $text;
102 return 1;
103 }
104  
105 sub header { $_[0]->{Header} };
106 sub content { $_[0]->{Content} };
107 sub content_ref { \$_[0]->{Content} };
108 sub status { $_[0]->{Status} };
109 sub url { $_[0]->{URL} };
110 sub base_url { $_[0]->{BaseURL} };
111 sub host { $_[0]->{Host} };
112 sub port { $_[0]->{Port} };
113 sub path { $_[0]->{Path} };
114
115
116
117  
118  
119 sub _read_from_socket {
120 undef $@;
121 my ($socket, $separator, $timeout) = @_;
122 my $line;
123 my $r = eval {
124 $SIG{ALRM} = sub { die "read TIMEOUT\n" };
125 local $/ = $separator;
126 alarm $timeout;
127 my $content = scalar <$socket>;
128 alarm 0;
129 return $content;
130 };
131 if ( $@ =~ /read TIMEOUT/ ) {
132 $ERROR = 'read TIMEOUT';
133 return undef;
134 }
135 return $r;
136 }
137  
138  
139 sub _open_socket_timeout {
140 # wrapper per IO:Socket::INET con gestione di timeout ed errori vari
141 my $timeout = shift;
142 my $s;
143 my $error;
144 DEBUG( "_open_socket_timeout() ", join(',', @_));
145 undef $!; undef $@;
146 $@ = eval {
147 $SIG{ALRM} = sub { die "connection TIMED OUT" };
148 alarm $timeout;
149 $s = IO::Socket::INET->new( @_ );
150 alarm 0;
151 $@ =~ s/IO::Socket::INET:\s+//;
152 return $@;
153 };
154
155 undef $ERROR;
156 if ($@ =~ /connection TIMED OUT/ ) {
157 $ERROR = 'connection TIMED OUT' ;
158 undef $s;
159 }
160 $ERROR ||= $@||$! unless $s;
161 DEBUG("\$s=$s -- \$!='$!' -- \$\@='$@' -- \$ERROR=$ERROR");
162 return $s;
163 }
164  
165 __END__