mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Add 'Upgrade' header parsing
Not parsing product tokens, I don't see much point.
This commit is contained in:
parent
17c9d45786
commit
298ba1f1bb
2 changed files with 7 additions and 11 deletions
|
@ -227,13 +227,7 @@ parse_header(Name, Req=#http_req{p_headers=PHeaders}) ->
|
||||||
|
|
||||||
%% @doc Default values for semantic header parsing.
|
%% @doc Default values for semantic header parsing.
|
||||||
-spec parse_header_default(http_header()) -> any().
|
-spec parse_header_default(http_header()) -> any().
|
||||||
parse_header_default('Accept') -> undefined;
|
|
||||||
parse_header_default('Accept-Charset') -> undefined;
|
|
||||||
parse_header_default('Accept-Encoding') -> undefined;
|
|
||||||
parse_header_default('Accept-Language') -> undefined;
|
|
||||||
parse_header_default('Connection') -> [];
|
parse_header_default('Connection') -> [];
|
||||||
parse_header_default('If-Match') -> undefined;
|
|
||||||
parse_header_default('If-None-Match') -> undefined;
|
|
||||||
parse_header_default(_Name) -> undefined.
|
parse_header_default(_Name) -> undefined.
|
||||||
|
|
||||||
%% @doc Semantically parse headers.
|
%% @doc Semantically parse headers.
|
||||||
|
@ -288,6 +282,11 @@ parse_header(Name, Req, Default)
|
||||||
fun (Value) ->
|
fun (Value) ->
|
||||||
cowboy_http:http_date(Value)
|
cowboy_http:http_date(Value)
|
||||||
end);
|
end);
|
||||||
|
parse_header(Name, Req, Default) when Name =:= 'Upgrade' ->
|
||||||
|
parse_header(Name, Req, Default,
|
||||||
|
fun (Value) ->
|
||||||
|
cowboy_http:nonempty_list(Value, fun cowboy_http:token_ci/2)
|
||||||
|
end);
|
||||||
parse_header(Name, Req, Default) ->
|
parse_header(Name, Req, Default) ->
|
||||||
{Value, Req2} = header(Name, Req, Default),
|
{Value, Req2} = header(Name, Req, Default),
|
||||||
{undefined, Value, Req2}.
|
{undefined, Value, Req2}.
|
||||||
|
|
|
@ -72,16 +72,13 @@ upgrade(ListenerPid, Handler, Opts, Req) ->
|
||||||
{'EXIT', _Reason} -> upgrade_error(Req)
|
{'EXIT', _Reason} -> upgrade_error(Req)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @todo We need a function to properly parse headers according to their ABNF,
|
|
||||||
%% instead of having ugly code like this case here.
|
|
||||||
%% @todo Upgrade is a list of products and should be parsed as such.
|
|
||||||
-spec websocket_upgrade(#state{}, #http_req{}) -> {ok, #state{}, #http_req{}}.
|
-spec websocket_upgrade(#state{}, #http_req{}) -> {ok, #state{}, #http_req{}}.
|
||||||
websocket_upgrade(State, Req) ->
|
websocket_upgrade(State, Req) ->
|
||||||
{ConnTokens, Req2}
|
{ConnTokens, Req2}
|
||||||
= cowboy_http_req:parse_header('Connection', Req),
|
= cowboy_http_req:parse_header('Connection', Req),
|
||||||
true = lists:member(<<"upgrade">>, ConnTokens),
|
true = lists:member(<<"upgrade">>, ConnTokens),
|
||||||
{WS, Req3} = cowboy_http_req:header('Upgrade', Req2),
|
%% @todo Should probably send a 426 if the Upgrade header is missing.
|
||||||
<<"websocket">> = cowboy_bstr:to_lower(WS),
|
{[<<"websocket">>], Req3} = cowboy_http_req:parse_header('Upgrade', Req2),
|
||||||
{Version, Req4} = cowboy_http_req:header(<<"Sec-Websocket-Version">>, Req3),
|
{Version, Req4} = cowboy_http_req:header(<<"Sec-Websocket-Version">>, Req3),
|
||||||
websocket_upgrade(Version, State, Req4).
|
websocket_upgrade(Version, State, Req4).
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue