File: google_multi.pl

package info (click to toggle)
libnet-https-nb-perl 0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 92 kB
  • sloc: perl: 230; makefile: 7
file content (112 lines) | stat: -rwxr-xr-x 2,639 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl

use strict;
use warnings;
use AnyEvent;
use Errno qw/EWOULDBLOCK EAGAIN/;
use lib '../lib';
use Net::HTTPS::NB;

# Get number of the search results for each specified language in parallel via encrypted google
# Make it easier with AnyEvent

my $loop = AnyEvent->condvar;
$loop->begin;

for my $q (qw(perl python ruby php lua)) {
	my $sock = Net::HTTPS::NB->new(Host => 'encrypted.google.com', Blocking => 0)
		or next;
	
	$loop->begin();
	
	my $wc; $wc = AnyEvent->io(
		fh => $sock,
		poll => 'w', # first wait until non-blocking socket connection completed
		cb => sub { wait_connection($wc, $loop, $sock, $q) }
	);
}

$loop->end;
$loop->recv();

# wait until non-blocking connection completed
sub wait_connection {
	undef $_[0]; # remove watcher completely
	my ($wc, $loop, $sock, $q) = @_;
	
	if ($sock->connected) { # handshake completed
		print "$q: Connected\n";
		$sock->write_request(GET => "/search?q=$q");
		my $wh; $wh = AnyEvent->io( # now wait headers
			fh => $sock,
			poll => 'r',
			cb => sub { wait_headers($wh, $loop, $sock, $q) }
		);
	}
	elsif($HTTPS_ERROR == HTTPS_WANT_READ) {
		$wc = AnyEvent->io( # handshake need reading
			fh => $sock,
			poll => 'r',
			cb => sub { wait_connection($wc, $loop, $sock, $q) }
		);
	}
	elsif($HTTPS_ERROR == HTTPS_WANT_WRITE) {
		$wc = AnyEvent->io( # handshake need writing
			fh => $sock,
			poll => 'w',
			cb => sub { wait_connection($wc, $loop, $sock, $q) }
		);
	}
	else {
		print "$q: Connection failed - $HTTPS_ERROR\n";
		$loop->end();
	}
}

# wait for full headers
sub wait_headers {
	my (undef, $loop, $sock, $q) = @_;
	
	if (my @h = $sock->read_response_headers()) {
		undef $_[0]; # remove headers watcher
		
		print "$q: HTTP code - $h[0]\n";
		my $body = '';
		
		unless (wait_body($_, $loop, $sock, $q, \$body)) {
			my $wb; $wb = AnyEvent->io( # now wait body
				fh => $sock,
				poll => 'r',
				cb => sub { wait_body($wb, $loop, $sock, $q, \$body) }
			);
		}
	}
	# else this sub will invoked again when new data will arrive
}

# wait for full body
sub wait_body {
	my (undef, $loop, $sock, $q, $body) = @_;
	
	my ($n, $buf);
	while (1) {
		$n = $sock->read_entity_body($buf, 1024);
		if ($n > 0) {
			substr($$body, length $$body) = $buf; # append body
		}
		elsif ((defined($n) && $n == 0) || (!defined($n) && $! != EWOULDBLOCK && $! != EAGAIN)) {
			# error or eof, but who cares?
			undef $_[0]; # remove body watcher
			my ($result) = $$body =~ /([\d,]+\s+results?)/;
			print "$q: ", $result||'unknown', "\n";
			$loop->end;
			return 1; # body readed
		}
		else {
			# wait more data
			last;
		}
	}
	
	0; # body still not readed
}