0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-16 05:00:24 +00:00

Add meta/2 and meta/3 to cowboy_http_req to save useful protocol information

* cowboy_http_protocol now defines 'websocket_version' as metadata.
* cowboy_http_rest now defines 'media_type', 'language', 'charset' as metadata.
This commit is contained in:
Loïc Hoguin 2011-12-19 09:44:24 +01:00
parent f9bd5d1061
commit f390dbd606
4 changed files with 40 additions and 17 deletions

View file

@ -30,9 +30,9 @@
%% <li>Firefox 6</li>
%% </ul>
%%
%% Version 8 is supported by the following browsers:
%% Version 8+ is supported by the following browsers:
%% <ul>
%% <li>Firefox 7</li>
%% <li>Firefox 7+</li>
%% <li>Chrome 14+</li>
%% </ul>
-module(cowboy_http_websocket).
@ -95,23 +95,24 @@ websocket_upgrade(State, Req) ->
%% third part of the challenge key, because proxies will wait for
%% a reply before sending it. Therefore we calculate the challenge
%% key only in websocket_handshake/3.
websocket_upgrade(undefined, State, Req) ->
websocket_upgrade(undefined, State, Req=#http_req{meta=Meta}) ->
{Origin, Req2} = cowboy_http_req:header(<<"Origin">>, Req),
{Key1, Req3} = cowboy_http_req:header(<<"Sec-Websocket-Key1">>, Req2),
{Key2, Req4} = cowboy_http_req:header(<<"Sec-Websocket-Key2">>, Req3),
false = lists:member(undefined, [Origin, Key1, Key2]),
EOP = binary:compile_pattern(<< 255 >>),
{ok, State#state{version=0, origin=Origin, challenge={Key1, Key2},
eop=EOP}, Req4};
eop=EOP}, Req4#http_req{meta=[{websocket_version, 0}|Meta]}};
%% Versions 7 and 8. Implementation follows the hybi 7 through 17 drafts.
websocket_upgrade(Version, State, Req)
websocket_upgrade(Version, State, Req=#http_req{meta=Meta})
when Version =:= <<"7">>; Version =:= <<"8">>;
Version =:= <<"13">> ->
{Key, Req2} = cowboy_http_req:header(<<"Sec-Websocket-Key">>, Req),
false = Key =:= undefined,
Challenge = hybi_challenge(Key),
IntVersion = list_to_integer(binary_to_list(Version)),
{ok, State#state{version=IntVersion, challenge=Challenge}, Req2}.
{ok, State#state{version=IntVersion, challenge=Challenge},
Req2#http_req{meta=[{websocket_version, IntVersion}|Meta]}}.
-spec handler_init(#state{}, #http_req{}) -> closed | none().
handler_init(State=#state{handler=Handler, opts=Opts},