Server.pm

filter: Statementstmt 30.0% 33 / 110 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 36.3% 8 / 22 Podpod 0.0% 0 / 9 totaltotal 18.5% 41 / 221 CC 31 SCAR 40.4
CC
31
Cov
19%
__ANON__
156.0
spawn_child_posix
72.0
fetch
56.0
CRAP 56.9
↑ Summary Connection.pm »
1 package Apache::Tika::Server;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Apache::Tika::DocInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # Consider if we really want/need it, instead of simply staying
9 # callback-based
10 #use Future::AsyncAwait;
11  
12 #use Filter::signatures;
13 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Apache::Tika::Server - Fire up/stop a Tika instance
19  
20 =head1 SYNOPSIS
21  
22 use Apache::Tika::Server;
23  
24 # Launch our own Apache Tika instance
25 my $tika= Apache::Tika::Server->new(
26 jarfile => $tika_path,
27 );
28 $tika->launch;
29  
30 my $fn= shift;
31  
32 use Data::Dumper;
33 my $info = $tika->get_all( $fn );
34 print Dumper $info->meta($fn);
35 print $info->content($fn);
36 # <html><body>...
37 print $info->meta->{"meta:language"};
38 # en
39  
40 =cut
41  
42 1 our $VERSION = '0.11';
43  
44 1 extends 'Apache::Tika::Async';
45  
46 0 ▶sub load_module {
Subroutine load_module: not called CC=1 SCAR=6.9
Pod undocumented
47 0 my( $module ) = @_;
48 0 $module =~ s!::!/!g;
49 0 require "$module.pm"
50 }
51  
52 1 has pid => (
53 is => 'rw',
54 #isa => 'Int',
55 );
56  
57 has host => (
58 is => 'ro',
59  
60 # this should be 127.0.0.1 or [::1] , depending on whether we
61 # are IPv4 or IPv6 ...
62 0 ▶ default => sub { 'localhost' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 1 );
64  
65 has port => (
66 is => 'ro',
67 #isa => 'Int',
68 0 ▶ default => sub { 9998 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 1 );
70  
71 has loglevel => (
72 is => 'ro',
73 #isa => 'Int',
74 0 ▶ default => sub { 'info' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 1 );
76  
77 1 has connection_class => (
78 is => 'ro',
79 default => 'Apache::Tika::Connection::Future',
80 );
81  
82 has ua => (
83 is => 'ro',
84 #isa => 'Str',
85 0 ▶ default => sub { load_module( $_[0]->connection_class ); $_[0]->connection_class->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 1 );
87  
88 0 ▶sub cmdline {
Subroutine cmdline: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my( $self )= @_;
90 $self->java,
91 0 @{$self->java_args},
92 '-jar',
93 $self->jarfile,
94 #'--port', $self->port,
95 '--config', $self->tika_config_temp_file,
96 0 @{$self->tika_args},
97 };
98  
99 0 ▶sub spawn_child_win32( $self, @cmd ) {
Subroutine spawn_child_win32: not called CC=1 SCAR=6.9
Pod undocumented
100 0 system(1, @cmd)
101 }
102  
103 0 ▶sub spawn_child_posix( $self, @cmd ) {
Subroutine spawn_child_posix: not called CC=8 SCAR=42.8
Pod undocumented
104 0 require POSIX;
105 0 POSIX->import("setsid");
106  
107 # daemonize
108 0 ▶ defined(my $pid = fork()) || die "can't fork: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $pid = fork) or die "can't fork: $!" 0 0 0
109 0 ▶ if( $pid ) { # non-zero now means I am the parent
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
110 0 return $pid;
111 };
112  
113 # We are the child, close about everything, then exec
114 0 ▶ chdir("/") || die "can't chdir to /: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir "/" or die "can't chdir to /: $!" 0 0 0
115 0 ▶ (setsid() != -1) || die "Can't start a new session: $!";
Branch: 1 branch
BranchTrueFalseTotal
setsid() != -1 or die "Can't start a new session: $!" 0 0 0
116 0 ▶ open(STDERR, ">&STDOUT") || die "can't dup stdout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or die "can't dup stdout: $!" 0 0 0
117 0 ▶ open(STDIN, "< /dev/null") || die "can't read /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "< /dev/null" or die "can't read /dev/null: $!" 0 0 0
118 0 ▶ open(STDOUT, "> /dev/null") || die "can't write to /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, "> /dev/null" or die "can't write to /dev/null: $!" 0 0 0
119 0 exec @cmd;
120 0 exit 1;
121 }
122  
123 0 ▶sub spawn_child( $self, @cmd ) {
Subroutine spawn_child: not called CC=2 SCAR=17.9
Pod undocumented
124 0 my ($pid);
125 0 ▶ if( $^O =~ /mswin/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /mswin/i) { } 0 0 0
126 0 $pid = $self->spawn_child_win32(@cmd)
127 } else {
128 0 $pid = $self->spawn_child_posix(@cmd)
129 };
130  
131 0 return $pid
132 }
133  
134 0 ▶sub launch( $self ) {
Subroutine launch: not called CC=3 SCAR=24.8
Pod undocumented
135 0 ▶ if( !$self->pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->pid) 0 0 0
136 0 my $cmdline= join " ", $self->cmdline; # well, for Windows...
137 #warn $cmdline;
138 0 ▶ my $pid= $self->spawn_child( $self->cmdline )
Branch: 1 branch
BranchTrueFalseTotal
my $pid = $self->spawn_child($self->cmdline) or croak("Couldn't launch [$cmdline]: $!/$^E") 0 0 0
139 or croak "Couldn't launch [$cmdline]: $!/$^E";
140 0 $self->pid( $pid );
141 0 sleep 2; # Java...
142 };
143 }
144  
145 0 ▶sub url {
Subroutine url: not called CC=2 SCAR=17.9
Pod undocumented
146 # Should return URI instead
147 0 my( $self, $type )= @_;
148 0 ▶ $type||= 'text';
Condition0%0/2
$type ||= "text"
l!l
00
MC/DC0%0/1
$type ||= "text"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
149  
150 my $url= {
151 text => 'rmeta',
152 test => 'tika', # but GET instead of PUT
153 meta => 'rmeta',
154 #all => 'all',
155 language => 'language/string',
156 all => 'rmeta',
157 # unpack
158 0 }->{ $type };
159  
160 0 sprintf
161 'http://%s:%s/%s',
162 $self->host,
163 $self->port,
164 $url
165 };
166  
167 # /rmeta
168 # /unpacker
169 # /all
170 # /tika
171 # /language
172 # hello world
173 0 ▶sub fetch {
Subroutine fetch: not called CC=7 SCAR=40.3
Pod undocumented
174 0 my( $self, %options )= @_;
175 0 ▶ $options{ type }||= 'text';
Condition0%0/2
$options{"type"} ||= "text"
l!l
00
MC/DC0%0/1
$options{"type"} ||= "text"
$options{"type"}
Truth table0%0/2
A
A= $options{"type"}
Aresult
11
00
176 0 my $url= $self->url( $options{ type } );
177  
178 0 ▶ if(! $options{ content } and $options{ filename }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $options{"content"} and $options{"filename"}) 0 0 0
Condition0%0/3
not $options{"content"} and $options{"filename"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $options{"content"} and $options{"filename"}
not $options{"content"} $options{"filename"}
Truth table0%0/3
not(A) and B
A= not $options{"content"}B= $options{"filename"}
ABresult
0X0
100
111
179 # read $options{ filename }
180 open my $fh, '<', $options{ filename }
181 0 ▶ or croak "Couldn't read '$options{ filename }': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $options{"filename"} or croak(qq[Couldn't read '$options{"filename"}': $!]) 0 0 0
182 0 binmode $fh;
183 0 local $/;
184 0 $options{ content } = <$fh>;
185 };
186  
187 0 my $method;
188 0 ▶ if( 'test' eq $options{ type } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('test' eq $options{'type'}) { } 0 0 0
189 0 $method= 'get';
190  
191 } else {
192 0 $method= 'put';
193 ;
194 };
195  
196 0 ▶ my $headers = $options{ headers } || {};
Condition0%0/2
$options{'headers'} || {}
l!l
00
MC/DC0%0/1
$options{'headers'} || {}
$options{'headers'}
Truth table0%0/2
A
A= $options{'headers'}
Aresult
11
00
197  
198 #my ($code,$res) = await
199 # $self->ua->request( $method, $url, $options{ content }, %$headers );
200 return $self->ua->request( $method, $url, $options{ content }, %$headers )
201 0 ▶ ->then(sub( $code, $res ) {
Subroutine __ANON__: not called CC=12 SCAR=50.5
202 0 my $info;
203 0 ▶ if( 'all' eq $options{ type }
Branch: 1 branch
BranchTrueFalseTotal
if ('all' eq $options{'type'} or 'text' eq $options{'type'} or 'meta' eq $options{'type'}) { } 0 0 0
Condition0%0/6
"all" eq $options{"type"} or "text" eq $options{"type"} or "meta" eq $options{"type"}
l!l&&r!l&&!r
000
"all" eq $options{"type"} or "text" eq $options{"type"}
l!l&&r!l&&!r
000
MC/DC0%0/3
"all" eq $options{"type"} or "text" eq $options{"type"} or "meta" eq $options{"type"}
"all" eq $options{"type"} "text" eq $options{"type"} "meta" eq $options{"type"}
Truth table0%0/4
A or B or C
A= "all" eq $options{"type"}B= "text" eq $options{"type"}C= "meta" eq $options{"type"}
ABCresult
1XX1
01X1
0011
0000
204 or 'text' eq $options{ type }
205 or 'meta' eq $options{ type } ) {
206 0 ▶ if( $code !~ /^2..$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code =~ /^2..$/) 0 0 0
207 0 croak "Got HTTP error code $code for '$options{ filename }'";
208 };
209 0 my $item = $res->[0];
210 # Should/could this be lazy?
211 0 my $c = delete $item->{'X-TIKA:content'};
212 # Ghetto-strip HTML we don't want:
213 0 ▶ if( $c =~ m!<body>(.*)</body>!s or $c =~ m!<body\s*/>!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ m[<body>(.*)</body>]s or $c =~ m[<body\s*/>]) { } 0 0 0
Condition0%0/3
$c =~ m[<body>(.*)</body>]s or $c =~ m[<body\s*/>]
l!l&&r!l&&!r
000
MC/DC0%0/2
$c =~ m[<body>(.*)</body>]s or $c =~ m[<body\s*/>]
$c =~ m[<body>(.*)</body>]s $c =~ m[<body\s*/>]
Truth table0%0/3
A or B
A= $c =~ m[<body>(.*)</body>]sB= $c =~ m[<body\s*/>]
ABresult
1X1
011
000
214 0 $c = $1;
215  
216 0 ▶ if( $item->{"Content-Type"} and $item->{"Content-Type"} =~ m!^text/plain\b!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"Content-Type"} and $item->{"Content-Type"} =~ m[^text/plain\b]) 0 0 0
Condition0%0/3
$item->{"Content-Type"} and $item->{"Content-Type"} =~ m[^text/plain\b]
!ll&&!rl&&r
000
MC/DC0%0/2
$item->{"Content-Type"} and $item->{"Content-Type"} =~ m[^text/plain\b]
$item->{"Content-Type"} $item->{"Content-Type"} =~ m[^text/plain\b]
Truth table0%0/3
A and B
A= $item->{"Content-Type"}B= $item->{"Content-Type"} =~ m[^text/plain\b]
ABresult
0X0
100
111
217 # Also strip the enclosing <p>..</p>
218 0 $c =~ s!\A\s*<p>(.*)\s*</p>\s*\z!$1!s;
219 };
220 } else {
221 0 warn "Couldn't find HTML body in response: $c";
222 };
223  
224 0 $info= Apache::Tika::DocInfo->new({
225 content => $c,
226 meta => $item,
227 });
228  
229 0 ▶ if( ! defined $info->{meta}->{"meta:language"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info->{"meta"}{"meta:language"}) 0 0 0
230 # Yay. Two requests.
231 0 my $lang_meta = $self->fetch(%options, type => 'language', 'Content-Type' => $item->{'Content-Type'})->get;
232 0 $info->{meta}->{"meta:language"} = $lang_meta->meta->{"info"};
233 };
234  
235 } else {
236 # Must be '/language'
237 0 ▶ if( $code !~ /^2..$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code =~ /^2..$/) 0 0 0
238 0 croak "Got HTTP error code $code";
239 };
240 0 ▶ if( ref $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res) { } 0 0 0
241 0 $res = $res->[0];
242 } else {
243 0 $res = { info => $res };
244 };
245  
246 0 my $c = delete $res->{'X-TIKA:content'};
247 0 $info= Apache::Tika::DocInfo->new({
248 meta => $res,
249 content => undef,
250 });
251 };
252 0 return Future->done($info)
253 })
254 0 }
255  
256 0 ▶sub DEMOLISH {
Subroutine DEMOLISH: not called CC=3 SCAR=24.8
Pod undocumented
257 0 ▶ kill -9 => $_[0]->pid
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0]->pid 0 0 0
Condition0%0/3
$_[0] and $_[0]->pid
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0]->pid
$_[0] $_[0]->pid
Truth table0%0/3
A and B
A= $_[0]B= $_[0]->pid
ABresult
0X0
100
111
258 if( $_[0] and $_[0]->pid );
259 }
260  
261 #__PACKAGE__->meta->make_immutable;
262  
263 1 1;
264  
265 =head1 REPOSITORY
266  
267 The public repository of this module is
268 L<https://github.com/Corion/Apache-Tika-Async>.
269  
270 =head1 SUPPORT
271  
272 The public support forum of this module is
273 L<https://perlmonks.org/>.
274  
275 =head1 BUG TRACKER
276  
277 Please report bugs in this module via the RT CPAN bug queue at
278 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Apache-Tika-Async>
279 or via mail to L<apache-tika-async-Bugs@rt.cpan.org>.
280  
281 =head1 AUTHOR
282  
283 Max Maischein C<corion@cpan.org>
284  
285 =head1 COPYRIGHT (c)
286  
287 Copyright 2014-2019 by Max Maischein C<corion@cpan.org>.
288  
289 =head1 LICENSE
290  
291 This module is released under the same terms as Perl itself.
292  
293 =cut
↑ Summary Connection.pm »