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