From d42bcca517d4e42c0aed0bf7d63cb8956a9aa6fb Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Fri, 12 Apr 2013 21:03:15 -0600 Subject: [PATCH] use parse_http_response to parse the backend response --- lib/Plack/App/Proxy/WebSocket.pm | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/Plack/App/Proxy/WebSocket.pm b/lib/Plack/App/Proxy/WebSocket.pm index c0a7cde..70b4d4f 100644 --- a/lib/Plack/App/Proxy/WebSocket.pm +++ b/lib/Plack/App/Proxy/WebSocket.pm @@ -6,8 +6,8 @@ use strict; use AnyEvent::Handle; use AnyEvent::Socket; +use HTTP::Parser::XS qw/parse_http_response HEADERS_AS_ARRAYREF/; use HTTP::Request; -use HTTP::Response; use Plack::Request; use URI; @@ -109,20 +109,17 @@ sub call { my $hdl = shift; my $buf = delete $hdl->{rbuf}; - if ($writer) { - $writer->write($buf); - return; - } - - if (($buffer .= $buf) =~ s/^(.+\r?\n\r?\n)//s) { - my $http = HTTP::Response->parse($1); - my @headers; - $http->headers->remove_header('Status'); - $http->headers->scan(sub { push @headers, @_ }); - $writer = $res->([$http->code, [@headers]]); - $writer->write($buffer) if $buffer; - $buffer = undef; - } + return $writer->write($buf) if $writer; + $buffer .= $buf; + + my ($ret, $http_version, $status, $message, $headers) = + parse_http_response($buffer, HEADERS_AS_ARRAYREF); + $server->push_shutdown if $ret == -2; + return if $ret < 0; + + $writer = $res->([$status, $headers]); + $writer->write(substr($buffer, $ret)); + $buffer = undef; }); # shut down the sockets and exit the loop if an error occurs @@ -135,7 +132,7 @@ sub call { $server->on_error(sub { $server->destroy; # get the client handle's attention - $writer->close; + $client->push_shutdown; }); }; -- 2.45.2