File: ForkHTTPClient.pm

package info (click to toggle)
sitescooper 3.1.2-1
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 3,000 kB
  • ctags: 662
  • sloc: perl: 8,677; makefile: 105
file content (492 lines) | stat: -rw-r--r-- 12,250 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#===========================================================================

package Sitescooper::ForkHTTPClient;

use strict;

require Exporter;
use Carp;
use Socket;
use FileHandle;
use IO::Handle;
use IO::Select;

use HTTP::Response;
use HTTP::Headers;

use Sitescooper::HTTPClient;
use Sitescooper::ForkedPasswordAsker;

use vars qw(
	$STATE_BUSY $STATE_READY $DBG_PROTOCOL
	@ISA @EXPORT $VERSION
	);

# $DBG_PROTOCOL	= 1;

@ISA = qw(Sitescooper::HTTPClient);
@EXPORT= qw();
$VERSION = "0.1";
sub Version { $VERSION; }

$STATE_READY    = 1;
$STATE_BUSY     = 2;

sub new {
  my $class = shift; $class = ref($class) || $class;
  my $scoop = shift;
  croak "scoop not defd" unless defined ($scoop);
  my $num_procs = shift;

  my $self = {
    'scoop'		=> $scoop,
    'num'		=> $num_procs,
    'loaders'		=> [ ],
  };

  $self->{DBG_PROT} = $DBG_PROTOCOL;

  bless ($self, $class);
  $self;
}

sub init {
  my ($self) = @_;

  my $i;
  for ($i = 0; $i < $self->{num}; $i++)
  {
    # set up the pipes using socketpair(). Phew, for a moment
    # I thought I'd have to hack IPC::Open3 ;)
    #
    my $fh = new FileHandle;
    socketpair($fh, FORKHTTP_PARENT, AF_UNIX,
    					SOCK_STREAM, PF_UNSPEC)
			        or  die "socketpair: $!";

    my $pid = fork;
    if ($pid == 0) {
      FORKHTTP_PARENT->autoflush(1);
      close $fh;
      $self->run();

    } else {
      die "cannot fork: $!" unless defined $pid;
      $self->{scoop}->dbg ("started HTTP handling process $pid");
      close FORKHTTP_PARENT;

      my $ldr = { };
      $ldr->{pid} = $pid;

      $fh->autoflush(1);
      $ldr->{fh} = $fh;

      $ldr->{state} = $STATE_READY;
      ${$self->{loaders}}[$i] = $ldr;
      $self->test_loader ($fh);
    }
  }
}

sub DESTROY {
  my $self = shift;

  if ($#{$self->{loaders}} < 0) {
    return;	# a subprocess

  } else {
    my $i;
    for ($i = 0; $i < $self->{num}; $i++) {
      my $ldr = ${$self->{loaders}}[$i];
      my $fh = $ldr->{fh};
      print $fh "QUIT\n";
      my $resp = <$fh>;
    }
  }
}

sub get_max_active_requests {
  my ($self) = @_;
  $self->{num};
}

sub can_preload {
  my ($self) = @_;
  1;
}

sub test_loader {
  my ($self, $fh) = @_;

  my $str = "TEST\n";
  print $fh $str;
  $self->{DBG_PROT} and $self->{scoop}->dbg (">".$str);

  $_ = <$fh>;
  $self->{DBG_PROT} and $self->{scoop}->dbg ("<".$_);
  /^200 test ok/ or die ("bad response from httpclient to TEST: $_");
}

# ===========================================================================
# HTTPClient client (sitescooper process) code:

sub start_get {
  my ($self, $ref, $url, $lastmod, $is_dynamic) = @_;

  my $i;
  for ($i = 0; $i < $self->{num}; $i++)
  {
    next unless (${$self->{loaders}}[$i]->{state} ==
    			$STATE_READY);
    my $ldr = ${$self->{loaders}}[$i];
    $self->start_httpclient_with_loader ($ldr, $ref, $url, $lastmod, $is_dynamic);

    my $state = new Sitescooper::HTTPRequestState();
    $state->{loader} = $ldr;
    return $state;
  }
  undef;
}

