File Coverage

blib/lib/IO/Buffered/HTTP.pm
Criterion Covered Total %
statement 55 64 85.9
branch 22 30 73.3
condition 3 11 27.2
subroutine 9 12 75.0
pod 7 7 100.0
total 96 124 77.4


line stmt bran cond sub pod time code
1             package IO::Buffered::HTTP;
2 8     8   40 use strict;
  8         14  
  8         318  
3 8     8   38 use warnings;
  8         13  
  8         168  
4 8     8   37 use Carp;
  8         15  
  8         445  
5              
6 8     8   38 use base ("IO::Buffered");
  8         13  
  8         683  
7              
8             # FIXME: Write documentation
9              
10             our $VERSION = '1.00';
11              
12             =head1 NAME
13              
14             IO::Buffered::HTTP - HTTP buffering
15              
16             =head1 DESCRIPTION
17              
18             =head1 SYNOPSIS
19              
20             =head1 METHODS
21              
22             =over
23              
24             =cut
25              
26 8     8   37 use base "Exporter";
  8         12  
  8         6562  
27              
28             our @EXPORT_OK = qw();
29              
30             =item new()
31              
32             =cut
33              
34             sub new {
35 2     2 1 5 my ($class, %opts) = @_;
36            
37 2 50 0     11 croak "Option MaxSize should be a positiv integer" if $opts{MaxSize} and !(
      33        
38             $opts{MaxSize} =~ /^\d+$/ and $opts{MaxSize} > 0);
39            
40 2 100       15 my %self = (
41             maxsize => $opts{MaxSize},
42             headeronly => (exists $opts{HeaderOnly} ? $opts{HeaderOnly} : 0),
43             buffer => '',
44             length => 0,
45             );
46            
47 2   33     37 return bless \%self, (ref $class || $class);
48             }
49              
50             =item flush($str, ...)
51              
52             =cut
53              
54             sub flush {
55 0     0 1 0 my $self = shift;
56 0         0 $self->{buffer} = join ('', @_);
57             }
58              
59             =item buffer()
60              
61             =cut
62              
63             sub buffer {
64 0     0 1 0 my $self = shift;
65 0         0 return $self->{buffer};
66             }
67              
68             =item write($str, ...)
69              
70             =cut
71              
72             sub write {
73 5     5 1 16 my $self = shift;
74 5         13 my $str = join ('', @_);
75            
76 5 50       22 if(my $maxsize = $self->{maxsize}) {
77 0         0 my $length = length($str) + length($self->{buffer});
78 0 0       0 if($length > $maxsize) {
79 0         0 croak "Buffer overrun";
80             }
81             }
82              
83 5         28 $self->{buffer} .= $str;
84             }
85              
86             =item read()
87              
88             =cut
89              
90             sub read {
91 7     7 1 1316 my ($self, $readlength) = (@_);
92 7         10 my @records;
93            
94             # FIXME: Something is boaken
95              
96 7 100 50     28 $self->{length} = ($readlength or -1) if $self->{length} < 0;
97             #print "hello: $self->{length}, $readlength\n";
98              
99 7         16 while($self->{length} >= 0) {
100 9 100       22 if(my $length = $self->{length}) {
101 2 50       7 if(length $self->{buffer} >= $length) {
102 2         5 push(@records, substr($self->{buffer}, 0, $length));
103 2         6 substr($self->{buffer}, 0, $length) = '';
104 2         2 $self->{length} = 0;
105             #$readlength = undef;
106 2 100       7 next if length($self->{buffer}) > 0;
107             }
108              
109             } else {
110 7         20 my $idx = index($self->{buffer}, "\r\n\r\n");
111             # Found what could be a header
112 7 100       16 if($idx >= 0) {
113 5         15 my $header = substr($self->{buffer}, 0, $idx + 4);;
114            
115 5 100       31 if($self->{headeronly}) {
    100          
116 2         3 push(@records, $header);
117 2         5 substr($self->{buffer}, 0, $idx + 4) = '';
118 2         4 $self->{length} = -1;
119            
120             } elsif($header =~ /Content-Length:\s+(\d+)/six) {
121 2         48 my $length = $1 + $idx + 4;
122 2 50       8 if(length $self->{buffer} >= $length) {
123 2         7 push(@records, substr($self->{buffer}, 0, $length));
124 2         4 substr($self->{buffer}, 0, $length) = '';
125 2 100       9 next if length($self->{buffer}) > 0;
126             } else {
127 0         0 $self->{length} = $length;
128             }
129              
130             } else {
131 1         3 push(@records, $header);
132 1         3 substr($self->{buffer}, 0, $idx + 4) = '';
133 1 50       6 next if length($self->{buffer}) > 0;
134             }
135             }
136             }
137            
138 7         11 last;
139             };
140              
141 7         24 return @records;
142             }
143              
144             =item returns_last()
145              
146             =cut
147              
148             sub returns_last {
149 0     0 1 0 return 1;
150             }
151              
152             =item read_last()
153              
154             =cut
155              
156             sub read_last {
157 1     1 1 1162 my ($self) = @_;
158 1         4 my @results = $self->read();
159 1 50       18 push(@results, $self->{buffer}) if $self->{buffer} ne '';
160 1         2 $self->{buffer} = '';
161 1         7 return @results;
162             }
163              
164             =back
165              
166             =head1 AUTHOR
167              
168             Troels Liebe Bentsen
169              
170             =head1 COPYRIGHT
171              
172             Copyright(C) 2008 Troels Liebe Bentsen
173              
174             This library is free software; you can redistribute it and/or modify
175             it under the same terms as Perl itself.
176              
177             =cut
178              
179             1;
180