1 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
2 |
|
|
## $Id: Proxy.pm,v 1.1 2002/12/16 20:14:39 cvsjoko Exp $ |
3 |
|
|
## ------------------------------------------------------------------------ |
4 |
|
|
## $Log: Proxy.pm,v $ |
5 |
|
|
## ------------------------------------------------------------------------ |
6 |
|
|
|
7 |
|
|
|
8 |
|
|
package OEF::Component::WebProxy; |
9 |
|
|
|
10 |
|
|
use strict; |
11 |
|
|
use warnings; |
12 |
|
|
|
13 |
|
|
use Data::Dumper; |
14 |
|
|
use LWP::UserAgent; |
15 |
|
|
use HTTP::Headers; |
16 |
|
|
|
17 |
|
|
use shortcuts qw( now ); |
18 |
|
|
|
19 |
|
|
# get logger instance |
20 |
|
|
my $logger = Log::Dispatch::Config->instance; |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
sub makeInternetRequest { |
24 |
|
|
|
25 |
|
|
my $self = shift; |
26 |
|
|
my $url = shift; |
27 |
|
|
|
28 |
|
|
my $ua = LWP::UserAgent->new( |
29 |
|
|
agent => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)', |
30 |
|
|
env_proxy => 1, |
31 |
|
|
keep_alive => 1, |
32 |
|
|
timeout => 30, |
33 |
|
|
); |
34 |
|
|
|
35 |
|
|
my $taskArgs; |
36 |
|
|
|
37 |
|
|
$logger->debug( __PACKAGE__ . "->makeInternetRequest( url $url ) started" ); |
38 |
|
|
$logger->info( "Internet request sent" ); |
39 |
|
|
my $response = $ua->get($url); |
40 |
|
|
$logger->debug( __PACKAGE__ . "->makeInternetRequest( url $url ) ready" ); |
41 |
|
|
$logger->info( "Internet request ready" ); |
42 |
|
|
|
43 |
|
|
#return if ($response->code() != 200); |
44 |
|
|
return if (!$response->is_success); |
45 |
|
|
|
46 |
|
|
return $response; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
sub fetchPageFromProxy { |
51 |
|
|
my $self = shift; |
52 |
|
|
my $url = shift; |
53 |
|
|
|
54 |
|
|
# trace |
55 |
|
|
#print Dumper($self); |
56 |
|
|
#exit; |
57 |
|
|
|
58 |
|
|
my $proxyObj = $self->{app}->{import}->remote('HttpProxy'); |
59 |
|
|
my @results = $self->{app}->{import}->select($proxyObj, $proxyObj->{url} eq $url); |
60 |
|
|
my $content = $results[0]->{content} if $results[0]->{content}; |
61 |
|
|
my $oktxt = "no"; |
62 |
|
|
if ($content) { |
63 |
|
|
$oktxt = "ok"; |
64 |
|
|
} |
65 |
|
|
$logger->debug( __PACKAGE__ . "->fetchPageFromProxy ...$oktxt" ); |
66 |
|
|
return $content; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
sub cacheResponse { |
70 |
|
|
my $self = shift; |
71 |
|
|
my $url = shift; |
72 |
|
|
my $response = shift; |
73 |
|
|
return unless $response; |
74 |
|
|
my $proxyObj = HttpProxy->new( |
75 |
|
|
stamp => now(), |
76 |
|
|
url => $url, |
77 |
|
|
content => $response->content(), |
78 |
|
|
request => Dumper($response->request()), |
79 |
|
|
headers => Dumper($response->{_headers}), |
80 |
|
|
code => $response->code(), |
81 |
|
|
status => $response->status_line(), |
82 |
|
|
age => $response->current_age(), |
83 |
|
|
); |
84 |
|
|
my $oktxt = "no"; |
85 |
|
|
if ($self->{app}->{import}->insert($proxyObj)) { |
86 |
|
|
$oktxt = "ok"; |
87 |
|
|
} |
88 |
|
|
$logger->debug( __PACKAGE__ . "->savePageToProxy ...$oktxt" ); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
sub getUrl { |
92 |
|
|
my $self = shift; |
93 |
|
|
my $url = shift; |
94 |
|
|
my $force = shift; |
95 |
|
|
|
96 |
|
|
#print "force: ", $force, "\n"; |
97 |
|
|
#exit; |
98 |
|
|
|
99 |
|
|
if (!$url) { |
100 |
|
|
$logger->error( __PACKAGE__ . "->getUrl: no url given" ); |
101 |
|
|
return; |
102 |
|
|
} |
103 |
|
|
$logger->debug( __PACKAGE__ . "->getUrl( url $url )" ); |
104 |
|
|
my $content; |
105 |
|
|
if ( !$force && ($content = $self->fetchPageFromProxy($url)) ) { |
106 |
|
|
#$self->cachePage($url, $content); |
107 |
|
|
#$logger->info( __PACKAGE__ . "->getUrl: Proxy hit!" ); |
108 |
|
|
} else { |
109 |
|
|
if (my $response = $self->makeInternetRequest($url) ) { |
110 |
|
|
$self->cacheResponse($url, $response); |
111 |
|
|
$content = $response->as_string(); |
112 |
|
|
} else { |
113 |
|
|
$logger->error( __PACKAGE__ . "->getUrl( url $url ) failed" ); |
114 |
|
|
} |
115 |
|
|
} |
116 |
|
|
return $content; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
1; |