sub start_httpclient_with_loader {
  my ($self, $ldr, $ref, $url, $lastmod, $is_dynamic) = @_;

  my $outfh = $ldr->{fh};
  my $infh = $ldr->{fh};
  $lastmod ||= "";

  my $str = "GET <referrer>$ref</referrer> <url>$url</url> ".
  		"<lastmod>$lastmod</lastmod> <isdyn>$is_dynamic</isdyn>\n";
  syswrite $outfh, $str, length $str;
  $self->{DBG_PROT} and $self->{scoop}->dbg (">".$str);

  $_ = getline ($infh);
  $self->{DBG_PROT} and $self->{scoop}->dbg ("<".$_);

  /^300 / or die ("bad response from httpclient after GET: $_");
  $ldr->{state} = $STATE_BUSY;
  1;
}

sub get_waiting_fh {
  my ($self, $state) = @_;
  my $ldr = $state->{loader};

  # if it's not in the BUSY state, something's gone wrong
  if ($ldr->{state} != $STATE_BUSY) {
    croak "loader not in STATE_BUSY";
  }
  $ldr->{fh};
}

sub ready_to_finish {
  my ($self, $state) = @_;
  my $ldr = $state->{loader};

  # if it's not in the BUSY state, something's gone wrong
  if ($ldr->{state} != $STATE_BUSY) {
    croak "loader not in STATE_BUSY";
  }

  my $sel = new IO::Select();
  $sel->add ($ldr->{fh});
  my @ready;
  if ((@ready = $sel->can_read (0.0)) && $#ready >= 0) { return 1; }
  0;
}

sub finish_get {
  my ($self, $state) = @_;

  my $ldr = $state->{loader};
  if ($ldr->{state} != $STATE_BUSY) {
    croak "loader not in STATE_BUSY";
  }
  my $resp = $self->finish_httpclient_for_loader ($ldr);
  $ldr->{state} = $STATE_READY;
  $resp;
}

sub finish_httpclient_for_loader {
  my ($self, $ldr) = @_;
  local ($_);
  my $infh = $ldr->{fh};
  my $outfh = $ldr->{fh};

  $_ = getline ($infh);
  $self->{DBG_PROT} and $self->{scoop}->dbg ("<".$_);
  if (s/^301 Need-Credentials://) {
    my $realm = undef;
    my $uri = undef;
    my $proxy = undef;

    s,<realm>(.*)</realm>,,g and $realm = $1;
    s,<uri>(.*)</uri>,,g and $uri = $1;
    s,<proxy>(.*)</proxy>,,g and $proxy = $1;

    if (!defined $realm || !defined $uri || !defined $proxy) {
      die ("bad response from httpclient in Need-Creds: $_");
    }

    my ($user, $pass) = 
    	$self->{scoop}->{useragent}->get_basic_credentials($realm, $uri, $proxy);
    my $str = "Provide-Credentials <user>$user</user> <pass>$pass</pass>\n";
    syswrite $outfh, $str, length $str;
    $_ = getline ($infh);
    $self->{DBG_PROT} and $self->{scoop}->dbg ("<".$_);
    /^302 / or die ("bad response from httpclient after Prov-Creds: $_");
    $ldr->{state} = $STATE_BUSY;
    return;
    # back to the select() call...

  } elsif (s/^201 //) {
    $_ = getline ($infh);
    $self->{DBG_PROT} and $self->{scoop}->dbg ("<".$_);
    /^Preload-length: (\d+)/ or
    	die ("bad response from httpclient, no Preload-length header: $_");
    my $len = $1+0;
    my $got = 0;
    my $respdata;
    while ($got < $len) {
      $got += sysread ($infh, $respdata, $len, $got);
    }
    $_ = getline ($infh);
    $self->{DBG_PROT} and $self->{scoop}->dbg ("<".$_);
    /^200 / or die ("bad response from httpclient, no 200: $_");
    return $self->data_to_response ($respdata);

  } else {
    die ("bad response from httpclient: $_");
  }
}

sub data_to_response {
  my ($self, $data) = @_;
  local ($_);

  my ($rc, $msg, %hdrs);

  $data =~ s/^(\d+) (.*?)\n//s;
  $rc = $1; $msg = $2;

  %hdrs = ();
  my $lasthdrname = "";
  while ($data =~ s/^(.*?)\n//s) {
    $_ = $1;
    /^\r?$/ and last;
    s/\r?$//;
    if (/^\s/) {
      $hdrs{$lasthdrname} .= $_; next;
    }
    /^(\S+): (.*)/ or die "bad HTTP hdr \"$_\"\n";

    $hdrs{$1} = $2; $lasthdrname = $1;
  }

  if (!defined $rc || !defined $msg || !defined $data)
  {
    die ("bad http message from httpclient: $data");
  }

  if ($rc == 504 && $msg == 'HTTPClient timed out') {
    return undef;		# timed out!
  }

  my $header = new HTTP::Headers;
  $header->header (%hdrs);
  my $resp = new HTTP::Response ($rc, $msg, $header, $data);
  $resp;
}

# ===========================================================================
# HTTPClient server (subprocess) code:

sub run {
  my ($self) = @_;
  local ($_);
  my $sofar = 0;

  my $asker = new Sitescooper::ForkedPasswordAsker();
  $self->{scoop}->{useragent}->set_password_asker ($asker);

  # we have to use sysread here, as we can't use <FORKHTTP_PARENT>
  # to read; buffering will screw up the IPC. TODO: write an efficient
  # fgets()-style read interface here :(
  while (defined ($_ = getline (\*FORKHTTP_PARENT))) {
    $self->{DBG_PROT} and $self->{scoop}->dbg ("handler$$> $_");

    if (/^TEST$/) {
      my $str = "200 test ok\n";
      syswrite FORKHTTP_PARENT, $str, length $str;

    } elsif (s/^GET //) {
      my ($referrer, $url, $lastmod, $isdyn);

      s,<referrer>(.*)</referrer>,,g and $referrer = $1;
      s,<url>(.*)</url>,,g and $url = $1;
      s,<lastmod>(.*)</lastmod>,,g and $lastmod = $1;
      s,<isdyn>(.*)</isdyn>,,g and $isdyn = $1;
      if (defined $lastmod && $lastmod eq '') { $lastmod = undef; }
      
      if (!defined $url) {
	$self->reply_with_err (401, "missing parameter",
		"A parameter was missing from GET request");
	next;
      }
      my $req = Sitescooper::LWPHTTPClient::make_http_request
					  ($referrer, $url, $lastmod, $isdyn);

      # use syswrite so the write is definitely not buffered; otherwise
      # if the URL is a file etc. this write will get mingled with the
      # 201 got message.
      my $str = "300 starting\n";
      syswrite FORKHTTP_PARENT, $str, length $str;

      my $timeout = 10;
      my $resp = Sitescooper::LWPHTTPClient::invoke_http_request
      						($req, $timeout);

      if (!defined $resp) {
	$self->timed_out ();
      }

      $self->reply_with_got ($resp->status_line . "\n".
		  $resp->headers_as_string . "\n". $resp->content);

    } elsif (s/^SETCRED //) {
      my $realm = undef;
      my $user = undef;
      my $pass = undef;

      s,<realm>(.*)</realm>,,g and $realm = $1;
      s,<user>(.*)</user>,,g and $user = $1;
      s,<pass>(.*)</pass>,,g and $pass = $1;

      if (!defined $realm || !defined $user || !defined $pass) {
	$self->reply_with_err (401, "missing parameter",
		"A parameter was missing from SETCRED request");
	next;
      }
      $self->{scoop}->{useragent}->set_credential ($realm, $user, $pass);

    } elsif (s/^CLRCRED //) {
      my $realm = undef;

      s,<realm>(.*)</realm>,,g and $realm = $1;
      
      if (!defined $realm) {
	$self->reply_with_err (401, "missing parameter",
		"A parameter was missing from CLRCRED request");
	next;
      }
      $self->{scoop}->{useragent}->clear_credential ($realm);

    } elsif (s/^QUIT//) {
      my $str = "200 bye then\n";
      syswrite FORKHTTP_PARENT, $str, length $str;
      exit;

    } else {
      warn "HTTPClient: got funny line: $_"; next;
    }
  }
  die "HTTP handler $$: sitescooper process closed connection\n";
}

sub timed_out {
  my ($self) = @_;

  $self->reply_with_got ("504 HTTPClient timed out\n\n".
	"<html><head><title>504 HTTPClient timed out</title></head>".
	"<body><h1>504 HTTPClient timed out</h1></body></html>\n");
}

sub reply_with_got {
  my ($self, $repl) = @_;

  my $str = "201 got\n".
  	"Preload-length: ".(length $repl)."\n";
  syswrite FORKHTTP_PARENT, $str, length $str;
  syswrite FORKHTTP_PARENT, $repl, length $repl;
  $str = "200 ok\n";
  syswrite FORKHTTP_PARENT, $str, length $str;
  $self->{DBG_PROT} and $self->{scoop}->dbg ("handler$$>".$str.$repl."200 ok\n");
}

sub reply_with_err {
  my ($self, $code, $err, $repl) = @_;

  my $str = "201 $err\n".
  	"Preload-length: ".(length $repl)."\n";
  syswrite (FORKHTTP_PARENT, $str, length($str));
  syswrite (FORKHTTP_PARENT, $repl, length($repl));
  $str = "200 ok\n";
  syswrite (FORKHTTP_PARENT, $str, length($str));
  $self->{DBG_PROT} and $self->{scoop}->dbg ("handler$$>".$str.$repl."200 ok\n");
}

sub getline {
  my ($fh) = @_;
  local ($_);
  my $sofar = 0;

  while (sysread ($fh, $_, 1, $sofar) > 0) {
    if (substr ($_, $sofar, 1) ne "\n") {
      $sofar++;
      next;
    }

    $sofar = 0;
    return $_;
  }

  undef;
}

1;

=pod

=head1 PROTOCOL

The ForkHTTPClient protocol used between the forked httpclient (p)
and the main sitescooper process (s) looks like this:

s: GET <referrer>http://foo</referrer> <url>http://url</url> <lastmod>93458345</lastmod> <isdyn>0</isdyn>
p: 300 starting
... time passes...
p: 201 got
p: Preload-length: [len]
p: [http response including status line and headers]
p: 200 ok

Repeat ad infinitum until (s) closes the connection. The <lastmod>...</lastmod>
parameter is optional.

Also supported:

s: SETCRED <realm>realm</realm> <user>user</user> <pass>pass</pass>
p: 200 credentials set ok
s: CLRCRED <realm>realm</realm>
p: 200 credentials cleared ok

to set credentials before a request is started, and

s: GET <referrer>http://foo</referrer> <url>http://url</url> <lastmod>93458345</lastmod> <isdyn>0</isdyn>
p: 300 starting
... time passes...
p: 301 Need-Credentials: <realm>realm</realm> <uri>uri</uri> <proxy>proxy</proxy>
s: Provide-Credentials <user>user</user> <pass>pass</pass>
p: 302 ok
... more time passes...
p: 201 got
p: Preload-length: [len]
p: [http response including status line and headers]
p: 200 ok

if credentials are called for while a request is in progress.

P can return the usual set of error codes (400-499, 500-599) if
an error condition occurs.

=cut