| 1 | # -*- perl -*- | ||
| 2 | # w3http.pm --- send http requests, janl's 12" mix for w3mir | ||
| 3 | # | ||
| 4 | $VERSION=1.0.23; | ||
| 5 | # | ||
| 6 | # This implements http/1.0 requests. We'll have problems with http/0.9 | ||
| 7 | # This is in no way specific to w3mir. | ||
| 8 | # | ||
| 9 | # IMPORTANT: The caller should initialize the C locale for some of the | ||
| 10 | # things here to work correctly (specifically the strftime function). | ||
| 11 | # | ||
| 12 | # This is a rewrite of http.pl by Oscar Nierstrasz; I copied the code he he | ||
| 13 | # copied from the camel book. Some functions written by Gorm Haug Eriksen | ||
| 14 | # (gorm@usit.uio.no) has been used as is. | ||
| 15 | # | ||
| 16 | # Contributors: | ||
| 17 | # Nicolai Langfeldt, janl@ifi.uio.no | ||
| 18 | # Gorm Haug Eriksen, gorm@usit.uio.no | ||
| 19 | # Chris Szurgot, szurgot@itribe.net | ||
| 20 | # Bernhard Weisshuh, bkw@weisshuhn.de | ||
| 21 | # Copying and modification is governed by the "Artistic License" enclosed in | ||
| 22 | # the w3mir distribution | ||
| 23 | # | ||
| 24 | # gorm : | ||
| 25 | # &w3http::get_last_modified return the last modified stamp on a file in | ||
| 26 | # the right format for use with http | ||
| 27 | # | ||
| 28 | # janl: | ||
| 29 | # &http::query: Send a http query. A completely general function to send a | ||
| 30 | # http query. Will extract header values, http response code and, optionaly, | ||
| 31 | # convert text files to local linefeed format. | ||
| 32 | # | ||
| 33 | # Variables to examine after a query | ||
| 34 | # $w3http::document: The document returned by the query, if any. | ||
| 35 | # $w3http::doclen: The length of the document | ||
| 36 | # $w3http::result: The numerical http result code. It may take these values: | ||
| 37 | # - Normal HTTP reply codes | ||
| 38 | # - 98: OS error, permanent, errormessage in $! | ||
| 39 | # - 99: Other permanent error: see $restext. | ||
| 40 | # - 100: Transient error: Timeout/connection broken | ||
| 41 | # $w3http::restext: The english(?) HTTP result or w3http generated message | ||
| 42 | # $w3http::header: The http header returned. | ||
| 43 | # $w3http::plaintexthtml: 1 if this doc is non-content-encoded text/html | ||
| 44 | # (as opposed to content-encoding: compressed content-type: text/html | ||
| 45 | # which needs decompression before we can inspect the html) | ||
| 46 | # The tests are somewhat longwinded so I do it just once here. | ||
| 47 | # %w3http::headval: Associative array of header values | ||
| 48 | # $w3http::headval{'CONTENT-TYPE'}: Derived content type, stripped of charset | ||
| 49 | # qualifiers and other distractions. | ||
| 50 | # $w3http::xfbytes: Transfered bytes, cumulative. Document part only. | ||
| 51 | # $w3http::headbytes: Bytes of headers received, cumulative. | ||
| 52 | # | ||
| 53 | # Variables that change http's behaviour/requests: | ||
| 54 | # $w3http::agent: User agent, default is basename of $0 | ||
| 55 | # $w3http::from: Request is from, default is user@host | ||
| 56 | # $w3http::version: The http version to use, only 1.0 is known to me. | ||
| 57 | # $w3http::timeout: How long to wait for new data to arrive, default is 600sec | ||
| 58 | # $w3http::buflen: Network read buffer size, default is 4096. It might give a | ||
| 59 | # speedup to tune this for specific servers' so it matches their send | ||
| 60 | # size. This size can be detected if we want to, I think. | ||
| 61 | # $w3http::debug: 1 debuging output, 2, more, 3 queries and replies | ||
| 62 | # $w3http::verbose: 0: say nothing, 1: print progress info | ||
| 63 | # $w3http::convert: Convert text/* documents to local newline convention? | ||
| 64 | # The default is to do it. | ||
| 65 | # $w3http::proxyserver: The name of the proxyserver to use. | ||
| 66 | # $w3http::proxyport: The port of the proxyserver to use. 0 if no proxyserver. | ||
| 67 | # $w3http::proxyuser: If this is set proxy authentication will be used. | ||
| 68 | # $w3http::proxypasswd: The password for proxy authentication | ||
| 69 | # | ||
| 70 | # Things gotten from main: | ||
| 71 | # - $main::win32: 1 if win32 restrictions apply to this system | ||
| 72 | # - $main::nulldevice: Bit sink file/device on this system. | ||
| 73 | # | ||
| 74 | # History (european date format dd/mm/yy): | ||
| 75 | # janl ??/??/95 -- Rewrite finished | ||
| 76 | # szurgot ??/??/95 -- Win32 compatability | ||
| 77 | # janl 16/05/96 -- Added SAVEBIN option, based on idea by szurgot | ||
| 78 | # szurgot 03/05/96 -- Corrected typo in check for content-length against | ||
| 79 | # retreive document length. Added test for zero-length | ||
| 80 | # documents (Not retreived because not-modified) | ||
| 81 | # szurgot 19/05/96 -- Win32 adaptions, fixes. | ||
| 82 | # janl 19/05/96 -- Chris won an argument, and janl simplified http | ||
| 83 | # retrival loop (-> version 1.0.4) | ||
| 84 | # janl 09/09/96 -- Incorporated a patch submited by Michael Kriby -> 1.0.5 | ||
| 85 | # janl 16/09/96 -- Support for authorization. -> 1.0.6 | ||
| 86 | # janl 27/09/96 -- Support for Accept header, lack pointed out by | ||
| 87 | # charles@ermine.ox.ac.uk: ... HTTP/1.1 (ยง14.1) says | ||
| 88 | # ``If no Accept header field is present, then it is | ||
| 89 | # assumed that the client accepts all media types, | ||
| 90 | # earlier versions of the protocol suggest that only | ||
| 91 | # text/plain and text/html will be offered by default.'' | ||
| 92 | # This contradicts my memory of a http/1.0 draft. | ||
| 93 | # Also added $ACCEPT option. | ||
| 94 | # janl 20/10/96 -- Now uses HTTP::Date to produce HTTP timestamps -> 1.0.7 | ||
| 95 | # janl 27/10/96 -- Didn't use to check if gethostbyname worked -> 1.0.8 | ||
| 96 | # janl 02/12/96 -- Forgot a unlink when renaming temporary files. | ||
| 97 | # janl 21/02/97 -- Multipele $ACCEPT options work. -> 1.0.9 | ||
| 98 | # janl 19/03/97 -- Now issues Host: header -> 1.0.10 | ||
| 99 | # janl 10/04/97 -- Changed from wwwurl to URI::URL, and various related | ||
| 100 | # changes. -> 1.0.11 | ||
| 101 | # janl 09/05/97 -- Microsoft ISS servers are _so_ broken -> 1.0.12 | ||
| 102 | # (don't close the write end of the HTTP socket after | ||
| 103 | # sending a query to them) | ||
| 104 | # janl 12/05/97 -- New version of perl caught some typos, fixed | ||
| 105 | # longstanding bug in the newline conversion bit. | ||
| 106 | # -> 1.0.13 | ||
| 107 | # janl 06/06/97 -- Demand Loading of MIME::BASE64 -> 1.0.14 | ||
| 108 | # janl 01/12/97 -- FAT filesystems drops LSB of modtime. Patch from | ||
| 109 | # Greg Lindhorst (gregli@microsoft.com) | ||
| 110 | # -- whoami does not exist on win32, hardwire a default | ||
| 111 | # value (unknown) (also Greg L.) -> 1.0.15 | ||
| 112 | # janl 01/22/98 -- Proxy authentication as outlined by Christian Geuer | ||
| 113 | # janl 02/20/98 -- Complex 'content-type' headers handled. -> 1.0.17 | ||
| 114 | # janl 04/20/98 -- Only newline convert text/html, everything else is | ||
| 115 | # handled as binary. -> 1.0.18 | ||
| 116 | # janl 12/05/98 -- Store tmpfile in its final destination directory | ||
| 117 | # avoiding asking movefile move it across filesystems. | ||
| 118 | # -> 1.0.19 | ||
| 119 | # janl 01/08/98 -- Timeout fix from Michael Gusev, also flag short doc | ||
| 120 | # as error. | ||
| 121 | # janl 24/09/98 -- Better error handling -> 1.0.20 | ||
| 122 | # bkw 17/12/98 -- Fixed problem with tempfile-generation when | ||
| 123 | # running in forget-mode (-f) | ||
| 124 | # janl 05/01/99 -- Referer: dropped if argument not true -> 1.0.21 | ||
| 125 | # janl 13/04/99 -- Added workaround for broken win32 perl resolving. | ||
| 126 | # janl 15/01/00 -- Patch to adapt to URI 1.0 from Takuya Tsumura and | ||
| 127 | # Andrey A. Chernov | ||
| 128 | # ams 02/02/01 -- Handle URLs with spaces better (use epath) | ||
| 129 | |||
| 130 | package w3http; | ||
| 131 | |||
| 132 | require 5.002; | ||
| 133 | use Socket; | ||
| 134 | use HTTP::Date; | ||
| 135 | use Sys::Hostname; | ||
| 136 | use URI::URL; | ||
| 137 | |||
| 138 | # Suplementary libwww-perl: | ||
| 139 | sub URI::URL::_generic::basename { | ||
| 140 | my $self = shift; | ||
| 141 | my @p = $self->path_components; | ||
| 142 | my $old = $p[-1]; | ||
| 143 | if (@_) { | ||
| 144 | splice(@p, -1, 1, shift); | ||
| 145 | $self->path_components(@p) | ||
| 146 | } | ||
| 147 | $old; | ||
| 148 | } | ||
| 149 | |||
| 150 | # The URI 1.0 library changed the internal organization a bit | ||
| 151 | # Thanks to Andrey A. Chernov for the patch! | ||
| 152 | |||
| 153 | sub URI::_generic::basename { | ||
| 154 | my $self = shift; | ||
| 155 | my @p = $self->path_segments; | ||
| 156 | my $old = $p[-1]; | ||
| 157 | if (@_) { | ||
| 158 | splice(@p, -1, 1, shift); | ||
| 159 | $self->path_segments(@p) | ||
| 160 | } | ||
| 161 | $old; | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | END { | ||
| 166 | # Remove tmp file and such in here. That means that main:: gotta catch | ||
| 167 | # interrupt signals and exit on them, so ENDs are executed. | ||
| 168 | } | ||
| 169 | |||
| 170 | use strict; | ||
| 171 | # Global variables, we want to share them: | ||
| 172 | use vars qw($GET $HEAD $GETURL $HEADURL $IFMOD $IFMODF $AUTHORIZ $REFERER); | ||
| 173 | use vars qw($SAVEBIN $ACCEPT $NOUSER $FREEHEAD $agent $version $timeout); | ||
| 174 | use vars qw($debug $convert $proxyserver $proxyport $xfbytes $headbytes); | ||
| 175 | use vars qw($verbose $result $restext $header $document); | ||
| 176 | use vars qw($plaintexthtml %headval $progress $doclen $proxyuser); | ||
| 177 | use vars qw($proxypasswd); | ||
| 178 | |||
| 179 | my $hasAlarm; # Win32 does not have any alarm | ||
| 180 | my $chime; # Has the alarm gone off yet? | ||
| 181 | my %address; # My own DNS cache | ||
| 182 | my $savALRM; # Saved ALRM handler | ||
| 183 | my $savPIPE; # Saved PIPE handler | ||
| 184 | |||
| 185 | # The main:: program should detect if we're running on win32 or not, | ||
| 186 | # somehow | ||
| 187 | if ($main::win32) { | ||
| 188 | warn "win32\n"; | ||
| 189 | # Compensate for lacks of win32 perl. | ||
| 190 | $hasAlarm=0; | ||
| 191 | # Seems to be unavailable in win32/perl5.001. It has to be in 5.003! | ||
| 192 | # eval "sub sockaddr_in { | ||
| 193 | # ($port, $thataddr) = @_; | ||
| 194 | # $sockaddr = 'S n a4 x8'; | ||
| 195 | # return pack($sockaddr, &AF_INET, $port, $thataddr); | ||
| 196 | # }"; | ||
| 197 | } else { | ||
| 198 | $hasAlarm=1; | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | # Find out some things | ||
| 203 | my $thishost = hostname(); | ||
| 204 | my $proto = getprotobyname("tcp"); | ||
| 205 | |||
| 206 | (my $name, undef) = gethostbyname($thishost); | ||
| 207 | chomp(my $user = $ENV{'LOGNAME'} || $ENV{'USER'} || `whoami` || 'unknown'); | ||
| 208 | my $from = "$user\@$name"; | ||
| 209 | |||
| 210 | my $nl = "\r\n"; | ||
| 211 | # Default values, change by assignment in using-program. | ||
| 212 | $agent = $0; $agent =~ s~.*/~~; # Basename | ||
| 213 | $version= "1.0"; | ||
| 214 | $timeout= 600; # Timeout while waiting for data/connection | ||
| 215 | my $buflen = 4096; # recv buffer length | ||
| 216 | $debug = 0; # Debuging output? | ||
| 217 | $convert = 1; # Convert newlines of text docs to local format | ||
| 218 | $proxyserver=''; # Proxy server. | ||
| 219 | $proxyport=0; # Proxy server port. 0 if no proxy. | ||
| 220 | $proxyuser=''; # Username for proxy authentication | ||
| 221 | $proxypasswd=''; # Password for proxy authentication | ||
| 222 | $xfbytes=0; # 0 bytes transfered, cumulative | ||
| 223 | $headbytes=0; # 0 bytes of headers, cumulative | ||
| 224 | $doclen=0; # 0 bytes in doc, pr. document | ||
| 225 | my $tmpfile="w3mir$$.tmp"; # Temporary filename | ||
| 226 | $verbose=0; # Verbosenes, 0: silent, 1: progress info | ||
| 227 | |||
| 228 | # Query opcodes | ||
| 229 | $GET = 1; # GET query. Arg: host,port,path | ||
| 230 | $HEAD = 2; # HEAD query. Arg: host,port,path | ||
| 231 | $GETURL = 3; # GET query. Arg: url | ||
| 232 | $HEADURL = 4; # HEAD query. Arg: url | ||
| 233 | # Here we lack PUT, which is not implemented | ||
| 234 | # Modify query thus: | ||
| 235 | $IFMOD = 101; # If-modified after: Arg: HTTP-date-str | ||
| 236 | $IFMODF = 102; # If-modified after file: Arg: local-file-name | ||
| 237 | $AUTHORIZ= 103; # Basic authorization. Arg: 'user:password' | ||
| 238 | $REFERER = 104; # Referer: Arg: Referer | ||
| 239 | $SAVEBIN = 105; # Write binary files to disk. Arg: File name | ||
| 240 | # If this opcode is used then main must provide | ||
| 241 | # a &main::movefile(oldname,newname) procedure | ||
| 242 | # that handles moving the tmp file to the | ||
| 243 | # final name/location. | ||
| 244 | $ACCEPT = 106; # Accept header value: Arg: value | ||
| 245 | $NOUSER = 107; # Don't insert user header. Arg: none | ||
| 246 | $FREEHEAD= 999; # Freeform header, one line. Arg: header | ||
| 247 | |||
| 248 | sub query { | ||
| 249 | # Build and send a HTTP query. And also receive response - janl 95/09/18 | ||
| 250 | # | ||
| 251 | # Return codes: 0 if it didn't work. 1 if it did work. | ||
| 252 | # HTTP style result code in w3http::$result and message in w3http::$restext | ||
| 253 | |||
| 254 | # We do next to no argument type checking btw. | ||
| 255 | |||
| 256 | my($host,$port,$request,$query,$method,$inp,$linp,$saveto,$save,$arg); | ||
| 257 | my($start,$wantbytes,$thataddr,$err,$headb,$tmpf,$ldoc,$nouser,$q,$accept); | ||
| 258 | my($origreq,$req_o,$plaintext); | ||
| 259 | |||
| 260 | # Something ought to be said | ||
| 261 | $result=99; | ||
| 262 | $restext='w3http: internal error'; | ||
| 263 | $nouser=0; | ||
| 264 | |||
| 265 | if ($version ne '1.0') { | ||
| 266 | warn "Unknown HTTP version $version, no request sent\n"; | ||
| 267 | return 0; | ||
| 268 | } | ||
| 269 | |||
| 270 | $accept=$saveto=$query=''; | ||
| 271 | |||
| 272 | # Find out what to ask for | ||
| 273 | |||
| 274 | while (defined($arg=shift)) { | ||
| 275 | if ($arg == $GET) { | ||
| 276 | $host=shift; | ||
| 277 | $port=shift; | ||
| 278 | $request=shift; | ||
| 279 | $req_o=url 'http://'.$host.':'.$port.$request; | ||
| 280 | if ($proxyport) { | ||
| 281 | $query.='GET http://'.$req_o->as_string; | ||
| 282 | } else { | ||
| 283 | $query.='GET '.$req_o->epath; | ||
| 284 | } | ||
| 285 | $query.=' HTTP/'.$version.$nl; | ||
| 286 | } elsif ($arg == $HEAD) { | ||
| 287 | $host=shift; | ||
| 288 | $port=shift; | ||
| 289 | $request=shift; | ||
| 290 | $req_o=url 'http://'.$host.':'.$port.$request; | ||
| 291 | if ($proxyport) { | ||
| 292 | $query.='HEAD '.$req_o->as_string; | ||
| 293 | } else { | ||
| 294 | $query.='HEAD '.$req_o->epath; | ||
| 295 | } | ||
| 296 | $query.=' HTTP/'.$version.$nl; | ||
| 297 | } elsif ($arg == $GETURL) { | ||
| 298 | $req_o=shift; | ||
| 299 | $req_o=url $req_o unless ref $req_o; | ||
| 300 | ($method,undef,undef,$host,$port,$request,undef,$q) = $req_o->crack; | ||
| 301 | if ($proxyport) { | ||
| 302 | $query.='GET '.$req_o->as_string; | ||
| 303 | } else { | ||
| 304 | $q=$req_o->equery; | ||
| 305 | $query.='GET '.($req_o->epath).($q?"?$q":''); | ||
| 306 | } | ||
| 307 | $query.=' HTTP/'.$version.$nl; | ||
| 308 | } elsif ($arg == $HEADURL) { | ||
| 309 | $req_o=shift; | ||
| 310 | $req_o=url $req_o unless ref $req_o; | ||
| 311 | if ($proxyport) { | ||
| 312 | $query.='HEAD '.$req_o->as_string; | ||
| 313 | } else { | ||
| 314 | $q=$req_o->equery; | ||
| 315 | $query.='HEAD '.$req_o->epath.($q?"?$q":''); | ||
| 316 | } | ||
| 317 | $query.=' HTTP/'.$version.$nl; | ||
| 318 | } elsif ($arg == $IFMOD) { | ||
| 319 | $query.='If-Modified-Since: '.(shift).$nl; | ||
| 320 | } elsif ($arg == $IFMODF) { | ||
| 321 | $query.='If-Modified-Since: '.&last_modified(shift).$nl; | ||
| 322 | } elsif ($arg == $AUTHORIZ) { | ||
| 323 | # Demand-load MIME::Base64 | ||
| 324 | if (!defined(&MIME::Base64::encode)) { | ||
| 325 | eval "use MIME::Base64;"; | ||
| 326 | die "w3http: Could not load MIME::Base64 module necessary for authentication\n" | ||
| 327 | unless defined(&MIME::Base64::encode); | ||
| 328 | } | ||
| 329 | $query.='Authorization: Basic '.MIME::Base64::encode(shift,'').$nl; | ||
| 330 | } elsif ($arg == $REFERER) { | ||
| 331 | my($referer)=shift; | ||
| 332 | $query.='Referer: '.$referer.$nl if $referer; | ||
| 333 | } elsif ($arg == $SAVEBIN) { | ||
| 334 | $saveto=shift; | ||
| 335 | } elsif ($arg == $ACCEPT) { | ||
| 336 | $accept.='Accept: '.(shift).$nl; | ||
| 337 | } elsif ($arg == $NOUSER) { | ||
| 338 | $nouser=1; | ||
| 339 | } elsif ($arg == $FREEHEAD) { | ||
| 340 | $query.=(shift).$nl; | ||
| 341 | } else { | ||
| 342 | warn "Unknown http query opcode: $arg\n"; | ||
| 343 | } | ||
| 344 | # Insert the last parts of the query: | ||
| 345 | } | ||
| 346 | |||
| 347 | $query.='Host: '.$req_o->netloc.$nl; | ||
| 348 | $query.='From: '.$from.$nl unless $nouser; | ||
| 349 | |||
| 350 | $accept='Accept: */*'.$nl unless $accept; | ||
| 351 | |||
| 352 | if ($proxyport) { | ||
| 353 | # Use proxy instead of originserver | ||
| 354 | $host=$proxyserver; | ||
| 355 | $port=$proxyport; | ||
| 356 | |||
| 357 | # Add authentication stuff to query | ||
| 358 | if ($proxyuser) { | ||
| 359 | # Demand-load MIME::Base64 | ||
| 360 | if (!defined(&MIME::Base64::encode)) { | ||
| 361 | eval "use MIME::Base64;"; | ||
| 362 | die "w3http: Could not load MIME::Base64 module necessary for authentication\n" | ||
| 363 | unless defined(&MIME::Base64::encode); | ||
| 364 | } | ||
| 365 | |||
| 366 | $query.='Proxy-Authorization: Basic '. | ||
| 367 | MIME::Base64::encode($proxyuser.':'.$proxypasswd); | ||
| 368 | |||
| 369 | print STDERR "\nProxyuser: [$proxyuser]\nProxypasswd: [$proxypasswd]\n" | ||
| 370 | if $debug>=2; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | $query.='User-Agent: '.$agent.$nl.$accept.$nl; | ||
| 375 | |||
| 376 | # If we're using proxy then set up things... | ||
| 377 | print STDERR "\nQUERY:\n",$query,"---\n" if $debug>=2; | ||
| 378 | |||
| 379 | # win32 fix: this should be added in case of troubles with | ||
| 380 | # gethostbyname. possible reason: nameserver down? | ||
| 381 | if ($host =~ /^\d+(\.\d+){3}$/) { | ||
| 382 | # in case gethostbyname will not work ... ;-) | ||
| 383 | $address{$host} = pack 'C4', (split /\./, $host); | ||
| 384 | } | ||
| 385 | |||
| 386 | # Find out who to ask, check if we know already | ||
| 387 | if (exists($address{$host})) { | ||
| 388 | # We know | ||
| 389 | $thataddr=$address{$host}; | ||
| 390 | } else { | ||
| 391 | # Cache miss, get and remember. | ||
| 392 | (my $fqdn, undef, undef, undef, $thataddr) = gethostbyname($host); | ||
| 393 | # Hostname lookup failure? Cache even misses. | ||
| 394 | if (defined($fqdn)) { | ||
| 395 | print STDERR "Lookup of $host:\nFQDN: $fqdn\n" | ||
| 396 | if $debug; | ||
| 397 | $address{$host}=$thataddr; | ||
| 398 | $address{$fqdn}=$thataddr if $fqdn ne $host; | ||
| 399 | } else { | ||
| 400 | $thataddr=$address{$host}=undef; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 404 | # Check if lookup failure, return | ||
| 405 | if (!defined($thataddr)) { | ||
| 406 | $restext='Host lookup failure'; | ||
| 407 | return; | ||
| 408 | } | ||
| 409 | |||
| 410 | $port=80 unless defined($port) && $port; | ||
| 411 | |||
| 412 | # When connected we might receive SIGPIPE. I'm not sure if the | ||
| 413 | # default behaviour of dying is beneficial in that case. If we get | ||
| 414 | # alarm a timeout has expired. | ||
| 415 | $savPIPE = $SIG{'PIPE'}; | ||
| 416 | $savALRM = $SIG{'ALRM'}; | ||
| 417 | |||
| 418 | $chime=0; # There has been no alarm yet | ||
| 419 | $SIG{'ALRM'} = \&timeout; | ||
| 420 | $SIG{'PIPE'} = \&ignore; | ||
| 421 | |||
| 422 | # Close the socket, just in case, and ignore error returns | ||
| 423 | close(FS); | ||
| 424 | |||
| 425 | socket(FS, AF_INET, SOCK_STREAM, $proto) or return &oserror; | ||
| 426 | warn "Got my socks on\n" if $debug; | ||
| 427 | |||
| 428 | my $paddr = sockaddr_in($port, $thataddr); | ||
| 429 | connect(FS, $paddr) or return &oserror; | ||
| 430 | warn "Connected\n" if $debug; | ||
| 431 | |||
| 432 | # Arrange timeout | ||
| 433 | alarm($timeout) if $hasAlarm; | ||
| 434 | |||
| 435 | # We have, in fact, received SIGPIPE on this line: | ||
| 436 | send(FS,$query,0) or return &oserror; | ||
| 437 | |||
| 438 | if ($chime) { | ||
| 439 | $result=100; | ||
| 440 | $restext='timeout sending query'; | ||
| 441 | return &resetsign; | ||
| 442 | } | ||
| 443 | |||
| 444 | $header=''; | ||
| 445 | $document=''; | ||
| 446 | $inp=' 'x$buflen; | ||
| 447 | $doclen=$chime=$plaintext=$plaintexthtml=$save=0; | ||
| 448 | |||
| 449 | # Breaks some M$ ISS servers: | ||
| 450 | # shutdown(FS,1); # Half-close socket, sending now not allowed | ||
| 451 | |||
| 452 | print STDERR ", receiving header" if $verbose>0; | ||
| 453 | |||
| 454 | # Retrive HTTP response HEADER. Why do I use recv and not <FS>? | ||
| 455 | # Because then the timeout can work correctly! | ||
| 456 | while (1) { | ||
| 457 | # Set up alarm to ensure recv returns within a reasonable timeframe | ||
| 458 | alarm($timeout) if $hasAlarm; | ||
| 459 | $err = recv(FS,$inp,$buflen,0); | ||
| 460 | # recv returned, cancel alarm. | ||
| 461 | alarm(0) if $hasAlarm; | ||
| 462 | |||
| 463 | # If there has been a timeout, then we quit now. The recv man page | ||
| 464 | # does not seem to allow recv to return the bytes received up to | ||
| 465 | # the timeout. | ||
| 466 | if ($chime) { | ||
| 467 | $result=100; | ||
| 468 | $restext='timeout fetching document'; | ||
| 469 | $!=0; | ||
| 470 | if ($save) { | ||
| 471 | unlink($tmpf) || | ||
| 472 | warn "Could not unlink $tmpf: $!\n"; | ||
| 473 | } | ||
| 474 | return &resetsign; | ||
| 475 | } | ||
| 476 | |||
| 477 | # recv returnes the undefined value on error | ||
| 478 | if (!defined($err)) { | ||
| 479 | warn "Error in recv: $!\n"; | ||
| 480 | last; | ||
| 481 | } | ||
| 482 | |||
| 483 | $linp=length($inp); | ||
| 484 | |||
| 485 | # If the returned input was 0 in length then we've gotten to the | ||
| 486 | # end of the response. | ||
| 487 | last unless $linp; | ||
| 488 | |||
| 489 | # Accounting | ||
| 490 | $xfbytes += $linp; | ||
| 491 | $doclen += $linp; | ||
| 492 | |||
| 493 | # Accumulate input | ||
| 494 | $header.=$inp; | ||
| 495 | |||
| 496 | # eof(SOCKET) has strange semantics it seems | ||
| 497 | # last if eof(FS); | ||
| 498 | |||
| 499 | # Check if header is complete | ||
| 500 | last if ($header =~ m/(\r?\n\r?\n)/); | ||
| 501 | } | ||
| 502 | |||
| 503 | my $orighead = $header; | ||
| 504 | |||
| 505 | if (length($header)==0) { | ||
| 506 | $restext='the HTTP reply header is empty!'; | ||
| 507 | return &resetsign; | ||
| 508 | } | ||
| 509 | |||
| 510 | if ($header =~ m/(\r?\n\r?\n)/) { | ||
| 511 | if ($`) { | ||
| 512 | $header=$`; | ||
| 513 | $document=$'; | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | # Adjust accounting | ||
| 518 | $headb = length($header)+length($1); | ||
| 519 | $headbytes += $headb; | ||
| 520 | $xfbytes -= $headb; | ||
| 521 | $doclen -= $headb; | ||
| 522 | |||
| 523 | # Pick headers to pieces | ||
| 524 | ($result,$restext,%headval)=&analyze_header($header); | ||
| 525 | |||
| 526 | if (!$result) { | ||
| 527 | print "\n\nw3mir: BOGUS HTTP REPLY:\n-----\n$header\n-----\n"; | ||
| 528 | print "\n\nw3mir: UNPROCESSED REPLY:\n-----\n$orighead\n-----\n"; | ||
| 529 | print "\nw3mir: QUERY WAS:\n-----\n$query\n-----\n"; | ||
| 530 | die; | ||
| 531 | } | ||
| 532 | |||
| 533 | print STDERR "REPLY:\n",$header,"\n---\n" if $debug>=2; | ||
| 534 | |||
| 535 | # Check if the document is a non-encoded text document. The contents | ||
| 536 | # could be (x-)?compress or (x-)gzip coded (compressed in other | ||
| 537 | # words). | ||
| 538 | |||
| 539 | $plaintext=defined($headval{'CONTENT-TYPE'}) && | ||
| 540 | (substr($headval{'CONTENT-TYPE'},0,5) eq 'text/' || 0) && | ||
| 541 | !defined($headval{'content-encoding'}); | ||
| 542 | $plaintexthtml=$plaintext && | ||
| 543 | ($headval{'CONTENT-TYPE'} eq 'text/html'); | ||
| 544 | |||
| 545 | if ($result==200) { | ||
| 546 | |||
| 547 | # Save this to a file, or not? Never save html files. | ||
| 548 | if ($saveto && !$plaintexthtml) { | ||
| 549 | # We're going to save this document directly into a file. This | ||
| 550 | # stresses the VM less when getting the large binares so often | ||
| 551 | # found at cool sites. | ||
| 552 | $save=1; | ||
| 553 | |||
| 554 | # Find a temporary filename | ||
| 555 | $tmpf=url "file:$saveto"; | ||
| 556 | $tmpf->basename($tmpfile); | ||
| 557 | $tmpf=$tmpf->unix_path; | ||
| 558 | |||
| 559 | # Find suitable final filename, one with no URL escapes | ||
| 560 | $saveto=(url "file:$saveto")->unix_path; | ||
| 561 | |||
| 562 | # If output to stdout then send it directly there rather than | ||
| 563 | # using disk unnecesarily. | ||
| 564 | $tmpf='-' if ($saveto eq '-'); | ||
| 565 | |||
| 566 | # If output is nulldevice (running -f), use it also for tmpfile, | ||
| 567 | # since it would otherwise try to create it in /dev under unix. | ||
| 568 | $tmpf=$main::nulldevice if ($saveto eq $main::nulldevice); | ||
| 569 | |||
| 570 | warn "USING TMPFILE: $tmpf\n" if $debug; | ||
| 571 | |||
| 572 | open(SAVE,">$tmpf") || | ||
| 573 | die "Could not open tmp file: $tmpf: $!\n"; | ||
| 574 | binmode SAVE; # It's a binary file... | ||
| 575 | } | ||
| 576 | |||
| 577 | if ($verbose>0) { | ||
| 578 | print STDERR ", document"; | ||
| 579 | print STDERR "->disk" if $save; | ||
| 580 | } | ||
| 581 | |||
| 582 | # Now retrive document itself. Se comments in header loop | ||
| 583 | $start=time; | ||
| 584 | $wantbytes = defined($headval{'content-length'})? | ||
| 585 | $headval{'content-length'}:0; | ||
| 586 | |||
| 587 | $ldoc=length($document); | ||
| 588 | |||
| 589 | while (1) { | ||
| 590 | alarm($timeout) if $hasAlarm; | ||
| 591 | recv(FS,$inp,$buflen,0); | ||
| 592 | alarm(0) if $hasAlarm; | ||
| 593 | |||
| 594 | if ($chime) { | ||
| 595 | $result=100; | ||
| 596 | $restext='timeout fetching document'; | ||
| 597 | $!=0; | ||
| 598 | if ($save) { | ||
| 599 | unlink($tmpf) || | ||
| 600 | warn "Could not unlink $tmpf: $!\n"; | ||
| 601 | } | ||
| 602 | return &resetsign; | ||
| 603 | } | ||
| 604 | |||
| 605 | $linp=length($inp); | ||
| 606 | |||
| 607 | last unless $linp || $ldoc; | ||
| 608 | $ldoc = 0; | ||
| 609 | |||
| 610 | $xfbytes += $linp; | ||
| 611 | $doclen += $linp; | ||
| 612 | |||
| 613 | if ($verbose>0 && time-$start>5) { | ||
| 614 | # Write progress info ... | ||
| 615 | if ($wantbytes) { | ||
| 616 | $progress = sprintf " %3d%%", $doclen/$wantbytes*100; | ||
| 617 | } else { | ||
| 618 | $progress = sprintf " %d", $doclen; | ||
| 619 | } | ||
| 620 | print STDERR $progress, "\ch"x(length($progress)); | ||
| 621 | # ...every 5 seconds | ||
| 622 | $start=time; | ||
| 623 | } | ||
| 624 | |||
| 625 | $document.=$inp; | ||
| 626 | |||
| 627 | if ($save) { | ||
| 628 | $err = print SAVE $document; | ||
| 629 | die "Error writing $tmpf: $!\n" unless $err; | ||
| 630 | $document=''; | ||
| 631 | } | ||
| 632 | |||
| 633 | # The eof test seems to work very oddly for sockets. | ||
| 634 | # last if eof(FS); | ||
| 635 | } | ||
| 636 | |||
| 637 | close(FS); # Close socket completely | ||
| 638 | |||
| 639 | print STDERR "DOCUMENT:\n----\n",$document,"\n----\n" if $debug>=255; | ||
| 640 | |||
| 641 | if ($wantbytes && | ||
| 642 | $wantbytes != $doclen) { | ||
| 643 | $result=100; | ||
| 644 | $restext='transfer error; too many bytes in document'; | ||
| 645 | $restext='document was incomplete' if ($wantbytes > $doclen) ; | ||
| 646 | print STDERR "SHORT DOCUMENT" if $debug>=16; | ||
| 647 | if ($save) { | ||
| 648 | unlink($tmpf) || warn "Could not unlink $tmpf: $!\n"; | ||
| 649 | } | ||
| 650 | return &resetsign; | ||
| 651 | } | ||
| 652 | |||
| 653 | # warn "XFB: $xfbytes, DL: $doclen\n"; | ||
| 654 | if ($save) { | ||
| 655 | close(SAVE); | ||
| 656 | &main::movefile($tmpf,$saveto); | ||
| 657 | } | ||
| 658 | |||
| 659 | # If this is a non-encoded text file and we're supposed to convert | ||
| 660 | # foreign newlines then we do it. It would be faster to do this | ||
| 661 | # with each chunk of input in the input loop, but this gives us | ||
| 662 | # two problems: | ||
| 663 | # - A \r\n newline could be split into two chunks. Thus escaping | ||
| 664 | # newline conversion. | ||
| 665 | # - It messes up the received bytes accounting rather badly. | ||
| 666 | # | ||
| 667 | # This used to be a test for $plaintext, the problem is that too | ||
| 668 | # many documents were typed as text/plain and so we corrupted | ||
| 669 | # binary files. This is bad. So now we're more paranoid about it: | ||
| 670 | # Only HTML gets converted. | ||
| 671 | if ($convert && $plaintexthtml) { | ||
| 672 | # Change non unix newlines to unix newlines. bare \r is known | ||
| 673 | # from macintosh (they hadta be different didn't they?), \r\n is | ||
| 674 | # known as 'network format' and from numerous systems, among | ||
| 675 | # them ms-dos. | ||
| 676 | $document =~ s~\r~\n~g unless $document =~ s~\r\n~\n~g; | ||
| 677 | warn "Newlines converted(?)\n" if $debug; | ||
| 678 | } | ||
| 679 | |||
| 680 | } # if $result == 200 | ||
| 681 | |||
| 682 | &resetsign; | ||
| 683 | return 1; | ||
| 684 | } | ||
| 685 | |||
| 686 | |||
| 687 | sub analyze_header { | ||
| 688 | my($header)=@_; | ||
| 689 | my($result,$restext,%headval,$hdln,$key,$value); | ||
| 690 | |||
| 691 | # Summary of the http spec on headers (with my comments): | ||
| 692 | # - Each header line ends in CRLF (or just LF, or maybe even just CR, | ||
| 693 | # anyways, it's easier if all is LF). | ||
| 694 | $header =~ s/\r/\n/mg unless $header =~ s/\r\n/\n/mg; | ||
| 695 | # - If a line starts with space then it's a continuation of the previous | ||
| 696 | # line (these I fold into one line). | ||
| 697 | $header =~ s/\n\s/ /mg; | ||
| 698 | # - The header field names are case insensitive (so I convert them to | ||
| 699 | # lowercase) | ||
| 700 | # - A field may appear twice, that is equivalent to listing the values | ||
| 701 | # in a comma separated list (so I fold them into a comma separated list)n | ||
| 702 | # - The field name and the field value are separated by ': ' | ||
| 703 | ($result,$restext) = $header =~ m~^HTTP/\d\.\d (\d\d\d) (.*)~; | ||
| 704 | # Shave off http result code from the header | ||
| 705 | $header =~ s~^.*\n~~; | ||
| 706 | |||
| 707 | warn "Header:\n$header\n---\n" if $debug>=3; | ||
| 708 | |||
| 709 | warn "Result: $result, Text: $restext\n" if $debug>=2; | ||
| 710 | |||
| 711 | %headval=(); | ||
| 712 | |||
| 713 | foreach $hdln (split(/\r?\n/m,$header)) { | ||
| 714 | ($key,$value)=split(': ',$hdln,2); | ||
| 715 | $key="\L$key"; | ||
| 716 | # Strip leading&trailing space off the reply, some servers use | ||
| 717 | # copious space after. | ||
| 718 | $value =~ s/^\s+|\s+$//g; | ||
| 719 | print STDERR "K: '$key', V: '$value'\n" if $debug>=2; | ||
| 720 | if (defined($headval{$key})) { | ||
| 721 | $headval{$key}.=", ".$value; | ||
| 722 | } else { | ||
| 723 | $headval{$key}=$value; | ||
| 724 | } | ||
| 725 | } | ||
| 726 | |||
| 727 | # See if there are any type parameters in the content-type header | ||
| 728 | # and if so remove them. | ||
| 729 | if (defined($headval{'content-type'})) { | ||
| 730 | my $val=$headval{'content-type'}; | ||
| 731 | ($val,undef)=split(';',$val,2) if ($val =~ /;/); | ||
| 732 | $headval{'CONTENT-TYPE'}=$val; | ||
| 733 | } | ||
| 734 | |||
| 735 | return ($result,$restext,%headval); | ||
| 736 | } | ||
| 737 | |||
| 738 | |||
| 739 | sub last_modified { | ||
| 740 | # will return the last modified time for a local file as a HTTP | ||
| 741 | # timestamp. | ||
| 742 | |||
| 743 | my(@tmp) = stat($_[0]); # file doesn't exist ok to fetch | ||
| 744 | |||
| 745 | # FAT file systems strip the LSB of the file time. Add it back in | ||
| 746 | # here before asking the server about a modified file. The only way | ||
| 747 | # this can fail is if the newer server file was saved one second | ||
| 748 | # after the first version (very unlikely). This isn't needed for | ||
| 749 | # NTFS file systems, but there is no good portable Perl way to | ||
| 750 | # determine the file system type. | ||
| 751 | $tmp[9] = $tmp[9] | 1 if ( $main::win32 ); | ||
| 752 | |||
| 753 | # now we got the last modified in a 32 bit integer. time to convert | ||
| 754 | # it and return | ||
| 755 | return time2str($tmp[9]); | ||
| 756 | } | ||
| 757 | |||
| 758 | |||
| 759 | sub timeout { | ||
| 760 | # Set timeout flag. The using procedure has to set other result codes. | ||
| 761 | $chime=1; # When this is 1 then the alarm has gone off | ||
| 762 | print STDERR "TIMEOUT!!!!\n" if $debug>=16; | ||
| 763 | } | ||
| 764 | |||
| 765 | |||
| 766 | sub ignore { | ||
| 767 | warn "I got SIGPIPE, ignoring it...\n"; | ||
| 768 | } | ||
| 769 | |||
| 770 | |||
| 771 | sub resetsign { | ||
| 772 | return 0 if !defined($savALRM); | ||
| 773 | $SIG{'ALRM'}=$savALRM; | ||
| 774 | undef $savALRM; | ||
| 775 | # $SIG{'PIPE'}=$savPIPE; | ||
| 776 | return 0; | ||
| 777 | } | ||
| 778 | |||
| 779 | |||
| 780 | sub oserror { | ||
| 781 | |||
| 782 | resetsign; | ||
| 783 | |||
| 784 | $result=98; | ||
| 785 | $restext='w3http: OS error'; | ||
| 786 | return 0; | ||
| 787 | |||
| 788 | } | ||
| 789 | |||
| 790 | |||
| 791 | 1; |