Permalink
Browse files

rename #http to #ws_http to avoid confusion + fix check_headers function

  • Loading branch information...
1 parent 463e81b commit 47f2240174013d24d51194978c10cb1f6e465607 @nniclausse nniclausse committed Oct 2, 2012
Showing with 274 additions and 211 deletions.
  1. +235 −176 doc/user_manual.html
  2. +1 −1 include/ts_websocket.hrl
  3. +22 −20 src/lib/http_utils.erl
  4. +11 −9 src/lib/websocket.erl
  5. +5 −5 src/tsung/ts_websocket.erl
View
Oops, something went wrong.
@@ -44,7 +44,7 @@
accept % Sec-Websocket-Accept header value
}).
--record(http, {
+-record(ws_http, {
status,
accept,
headers=[],
@@ -9,7 +9,7 @@
%%--------------------------------------------------------------------
%% Func: parse_headers/3
%% Purpose: Parse HTTP headers line by line
-%% Returns: {ok, #http, Body}
+%% Returns: {ok, #ws_http, Body}
%%--------------------------------------------------------------------
parse_headers(Http, Tail) ->
case get_line(Tail) of
@@ -21,23 +21,25 @@ parse_headers(Http, Tail) ->
check_headers(Headers, RequiredHeaders) ->
F = fun({Tag, Val}) ->
- Term = if
- is_atom (Tag) -> Tag;
- true -> string:to_lower(Tag)
- end,
%% see if the required Tag is in the Headers
- case proplists:get_value(Term, Headers) of
+ case proplists:get_value(Tag, Headers) of
%% header not found, keep in list
- false -> true;
+ 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
- _ -> true
- end
+ H ->
+ case string:to_lower(HVal) of
+ Val -> false;
+ _ ->
+ ?LOGF("wrong val ~p ~p~n",[HVal,Val],?DEB),
+ true
+ end
+ end
end
end,
@@ -49,32 +51,32 @@ check_headers(Headers, RequiredHeaders) ->
%%--------------------------------------------------------------------
%% Func: parse_status/2
%% Purpose: Parse HTTP status
-%% Returns: #http
+%% Returns: #ws_http
%%--------------------------------------------------------------------
parse_status([A,B,C|_], Http) ->
Status=list_to_integer([A,B,C]),
- Http#http{status = Status}.
+ Http#ws_http{status = Status}.
%%--------------------------------------------------------------------
%% Func: parse_line/3
%% Purpose: Parse a HTTP header
-%% Returns: #http
+%% 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#http{close=true});
+ parse_status(TailLine, Http#ws_http{close=true});
parse_line("upgrade: " ++ TailLine, Http) ->
- Headers = [{'Upgrade', TailLine} | Http#http.headers],
- Http#http{headers=Headers};
+ Headers = [{'Upgrade', TailLine} | Http#ws_http.headers],
+ Http#ws_http{headers=Headers};
parse_line("connection: " ++ TailLine, Http) ->
- Headers = [{'Connection', TailLine} | Http#http.headers],
- Http#http{headers=Headers};
+ 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#http.headers],
- Http#http{headers=Headers, accept=TailLine};
+ Headers = [{'Sec-WebSocket-Accept', TailLine} |
+ Http#ws_http.headers],
+ Http#ws_http{headers=Headers, accept=TailLine};
parse_line(_Line, Http) ->
Http.
View
@@ -50,7 +50,7 @@ handshake_request(Host, Path, SubProtocol, Version) ->
"Sec-WebSocket-Protocol: " ++ SubProtocol ++ "\r\n"
end,
Handshake = list_to_binary([
- Req, Value,
+ Req, Value,
"Sec-WebSocket-Key: ", Key, "\r\n",
"Pragma: no-cache:\r\n",
"Cache-control: no-cache:\r\n",
@@ -59,25 +59,27 @@ handshake_request(Host, Path, SubProtocol, Version) ->
{Handshake, Accept}.
check_handshake(Response, Accept) ->
+ ?DebugF("Check handshake, response is : ~p~n",[Response]),
RespString = binary_to_list(Response),
- case http_utils:parse_headers(#http{}, RespString) of
- {ok, Result=#http{status = 101}} ->
+ case http_utils:parse_headers(#ws_http{}, RespString) of
+ {ok, Result=#ws_http{status = 101}} ->
RequiredHeaders = [
{'Upgrade', "websocket"},
- {'Connection', "Upgrade"},
- {'Sec-Websocket-Accept', ignore}
+ {'Connection', "upgrade"},
+ {'Sec-WebSocket-Accept', ignore}
],
- case http_utils:check_headers(Result#http.headers,
+ case http_utils:check_headers(Result#ws_http.headers,
RequiredHeaders) of
true ->
- RecvAcc = Result#http.accept,
- case RecvAcc of
+ RecvAcc = Result#ws_http.accept,
+ case RecvAcc of
Accept ->
ok;
_ ->
{error, mismatch_accept}
end;
- _ ->
+ Miss ->
+ ?LOGF("Websocket handshake: Missing header ~p~n",[Miss],?DEB),
{error, miss_headers}
end;
_ ->
@@ -106,21 +106,21 @@ parse(Data, State=#state_rcv{acc = [], datasize= 0}) ->
%% handshake stage, parse response, and validate
parse(Data, State=#state_rcv{acc = [],
- session = WebsocketSession})
+ session = WebsocketSession})
when WebsocketSession#websocket_session.status == waiting_handshake ->
Acc = list_to_binary(State#state_rcv.acc),
Header = <<Acc/binary, Data/binary>>,
Accept = WebsocketSession#websocket_session.accept,
case websocket:check_handshake(Header, Accept) of
ok ->
- ?Debug("handshake success: ~n"),
- ts_mon:add({count, websocket_succ}),
- {State#state_rcv{ack_done = true,
+ ?Debug("handshake success:~n"),
+ ts_mon:add({count, websocket_succ}),
+ {State#state_rcv{ack_done = true,
session = WebsocketSession#websocket_session{
status = connected}}, [], false};
{error, Reason} ->
- ?DebugF("handshake fail: ~n", [Reason]),
+ ?DebugF("handshake fail: ~p~n", [Reason]),
ts_mon:add({count, websocket_fail}),
{State#state_rcv{ack_done = true}, [], true}
end;

0 comments on commit 47f2240

Please sign in to comment.