Filter/HTTPHead.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 204 CC 12 SCAR 40.1
CC
12
Cov
0%
get_one
132.0
get_pending
6.0
new
2.0
CRAP 55.3
1 package POE::Filter::HTTPHead_Line;
2 # vim: ts=2 sw=2 expandtab
3 $POE::Filter::HTTPHead_Line::VERSION = '0.949';
4 use warnings;
5 use strict;
6  
7 use base 'POE::Filter';
8  
9 use HTTP::Response;
10  
11 use constant {
12 FRAMING_BUFFER => 0,
13 CURRENT_STATE => 1,
14 WORK_RESPONSE => 2,
15 PROTOCOL_VERSION => 3,
16 };
17  
18 use constant {
19 STATE_STATUS => 0x01, # waiting for a status line
20 STATE_HEADER => 0x02, # gotten status, looking for header or end
21 };
22  
23 use constant DEBUG => 0;
24  
25 sub new {
26 my $type = shift;
27  
28 my $self = bless [
29 [], # FRAMING_BUFFER
30 STATE_STATUS, # CURRENT_STATE
31 undef, # WORK_RESPONSE
32 "0.9", # PROTOCOL_VERSION
33 ], $type;
34  
35 $self;
36 }
37  
38 sub get_one_start {
39 my ($self, $chunks) = @_;
40  
41 # We're receiving newline-terminated lines. Strip off any carriage
42 # returns that might be left over.
43 s/\x0D$// foreach @$chunks;
44 s/^\x0D// foreach @$chunks;
45  
46 push (@{$self->[FRAMING_BUFFER]}, @$chunks);
47 #warn "now got ", scalar @{$self->[FRAMING_BUFFER]}, " lines";
48 }
49  
50 sub get_one {
51 my $self = shift;
52  
53 # Process lines while we have them.
54 LINE: while (@{$self->[FRAMING_BUFFER]}) {
55 my $line = shift @{$self->[FRAMING_BUFFER]};
56  
57 # Waiting for a status line.
58 if ($self->[CURRENT_STATE] == STATE_STATUS) {
59 DEBUG and warn "----- Waiting for a status line.\n";
60  
61 # Does the line look like a status line?
62 if ($line =~ m!^(\d{3})\s+(.+?)\s+HTTP/(\d+\.\d+)$!) {
63 $self->[PROTOCOL_VERSION] = $3;
64 $self->[WORK_RESPONSE] = HTTP::Response->new($1, $2);
65 $self->[WORK_RESPONSE]->protocol('HTTP/' . $self->[PROTOCOL_VERSION]);
66 $self->[CURRENT_STATE] = STATE_HEADER;
67 DEBUG and warn "Got a status line";
68 next LINE;
69 }
70 elsif ($line =~ m!^(\d{3})\s+(.+?)$!) {
71 $self->[PROTOCOL_VERSION] = 0.9;
72 $self->[WORK_RESPONSE] = HTTP::Response->new($1, $2);
73 $self->[WORK_RESPONSE]->protocol('HTTP/' . $self->[PROTOCOL_VERSION]);
74 $self->[CURRENT_STATE] = STATE_HEADER;
75 DEBUG and warn "Got a status line";
76 next LINE;
77 }
78 elsif ($line =~ m!^(\d{3})$!) {
79 $self->[PROTOCOL_VERSION] = 0.9;
80 $self->[WORK_RESPONSE] = HTTP::Response->new($1);
81 $self->[WORK_RESPONSE]->protocol('HTTP/' . $self->[PROTOCOL_VERSION]);
82 $self->[CURRENT_STATE] = STATE_HEADER;
83 DEBUG and warn "Got a status line";
84 next LINE;
85 }
86 elsif ($line =~ m!^HTTP/(\d+\.\d+)\s+(\d{3})\s+(.*?)\s*$!) {
87 $self->[PROTOCOL_VERSION] = $1;
88 $self->[WORK_RESPONSE] = HTTP::Response->new($2, $3);
89 $self->[WORK_RESPONSE]->protocol('HTTP/' . $self->[PROTOCOL_VERSION]);
90 $self->[CURRENT_STATE] = STATE_HEADER;
91 DEBUG and warn "Got a status line";
92 next LINE;
93 }
94 elsif ($line =~ m!^HTTP/(\d+\.\d+)\s+(\d{3})\s*$!) {
95 $self->[PROTOCOL_VERSION] = $1;
96 $self->[WORK_RESPONSE] = HTTP::Response->new($2);
97 $self->[WORK_RESPONSE]->protocol('HTTP/' . $self->[PROTOCOL_VERSION]);
98 $self->[CURRENT_STATE] = STATE_HEADER;
99 DEBUG and warn "Got a status line";
100 next LINE;
101 }
102  
103 # We have a line, but it doesn't look like a HTTP/1.1 status
104 # line. Assume it's an HTTP/0.9 response and fabricate headers.
105 # Also, put the line back. It's part of the content.
106 DEBUG and warn "Faking HTTP/0.9 headers (first line not status).\n";
107 my $resp = HTTP::Response->new (
108 '200', 'OK', ['Content-Type' => 'text/html'], $line
109 );
110 $resp->protocol('HTTP/0.9');
111 #unshift @{$self->[FRAMING_BUFFER]}, $line;
112 return [ $resp ];
113 }
114  
115 # A blank line signals the end of headers.
116 if ($line =~ /^\s*$/) {
117 DEBUG and warn "Got a blank line. End of headers.\n";
118 $self->[CURRENT_STATE] = STATE_STATUS;
119 return [$self->[WORK_RESPONSE]];
120 }
121  
122 # We have a potential header line. Try to identify it's end.
123 my $i = 0;
124 CONTINUATION: while ($i < @{$self->[FRAMING_BUFFER]}) {
125 # Forward-looking line begins with whitespace. It's a
126 # continuation of the previous line.
127 $i++, next CONTINUATION if $self->[FRAMING_BUFFER]->[$i] =~ /^\s+\S/;
128  
129 DEBUG and warn "Found end of header ($i)\n";
130  
131 # Forward-looking line isn't a continuation line. All buffer
132 # lines before it are part of the current header.
133 if ($i) {
134 $line .= $_ foreach (
135 map { s/^\s+//; $_ }
136 splice(@{$self->[FRAMING_BUFFER]}, 0, $i)
137 );
138 }
139  
140 DEBUG and warn "Full header read: $line\n";
141  
142 # And parse the line.
143 if (
144 $line =~ m{
145 ^
146 ([^\x00-\x19()<>@,;:\\""\/\[\]\?={}\x20\t]+):
147 \s*([^\x00-\x07\x09-\x19]+)
148 $
149 }x
150 ) {
151 DEBUG and warn " header($1) value($2)\n";
152 $self->[WORK_RESPONSE]->push_header($1, $2)
153 }
154  
155 next LINE;
156 }
157  
158 # We didn't find a complete header. Put the line back, and wait
159 # for more input.
160 DEBUG and warn "Incomplete header. Waiting for more.\n";
161 unshift @{$self->[FRAMING_BUFFER]}, $line;
162 return [];
163 }
164  
165 # Didn't return anything else, so we don't have anything.
166 return [];
167 }
168  
169 #=for future
170 #
171 #sub put {
172 # my ($self, $responses) = @_;
173 # my $out;
174 #
175 # foreach my $response (@$responses) {
176 # $out = $response->as_string
177 # }
178 #
179 # $out;
180 #}
181 #
182 #=cut
183  
184 sub get_pending {
185 my $self = shift;
186 return $self->[FRAMING_BUFFER];
187 }
188  
189 package POE::Filter::HTTPHead;
190 $POE::Filter::HTTPHead::VERSION = '0.949';
191 use strict;
192  
193 =head1 NAME
194  
195 POE::Filter::HTTPHead - filter data as HTTP::Response objects
196  
197 =head1 VERSION
198  
199 version 0.949
200  
201 =head1 SYNOPSYS
202  
203 $filter = POE::Filter::HTTPHead->new();
204 $arrayref_of_response_objects =
205 $filter->get($arrayref_of_raw_chunks_from_driver);
206  
207 $arrayref_of_leftovers = $filter->get_pending();
208  
209 =head1 DESCRIPTION
210  
211 The HTTPHead filter turns stream data that has the appropriate format
212 into a HTTP::Response object. In an all-POE world, this would sit on
213 the other end of a connection as L<POE::Filter::HTTPD>
214  
215 =cut
216  
217 use base qw(POE::Filter::Stackable);
218 use POE::Filter::Line;
219  
220 =head2 new
221  
222 Creates a new filter to parse HTTP headers. Takes no parameters, and
223 returns a shiny new POE::Filter::HTTPHead object.
224  
225 =cut
226  
227 sub new {
228 my $type = shift;
229  
230 # Look for EOL defined as linefeed. We'll strip off possible
231 # carriage returns in HTTPHead_Line's get_one_start().
232  
233 my $self = $type->SUPER::new(
234 Filters => [
235 POE::Filter::Line->new(Literal => "\x0A"),
236 POE::Filter::HTTPHead_Line->new,
237 ],
238 );
239  
240 return bless $self, $type;
241 }
242  
243 =head1 METHODS
244  
245 See L<POE::Filter> for documentation of the public API.
246  
247 =head2 get_pending
248  
249 Returns unparsed data pending in this filter's input buffer. It's
250 used by POE::Wheel objects to seamlessly switch between filters.
251  
252 Details may be found in the POE::Filter documentation.
253  
254 =cut
255  
256 sub get_pending {
257 my $self = shift;
258  
259 my @pending = map {"$_\n"} @{$self->[0]->[1]->get_pending};
260 my $lines = $self->[0]->[0]->get_pending;
261 push (@pending, @$lines) if (defined $lines);
262  
263 return \@pending;
264 }
265  
266 #=for future?
267 #
268 #sub put {
269 # my $self = shift;
270 # return $self->[0]->[1]->put (@_);
271 #}
272 #
273 #=cut
274  
275 1;