Skip to content

Commit

Permalink
mv function from http_utils into websocket module and remove http_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
nniclausse committed Oct 2, 2012
1 parent acb7082 commit 68348c9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 84 deletions.
82 changes: 0 additions & 82 deletions src/lib/http_utils.erl

This file was deleted.

80 changes: 78 additions & 2 deletions src/lib/websocket.erl
Expand Up @@ -61,14 +61,14 @@ handshake_request(Host, Path, SubProtocol, Version) ->
check_handshake(Response, Accept) -> check_handshake(Response, Accept) ->
?DebugF("Check handshake, response is : ~p~n",[Response]), ?DebugF("Check handshake, response is : ~p~n",[Response]),
RespString = binary_to_list(Response), RespString = binary_to_list(Response),
case http_utils:parse_headers(#ws_http{}, RespString) of case parse_headers(#ws_http{}, RespString) of
{ok, Result=#ws_http{status = 101}} -> {ok, Result=#ws_http{status = 101}} ->
RequiredHeaders = [ RequiredHeaders = [
{'Upgrade', "websocket"}, {'Upgrade', "websocket"},
{'Connection', "upgrade"}, {'Connection', "upgrade"},
{'Sec-WebSocket-Accept', ignore} {'Sec-WebSocket-Accept', ignore}
], ],
case http_utils:check_headers(Result#ws_http.headers, case check_headers(Result#ws_http.headers,
RequiredHeaders) of RequiredHeaders) of
true -> true ->
RecvAcc = Result#ws_http.accept, RecvAcc = Result#ws_http.accept,
Expand Down Expand Up @@ -213,3 +213,79 @@ handle_data(<<_Fin:1, 0:3, _Opcode:4, 1:1, _PayloadLen:7,
_PayloadData/binary>>) -> _PayloadData/binary>>) ->
%% Error, Server to client message can't be masked %% Error, Server to client message can't be masked
{close, masked}. {close, masked}.


%%--------------------------------------------------------------------
%% Func: parse_headers/3
%% Purpose: Parse HTTP headers line by line
%% Returns: {ok, #ws_http, Body}
%%--------------------------------------------------------------------
parse_headers(Http, Tail) ->
case ts_http_common:get_line(Tail) of
{line, Line, Tail2} ->
parse_headers(parse_line(Line, Http), Tail2);
{lastline, Line, _} ->
{ok, parse_line(Line, Http)}
end.

check_headers(Headers, RequiredHeaders) ->
F = fun({Tag, Val}) ->
%% see if the required Tag is in the Headers
case proplists:get_value(Tag, Headers) of
%% header not found, keep in list
undefined -> true;
HVal ->
case Val of
%% ignore value -> ok, remove from list
ignore -> false;
%% expected val -> ok, remove from list
HVal -> false;
%% val is different, keep in list
H ->
case string:to_lower(HVal) of
Val -> false;
_ ->
?LOGF("wrong val ~p ~p~n",[HVal,Val],?DEB),
true
end
end
end
end,

case lists:filter(F, RequiredHeaders) of
[] -> true;
MissingHeaders -> MissingHeaders
end.

%%--------------------------------------------------------------------
%% Func: parse_status/2
%% Purpose: Parse HTTP status
%% Returns: #ws_http
%%--------------------------------------------------------------------
parse_status([A,B,C|_], Http) ->
Status=list_to_integer([A,B,C]),
ts_mon:add({ count, Status }),
Http#ws_http{status = Status}.

%%--------------------------------------------------------------------
%% Func: parse_line/3
%% Purpose: Parse a HTTP header
%% Returns: #ws_http
%%--------------------------------------------------------------------
parse_line("http/1.1 " ++ TailLine, Http)->
parse_status(TailLine, Http);
parse_line("http/1.0 " ++ TailLine, Http)->
parse_status(TailLine, Http#ws_http{close=true});

parse_line("upgrade: " ++ TailLine, Http) ->
Headers = [{'Upgrade', TailLine} | Http#ws_http.headers],
Http#ws_http{headers=Headers};
parse_line("connection: " ++ TailLine, Http) ->
Headers = [{'Connection', TailLine} | Http#ws_http.headers],
Http#ws_http{headers=Headers};
parse_line("sec-websocket-accept: " ++ TailLine, Http) ->
Headers = [{'Sec-WebSocket-Accept', TailLine} |
Http#ws_http.headers],
Http#ws_http{headers=Headers, accept=TailLine};
parse_line(_Line, Http) ->
Http.

0 comments on commit 68348c9

Please sign in to comment.