CAB/Client/HTTP.pm untested

filter: Statementstmt 0.0% 0 / 205 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 446 CC 55 SCAR 52.4
CC
55
Cov
0%
analyzeDataRef
650.0
connected
30.0
urlEncode
30.0
CRAP 189.3
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Client::HTTP.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: DTA::CAB generic HTTP server clients
6  
7 package DTA::CAB::Client::HTTP;
8 use DTA::CAB;
9 use DTA::CAB::Datum ':all';
10 use DTA::CAB::Utils ':all';
11 use DTA::CAB::Client;
12 #use DTA::CAB::Client::XmlRpc;
13 use LWP::UserAgent;
14 use HTTP::Status;
15 use HTTP::Request::Common;
16 use URI::Escape qw(uri_escape_utf8);
17 #use Encode qw(encode decode encode_utf8 decode_utf8);
18 use Carp qw(confess);
19 use strict;
20  
21 ##==============================================================================
22 ## Globals
23 ##==============================================================================
24  
25 our @ISA = qw(DTA::CAB::Client);
26  
27 BEGIN {
28 *isa = \&UNIVERSAL::isa;
29 *can = \&UNIVERSAL::can;
30 }
31  
32 ##==============================================================================
33 ## Constructors etc.
34 ##==============================================================================
35  
36 ## $obj = CLASS_OR_OBJ->new(%args)
37 ## + object structure: HASH ref
38 ## {
39 ## ##-- server
40 ## serverURL => $url, ##-- default: localhost:8000
41 ## timeout => $timeout, ##-- timeout in seconds, default: 300 (5 minutes)
42 ## mode => $queryMode, ##-- query mode: qw(get post xpost xmlrpc); default='xpost' (post with get-like parameters)
43 ## post => $postmode, ##-- post mode; one of 'urlencoded' (default), 'multipart'
44 ## rpcns => $prefix, ##-- prefix for XML-RPC analyzer names (default='dta.cab.')
45 ## rpcpath => $path, ##-- path part of URL for XML-RPC (default='/xmlrpc')
46 ##
47 ## format => $formatName, ##-- default query I/O format (default='json')
48 ## #encoding => $encoding, ##-- query encoding (always utf8)
49 ## cacheGet => $bool, ##-- allow cached response from server? (default=1)
50 ## cacheSet => $bool, ##-- allow caching of server response? (default=1)
51 ##
52 ## ##-- debugging
53 ## tracefh => $fh, ##-- dump requests to $fh if defined (default=undef)
54 ## testConnect => $bool, ##-- if true connected() will send a test query (default=true)
55 ##
56 ## ##-- underlying LWP::UserAgent
57 ## ua => $ua, ##-- underlying LWP::UserAgent object
58 ## uargs => \%args, ##-- options to LWP::UserAgent->new()
59 ##
60 ## ##-- optional underlying DTA::CAB::Client::XmlRpc
61 ## rclient => $xmlrpc_client, ##-- underlying DTA::CAB::Client::XmlRpc object
62 ## }
63 sub new {
64 my $that = shift;
65 return $that->SUPER::new(
66 ##-- server
67 serverURL => 'http://localhost:8000',
68 #encoding => 'UTF-8',
69 timeout => 300,
70 testConnect => 1,
71 mode => 'xpost',
72 #post => 'multipart',
73 post => 'urlencoded',
74 rpcns => 'dta.cab.',
75 rpcpath => '/xmlrpc',
76 ##
77 cacheGet=>1,
78 cacheSet=>1,
79 ##
80 format => 'json',
81 ##
82 ##-- low-level stuff
83 ua => undef,
84 uargs => {},
85 ##
86 ##-- user args
87 @_,
88 );
89 }
90  
91 ##==============================================================================
92 ## Methods: Generic Client API: Connections
93 ##==============================================================================
94  
95 ## $bool = $cli->connected()
96 sub connected {
97 my $cli = shift;
98 return $cli->rclient->connected if ($cli->{mode} eq 'xmlrpc');
99 return 0 if (!$cli->{ua});
100 return 1 if (!$cli->{testConnect});
101  
102 ##-- send a test query (system.identity())
103 my $rsp = $cli->uhead($cli->lwpUrl);
104 return $rsp && $rsp->is_success ? 1 : 0;
105 }
106  
107 ## $bool = $cli->connect()
108 ## + establish connection
109 ## + really does nothing but create the LWP::UserAgent object
110 sub connect {
111 my $cli = shift;
112 return $cli->rclient->connect if ($cli->{mode} eq 'xmlrpc');
113 $cli->ua()
114 or $cli->logdie("could not create underlying LWP::UserAgent: $!");
115 return $cli->connected();
116 }
117  
118 ## $bool = $cli->disconnect()
119 ## + really just deletes the LWP::UserAgent object
120 sub disconnect {
121 my $cli = shift;
122 $cli->rclient->disconnect();
123 delete @$cli{qw(ua rclient)};
124 return 1;
125 }
126  
127 ## @analyzers = $cli->analyzers()
128 ## + appends '/list' to $cli->lwpUrl() and parses returns
129 ## list of raw text lines returned
130 ## + die()s on error
131 sub analyzers {
132 my $cli = shift;
133 return $cli->rclient->analyzers() if ($cli->{mode} eq 'xmlrpc');
134  
135 my $url = $cli->lwpUrl.'/list?f=tt';
136 my $rsp = $cli->uget($url);
137 $cli->logdie("analyzers(): GET $url failed: ", $rsp->status_line)
138 if (!$rsp || $rsp->is_error);
139 my $content = $rsp->content;
140 return grep {defined($_) && $_ ne ''} split(/\r?\n/,$content);
141 }
142  
143 ##==============================================================================
144 ## Methods: Utils
145 ##==============================================================================
146  
147 ## $url = $cli->lwpUrl()
148 ## $url = $cli->lwpUrl($url)
149 ## + gets LWP-style URL $url
150 ## + support for HTTP over UNIX sockets:
151 ## - "unix:/path/to/unix/socket|http:///uri/path" (apache mod_proxy style)
152 ## - "http:/path/to/unix/socket//uri/path" (LWP::Protocol::http::SocketUnixAlt)
153 ## - "http+unix:/path/to/unix/socket//uri/path" (native http+unix support)
154 sub lwpUrl {
155 my ($cli,$url) = @_;
156 return undef if (! ($url ||= $cli->{serverURL}) );
157  
158 if ($url =~ m{^(.+?)\+unix:(?://)?(.+?)[/\|]/(.*)$}i) {
159 ##-- http+unix syntax
160 my ($scheme,$sockpath,$uripath) = ($1,$2,$3);
161 return "${scheme}:${sockpath}//${uripath}";
162 }
163 elsif ($url =~ m{^unix:(?://)?(.+?)(?:\||\%7C)(.*)$}i) {
164 ##-- apache mod_proxy syntax
165 my ($sockpath,$uristr) = ($1,$2);
166 my $uri = URI->new($uristr)->as_string;
167 $uri =~ s{//+}{${sockpath}//};
168 return $uri;
169 }
170  
171 return $url;
172 }
173  
174 ## $agent = $cli->ua()
175 ## + gets underlying LWP::UserAgent object, caching if required
176 sub ua {
177 return $_[0]{ua} if (defined($_[0]{ua}));
178 return $_[0]{ua} = LWP::UserAgent->new(%{$_[0]->{uargs}});
179 }
180  
181 ## $rclientent = $cli->rclient()
182 ## + gets underlying DTA::CAB::Client::XmlRpc object, caching if required
183 sub rclient {
184 return $_[0]{rclient} if (defined($_[0]{rclient}));
185 ##
186 require DTA::CAB::Client::XmlRpc;
187 my $cli = shift;
188 my $xuri = URI->new($cli->lwpUrl);
189 $xuri->path($cli->{rpcpath});
190 return $cli->{rclient} = DTA::CAB::Client::XmlRpc->new(%$cli, serverURL=>$xuri->as_string);
191 }
192  
193 ## $uriStr = $cli->urlEncode(\%form)
194 ## $uriStr = $cli->urlEncode(\@form)
195 ## $uriStr = $cli->urlEncode( $str)
196 sub urlEncode {
197 my ($cli,$form) = @_;
198 my $uri = URI->new;
199 if (isa($form,'ARRAY')) {
200 $uri->query_form([map {utf8::encode($_) if (utf8::is_utf8($_)); $_} @$form]);
201 }
202 elsif (isa($form,'HASH')) {
203 $uri->query_form([map {utf8::encode($_) if (utf8::is_utf8($_)); $_}
204 map {($_=>$form->{$_})}
205 sort keys %$form]);
206 }
207 else {
208 return uri_escape_utf8($form);
209 }
210 return $uri->query;
211 }
212  
213 ## $response = $cli->urequest($httpRequest)
214 ## + gets response for $httpRequest using $cli->ua()
215 ## + also traces request to $cli->{tracefh} if defined
216 sub urequest {
217 my ($cli,$hreq) = @_;
218  
219 ##-- check for unix sockets
220 return $cli->urequest_unix($hreq) if ($hreq->uri->path =~ m{[^/]//});
221  
222 $cli->{tracefh}->print("\n__BEGIN_REQEST__\n", $hreq->as_string, "__END_REQUEST__\n") if (defined($cli->{tracefh}));
223 return $cli->ua->request($hreq);
224 }
225  
226 ## $response = $cli->urequest_unix($httpRequest)
227 ## + guts for urequest() over UNIX sockets
228 sub urequest_unix {
229 my ($cli,$hreq) = @_;
230  
231 ##-- setup LWP::Protocol::http::SocketUnixAlt handlers
232 require LWP::Protocol::http::SocketUnixAlt;
233 my $http_impl = LWP::Protocol::implementor("http");
234 LWP::Protocol::implementor('http' => 'LWP::Protocol::http::SocketUnixAlt');
235  
236 ##-- suppress irritating warnings from LWP::Protocol::http via LWP::Protocol::http::SocketUnixAlt
237 my $sigwarn = $SIG{__WARN__};
238 local $SIG{__WARN__} = sub {
239 return if ($_[0] =~ m{Use of uninitialized value \$hhost.*LWP/Protocol/http\.pm});
240 $sigwarn ? $sigwarn->(@_) : warn(@_);
241 };
242  
243 $cli->{tracefh}->print("\n__BEGIN_REQEST__\n", $hreq->as_string, "__END_REQUEST__\n") if (defined($cli->{tracefh}));
244 my $rsp = $cli->ua->request($hreq);
245  
246 ##-- reset handlers
247 LWP::Protocol::implementor('http' => $http_impl);
248  
249 return $rsp;
250 }
251  
252 ## $response = $cli->uhead($url, Header=>Value,...)
253 sub uhead {
254 return $_[0]->urequest(HEAD @_[1..$#_]);
255 }
256  
257 ## $response = $cli->uget($url, Header=>Value,...)
258 sub uget {
259 #return $_[0]->ua->get(@_[1..$#_]);
260 return $_[0]->urequest(GET @_[1..$#_]);
261 }
262  
263 ## $response = $cli->upost( $url )
264 ## $response = $cli->upost( $url, $content, Header => Value,... )
265 ## $response = $cli->upost( $url, \$content, Header => Value,... )
266 ## $response = $cli->upost( $url, \%form, Header => Value,... )
267 ## + specify 'Content-Type'=>'form-data' to get "multipart/form-data" forms
268 sub upost {
269 #return $_[0]->ua->post(@_[1..$#_]);
270 my ($hreq);
271 if (isa($_[2],'HASH')) {
272 ##-- form
273 $hreq = POST @_[1..$#_];
274 }
275 elsif (isa($_[2],'SCALAR')) {
276 ##-- content reference
277 $hreq = POST $_[1], @_[3..$#_];
278 $hreq->content_ref($_[2]);
279 }
280 else {
281 ##-- content string
282 $hreq = POST $_[1], @_[3..$#_], Content=>$_[2];
283 }
284 return $_[0]->urequest($hreq);
285 }
286  
287 ## $response = $cli->uget_form($url, \%form)
288 ## $response = $cli->uget_form($url, \@form, @headers)
289 sub uget_form {
290 my ($cli,$url,$form,@headers) = @_;
291 return $cli->uget(($url.'?'.$cli->urlEncode($form)),@headers);
292 }
293  
294 ## $response = $cli->uxpost($url, \%form, $content, @headers)
295 ## $response = $cli->uxpost($url, \%form, \$content, @headers);
296 ## + encodes \%form as url-internal parameters (as for uget_form())
297 ## + POST data is $content
298 sub uxpost {
299 #my ($cli,$url,$form,$data,@headers) = @_;
300 return $_[0]->upost(($_[1].'?'.$_[0]->urlEncode($_[2])), @_[3..$#_]);
301 }
302  
303 ##==============================================================================
304 ## Methods: Generic Client API: Queries
305 ##==============================================================================
306  
307 ## $fmt = $cli->getFormat(\%opts)
308 ## + returns a new DTA::CAB::Format object appropriate for
309 ## parsing/formatting a $cli query with \%opts
310 sub getFormat {
311 my ($cli,$opts) = @_;
312 my $fc = $opts->{format} || $opts->{fmt} || $cli->{format} || $DTA::CAB::Format::CLASS_DEFAULT;
313 return DTA::CAB::Format->newFormat($fc);
314 }
315  
316 ## $response = $cli->analyzeDataRef($analyzer, \$data_str, \%opts)
317 ## + client-side %opts
318 ## contentType => $mimeType, ##-- Content-Type to apply for mode='xpost'
319 ## #encoding => $charset, ##-- character set for mode='xpost'; also used by server
320 ## qraw => $bool, ##-- if true, query is a raw untokenized string (default=false)
321 ## headers => $headers, ##-- HASH or ARRAY or HTTP::Headers object
322 ## cacheGet => $bool, ##-- locally override $cli->{cacheGet}
323 ## cacheSet => $bool, ##-- locally override $cli->{cacheSet}
324 ## + server-side %opts: see DTA::CAB::Server::HTTP::Handler::Query
325 sub analyzeDataRef {
326 my ($cli,$aname,$dataref,$opts) = @_;
327 return $cli->rclient->analyzeData($cli->{rpcns}.$aname,$$dataref,$opts) if ($cli->{mode} eq 'xmlrpc');
328  
329 ##-- get headers as ARRAY
330 my $headers = $opts->{headers};
331 if (UNIVERSAL::isa($headers,'HTTP::Headers')) {
332 $headers = [];
333 $opts->{headers}->scan(sub {push(@$headers,@_)});
334 } elsif (UNIVERSAL::isa($headers,'HASH')) {
335 $headers = [ %$headers ];
336 } elsif (UNIVERSAL::isa($headers,'ARRAY')) {
337 $headers = [ @$headers ];
338 } else {
339 ##-- unknown headers
340 $headers = [];
341 }
342 ##-- headers: cache control
343 my $cacheControl = join(', ',
344 ((exists($opts->{cacheGet}) ? $opts->{cacheGet} : $cli->{cacheGet}) ? qw() : 'no-cache'),
345 ((exists($opts->{cacheSet}) ? $opts->{cacheSet} : $cli->{cacheSet}) ? qw() : 'no-store'),
346 );
347 push(@$headers, 'Cache-Control'=>$cacheControl) if ($cacheControl);
348  
349 ##-- build form
350 my %form = (
351 fmt=>$cli->{format},
352 #enc=>$cli->{encoding},
353 ($opts ? %$opts : qw()),
354 a=>$aname,
355 );
356  
357 ##-- sanity checks (long parameter names clobber short names)
358 #$form{enc} = $form{encoding} if ($form{encoding});
359 $form{fmt} = $form{format} if ($form{format});
360 delete(@form{qw(format encoding qraw headers cacheGet cacheSet)});
361 delete(@form{grep {!defined($form{$_})} keys %form});
362  
363 ##-- content-type hacks
364 my $ctype = $opts->{contentType};
365 $ctype = 'application/octet-stream' if (!$ctype);
366 delete(@form{qw(q qd contentType)});
367  
368 ##-- compatibility check / raw vs. formatted
369 my $qname = $opts->{qraw} ? 'q' : 'qd';
370 my $qmode = $cli->{mode};
371 if ($qname eq 'q' && $cli->{mode} eq 'xpost') {
372 $cli->logcarp("analyzeDataRef(): 'xpost' method not supported for raw queries; using 'post' instead");
373 $qmode = 'post';
374 }
375  
376 ##-- get response
377 my ($rsp);
378 if ($qmode eq 'get') {
379 $form{$qname} = $$dataref;
380 $rsp = $cli->uget_form($cli->lwpUrl, \%form, @$headers);
381 }
382 else {
383 ##-- encode (for HTTP::Request v5.810 e.g. on services)
384 foreach (values %form) {
385 utf8::encode($_) if (utf8::is_utf8($_));
386 }
387  
388 ##-- encode dataref
389 utf8::encode($$dataref) if (utf8::is_utf8($$dataref));
390  
391 if ($qmode eq 'post') {
392 ##-- post most
393 $form{$qname} = $$dataref;
394 $rsp = $cli->upost($cli->lwpUrl, \%form,
395 @$headers,
396 ($cli->{post} && $cli->{post} eq 'multipart' ? ('Content-Type'=>'form-data') : qw()),
397 );
398 }
399 elsif ($qmode eq 'xpost') {
400 ##-- xpost mode
401 $ctype .= "; charset=\"UTF-8\"" if ($ctype !~ /octet-stream/ && $ctype !~ /\bcharset=/);
402 $rsp = $cli->uxpost($cli->lwpUrl,
403 \%form,
404 $$dataref,
405 @$headers,
406 'Content-Type'=>$ctype);
407 }
408 }
409  
410 if (!$rsp) {
411 ##-- should never happen
412 $rsp = HTTP::Response->new(RC_NOT_IMPLEMENTED, "not implemented: unknown client mode '$qmode'");
413 }
414  
415 ##-- trace server response
416 $cli->{tracefh}->print("\n__BEGIN_RESPONSE__\n", ($rsp ? $rsp->as_string : '(undef)'), "__END_RESPONSE__\n") if (defined($cli->{tracefh}));
417 return $rsp;
418 }
419  
420 ## undef = $cli->serverError($rsp)
421 ## + handle server error responses
422 ## + default implementation just calls $cli->logconfess()
423 sub serverError {
424 my ($cli,$rsp) = @_;
425 $cli->logconfess("server error: " . $rsp->status_line . "\n" . $rsp->content);
426 #$cli->logcroak("server error: " . $rsp->status_line . "\n" . $rsp->content . ' ');
427 #$cli->logdie("server error: " . $rsp->status_line . "\n" . $rsp->content);
428 #confess (ref($cli) . ": server error: " . $rsp->status_line . "\n" . $rsp->content);
429 }
430  
431 ## $data_str = $cli->analyzeData($analyzer, \$data_str, \%opts)
432 ## + wrapper for analyzeDataRef()
433 ## + die()s on error
434 ## + you should pass $opts->{'Content-Type'} as some sensible value
435 sub analyzeData {
436 my ($cli,$aname,$data,$opts) = @_;
437 return $cli->rclient->analyzeData($cli->{rpcns}.$aname,$data,$opts) if ($cli->{mode} eq 'xmlrpc');
438 ##
439 my $rsp = $cli->analyzeDataRef($aname,\$data,$opts);
440 return $cli->serverError($rsp) if ($rsp->is_error);
441 return $rsp->content;
442 }
443  
444 ## $doc = $cli->analyzeDocument($analyzer, $doc, \%opts)
445 sub analyzeDocument {
446 my ($cli,$aname,$doc,$opts) = @_;
447 return $cli->rclient->analyzeDocument($cli->{rpcns}.$aname,$doc,$opts) if ($cli->{mode} eq 'xmlrpc');
448 ##
449 my ($str);
450 my $fmt = $cli->getFormat($opts)
451 or $cli->logdie("analyzeDocument(): could not create document formatter");
452 $fmt->toString(\$str)->putDocument($doc)->flush()
453 or $cli->logdie("analyzeDocument(): could not format document with class ".ref($fmt).": $!");
454 ##
455 my $rsp = $cli->analyzeDataRef($aname,\$str,{($opts ? %$opts : qw()),
456 fmt => $fmt->shortName,
457 #enc => $fmt->{encoding},
458 contentType=>$fmt->mimeType,
459 });
460 return $cli->serverError($rsp) if ($rsp->is_error);
461 return $fmt->parseString($rsp->content_ref);
462 }
463  
464 ## $sent = $cli->analyzeSentence($analyzer, $sent, \%opts)
465 sub analyzeSentence {
466 my ($cli,$aname,$sent,$opts) = @_;
467 return $cli->rclient->analyzeSentence($cli->{rpcns}.$aname,$sent,$opts) if ($cli->{mode} eq 'xmlrpc');
468 ##
469 my $doc = toDocument [toSentence $sent];
470 $doc = $cli->analyzeDocument($aname,$doc,$opts)
471 or $cli->logdie("analyzeSentence(): could not analyze temporary document: $!");
472 return $doc->{body}[0];
473 }
474  
475 ## $tok = $cli->analyzeToken($analyzer, $tok, \%opts)
476 sub analyzeToken {
477 my ($cli,$aname,$tok,$opts) = @_;
478 return $cli->rclient->analyzeToken($cli->{rpcns}.$aname,$tok,$opts) if ($cli->{mode} eq 'xmlrpc');
479 ##
480 my $doc = toDocument [toSentence [toToken $tok]];
481 $doc = $cli->analyzeDocument($aname,$doc,$opts)
482 or $cli->logdie("analyzeToken(): could not analyze temporary document: $!");
483 return $doc->{body}[0]{tokens}[0];
484 }
485  
486  
487 1; ##-- be happy
488  
489 __END__