| 1 | package GetWeb::Fetcher; | ||
| 2 | |||
| 3 | use MailBot::Config; | ||
| 4 | |||
| 5 | use MIME::Base64 qw(encode_base64); | ||
| 6 | |||
| 7 | use HTML::FormatText; | ||
| 8 | use HTML::Parse; | ||
| 9 | use GetWeb::Util; | ||
| 10 | |||
| 11 | use LWP::Protocol; | ||
| 12 | use URI::URL; | ||
| 13 | use HTML::LinkExtor; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | &URI::URL::implementor('file','GetWeb::File'); | ||
| 17 | |||
| 18 | my $pUserAgentCallback = sub { | ||
| 19 | my ($data,$response) = @_; | ||
| 20 | |||
| 21 | if (! $$response{"GETWEB_CHECKED_SIZE"}++) | ||
| 22 | { | ||
| 23 | my $config = MailBot::Config::current; | ||
| 24 | my $maxLength = $config -> getMaxSize; | ||
| 25 | |||
| 26 | # may create an 'X-Died' header | ||
| 27 | if ($response -> content_length >= $maxLength) | ||
| 28 | { | ||
| 29 | my $msg = "file size exceeded $maxLength bytes"; | ||
| 30 | $response -> message($msg); | ||
| 31 | $response -> code(500); | ||
| 32 | die "$msg\n"; | ||
| 33 | } | ||
| 34 | |||
| 35 | } | ||
| 36 | $response -> add_content($data); | ||
| 37 | }; | ||
| 38 | |||
| 39 | sub new | ||
| 40 | { | ||
| 41 | my $type = shift; | ||
| 42 | my $ua = shift; | ||
| 43 | |||
| 44 | my $self = {UA => $ua, | ||
| 45 | REDIRECT_LIST => []}; | ||
| 46 | bless($self,$type); | ||
| 47 | |||
| 48 | $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub base | ||
| 52 | { | ||
| 53 | my $self = shift; | ||
| 54 | my $urlString = shift; | ||
| 55 | my $cwd = shift; | ||
| 56 | |||
| 57 | $$self{URL} = $urlString; | ||
| 58 | #$$self{REQUEST} = GetWeb::Util::safeRequest($urlString, $cwd); | ||
| 59 | $$self{REQUEST} = GetWeb::Util::safeRequest($urlString, " "); | ||
| 60 | undef; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub authorizeUser | ||
| 64 | { | ||
| 65 | my ($self, $user) = @_; | ||
| 66 | $self -> {USER} = $user; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub authorizePassword | ||
| 70 | { | ||
| 71 | my ($self, $password) = @_; | ||
| 72 | $self -> {PASSWORD} = $password; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub setAuthHead | ||
| 76 | { | ||
| 77 | my ($self, $request) = @_; | ||
| 78 | |||
| 79 | my $user = $self -> {USER}; | ||
| 80 | my $password = $self -> {PASSWORD}; | ||
| 81 | |||
| 82 | (defined $user or defined $password) or return; | ||
| 83 | |||
| 84 | (defined $user and defined $password) or | ||
| 85 | die "UNAVAILABLE: must specify both username and password\n"; | ||
| 86 | |||
| 87 | # next 3 lines taken from LWP::UserAgent::request | ||
| 88 | |||
| 89 | my $uidpwd = "$user:$password"; | ||
| 90 | |||
| 91 | my $scheme = 'Basic'; | ||
| 92 | my $header = "$scheme " . encode_base64($uidpwd, ''); | ||
| 93 | $request->header('Authorization' => $header); | ||
| 94 | } | ||
| 95 | |||
| 96 | # jfj also follow text substrings | ||
| 97 | # jfj extract links more elegantly | ||
| 98 | |||
| 99 | sub follow | ||
| 100 | { | ||
| 101 | my $self = shift; | ||
| 102 | my $follow = shift; | ||
| 103 | |||
| 104 | my $paRedirect = $$self{REDIRECT_LIST}; | ||
| 105 | |||
| 106 | $follow =~ /^\d+$/ | ||
| 107 | or die "SYNTAX ERROR: $follow must be a link number"; | ||
| 108 | |||
| 109 | my $req = $$self{REQUEST}; | ||
| 110 | |||
| 111 | $self -> setAuthHead($req); | ||
| 112 | my $response = $$self{UA} -> request($req,$pUserAgentCallback); | ||
| 113 | |||
| 114 | push(@$paRedirect,"following link $follow"); | ||
| 115 | |||
| 116 | if (! $response -> is_success) | ||
| 117 | { | ||
| 118 | $$self{RESPONSE} = $response; | ||
| 119 | return undef; | ||
| 120 | } | ||
| 121 | |||
| 122 | my $baseURL = eval {$response -> base} || $$self{URL}; | ||
| 123 | |||
| 124 | my $encoder = new GetWeb::Encoder(); | ||
| 125 | $encoder -> encode($response -> content, $response -> content_type, | ||
| 126 | $baseURL); | ||
| 127 | |||
| 128 | eval | ||
| 129 | { | ||
| 130 | $$self{URL} = $response -> base; | ||
| 131 | }; | ||
| 132 | |||
| 133 | my $pText = $encoder -> getTextRef(); | ||
| 134 | |||
| 135 | if ($$pText =~ /^\[$follow\] (\S+:(\\\n|.)+)/m) | ||
| 136 | { | ||
| 137 | my $urlString = $1; | ||
| 138 | $urlString =~ s/\\\n//g; | ||
| 139 | $$self{URL} = $urlString; | ||
| 140 | $$self{REQUEST} = GetWeb::Util::safeRequest($urlString); | ||
| 141 | |||
| 142 | return $response; | ||
| 143 | } | ||
| 144 | |||
| 145 | push(@$paRedirect,"could not follow link $follow, sorry"); | ||
| 146 | die "UNAVAILABLE: could not follow link $follow, no such reference"; | ||
| 147 | # jfj return a new category of error condition for not following links | ||
| 148 | return undef; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub getNoteRef | ||
| 152 | { | ||
| 153 | shift->{REDIRECT_LIST}; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub getLinks | ||
| 157 | { | ||
| 158 | my $self = shift; | ||
| 159 | my $paLinkType = shift; # 'a' or 'img' | ||
| 160 | |||
| 161 | my $response = $self -> {RESPONSE}; | ||
| 162 | return [] unless defined $response; | ||
| 163 | |||
| 164 | my $content = $response -> content; | ||
| 165 | |||
| 166 | my $extor = new HTML::LinkExtor; | ||
| 167 | $extor -> parse($content); | ||
| 168 | my @links = $extor -> links; | ||
| 169 | |||
| 170 | my $base = $self -> {URL}; | ||
| 171 | $base -> frag(undef); | ||
| 172 | |||
| 173 | my %hHREF = (); | ||
| 174 | my $link; | ||
| 175 | foreach $link (@links) | ||
| 176 | { | ||
| 177 | my $linkType = shift @$link; | ||
| 178 | next unless grep($linkType eq $_,@$paLinkType); | ||
| 179 | my %attr = @$link; | ||
| 180 | |||
| 181 | my $HREF = $attr{href}; | ||
| 182 | next unless defined $HREF; | ||
| 183 | |||
| 184 | my $url = new URI::URL($HREF,$base); | ||
| 185 | $url -> frag(undef); # ignore fragments | ||
| 186 | |||
| 187 | my $urlString = $url -> abs; | ||
| 188 | |||
| 189 | # URI::URL::eq is flaky, so also do strcmp: | ||
| 190 | next if $url -> eq($base); | ||
| 191 | next if $base -> abs eq $urlString; | ||
| 192 | |||
| 193 | my $scheme = $url -> scheme; | ||
| 194 | next if grep($scheme eq $_, (qw(mailto telnet news))); | ||
| 195 | |||
| 196 | # jf avoid duplicates like www.foo.com and WWW.FOO.COM | ||
| 197 | $hHREF{$urlString} = 1; | ||
| 198 | } | ||
| 199 | |||
| 200 | my @aHREF = keys %hHREF; | ||
| 201 | |||
| 202 | \@aHREF; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub fetch | ||
| 206 | { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | my $response = $$self{RESPONSE}; | ||
| 210 | return $response if defined $response; | ||
| 211 | |||
| 212 | my $req = $$self{REQUEST}; | ||
| 213 | |||
| 214 | $self -> setAuthHead($req); | ||
| 215 | |||
| 216 | #die "about to send request\n"; | ||
| 217 | $response = $$self{UA} -> request($req,$pUserAgentCallback); | ||
| 218 | |||
| 219 | $$self{RESPONSE} = $response; | ||
| 220 | |||
| 221 | #$self -> getLinks; | ||
| 222 | |||
| 223 | $response; | ||
| 224 | } | ||
| 225 | |||
| 226 | 1; |