| 1 | package POE::Filter::HTTPChunk; | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | $POE::Filter::HTTPChunk::VERSION = '0.949'; | ||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use bytes; | ||
| 9 | use base 'POE::Filter'; | ||
| 10 | |||
| 11 | use HTTP::Response; | ||
| 12 | |||
| 13 | use constant { | ||
| 14 | FRAMING_BUFFER => 0, | ||
| 15 | CURRENT_STATE => 1, | ||
| 16 | CHUNK_SIZE => 2, | ||
| 17 | CHUNK_BUFFER => 3, | ||
| 18 | TRAILER_HEADERS => 4, | ||
| 19 | }; | ||
| 20 | |||
| 21 | use constant { | ||
| 22 | STATE_SIZE => 0x01, # waiting for a status line | ||
| 23 | STATE_DATA => 0x02, # received status, looking for header or end | ||
| 24 | STATE_TRAILER => 0x04, # received status, looking for header or end | ||
| 25 | }; | ||
| 26 | |||
| 27 | use constant DEBUG => 0; | ||
| 28 | |||
| 29 | my $HEX = qr/[\dA-Fa-f]/o; | ||
| 30 | |||
| 31 | |||
| 32 | sub new { | ||
| 33 | my ($class) = @_; | ||
| 34 | |||
| 35 | my $self = bless [ | ||
| 36 | [], # FRAMING_BUFFER | ||
| 37 | STATE_SIZE, # CURRENT_STATE | ||
| 38 | 0, # CHUNK_SIZE | ||
| 39 | '', # CHUNK_BUFFER | ||
| 40 | undef, # TRAILER_HEADERS | ||
| 41 | ], $class; | ||
| 42 | |||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | =for later | ||
| 48 | |||
| 49 | my $TEXT = qr/[^[:cntrl:]]/o; | ||
| 50 | my $qdtext = qr/[^[:cntrl:]\"]/o; #<any TEXT except <">> | ||
| 51 | my $quoted_pair = qr/\\[[:ascii:]]/o; | ||
| 52 | my $quoted_string = qr/\"(?:$qdtext|$quoted_pair)\"/o; | ||
| 53 | my $separators = "[^()<>@,;:\\"\/\[\]\?={} \t"; | ||
| 54 | my $notoken = qr/(?:[[:cntrl:]$separators]/o; | ||
| 55 | |||
| 56 | my $chunk_ext_name = $token; | ||
| 57 | my $chunk_ext_val = qr/(?:$token|$quoted_string)/o; | ||
| 58 | |||
| 59 | my $chunk_extension = qr/(?:;$chunk_ext_name(?:$chunk_ext_val)?)/o; | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | |||
| 64 | sub get_one_start { | ||
| 65 | my ($self, $chunks) = @_; | ||
| 66 | |||
| 67 | #warn "GOT MORE DATA"; | ||
| 68 | push (@{$self->[FRAMING_BUFFER]}, @$chunks); | ||
| 69 | #warn "NUMBER OF CHUNKS is now ", scalar @{$self->[FRAMING_BUFFER]}; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub get_one { | ||
| 74 | my $self = shift; | ||
| 75 | |||
| 76 | my $retval = []; | ||
| 77 | while (defined (my $chunk = shift (@{$self->[FRAMING_BUFFER]}))) { | ||
| 78 | #warn "CHUNK IS SIZE ", length($chunk); | ||
| 79 | #warn join( | ||
| 80 | # ",", map {sprintf("%02X", ord($_))} split (//, substr ($chunk, 0, 10)) | ||
| 81 | #); | ||
| 82 | #warn "NUMBER OF CHUNKS is ", scalar @{$self->[FRAMING_BUFFER]}; | ||
| 83 | DEBUG and warn "STATE is ", $self->[CURRENT_STATE]; | ||
| 84 | |||
| 85 | # if we're not in STATE_DATA, we need to have a newline sequence | ||
| 86 | # in our hunk of content to find out how far we are. | ||
| 87 | unless ($self->[CURRENT_STATE] & STATE_DATA) { | ||
| 88 | if ($chunk !~ /.\015?\012/s) { | ||
| 89 | #warn "SPECIAL CASE"; | ||
| 90 | if (@{$self->[FRAMING_BUFFER]} == 0) { | ||
| 91 | #warn "pushing $chunk back"; | ||
| 92 | unshift (@{$self->[FRAMING_BUFFER]}, $chunk); | ||
| 93 | return $retval; | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | $chunk .= shift (@{$self->[FRAMING_BUFFER]}); | ||
| 97 | #warn "added to $chunk"; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | if ($self->[CURRENT_STATE] & STATE_SIZE) { | ||
| 103 | DEBUG and warn "Finding chunk length marker"; | ||
| 104 | if ( | ||
| 105 | $chunk =~ s/^($HEX+)[^\S\015\012]*(?:;.*?)?[^\S\015\012]*\015?\012//s | ||
| 106 | ) { | ||
| 107 | my $length = hex($1); | ||
| 108 | DEBUG and warn "Chunk should be $length bytes"; | ||
| 109 | $self->[CHUNK_SIZE] = $length; | ||
| 110 | if ($length == 0) { | ||
| 111 | $self->[TRAILER_HEADERS] = HTTP::Headers->new; | ||
| 112 | $self->[CURRENT_STATE] = STATE_TRAILER; | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | $self->[CURRENT_STATE] = STATE_DATA; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | # ok, this is a hack. skip to the next line if we | ||
| 120 | # don't find the chunk length, it might just be an extra | ||
| 121 | # line or something, and the chunk length always is on | ||
| 122 | # a line of it's own, so this seems the only way to recover | ||
| 123 | # somewhat. | ||
| 124 | #TODO: after discussing on IRC, the concensus was to return | ||
| 125 | #an error Response here, and have the client shut down the | ||
| 126 | #connection. | ||
| 127 | DEBUG and warn "DIDN'T FIND CHUNK LENGTH $chunk"; | ||
| 128 | my $replaceN = $chunk =~ s/.*?\015?\012//s; | ||
| 129 | unshift (@{$self->[FRAMING_BUFFER]}, $chunk) if ($replaceN == 1); | ||
| 130 | return $retval; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | if ($self->[CURRENT_STATE] & STATE_DATA) { | ||
| 135 | my $len = $self->[CHUNK_SIZE] - length ($self->[CHUNK_BUFFER]); | ||
| 136 | DEBUG and | ||
| 137 | warn "going for length ", $self->[CHUNK_SIZE], " (need $len more)"; | ||
| 138 | my $newchunk = $self->[CHUNK_BUFFER]; | ||
| 139 | $self->[CHUNK_BUFFER] = ""; | ||
| 140 | $newchunk .= substr ($chunk, 0, $len, ''); | ||
| 141 | #warn "got " . length($newchunk) . " bytes of data"; | ||
| 142 | if (length $newchunk != $self->[CHUNK_SIZE]) { | ||
| 143 | #smaller, so wait | ||
| 144 | $self->[CHUNK_BUFFER] = $newchunk; | ||
| 145 | next; | ||
| 146 | } | ||
| 147 | $self->[CURRENT_STATE] = STATE_SIZE; | ||
| 148 | #warn "BACK TO FINDING CHUNK SIZE $chunk"; | ||
| 149 | if (length ($chunk) > 0) { | ||
| 150 | DEBUG and warn "we still have a bit $chunk ", length($chunk); | ||
| 151 | #warn "'", substr ($chunk, 0, 10), "'"; | ||
| 152 | $chunk =~ s/^\015?\012//s; | ||
| 153 | #warn "'", substr ($chunk, 0, 10), "'"; | ||
| 154 | unshift (@{$self->[FRAMING_BUFFER]}, $chunk); | ||
| 155 | } | ||
| 156 | push @$retval, $newchunk; | ||
| 157 | #return [$newchunk]; | ||
| 158 | } | ||
| 159 | |||
| 160 | if ($self->[CURRENT_STATE] & STATE_TRAILER) { | ||
| 161 | while ($chunk =~ s/^([-\w]+):\s*(.*?)\015?\012//s) { | ||
| 162 | DEBUG and warn "add trailer header $1"; | ||
| 163 | $self->[TRAILER_HEADERS]->push_header ($1, $2); | ||
| 164 | } | ||
| 165 | #warn "leftover: ", $chunk; | ||
| 166 | #warn join ( | ||
| 167 | # ",", | ||
| 168 | # map {sprintf("%02X", ord($_))} split (//, substr ($chunk, 0, 10)) | ||
| 169 | #), "\n"; | ||
| 170 | if ($chunk =~ s/^\015?\012//s) { | ||
| 171 | my $headers = delete $self->[TRAILER_HEADERS]; | ||
| 172 | |||
| 173 | push (@$retval, $headers); | ||
| 174 | DEBUG and warn "returning ", scalar @$retval, "responses"; | ||
| 175 | unshift (@{$self->[FRAMING_BUFFER]}, $chunk) if (length $chunk); | ||
| 176 | return $retval; | ||
| 177 | } | ||
| 178 | if (@{$self->[FRAMING_BUFFER]}) { | ||
| 179 | $self->[FRAMING_BUFFER]->[0] = $chunk . $self->[FRAMING_BUFFER]->[0]; | ||
| 180 | } else { | ||
| 181 | unshift (@{$self->[FRAMING_BUFFER]}, $chunk); | ||
| 182 | return $retval; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | return $retval; | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | =for future | ||
| 191 | |||
| 192 | sub put { | ||
| 193 | die "not implemented yet"; | ||
| 194 | } | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | |||
| 199 | sub get_pending { | ||
| 200 | my $self = shift; | ||
| 201 | return $self->[FRAMING_BUFFER] if @{$self->[FRAMING_BUFFER]}; | ||
| 202 | return undef; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | __END__ |