| 1 | # -*- mode: Perl; -*- | ||
| 2 | package NewsClipper::Server::CGI; | ||
| 3 | |||
| 4 | # This package implements the slower CGI-based interface to the handler | ||
| 5 | # server. | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use Carp; | ||
| 9 | use LWP::UserAgent; | ||
| 10 | |||
| 11 | use vars qw( $VERSION @ISA ); | ||
| 12 | @ISA = qw( NewsClipper::Server ); | ||
| 13 | |||
| 14 | $VERSION = 0.10; | ||
| 15 | |||
| 16 | use NewsClipper::Globals; | ||
| 17 | |||
| 18 | # Cache used to avoid unnecessary processing of handlers | ||
| 19 | my %downloadedCode; | ||
| 20 | my %handler_type; | ||
| 21 | |||
| 22 | # Avoid multiple warnings about the server being down | ||
| 23 | my $already_warned_server_down = 0; | ||
| 24 | |||
| 25 | # The user agent | ||
| 26 | my $userAgent; | ||
| 27 | |||
| 28 | # ------------------------------------------------------------------------------ | ||
| 29 | |||
| 30 | sub new | ||
| 31 | { | ||
| 32 | my $proto = shift; | ||
| 33 | |||
| 34 | # We take the ref if "new" was called on an object, and the class ref | ||
| 35 | # otherwise. | ||
| 36 | my $class = ref($proto) || $proto; | ||
| 37 | |||
| 38 | # Create an "object" | ||
| 39 | my $self = {}; | ||
| 40 | |||
| 41 | # Make the object a member of the class | ||
| 42 | bless ($self, $class); | ||
| 43 | |||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | # ------------------------------------------------------------------------------ | ||
| 48 | |||
| 49 | # Download the handler type from the remote server, caching it locally in | ||
| 50 | # %handler_type. The first return value is the status of the request, and the | ||
| 51 | # second is the handler type. The status is 0 if the server can't be | ||
| 52 | # contacted, or the returned data can't be parsed. | ||
| 53 | |||
| 54 | sub Get_Handler_Type | ||
| 55 | { | ||
| 56 | my $self = shift; | ||
| 57 | my $handler_name = shift; | ||
| 58 | my $ncversion = shift; | ||
| 59 | |||
| 60 | dprint "Downloading handler type information.\n"; | ||
| 61 | |||
| 62 | if (exists $handler_type{$handler_name}) | ||
| 63 | { | ||
| 64 | dprint "Reusing cached handler type information " . | ||
| 65 | "($handler_name is $handler_type{$handler_name})"; | ||
| 66 | return (1,$handler_type{$handler_name}); | ||
| 67 | } | ||
| 68 | |||
| 69 | my $url = "http://" . $self->{handler_server} . | ||
| 70 | "/cgi-bin/getinfo?field=Name&string=$handler_name&" . | ||
| 71 | "print=Type&ncversion=$ncversion"; | ||
| 72 | |||
| 73 | my $data = _Download_URL($url); | ||
| 74 | |||
| 75 | return (0,undef) unless defined $data; | ||
| 76 | |||
| 77 | if ($$data =~ /Type +: (.*)/) | ||
| 78 | { | ||
| 79 | $handler_type{$handler_name} = $1; | ||
| 80 | return (1,$handler_type{$handler_name}); | ||
| 81 | } | ||
| 82 | else | ||
| 83 | { | ||
| 84 | lprint reformat dequote <<" EOF"; | ||
| 85 | ERROR: Couldn't parse handler type information fetched from server. Please | ||
| 86 | send email to bugreport\@newsclipper.com describing this message. Fetched | ||
| 87 | content was: | ||
| 88 | $$data | ||
| 89 | EOF | ||
| 90 | return (0,undef); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | # ------------------------------------------------------------------------------ | ||
| 95 | |||
| 96 | # This function downloads a new handler from the handler database. The first | ||
| 97 | # argument is the name of the handler. The second argument is the version | ||
| 98 | # number of the current handler. You should call Get_New_Handler_Version before | ||
| 99 | # calling this function. | ||
| 100 | |||
| 101 | # This function returns two values: | ||
| 102 | # - an error code: (okay, not found, failed: error message) | ||
| 103 | # - the handler (if the error code is okay) | ||
| 104 | |||
| 105 | sub Get_Handler($$) | ||
| 106 | { | ||
| 107 | my $self = shift; | ||
| 108 | my $handler_name = shift; | ||
| 109 | my $version = shift; | ||
| 110 | my $ncversion = shift; | ||
| 111 | |||
| 112 | dprint "Downloading code for handler \"$handler_name\""; | ||
| 113 | |||
| 114 | if (defined $downloadedCode{$handler_name}) | ||
| 115 | { | ||
| 116 | dprint "Reusing already downloaded code."; | ||
| 117 | return ('okay',$downloadedCode{$handler_name}); | ||
| 118 | } | ||
| 119 | |||
| 120 | my $url; | ||
| 121 | |||
| 122 | $url = "http://" . $self->{handler_server} . | ||
| 123 | "/cgi-bin/gethandler?tag=$handler_name&" . | ||
| 124 | "ncversion=$ncversion&version=$version"; | ||
| 125 | |||
| 126 | my $data = _Download_URL($url); | ||
| 127 | |||
| 128 | # If either the download failed, or the thing we got back doesn't look like | ||
| 129 | # a handler... | ||
| 130 | if ((!defined $data) || ($$data !~ /package NewsClipper/)) | ||
| 131 | { | ||
| 132 | my $error_message = reformat dequote <<" EOF"; | ||
| 133 | failed: Couldn't download handler $handler_name. Maybe the server is | ||
| 134 | down. Try again in a while, and send email to bugreport\@newsclipper.com | ||
| 135 | if the problem persists. | ||
| 136 | EOF | ||
| 137 | |||
| 138 | $error_message .= " Message from server is: $$data\n" if defined $data; | ||
| 139 | |||
| 140 | return ($error_message,undef); | ||
| 141 | } | ||
| 142 | |||
| 143 | return ('not found',undef) if $$data =~ /^Handler not found/; | ||
| 144 | |||
| 145 | $downloadedCode{$handler_name} = $$data; | ||
| 146 | return ('okay',$$data); | ||
| 147 | } | ||
| 148 | |||
| 149 | # ------------------------------------------------------------------------------ | ||
| 150 | |||
| 151 | # Computes the most recent version number for a working handler. | ||
| 152 | # Returns a status value and the version. The status can be one of 'okay', | ||
| 153 | # 'failed', or 'not found'. | ||
| 154 | |||
| 155 | sub _Get_Latest_Working_Handler_Version | ||
| 156 | { | ||
| 157 | my $self = shift; | ||
| 158 | my $handler_name = shift; | ||
| 159 | my $ncversion = shift; | ||
| 160 | |||
| 161 | my $url = "http://" . $self->{handler_server} . | ||
| 162 | "/cgi-bin/checkversion?tag=$handler_name&ncversion=$ncversion" . | ||
| 163 | "&debug=0"; | ||
| 164 | |||
| 165 | my $data = _Download_URL($url); | ||
| 166 | |||
| 167 | return ('failed',undef) unless defined $data; | ||
| 168 | return ('not found',undef) if $$data =~ /^Handler not found/; | ||
| 169 | |||
| 170 | if ($$data =~ /(\S+)/) | ||
| 171 | { | ||
| 172 | my $newVersion = $1; | ||
| 173 | return ('okay',$newVersion); | ||
| 174 | } | ||
| 175 | else | ||
| 176 | { | ||
| 177 | lprint reformat dequote <<" EOF"; | ||
| 178 | ERROR: Couldn't parse handler version information fetched from server. Please | ||
| 179 | send email to bugreport\@newsclipper.com describing this message. Fetched | ||
| 180 | content was: | ||
| 181 | $$data | ||
| 182 | EOF | ||
| 183 | return ('failed',undef); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | # ------------------------------------------------------------------------------ | ||
| 188 | |||
| 189 | # Computes the most recent guaranteed-compatible version number for a working | ||
| 190 | # handler. Returns a status value and the version. The status can be one of | ||
| 191 | # 'okay', 'no update', 'failed', or 'not found'. The parameter $version | ||
| 192 | # should be defined. | ||
| 193 | |||
| 194 | sub _Get_Compatible_Working_Handler_Version | ||
| 195 | { | ||
| 196 | my $self = shift; | ||
| 197 | my $handler_name = shift; | ||
| 198 | my $ncversion = shift; | ||
| 199 | my $version = shift; | ||
| 200 | |||
| 201 | my $url = "http://" . $self->{handler_server} . | ||
| 202 | "/cgi-bin/checkversion?tag=$handler_name&ncversion=$ncversion" . | ||
| 203 | "&version=$version&debug=1"; | ||
| 204 | |||
| 205 | my $data = _Download_URL($url); | ||
| 206 | |||
| 207 | return ('failed',undef) unless defined $data; | ||
| 208 | return ('not found',undef) if $$data =~ /^Handler not found/; | ||
| 209 | |||
| 210 | return ('no update',undef) if $$data =~ /^No new version available/; | ||
| 211 | |||
| 212 | if ($$data =~ /(\S+)/) | ||
| 213 | { | ||
| 214 | my $newVersion = $1; | ||
| 215 | return ('okay',$newVersion); | ||
| 216 | } | ||
| 217 | else | ||
| 218 | { | ||
| 219 | lprint reformat dequote <<" EOF"; | ||
| 220 | ERROR: Couldn't parse handler version information fetched from server. Please | ||
| 221 | send email to bugreport\@newsclipper.com describing this message. Fetched | ||
| 222 | content was: | ||
| 223 | $$data | ||
| 224 | EOF | ||
| 225 | return ('failed',undef); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | # ------------------------------------------------------------------------------ | ||
| 230 | |||
| 231 | # Gets the entire content from a URL. file:// supported | ||
| 232 | |||
| 233 | sub _Download_URL($) | ||
| 234 | { | ||
| 235 | my $url = shift; | ||
| 236 | |||
| 237 | # Create the user agent if we haven't already. (We create it on demand.) | ||
| 238 | if (!defined $userAgent) | ||
| 239 | { | ||
| 240 | $userAgent = new LWP::UserAgent; | ||
| 241 | } | ||
| 242 | |||
| 243 | dprint "Downloading URL:"; | ||
| 244 | dprint " $url"; | ||
| 245 | |||
| 246 | $userAgent->timeout($config{socket_timeout}); | ||
| 247 | $userAgent->proxy(['http', 'ftp'], $config{proxy}) | ||
| 248 | if $config{proxy} ne ''; | ||
| 249 | my $request = new HTTP::Request GET => "$url"; | ||
| 250 | if ($config{proxy_username} ne '') | ||
| 251 | { | ||
| 252 | $request->proxy_authorization_basic($config{proxy_username}, | ||
| 253 | $config{proxy_password}); | ||
| 254 | } | ||
| 255 | |||
| 256 | my $result; | ||
| 257 | my $numTriesLeft = $config{socket_tries}; | ||
| 258 | |||
| 259 | do | ||
| 260 | { | ||
| 261 | $result = $userAgent->request($request); | ||
| 262 | $numTriesLeft--; | ||
| 263 | } until ($numTriesLeft == 0 || $result->is_success); | ||
| 264 | |||
| 265 | return undef unless $result->is_success; | ||
| 266 | |||
| 267 | my $content = $result->content; | ||
| 268 | |||
| 269 | # Strip linefeeds off the lines | ||
| 270 | $content =~ s/\r//gs; | ||
| 271 | |||
| 272 | return \$content; | ||
| 273 | } | ||
| 274 | |||
| 275 | # ------------------------------------------------------------------------------ | ||
| 276 | |||
| 277 | 1; |