Interface.pm

filter: Statementstmt 9.2% 46 / 498 Branchbran 1.3% 3 / 224 Conditioncond 3.1% 1 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 21.9% 9 / 41 Podpod 70.0% 21 / 30 totaltotal 9.4% 80 / 845 CC 131 SCAR 51.0
CC
131
Cov
7%
finish
756.0
use_threads
210.0
thread_then
132.0
CRAP 163.5
↑ Summary UserSearch.pm »
1 # E2::Interface
2 # Jose M. Weeks <jose@joseweeks.com>
3 # 07 August 2003
4 #
5 # See bottom for pod documentation.
6  
7 package E2::Interface;
8  
9 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 our $VERSION = "0.34";
15  
16 # This module also require()s the following modules in the body of
17 # certain methods (threading loads faster this way than with use.
18 # XML::Twig
19 # LWP::UserAgent;
20 # HTTP::Request::Common qw(GET HEAD POST);
21 # HTTP::Cookies;
22 # URI::Escape;
23 # E2::Ticker;
24  
25 # Threading, if supported
26  
27 0 ▶eval "
Subroutine BEGIN: called CC=1 SCAR=1.2
28 use threads;
29 use threads::shared;
30 use Thread::Queue;
31 ";
32 8 our $THREADED = !$@;
33  
34 # Unicode
35  
36 8 ▶eval "
Subroutine BEGIN: called CC=1 SCAR=0.0
37 use Encode;
38 ";
39 8 our $ENCODED = !$@;
40  
41 8 our $DEBUG = 0; # Debug info: set to 1 for basic debug info,
42 # 2 to add a message for each sub,
43 # 3 to add data dumping
44  
45 # Get OS string
46  
47 8 our $OS_STRING;
48  
49 8 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=28.2
50 8 ▶ if( -x '/bin/uname' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x '/bin/uname') { } 8 0 8
51 8 $OS_STRING = `/bin/uname -srmo`;
52 8 chomp( $OS_STRING );
53 } else {
54 0 $OS_STRING = $^O;
55 0 ▶ if( $OS_STRING eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($E2::Interface::OS_STRING eq "MSWin32") 0 0 0
56 0 my $s;
57 0 eval "use Win32";
58 0 ▶ if( !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
59 0 $s = join ' ', &Win32::GetOSName;
60 }
61  
62 0 ▶ $OS_STRING = $s if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 0 0
63 }
64 }
65 }
66  
67 sub new;
68 sub clone;
69  
70 sub login;
71 sub verify_login;
72 sub logout;
73 sub process_request;
74  
75 sub domain;
76 sub cookie;
77 sub parse_links;
78 sub document;
79 sub logged_in;
80 sub agentstring;
81  
82 sub version;
83 sub client_name;
84 sub debug;
85  
86 sub this_username;
87 sub this_user_id;
88  
89 sub decode_xml;
90  
91 sub use_threads;
92 sub job_id;
93 sub thread_then;
94 sub finish;
95  
96 # Private
97  
98 sub start_job;
99 sub extract_cookie;
100 sub post_process;
101 sub process_request_raw;
102  
103 ################################################################################
104 # Class methods
105 ################################################################################
106  
107 0 ▶sub version { return $VERSION }
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
108 0 ▶sub client_name { return "e2interface-perl" }
Subroutine client_name: not called CC=1 SCAR=6.9
Pod documented
109 0 ▶sub decode_xml {
Subroutine decode_xml: not called CC=3 SCAR=24.8
Pod undocumented
110 0 my( undef, $s ) = @_;
111 0 ▶ return $s if !$ENCODED;
Branch: 1 branch
BranchTrueFalseTotal
unless $E2::Interface::ENCODED 0 0 0
112 0 ▶ return decode_utf8($s) || $s;
Condition0%0/3
&decode_utf8($s) || $s
l!l&&r!l&&!r
000
MC/DC0%0/2
&decode_utf8($s) || $s
&decode_utf8($s) $s
Truth table0%0/3
A || B
A= &decode_utf8($s)B= $s
ABresult
1X1
011
000
113 }
114  
115 0 ▶sub debug {
Subroutine debug: not called CC=4 SCAR=30.0
Pod documented
116 0 my (undef, $d) = @_;
117  
118 0 ▶ if( $d && !$DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d and not $E2::Interface::DEBUG) 0 0 0
Condition0%0/3
$d and not $E2::Interface::DEBUG
!ll&&!rl&&r
000
MC/DC0%0/2
$d and not $E2::Interface::DEBUG
$d not $E2::Interface::DEBUG
Truth table0%0/3
A and not(B)
A= $dB= not $E2::Interface::DEBUG
ABresult
0X0
100
111
119  
120 # Print e2interface info
121  
122 0 print '-' x 80 . "\n";
123 0 print &client_name . '/' . &version .
124 " by Jose M. Weeks <jose\@joseweeks.com> (Simpleton)\n";
125 0 printf "Perl v%vd", $^V;
126 0 ▶ print "; $OS_STRING;" . ' Threads ' .
Branch: 1 branch
BranchTrueFalseTotal
$E2::Interface::THREADED ? : 0 0 0
127 ($THREADED ? '' : 'UN' ) . "AVAILABLE\n";
128 0 print '-' x 80 . "\n";
129 }
130  
131 0 $DEBUG = $d;
132 }
133  
134 6 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
135 6 my $arg = shift;
136 6 ▶ my $class = ref( $arg ) || $arg;
Condition33%1/3
ref $arg || $arg
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $arg || $arg
ref $arg $arg
Truth table33%1/3
A || B
A= ref $argB= $arg
ABresult
1X1
011
000
137 6 my $self = {};
138  
139 6 ▶ warn "Creating $class object" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 6 6
140  
141 # All of these are references so that we can clone()
142 # copies and any changes after the cloning affect all
143 # clones.
144  
145 6 $self->{this_username} = \(my $a = 'Guest User');
146 6 $self->{this_user_id} = \(my $b);
147
148 6 $self->{agentstring} = \(my $c);
149 6 $self->{cookie} = \(my $d);
150  
151 6 $self->{parse_links} = \(my $e);
152 6 $self->{domain} = \(my $f = "everything2.com" );
153  
154 6 $self->{threads} = \(my $ta);
155 6 $self->{next_job_id} = \(my $tb = 1);
156 6 $self->{job_to_thread} = \(my $tc);
157 6 $self->{post_commands} = \(my $td);
158 6 $self->{final_commands} = \(my $te);
159 6 $self->{finished} = \(my $tf);
160
161 6 return bless $self, $class;
162 }
163  
164 ################################################################################
165 # Object Methods
166 ################################################################################
167  
168 0 ▶sub clone {
Subroutine clone: not called CC=4 SCAR=30.0
Pod documented
169 0 ▶ my $self = shift or croak "Usage: clone E2INTERFACE_DEST, E2INTERFACE_SRC";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: clone E2INTERFACE_DEST, E2INTERFACE_SRC") 0 0 0
170 0 ▶ my $src = shift or croak "Usage: clone E2INTERFACE_DEST, E2INTERFACE_SRC";
Branch: 1 branch
BranchTrueFalseTotal
my $src = shift() or croak("Usage: clone E2INTERFACE_DEST, E2INTERFACE_SRC") 0 0 0
171  
172 0 ▶ warn "E2::Interface::clone\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
173  
174 0 $self->{agentstring} = $src->{agentstring};
175 0 $self->{this_username} = $src->{this_username};
176 0 $self->{this_user_id} = $src->{this_user_id};
177 0 $self->{parse_links} = $src->{parse_links};
178 0 $self->{domain} = $src->{domain};
179 0 $self->{cookie} = $src->{cookie};
180 0 $self->{threads} = $src->{threads};
181 0 $self->{next_job_id} = $src->{next_job_id};
182 0 $self->{job_to_thread} = $src->{job_to_thread};
183 0 $self->{post_commands} = $src->{post_commands};
184 0 $self->{final_commands} = $src->{final_commands};
185 0 $self->{finished} = $src->{finished};
186
187 0 return $self;
188 }
189  
190 0 ▶sub login {
Subroutine login: not called CC=5 SCAR=34.0
Pod documented
191 0 ▶ my $self = shift or croak( "Usage: login E2INTERFACE, USERNAME, PASSWORD" );
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: login E2INTERFACE, USERNAME, PASSWORD") 0 0 0
192 0 ▶ my $username = shift or croak( "Usage: login E2INTERFACE, USERNAME, PASSWORD" );
Branch: 1 branch
BranchTrueFalseTotal
my $username = shift() or croak("Usage: login E2INTERFACE, USERNAME, PASSWORD") 0 0 0
193 0 ▶ my $password = shift or croak( "Usage: login E2INTERFACE, USERNAME, PASSWORD" );
Branch: 1 branch
BranchTrueFalseTotal
my $password = shift() or croak("Usage: login E2INTERFACE, USERNAME, PASSWORD") 0 0 0
194  
195 0 ▶ warn "E2::Interface::login\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
196  
197 0 require E2::Ticker;
198  
199 return $self->thread_then(
200 [
201 \&process_request,
202 $self,
203 op => 'login',
204 user => $username,
205 passwd => $password,
206 node => $E2::Ticker::xml_title{session}
207 ],
208 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
209 0 my $xml = shift;
210  
211 0 ▶ if( $xml =~ /<currentuser .*?user_id="(.*?)".*?>(.*?)</s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ /<currentuser .*?user_id="(.*?)".*?>(.*?)</s) { } 0 0 0
212 0 ${$self->{this_username}} = $2;
213 0 ${$self->{this_user_id}} = $1;
214 } else {
215 0 croak "Invalid document";
216 }
217  
218 0 ▶ return $self->cookie && 1;
Condition0%0/2
$self->cookie && 1
!ll
00
MC/DC0%0/1
$self->cookie && 1
$self->cookie
Truth table0%0/2
A
A= $self->cookie
Aresult
00
11
219 0 });
220 }
221  
222 0 ▶sub verify_login {
Subroutine verify_login: not called CC=3 SCAR=24.8
Pod documented
223 0 my $self = shift;
224
225 0 require E2::Ticker;
226  
227 0 ▶ warn "E2::Interface::verify_login\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
228  
229 0 ▶ return undef if !$self->logged_in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 0 0
230  
231 return $self->thread_then(
232 [
233 \&process_request,
234 $self,
235 node => $E2::Ticker::xml_title{session}
236 ],
237 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
238 0 my $xml = shift;
239
240 0 ▶ if( $xml =~ /<currentuser .*?user_id="(.*?)".*?>(.*?)</s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ /<currentuser .*?user_id="(.*?)".*?>(.*?)</s) { } 0 0 0
241 0 ${$self->{this_username}} = $2;
242 0 ${$self->{this_user_id}} = $1;
243 } else {
244 0 croak "Invalid document";
245 }
246  
247 0 ▶ return $self->cookie && 1;
Condition0%0/2
$self->cookie && 1
!ll
00
MC/DC0%0/1
$self->cookie && 1
$self->cookie
Truth table0%0/2
A
A= $self->cookie
Aresult
00
11
248 0 });
249 }
250  
251 0 ▶sub logout {
Subroutine logout: not called CC=3 SCAR=24.8
Pod documented
252 0 ▶ my $self = shift or croak "Usage: logout E2INTERFACE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: logout E2INTERFACE") 0 0 0
253  
254 0 ▶ warn "E2::Interface::logout\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
255  
256 0 $self->cookie( undef );
257 0 ${$self->{this_username}} = 'Guest User';
258 0 ${$self->{this_user_id}} = undef;
259  
260 0 return 1;
261 }
262  
263 0 ▶sub process_request {
Subroutine process_request: not called CC=8 SCAR=42.8
Pod documented
264 0 ▶ my $self = shift
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: process_request E2INTERFACE, [ ATTR => VAL [ , ATTR2 => VAL2 , ... ] ]") 0 0 0
265 or croak "Usage: process_request E2INTERFACE, [ ATTR => VAL [ , ATTR2 => VAL2 , ... ] ]";
266 0 ▶ my %pairs = @_
Branch: 1 branch
BranchTrueFalseTotal
my(%pairs) = or croak("Usage: process_request E2INTERFACE, [ ATTR => VAL [ , ATTR2 => VAL2 , ... ] ]") 0 0 0
267 or croak "Usage: process_request E2INTERFACE, [ ATTR => VAL [ , ATTR2 => VAL2 , ... ] ]";
268  
269 0 ▶ warn "E2::Interface::process_request\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
270  
271 # If we're dealing with threads, send a process_request message
272  
273 0 ▶ if( ${$self->{threads}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->{"threads"};}) 0 0 0
274 return $self->start_job(
275 'POST',
276 'http://' . $self->domain . '/',
277 $self->cookie,
278 0 ▶ ${$self->{agentstring}},
Branch: 1 branch
BranchTrueFalseTotal
$self->parse_links ? : 0 0 0
279 ($self->parse_links ? () : (links_noparse => 1)),
280 %pairs
281 );
282 }
283  
284 # Otherwise, just process the request
285  
286 my $response = process_request_raw(
287 'POST',
288 'http://' . $self->domain . '/',
289 $self->cookie,
290 0 ▶ ${$self->{agentstring}},
Branch: 1 branch
BranchTrueFalseTotal
$self->parse_links ? : 0 0 0
291 ($self->parse_links?():(links_noparse => 1)),
292 %pairs
293 );
294  
295 0 my $c = extract_cookie( $response );
296 0 ▶ $self->cookie( $c ) if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
297  
298 0 return $self->{last_document} = post_process( $response );
299 }
300  
301 0 ▶sub this_username {
Subroutine this_username: not called CC=2 SCAR=17.9
Pod documented
302 0 ▶ my $self = shift or croak "Usage: this_username E2INTERFACE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: this_username E2INTERFACE") 0 0 0
303 0 return ${$self->{this_username}};
304 }
305  
306 0 ▶sub this_user_id {
Subroutine this_user_id: not called CC=2 SCAR=17.9
Pod documented
307 0 ▶ my $self = shift or croak "Usage: this_user_id E2INTERFACE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: this_user_id E2INTERFACE") 0 0 0
308 0 return ${$self->{this_user_id}};
309 }
310  
311 0 ▶sub logged_in {
Subroutine logged_in: not called CC=3 SCAR=24.8
Pod documented
312 0 ▶ my $self = shift or croak "Usage: logged_in E2INTERFACE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: logged_in E2INTERFACE") 0 0 0
313  
314 0 ▶ return ${$self->{cookie}} && 1;
Condition0%0/2
${$self->{'cookie'};} && 1
!ll
00
MC/DC0%0/1
${$self->{'cookie'};} && 1
${$self->{'cookie'};}
Truth table0%0/2
A
A= ${$self->{'cookie'};}
Aresult
00
11
315 }
316  
317 0 ▶sub domain {
Subroutine domain: not called CC=3 SCAR=24.8
Pod documented
318 0 ▶ my $self = shift or croak "Usage: domain E2INTERFACE [, DOMAIN ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: domain E2INTERFACE [, DOMAIN ]") 0 0 0
319
320 0 ▶ ${$self->{domain}} = $_[0] if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
321
322 0 return ${$self->{domain}};
323 }
324  
325 0 ▶sub cookie {
Subroutine cookie: not called CC=4 SCAR=30.0
Pod documented
326 0 ▶ my $self = shift or croak "Usage: cookie E2INTERFACE [, COOKIE ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: cookie E2INTERFACE [, COOKIE ]") 0 0 0
327  
328 0 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
329 0 ${$self->{cookie}} = $_[0];
330  
331 0 ▶ if( $_[0] =~ /(.*?)%257C/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /(.*?)%257C/) 0 0 0
332 0 ${$self->{this_username}} = $1;
333 }
334 }
335  
336 0 return ${$self->{cookie}};
337 }
338  
339 0 ▶sub agentstring {
Subroutine agentstring: not called CC=3 SCAR=24.8
Pod documented
340 0 ▶ my $self = shift or croak "Usage: agentstring E2INTERFACE [, STRING ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: agentstring E2INTERFACE [, STRING ]") 0 0 0
341  
342 0 ▶ ${$self->{agentstring}} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
343  
344 0 return ${$self->{agentstring}};
345 }
346  
347 0 ▶sub parse_links {
Subroutine parse_links: not called CC=3 SCAR=24.8
Pod undocumented
348 0 ▶ my $self = shift or croak "Usage: parse_links E2INTERFACE [ , BOOL ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: parse_links E2INTERFACE [ , BOOL ]") 0 0 0
349  
350 0 ▶ ${$self->{parse_links}} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
351  
352 0 return ${$self->{parse_links}};
353 }
354  
355 0 ▶sub document {
Subroutine document: not called CC=2 SCAR=17.9
Pod documented
356 0 ▶ my $self = shift or croak "Usage: xml E2INTERFACE";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: xml E2INTERFACE") 0 0 0
357  
358 0 return $self->{last_document};
359 }
360  
361 6 ▶sub parse_twig {
Subroutine parse_twig: called CC=6 SCAR=32.7
Pod undocumented
362 0 ▶ if( @_ != 3 ) { croak "Usage: parse_twig E2INTERFACE, XML, HANDLERS"; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 3) 0 6 6
363 6 my ( $self, $xml, $handlers ) = @_;
364
365 6 require XML::Twig;
366  
367 0 ▶ warn "E2::Interface::parse_twig\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
368  
369 0 my $twig = new XML::Twig(
370 # keep_encoding => 1,
371 twig_handlers => $handlers
372 );
373  
374 # If we're using a version of perl that allows us to do it, make sure
375 # the string is in perl's internal representation, then encode into
376 # UTF8.
377  
378 0 ▶ if( $ENCODED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($E2::Interface::ENCODED) 0 0 0
379 0 ▶ $xml = decode_utf8( $xml ) || $xml;
Condition0%0/3
&decode_utf8($xml) || $xml
l!l&&r!l&&!r
000
MC/DC0%0/2
&decode_utf8($xml) || $xml
&decode_utf8($xml) $xml
Truth table0%0/3
A || B
A= &decode_utf8($xml)B= $xml
ABresult
1X1
011
000
380 0 $xml = encode_utf8( $xml );
381 }
382  
383 0 ▶ if( !$twig->safe_parse( $xml, ProtocolEncoding => 'UTF-8' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $twig->safe_parse($xml, "ProtocolEncoding", "UTF-8")) 0 0 0
384 0 chomp $@;
385 0 croak "Parse error: $@";
386 }
387 }
388  
389 ################################################################################
390 #
391 # Threading in e2interface.
392 #
393 # (The background thread)
394 #
395 # 1: thread_then creates NUM background threads (sub _thread), each
396 # with its own input and output queue
397 # 2: each _thread waits for a two value list on its input queue:
398 # a. job_id
399 # b. reference to a list identical to the parameter list to
400 # process_request_raw
401 # 3: each _thread calls process_request_raw, then calls extract_cookie
402 # and post_process on the response, and returns the following on
403 # it output queue:
404 # a. job_id
405 # b. reference to a hash with the following keys:
406 # exception - exception string: only defined on exception
407 # cookie - the return value of extract_cookie
408 # text - the return value of post_process
409 #
410 # (The main thread) -- (these bubble upward from the lowlevel methods, so
411 # this mainly ordered backward, but follows the return
412 # values upward)
413 #
414 # 1: start_job takes the same parameters as process_request_raw. It
415 # passes this list off to the first convenient background thread
416 # and stores the job_id -> thread mapping. It returns (-1, job_id)
417 # 2: process_request, if threading has been enabled, calls start_job
418 # and returns (-1, job_id)
419 # 3: thread_then takes two code references as parameters. It calls the
420 # first code reference.
421 # a. if this reference returns (-1,job_id), it stores the second
422 # reference to be executed when that first one finishes,
423 # and to be passed its return value as its parameters.
424 # This allows thread_then to be chained, each return value
425 # passed to the next stored code reference.
426 # b. If this reference returns anything else, it passes this
427 # value directly to the second code reference and then
428 # returns the subsequent return value.
429 # in effect, thread_then allows code to be executed regardless of
430 # whether or not it calls a method that gets passed to a background
431 # thread.
432 # 4: finish checks the output queue of the background threads. If
433 # the specified job hasn't finished yet, it returns (-1, job_id).
434 # If it has finished, finish executes any stored code references
435 # (those that thread_then stored). It returns the return value of
436 # the final stored code reference.
437 #
438 ################################################################################
439  
440 0 ▶sub use_threads {
Subroutine use_threads: not called CC=14 SCAR=53.5
Pod documented
441 0 ▶ my $self = shift or croak "Usage: use_threads E2INTERFACE [ COUNT ]";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Usage: use_threads E2INTERFACE [ COUNT ]") 0 0 0
442 0 ▶ my $count = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
443  
444 0 ▶ warn "E2::Interface::use_threads\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
445  
446 0 ▶ if( ! $THREADED ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($E2::Interface::THREADED) 0 0 0
447 0 ▶ warn "Unable to use_threads: ithreads not available" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG 0 0 0
448 0 return undef;
449 }
450  
451 0 ▶ if( $count < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 1) 0 0 0
452 0 ▶ warn "Unable to use_threads: invalid number $count" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG 0 0 0
453 0 return undef;
454 }
455  
456 0 ▶ if( ${$self->{threads}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->{"threads"};}) 0 0 0
457 0 ▶ warn "Unable to use_threads: threads already in use" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG 0 0 0
458 0 return undef;
459 }
460  
461 0 ▶ warn "Threading enabled (using $count thread" .
Branch: 2 branches
BranchTrueFalseTotal
if $E2::Interface::DEBUG 0 0 0
$count > 1 ? : 0 0 0
462 ($count > 1 ? 's' : '') . ")\n" if $DEBUG;
463  
464 0 ${$self->{threads}} = [];
465 0 ▶ for( my $i = 0; $i < $count; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 0 0 0
466 0 my %t = (
467 to_q => Thread::Queue->new,
468 from_q => Thread::Queue->new,
469 );
470
471 $t{thread} = threads->create(
472 \&_thread,
473 $t{to_q},
474 $t{from_q}
475 0 );
476  
477 0 ▶ if( ! $t{thread} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t{"thread"}) 0 0 0
478 0 croak "Unable to create thread";
479 }
480  
481 0 push @{${$self->{threads}}}, \%t;
482 }
483  
484 0 return 1;
485  
486 # _thread( INPUT_QUEUE, OUTPUT_QUEUE )
487  
488 0 ▶ sub _thread {
Subroutine _thread: not called CC=5 SCAR=34.0
489 0 my $from_q = shift;
490 0 my $to_q = shift;
491 0 my $id;
492  
493 0 ▶ warn "Spawned new thread\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG 0 0 0
494  
495 0 ▶ while( $id = $from_q->dequeue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id = $from_q->dequeue) 0 0 0
496 0 my $req = $from_q->dequeue;
497 0 my $resp;
498 0 my %r : shared;
499  
500 0 ▶ warn "Processing job $id" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
501  
502 0 eval { $resp = process_request_raw( @$req ) };
503 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
504 0 $r{exception} = $@;
505 } else {
506 0 $r{cookie} = extract_cookie( $resp );
507 0 $r{text} = post_process( $resp );
508 }
509
510 0 $to_q->enqueue( $id, \%r );
511 }
512 }
513 }
514  
515 0 ▶sub join_threads {
Subroutine join_threads: not called CC=4 SCAR=30.0
Pod documented
516 0 my $self = shift;
517  
518 0 foreach( @{${$self->{threads}}} ) {
519 0 $_->{to_q}->enqueue( 0 );
520 0 $_->{thread}->join;
521 }
522  
523 # Finish the jobs
524  
525 0 my @r; my @i;
526 0 ▶ while( @i = $self->finish ) { push @r, \@i if $i[0] ne "-1" }
Branch: 2 branches
BranchTrueFalseTotal
if (@i = $self->finish) 0 0 0
if $i[0] ne -1 0 0 0
527  
528 # Dismantle the threading
529  
530 0 ${$self->{threads}} = undef;
531 0 ${$self->{next_job_id}} = undef;
532 0 ${$self->{job_to_thread}} = undef;
533 0 ${$self->{post_commands}} = undef;
534 0 ${$self->{final_commands}} = undef;
535 0 ${$self->{finished}} = undef;
536  
537 0 return @r;
538 }
539  
540 0 ▶sub detach_threads {
Subroutine detach_threads: not called CC=4 SCAR=30.0
Pod documented
541 0 my $self = shift;
542
543 0 foreach( @{${$self->{threads}}} ) {
544 0 $_->{to_q}->enqueue( 0 );
545 0 $_->{thread}->detach;
546 }
547  
548 # Finish all jobs that are ready to be finished
549  
550 0 my @r; my @i;
551 0 ▶ while( @i = $self->finish ) { push @r, \@i if $i[0] ne "-1" }
Branch: 2 branches
BranchTrueFalseTotal
if (@i = $self->finish) 0 0 0
if $i[0] ne -1 0 0 0
552  
553 # Dismantle the threading
554  
555 0 ${$self->{threads}} = undef;
556 0 ${$self->{next_job_id}} = undef;
557 0 ${$self->{job_to_thread}} = undef;
558 0 ${$self->{post_commands}} = undef;
559 0 ${$self->{final_commands}} = undef;
560 0 ${$self->{finished}} = undef;
561
562 0 return @r;
563 }
564  
565 0 ▶sub thread_then {
Subroutine thread_then: not called CC=11 SCAR=48.8
Pod documented
566 0 my $self = shift;
567 0 my $cmd = shift;
568 0 my $post = shift;
569 0 my $final = shift;
570  
571 0 ▶ warn "E2::Interface::thread_then\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
572
573 # warn 'Dump of $cmd:' . Dumper( $cmd ) if $DEBUG > 2;
574 0 ▶ warn 'Adding post-command' if $post && $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $post and $E2::Interface::DEBUG > 2 0 0 0
Condition0%0/3
$post and $E2::Interface::DEBUG > 2
!ll&&!rl&&r
000
MC/DC0%0/2
$post and $E2::Interface::DEBUG > 2
$post $E2::Interface::DEBUG > 2
Truth table0%0/3
A and B
A= $postB= $E2::Interface::DEBUG > 2
ABresult
0X0
100
111
575 0 my @response;
576  
577 # Run command. If not threaded, run its post command and
578 # return
579  
580 0 ▶ if( ref $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd) { } 0 0 0
581 0 my $c = shift @$cmd;
582 0 @response = &$c( @$cmd );
583 } else {
584 0 @response = &$cmd( @_ );
585 }
586
587 0 ▶ if( !$response[0] || $response[0] ne "-1" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response[0] or $response[0] ne -1) 0 0 0
Condition0%0/3
not $response[0] or $response[0] ne -1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response[0] or $response[0] ne -1
not $response[0] $response[0] ne -1
Truth table0%0/3
not(A) or B
A= not $response[0]B= $response[0] ne -1
ABresult
1X1
011
000
588 0 my @r = &$post( @response );
589 0 ▶ &$final if $final;
Branch: 1 branch
BranchTrueFalseTotal
if $final 0 0 0
590 0 ▶ return ( @r>1 ? @r : $r[0] );
Branch: 1 branch
BranchTrueFalseTotal
@r > 1 ? : 0 0 0
591 }
592  
593 # If we're here, we called a threaded routine. Add the post
594 # command to its caller's list
595  
596 0 ▶ warn "Job deferred and assigned id $response[1]" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 2 0 0 0
597  
598 0 push @{${$self->{post_commands}}->{$response[1]}}, $post;
599 0 ▶ push @{${$self->{final_commands}}->{$response[1]}}, $final if $final;
Branch: 1 branch
BranchTrueFalseTotal
if $final 0 0 0
600  
601 0 return @response;
602 }
603  
604 0 ▶sub finish {
Subroutine finish: not called CC=27 SCAR=66.3
Pod documented
605 0 my $self = shift;
606 0 my $job = shift;
607  
608 0 my $response;
609  
610 0 ▶ warn "E2::Interface::finish\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
611 0 ▶ warn "Job id = $job" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 2 0 0 0
612  
613 # What we're going to do here is get a $job (if we haven't been passed
614 # one), and get a $response hash for that job. Otherwise, return.
615  
616 # If $job is undefined, find the first finished job and return it
617  
618 0 ▶ if( ! defined $job ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $job) { } 0 0 0
elsif (${$$self{'finished'};}->{$job}) { } 0 0 0
619  
620 # Get it off the list of finished jobs, if possible;
621  
622 0 (my $k) = keys %{${$self->{finished}}};
623 0 ▶ if( $k ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k) { } 0 0 0
624 0 ▶ warn "Job previously finished, returning" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 2 0 0 0
625 0 $job = $k;
626 0 $response = delete ${$self->{finished}}->{$k};
627  
628 # Otherwise, check all the queues for finished jobs
629  
630 } else {
631 0 my $pending = 0; # Count pending jobs, so we know
632 # whether there are any left or not
633
634 0 ▶ for( my $i = 0; $i < @{${$self->{threads}}}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{${$self->{"threads"};};}) 0 0 0
635 0 my $t = ${$self->{threads}}->[$i];
636 0 my $pending += $t->{to_q}->pending;
637 0 my $id = $t->{from_q}->dequeue_nb;
638  
639 0 ▶ if( $id ) { # Got one
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 0 0 0
640 0 $response = $t->{from_q}->dequeue;
641 0 $job = $id;
642 0 last;
643 }
644 }
645  
646 0 ▶ if( ! $response ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 0 0 0
647  
648 # If there are no pending jobs, return a
649 # false value. otherwise, return a
650 # non-specific deferred value
651  
652 0 ▶ if( ! $pending ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pending) 0 0 0
653 0 return ();
654 }
655  
656 0 return (-1, -1);
657 }
658 }
659
660 # Otherwise ($job _is_ defined), so first check to see if
661 # we've already pulled this job off the queue.
662  
663 0 } elsif( ${$self->{finished}}->{$job} ) {
664 0 ▶ warn "Job previously finished, returning" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 2 0 0 0
665 0 $response = ${$self->{finished}}->{$job};
666 0 delete ${$self->{finished}}->{$job};
667  
668 # Otherwise, try to get it off the queue; return a deferred value
669 # if we can't.
670  
671 } else {
672 0 my $thr = ${$self->{job_to_thread}}->{$job};
673
674 0 ▶ warn "Unable to find thread for job $job" if $DEBUG && !$thr;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG and not $thr 0 0 0
Condition0%0/3
$E2::Interface::DEBUG and not $thr
!ll&&!rl&&r
000
MC/DC0%0/2
$E2::Interface::DEBUG and not $thr
$E2::Interface::DEBUG not $thr
Truth table0%0/3
A and not(B)
A= $E2::Interface::DEBUGB= not $thr
ABresult
0X0
100
111
675
676 0 ▶ return () if !$thr;
Branch: 1 branch
BranchTrueFalseTotal
unless $thr 0 0 0
677  
678 0 ▶ while( my $id = $thr->{from_q}->dequeue_nb ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $thr->{"from_q"}->dequeue_nb) 0 0 0
679  
680 # Get response
681  
682 0 my $r = $thr->{from_q}->dequeue;
683
684 0 ▶ warn "Retrieved job $id" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 2 0 0 0
685  
686 0 delete ${$self->{job_to_thread}}->{$id};
687  
688 0 ▶ if( $id == $job ) { # The right job?
Branch: 1 branch
BranchTrueFalseTotal
if ($id == $job) { } 0 0 0
689 0 $response = $r;
690 0 last;
691 } else {
692 # Store for later
693 0 ${$self->{finished}}->{$id} = $r;
694 }
695 }
696
697 # Now, if the job is complete, $response will contain
698 # a value. If it doesn't, return -1 and set job_id
699 # (tell the caller that the command is still deferred).
700  
701 0 ▶ if( ! $response ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 0 0 0
702 0 ▶ warn "Deferring job $job" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 2 0 0 0
703 0 return (-1, $job);
704 }
705 }
706  
707 # At this point, we have a valid $job and $response. Do
708 # post-processing, exception-handling, etc., and return.
709  
710 # If we've received an exception, now is the time to
711 # throw it.
712  
713 0 ▶ if( $response->{exception} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"exception"}) 0 0 0
714
715 # Execute any final commands and clear all commands
716
717 0 foreach( @{${$self->{final_commands}}->{$job}} ) { &$_ }
718 0 delete ${$self->{post_commands}}->{$job};
719 0 delete ${$self->{final_commands}}->{$job};
720
721 # throw
722  
723 0 die $response->{exception};
724 }
725  
726 # Now, finish the command and return
727  
728 0 ▶ $self->cookie( $response->{cookie} ) if $response->{cookie};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"cookie"} 0 0 0
729  
730 # Save document
731  
732 0 $self->{last_document} = $response->{text};
733  
734 # Execute any post code, passing the return values of one
735 # as the parameters of the next
736
737 0 my @param = ( $response->{text} );
738 0 my @ret = ( $response->{text} );
739
740 0 ▶ warn "Executing " . scalar @{${$self->{post_commands}}->{$job}} .
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 2 0 0 0
741 "post-commands" if $DEBUG > 2;
742  
743 0 eval {
744 0 ▶ while( my $c = shift @{${$self->{post_commands}}->{$job}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = shift @{$${$$self{"post_commands"};}{$job};}) 0 0 0
745 0 @ret = &$c( @param );
746 0 @param = @ret;
747 }
748 };
749 0 my $exc = $@;
750
751 # Execute any 'final' commands. These have no return values.
752  
753 0 foreach( @{${$self->{final_commands}}->{$job}} ) { &$_ }
754 0 delete ${$self->{post_commands}}->{$job};
755 0 delete ${$self->{final_commands}}->{$job};
756  
757 # If post-processing threw any exceptions, re-throw them
758  
759 0 ▶ die $exc if $exc;
Branch: 1 branch
BranchTrueFalseTotal
if $exc 0 0 0
760
761 0 return ( $job, @ret );
762 }
763  
764 0 ▶sub start_job {
Subroutine start_job: not called CC=6 SCAR=37.4
Pod undocumented
765 0 my $self = shift;
766  
767 0 ▶ warn "E2::Interface::start_job\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
768
769 # Find the first open thread, or the one with the
770 # least jobs pending.
771  
772 0 my $min = 9999;
773 0 my $thr = ${$self->{threads}}->[0];
774  
775 0 foreach( @{${$self->{threads}}} ) {
776 0 ▶ if( !$_->{to_q}->pending ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $_->{'to_q'}->pending) { } 0 0 0
elsif ($_->{'to_q'}->pending < $min) { } 0 0 0
777 0 $thr = $_;
778 0 last;
779 } elsif( $_->{to_q}->pending < $min ) {
780 0 $min = $_->{to_q}->pending;
781 0 $thr = $_;
782 }
783 }
784  
785 # Send the message
786  
787 0 my $job = ${$self->{next_job_id}}++;
788 0 my @job : shared = @_;
789  
790 0 ▶ warn "Handing $job off to $thr" if $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 2 0 0 0
791  
792 0 $thr->{to_q}->enqueue( $job, \@job );
793  
794 0 ${$self->{job_to_thread}}->{$job} = $thr;
795  
796 0 return (-1, $job);
797 }
798  
799 ################################################################################
800 # Private, non-method subroutines
801 ################################################################################
802  
803 # Usage: my $cookie = extract_cookie( RESPONSE )
804 #
805 # Extracts a cookie from an LWP::UserAgent object.
806  
807 0 ▶sub extract_cookie {
Subroutine extract_cookie: not called CC=5 SCAR=34.0
Pod undocumented
808 0 require HTTP::Cookies;
809  
810 0 my $response = shift;
811 0 my $c = HTTP::Cookies->new;
812  
813 0 ▶ warn "E2::Interface::extract_cookie\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
814
815 0 $c->extract_cookies( $response );
816  
817 # It seems that the cookie value may or may not be surrounded by
818 # quotation marks, so deal with either eventuality.
819  
820 0 $c->as_string =~ /userpass=(.*?);/;
821 0 my $s = $1;
822 0 ▶ $s =~ s/^"(.*)"$/$1/ if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 0 0
823  
824 0 ▶ warn "Cookie found: $s" if $1 && $DEBUG > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $E2::Interface::DEBUG > 2 0 0 0
Condition0%0/3
$1 and $E2::Interface::DEBUG > 2
!ll&&!rl&&r
000
MC/DC0%0/2
$1 and $E2::Interface::DEBUG > 2
$1 $E2::Interface::DEBUG > 2
Truth table0%0/3
A and B
A= $1B= $E2::Interface::DEBUG > 2
ABresult
0X0
100
111
825
826 0 return $s;
827 }
828  
829 # Usage: $string = post_process STRING
830 #
831 # Turns the return value of process_request_raw into a
832 # string. Fixes encoding as well.
833  
834 0 ▶sub post_process {
Subroutine post_process: not called CC=3 SCAR=24.8
Pod undocumented
835 0 ▶ my $resp = shift or croak "Usage: post_process RESPONSE";
Branch: 1 branch
BranchTrueFalseTotal
my $resp = shift() or croak("Usage: post_process RESPONSE") 0 0 0
836  
837 0 require HTTP::Request;
838  
839 0 ▶ warn "E2::Interface::post_process\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
840
841 0 my $s = $resp->as_string;
842  
843 # Strip HTTP headers
844  
845 0 $s =~ s/.*?\n\n//s;
846  
847 ##### These are workarounds for some of the broken XML that
848 ##### displaytype=xmltrue outputs due to unescaped text.
849  
850 # E2 doesn't properly escape a number of titles in e2links, so
851 # do it here....
852  
853 0 ▶ my $encode = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
854 0 local $_ = shift;
855 0 s/</&lt;/sg;
856 0 s/>/&gt;/sg;
857 0 s/&/amp;/sg;
858 0 return $_;
859 0 };
860
861 0 $s =~ s/(<e2link .*?>)(.*?)(<\/e2link>)/$1 . &$encode($2) . $3/esg;
862  
863 # Escape the various entities that have not been escaped
864  
865 #my %valid = ( amp => 1, lt => 1, gt => 1 );
866 #$s =~ s/\&(\w+?);/$valid{lc($1)} ? "\&$1;" : "\&amp;$1;"/sge;
867  
868 # For &, <, and > which haven't been escaped, escape them (if we
869 # can be sure they're not valid xml.
870  
871 # $s =~ s/\&(?!\w+;)/&amp;/sg;
872 #$s =~ s/<(?![\w\/?][^<]*>)/&lt;/sg;
873 #$s =~ s/>/($` =~ m-<[\w\/?][^>]*$-s) ? '>' : '&gt;'/sge;
874  
875 # Demoronize and return
876  
877 0 return &demoronise($s);
878 }
879  
880 0 ▶sub demoronise {
Subroutine demoronise: not called CC=1 SCAR=6.9
Pod undocumented
881 0 local $_ = shift;
882  
883 # This has been adapted from a public domain script called
884 # demoroniser.pl by John Walker (can be found at
885 # http://www.fourmilab.ch/webtools/demoroniser/ ). That script
886 # replaced MS "smart quotes" and other nonstandard characters
887 # with their plaintext equivalents.
888 #
889 # I've modified them to convert, instead, to their HTML entity
890 # equivalents.
891
892 # Map strategically incompatible non-ISO characters in the
893 # range 0x82 -- 0x9F into plausible substitutes where
894 # possible.
895  
896 0 if( 0 ) { # Convert to html entities
897
898 s/\x82/&amp;sbquo;/sg;
899 s/\x83/&amp;fnof;/sg;
900 s/\x84/&amp;bdquo;/sg;
901 s/\x85/&amp;hellip;/sg;
902 s/\x86/&amp;dagger;/sg;
903 s/\x87/&amp;Dagger;/sg;
904 s/\x88/&amp;circ;/sg;
905 s/\x89/&amp;permil;/sg;
906 s/\x8A/&amp;Scaron;/sg;
907 s/\x8B/&amp;lsaquo;/sg;
908 s/\x8C/&amp;OElig;/sg;
909  
910 s/\x91/&amp;lsquo;/sg;
911 s/\x92/&amp;rsquo;/sg;
912 s/\x93/&amp;ldquo;/sg;
913 s/\x94/&amp;rdquo;/sg;
914 s/\x95/&amp;bull;/sg;
915 s/\x96/&amp;ndash;/sg;
916 s/\x97/&amp;mdash;/sg;
917 s/\x98/&amp;tilde;/sg;
918 s/\x99/&amp;trade;/sg;
919 s/\x9A/&amp;scaron;/sg;
920 s/\x9B/&amp;rsaquo;/sg;
921 s/\x9C/&amp;oelig;/sg;
922  
923 } else { # This is not executed; if it were, it would convert
924 # broken MS encoding to plaintext equiv (this is how
925 # demoronise.pl handled it).
926
927 0 s/\x82/,/g;
928 0 s-\x83-<em>f</em>-g;
929 0 s/\x84/,,/g;
930 0 s/\x85/.../g;
931  
932 0 s/\x88/^/g;
933 0 s-\x89- °/°°-g;
934  
935 0 s/\x8B/</g;
936 0 s/\x8C/Oe/g;
937  
938 0 s/\x91/`/g;
939 0 s/\x92/'/g;
940 0 s/\x93/"/g;
941 0 s/\x94/"/g;
942 0 s/\x95/*/g;
943 0 s/\x96/-/g;
944 0 s/\x97/--/g;
945 0 s-\x98-<sup>~</sup>-g;
946 0 s-\x99-<sup>TM</sup>-g;
947  
948 0 s/\x9B/>/g;
949 0 s/\x9C/oe/g;
950 }
951  
952 # Supply missing semicolon at end of numeric entity if
953 # Billy's bozos left it out.
954  
955 0 s/(&#[0-2]\d\d)\s/$1; /g;
956  
957 # Fix dimbulb obscure numeric rendering of &lt; &gt; &amp;
958  
959 0 s/&#038;/&amp;/g;
960 0 s/&#060;/&lt;/g;
961 0 s/&#062;/&gt;/g;
962  
963 0 return $_;
964 }
965  
966 0 ▶sub old_demoronise {
Subroutine old_demoronise: not called CC=1 SCAR=6.9
Pod undocumented
967 0 my $s = shift;
968
969 # This has been adapted from a public domain script called
970 # demoronizer.pl by John Walker (can be found at
971 # http://www.fourmilab.ch/webtools/demoroniser/ ). That script
972 # replaced MS "smart quotes" and other nonstandard characters
973 # with their plaintext equivalents.
974 #
975 # I've modified them to convert, instead, to their UTF-8
976 # equivalents.
977  
978 # (Christ this is some line noise...)
979  
980 0 $s =~ s/\xC2\x82/\xE2\x80\x98/sg; # &sbquo;
981 0 $s =~ s/\xC2\x83/\xC6\x92/sg; # &fnof;
982 0 $s =~ s/\xC2\x84/\xE2\x80\x9E/sg; # &bdquo;
983 0 $s =~ s/\xC2\x85/\xE2\x80\xA6/sg; # &hellip;
984 0 $s =~ s/\xC2\x86/\xE2\x80\xA0/sg; # &dagger;
985 0 $s =~ s/\xC2\x87/\xE2\x80\xA1/sg; # &Dagger;
986 0 $s =~ s/\xC2\x88/\xCB\x86/sg; # &circ;
987 0 $s =~ s/\xC2\x89/\xE2\x80\xB0/sg; # &permil;
988 0 $s =~ s/\xC2\x8A/\xC5\xA0/sg; # &Scaron;
989 0 $s =~ s/\xC2\x8B/\xE2\x80\xB9/sg; # &lsaquo;
990 0 $s =~ s/\xC2\x8C/\xC5\x92/sg; # &OElig;
991 0 $s =~ s/\xC2\x91/\xE2\x80\x98/sg; # &lsquo;
992 0 $s =~ s/\xC2\x92/\xE2\x80\x99/sg; # &rsquo;
993 0 $s =~ s/\xC2\x93/\xE2\x80\x9C/sg; # &ldquo;
994 0 $s =~ s/\xC2\x94/\xE2\x80\x9D/sg; # &rdquo;
995 0 $s =~ s/\xC2\x95/\xE2\x80\xA2/sg; # &bull;
996 0 $s =~ s/\xC2\x96/\xE2\x80\x93/sg; # &ndash;
997 0 $s =~ s/\xC2\x97/\xE2\x80\x94/sg; # &mdash;
998 0 $s =~ s/\xC2\x98/\xDC\xB2/sg; # &tilde;
999 0 $s =~ s/\xC2\x99/\xE2\x84\xA2/sg; # &trade;
1000 0 $s =~ s/\xC2\x9A/\xC5\xA1/sg; # &scaron;
1001 0 $s =~ s/\xC2\x9B/\xE2\x80\xBA/sg; # &rsaquo;
1002 0 $s =~ s/\xC2\x9C/\xC5\x93/sg; # &oelig;
1003
1004 0 return $s;
1005 }
1006  
1007 # Usage: process_request_raw METHOD, URL, COOKIE, AGENTSTR [, ATTR_PAIRS ... ]
1008 # METHOD is one of 'GET', 'POST', 'HEAD', etc.
1009 # URL is the base url of the request (the part before the '?')
1010 # COOKIE is an attribute=value pair to be used as a cookie
1011 # AGENTSTR is the agent string to be used for the request
1012 # ATTR_PAIRS is a set of list of attribute=value pairs to be
1013 # used to fetch the url.
1014 # Returns: a LWP::UserAgent response object
1015  
1016 0 ▶sub process_request_raw {
Subroutine process_request_raw: not called CC=9 SCAR=45.0
Pod undocumented
1017 0 ▶ if( @_ < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 3) 0 0 0
1018 0 croak "Usage: process_request_raw" .
1019 "METHOD, URL, COOKIE, AGENTSTR [, ATTR_PAIRS ]";
1020 }
1021
1022 0 require LWP::UserAgent;
1023 0 require HTTP::Request::Common;
1024 0 import HTTP::Request::Common 'POST';
1025 0 require HTTP::Cookies;
1026  
1027 0 my $req = shift;
1028 0 my $url = shift;
1029 0 my $cookie = shift;
1030 0 my $agentstr = shift;
1031 0 my %pairs = @_;
1032  
1033 0 ▶ warn "E2::Interface::process_request_raw\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $E2::Interface::DEBUG > 1 0 0 0
1034  
1035 # Put together an agentstring and cookie, and create an
1036 # LWP::UserAgent object to hold them
1037  
1038 0 my $str = client_name . '/' . version . " ($OS_STRING)";
1039 0 ▶ $str = "$agentstr $str" if $agentstr;
Branch: 1 branch
BranchTrueFalseTotal
if $agentstr 0 0 0
1040
1041 # warn "\$req = $req\n\$url = $url\n\$cookie = $cookie\n" .
1042 # "\$agentstr = $agentstr\nAttribute pairs:" . Dumper( \%pairs )
1043 # if $DEBUG > 2;
1044  
1045 0 my $agent = LWP::UserAgent->new(
1046 agent => $str,
1047 cookie_jar => HTTP::Cookies->new
1048 );
1049
1050 0 ▶ if( $cookie ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie) 0 0 0
1051 0 $url =~ m-//(.*?)/-; # extract domain
1052
1053 0 $agent->cookie_jar->set_cookie(
1054 0,
1055 'userpass',
1056 $cookie,
1057 '/',
1058 $1,
1059 undef,
1060 1,
1061 0,
1062 9999999
1063 );
1064 }
1065  
1066  
1067 # Execute the request
1068  
1069 0 my $request;
1070  
1071 0 ▶ if( $req eq "POST" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req eq 'POST') { } 0 0 0
1072  
1073 0 $request = POST( $url => [ %pairs ] );
1074  
1075 } else {
1076
1077 0 my $s = "$url?";
1078 0 my $prepend = "";
1079  
1080 0 foreach( keys %pairs ) {
1081 $s .= $prepend . uri_escape( $_ ) . "=" .
1082 0 uri_escape( $pairs{$_} );
1083 0 ▶ if( !$prepend ) { $prepend = '&'; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($prepend) 0 0 0
1084 }
1085
1086 0 $request = HTTP::Request->new( $req => $s );
1087 }
1088  
1089 0 my $response = $agent->simple_request( $request );
1090 0 ▶ if( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
1091 0 croak "Unable to process request";
1092 }
1093 0 return $response;
1094 }
1095  
1096 8 1;
1097 __END__
↑ Summary UserSearch.pm »