From 381c1780738cbb02f0065f92991bc43ccd53a0c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 20 Oct 2011 14:11:17 +0200 Subject: Add a cowboy_http_req:upgrade_reply/3 function and use it for websockets This function doesn't try to add any additional header besides the Connection: Upgrade header. It also doesn't accept a body. It should be used for the intermediate reply to an upgrade process, before the real reply is sent (if any, for example when using TLS). --- src/cowboy_http_req.erl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/cowboy_http_req.erl') diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl index da7c16f..06f1e6b 100644 --- a/src/cowboy_http_req.erl +++ b/src/cowboy_http_req.erl @@ -38,7 +38,8 @@ -export([ reply/2, reply/3, reply/4, - chunked_reply/2, chunked_reply/3, chunk/2 + chunked_reply/2, chunked_reply/3, chunk/2, + upgrade_reply/3 ]). %% Response API. -export([ @@ -374,6 +375,17 @@ chunk(Data, #http_req{socket=Socket, transport=Transport, resp_state=chunks}) -> Transport:send(Socket, [integer_to_list(iolist_size(Data), 16), <<"\r\n">>, Data, <<"\r\n">>]). +%% @doc Send an upgrade reply. +-spec upgrade_reply(http_status(), http_headers(), #http_req{}) + -> {ok, #http_req{}}. +upgrade_reply(Status, Headers, Req=#http_req{socket=Socket, transport=Transport, + resp_state=waiting}) -> + Head = response_head(Status, Headers, [ + {<<"Connection">>, <<"Upgrade">>} + ]), + Transport:send(Socket, Head), + {ok, Req#http_req{resp_state=done}}. + %% Misc API. %% @doc Compact the request data by removing all non-system information. -- cgit v1.2.3