File Coverage

blib/lib/HTTP/Server/Simple/Recorder.pm
Criterion Covered Total %
statement 44 47 93.6
branch 7 14 50.0
condition 1 2 50.0
subroutine 10 11 90.9
pod 0 4 0.0
total 62 78 79.4


line stmt bran cond sub pod time code
1             package HTTP::Server::Simple::Recorder;
2              
3             our $VERSION = '0.03';
4              
5 3     3   188107 use warnings;
  3         7  
  3         113  
6 3     3   16 use strict;
  3         6  
  3         110  
7 3     3   15 use Carp;
  3         9  
  3         323  
8              
9 3     3   968 use IO::File;
  3         12145  
  3         1572  
10              
11             sub stdio_handle {
12 7     7 0 253569 my $self = shift;
13 7 100       42 if (@_) {
14 2         5 my $handle = $_[0];
15 2         20 $self->{'_recorder_stdio_handle'} = $handle;
16            
17 2         7 my $serial = ++ $self->{'_recorder_serial'};
18 2         50 my $prefix = $self->recorder_prefix;
19              
20 2         31 my $infile = "$prefix.$serial.in";
21 2         6 my $outfile = "$prefix.$serial.out";
22              
23 2 50       32 my $in = IO::File->new("$infile", ">") or die "Couldn't open $infile: $!";
24 2         603 $in->autoflush(1);
25 2 50       245 my $out = IO::File->new("$outfile", ">") or die "Couldn't open $outfile: $!";
26 2         247 $out->autoflush(1);
27              
28 2         88 $self->{'_recorder_stdin_handle'} = IO::Tee::Binmode->new($handle, $in);
29 2         348 $self->{'_recorder_stdout_handle'} = IO::Tee::Binmode->new($handle, $out);
30             }
31 7         153 return $self->{'_recorder_stdio_handle'};
32             }
33              
34             sub stdin_handle {
35 2     2 0 866 my $self = shift;
36 2         37 return $self->{'_recorder_stdin_handle'};
37             }
38              
39             sub stdout_handle {
40 2     2 0 13 my $self = shift;
41 2         14 return $self->{'_recorder_stdout_handle'};
42             }
43              
44 0     0 0 0 sub recorder_prefix { "/tmp/http-server-simple-recorder"; }
45              
46             package IO::Tee::Binmode;
47              
48 3     3   21 use base qw/IO::Tee/;
  3         6  
  3         3132  
49              
50             sub BINMODE {
51 4     4   29 my $self = shift;
52 4         6 my $ret = 1;
53 4 50       22 if (@_) {
54 4 50       18 for my $fh (@$self) { undef $ret unless binmode $fh, $_[0] }
  8         45  
55             } else {
56 0 0       0 for my $fh (@$self) { undef $ret unless binmode $fh }
  0         0  
57             }
58 4         12 return $ret;
59             }
60              
61             sub READ {
62 42     42   688 my $self = shift;
63 42         149 my $bytes = $self->[0]->read(@_);
64             # add the || 0 to silence warnings
65 42 50 50     453 $bytes and $self->_multiplex_input(substr($_[0], $_[2] || 0, $bytes));
66 42         962 $bytes;
67             }
68              
69              
70             1; # Magic true value required at end of module
71             __END__