From 95e05d822f46e791a919f4e966879b4827989669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 29 Mar 2012 01:14:44 +0200 Subject: Add chunked transfer encoding support and rework the body reading API Introduces 3 low level functions and updates the existing higher levels functions. The new primitives are has_body/1, body_length/1 and stream_body/1. In addition to that, a helper function init_stream/4 has been added. Streaming a body implies to decode the Transfer-Encoding and Content-Encoding used for the body. By default, Cowboy will try to figure out what was used and decode them properly. You can override this if you want to disable this behavior or simply support more encodings by calling the init_stream/4 function before you start streaming the body. --- src/cowboy_http_websocket.erl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/cowboy_http_websocket.erl') diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl index f08405b..bc28712 100644 --- a/src/cowboy_http_websocket.erl +++ b/src/cowboy_http_websocket.erl @@ -175,12 +175,18 @@ websocket_handshake(State=#state{version=0, origin=Origin, %% We replied with a proper response. Proxies should be happy enough, %% we can now read the 8 last bytes of the challenge keys and send %% the challenge response directly to the socket. - case cowboy_http_req:body(8, Req2) of - {ok, Key3, Req3} -> + %% + %% We use a trick here to read exactly 8 bytes of the body regardless + %% of what's in the buffer. + {ok, Req3} = cowboy_http_req:init_stream( + fun cowboy_http:te_identity/2, {0, 8}, + fun cowboy_http:ce_identity/1, Req2), + case cowboy_http_req:body(Req3) of + {ok, Key3, Req4} -> Challenge = hixie76_challenge(Key1, Key2, Key3), Transport:send(Socket, Challenge), handler_before_loop(State#state{messages=Transport:messages()}, - Req3, HandlerState, <<>>); + Req4, HandlerState, <<>>); _Any -> closed %% If an error happened reading the body, stop there. end; -- cgit v1.2.3