43
while (defined (my $line = shift (@{$self->[FRAMING_BUFFER]}))) {
44
DEBUG and warn "LINE $line";
45
if ($self->[CURRENT_STATE] == STATE_STATUS) {
46
DEBUG and warn "in status";
47
# Expect a status line.
48
if ($line =~ m|^(?:HTTP/(\d+\.\d+) )?(\d{3})\s*(.+)?$|) {
49
$self->[PROTOCOL_VERSION] = $1 if defined $1;
50
$self->[WORK_RESPONSE] = HTTP::Response->new ($2, $3);
51
$self->[WORK_RESPONSE]->protocol('HTTP/' . $self->[PROTOCOL_VERSION]);
52
$self->[CURRENT_STATE] = STATE_HEADER;
56
my $resp = HTTP::Response->new (
57
'200', 'OK', ['Content-Type' => 'text/html'], $line
59
$resp->protocol('HTTP/0.9');
65
$self->[CURRENT_STATE] = STATE_STATUS;
66
DEBUG and warn "return response";
67
return [$self->[WORK_RESPONSE]];
69
DEBUG and warn "in headers";
70
unless (@{$self->[FRAMING_BUFFER]} > 0) {
71
unshift (@{$self->[FRAMING_BUFFER]}, $line);
74
DEBUG and warn "got more lines";
75
while ($self->[FRAMING_BUFFER]->[0] && $self->[FRAMING_BUFFER]->[0] =~ /^[\t ]/) {
76
my $next_line = shift (@{$self->[FRAMING_BUFFER]});
77
$next_line =~ s/^[\t ]+//;
80
#warn "unfolded one: $line";
84
([^\x00-\x19()<>@,;:\\""\/\[\]\?={}\x20\t]+):
85
\s*([^\x00-\x07\x09-\x19]+)
89
$self->[WORK_RESPONSE]->push_header($1, $2)
47
# Process lines while we have them.
48
LINE: while (@{$self->[FRAMING_BUFFER]}) {
49
my $line = shift @{$self->[FRAMING_BUFFER]};
51
# Waiting for a status line.
52
if ($self->[CURRENT_STATE] == STATE_STATUS) {
53
DEBUG and warn "----- Waiting for a status line.\n";
55
# Does the line look like a status line?
56
if ($line =~ m|^(?:HTTP/(\d+\.\d+) )?(\d{3})\s*(.+)?$|) {
57
$self->[PROTOCOL_VERSION] = $1 if defined $1;
58
$self->[WORK_RESPONSE] = HTTP::Response->new ($2, $3);
59
$self->[WORK_RESPONSE]->protocol('HTTP/' . $self->[PROTOCOL_VERSION]);
60
$self->[CURRENT_STATE] = STATE_HEADER;
62
# We're done with the line. Try the next one.
63
DEBUG and warn "Got a status line.\n";
67
# We have a line, but it doesn't look like a HTTP/1.1 status
68
# line. Assume it's an HTTP/0.9 response and fabricate headers.
69
# Also, put the line back. It's part of the content.
70
DEBUG and warn "Faking HTTP/0.9 headers (first line not status).\n";
71
my $resp = HTTP::Response->new (
72
'200', 'OK', ['Content-Type' => 'text/html'], $line
74
$resp->protocol('HTTP/0.9');
75
#unshift @{$self->[FRAMING_BUFFER]}, $line;
79
# A blank line signals the end of headers.
80
if ($line =~ /^\s*$/) {
81
DEBUG and warn "Got a blank line. End of headers.\n";
82
$self->[CURRENT_STATE] = STATE_STATUS;
83
return [$self->[WORK_RESPONSE]];
86
# We have a potential header line. Try to identify it's end.
88
CONTINUATION: while ($i < @{$self->[FRAMING_BUFFER]}) {
89
# Forward-looking line begins with whitespace. It's a
90
# continuation of the previous line.
91
$i++, next CONTINUATION if $self->[FRAMING_BUFFER]->[$i] =~ /^\s+\S/;
93
DEBUG and warn "Found end of header ($i)\n";
95
# Forward-looking line isn't a continuation line. All buffer
96
# lines before it are part of the current header.
100
splice(@{$self->[FRAMING_BUFFER]}, 0, $i)
104
DEBUG and warn "Full header read: $line\n";
106
# And parse the line.
110
([^\x00-\x19()<>@,;:\\""\/\[\]\?={}\x20\t]+):
111
\s*([^\x00-\x07\x09-\x19]+)
115
DEBUG and warn " header($1) value($2)\n";
116
$self->[WORK_RESPONSE]->push_header($1, $2)
122
# We didn't find a complete header. Put the line back, and wait
124
DEBUG and warn "Incomplete header. Waiting for more.\n";
125
unshift @{$self->[FRAMING_BUFFER]}, $line;
129
# Didn't return anything else, so we don't have anything.