Skip to content
Browse files

minor code cleanup

  • Loading branch information...
1 parent ab4f25e commit 4ebda226ed578e6e8058eb23e8028f22bc97f7ac @vinoski vinoski committed Aug 27, 2010
Showing with 10 additions and 14 deletions.
  1. +10 −14 src/yaws_websockets.erl
View
24 src/yaws_websockets.erl
@@ -33,7 +33,8 @@ handshake(Arg, ContentPid, SocketMode) ->
undefined -> "ws://" ++ Host ++ Path;
_ -> "wss://" ++ Host ++ Path
end,
- Handshake = handshake(ProtocolVersion, Arg, CliSock, WebSocketLocation, Origin, Protocol),
+ Handshake = handshake(ProtocolVersion, Arg, CliSock,
+ WebSocketLocation, Origin, Protocol),
case SC#sconf.ssl of
undefined ->
gen_tcp:send(CliSock, Handshake),
@@ -56,7 +57,6 @@ handshake(Arg, ContentPid, SocketMode) ->
end,
exit(normal).
-
handshake(ws_76, Arg, CliSock, WebSocketLocation, Origin, Protocol) ->
{ok, Challenge} = gen_tcp:recv(CliSock, 8),
Key1 = secret_key("sec-websocket-key1", Arg#arg.headers),
@@ -70,22 +70,20 @@ handshake(ws_76, Arg, CliSock, WebSocketLocation, Origin, Protocol) ->
"Sec-WebSocket-Protocol: ", Protocol, "\r\n",
"\r\n", ChallengeResponse];
-handshake(ws_75, _Arg, _CliSock, WebSocketLocation, Origin, Protocol) ->
+handshake(ws_75, _Arg, _CliSock, WebSocketLocation, Origin, _Protocol) ->
["HTTP/1.1 101 Web Socket Protocol Handshake\r\n",
"Upgrade: WebSocket\r\n",
"Connection: Upgrade\r\n",
"WebSocket-Origin: ", Origin, "\r\n",
"WebSocket-Location: ", WebSocketLocation, "\r\n",
"\r\n"].
-
ws_version(Headers) ->
case query_header("sec-websocket-key1", Headers) of
undefined -> ws_75;
_ -> ws_76
end.
-
%% This should take care of all the Data Framing scenarios specified in
%% http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-66#page-26
unframe_one(DataFrames) ->
@@ -94,9 +92,10 @@ unframe_one(DataFrames) ->
T when (T =< 127) ->
%% {ok, FF_Ended_Frame} = re:compile("^.(.*)\\xFF(.*?)", [ungreedy]),
FF_Ended_Frame = {re_pattern,2,0,
- <<69,82,67,80,71,0,0,0,16,2,0,0,5,0,0,0,2,0,0,0,0,0,255,2,40,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93,0,27,25,12,94,0,7,0,1,57,
- 12,84,0,7,27,255,94,0,7,0,2,56,12,84,0,7,84,0,27,0>>},
+ <<69,82,67,80,71,0,0,0,16,2,0,0,5,0,0,0,2,0,0,0,
+ 0,0,255,2,40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93,0,
+ 27,25,12,94,0,7,0,1,57,12,84,0,7,27,255,94,0,7,
+ 0,2,56,12,84,0,7,84,0,27,0>>},
{match, [Data, NextFrame]} =
re:run(DataFrames, FF_Ended_Frame,
[{capture, all_but_first, binary}]),
@@ -123,10 +122,9 @@ get_origin_header(Headers) ->
get_protocol_header(Headers) ->
query_header("sec-websocket-protocol", Headers, "unknown").
-
query_header(HeaderName, Headers) ->
query_header(HeaderName, Headers, undefined).
-
+
query_header(Header, #headers{other=L}, Default) ->
lists:foldl(fun({http_header,_,K0,_,V}, undefined) ->
K = case is_atom(K0) of
@@ -150,12 +148,11 @@ secret_key(KeyName, Headers) ->
undefined ->
0;
Key ->
- Digits = lists:filter(fun(C) -> (C >= 48) andalso (C =< 57) end, Key),
+ Digits = lists:filter(fun(C) -> C >= 48 andalso C =< 57 end, Key),
NumberOfSpaces = length(lists:filter(fun(C) -> C == 32 end, Key)),
list_to_integer(Digits) div NumberOfSpaces
end.
-
challenge(Key1, Key2, Challenge) ->
BinaryAnswer =
crypto:md5_final(
@@ -172,7 +169,7 @@ digits32(Num) ->
Digit2 = Num3 rem 256,
Digit1 = Num3 div 256,
[Digit1, Digit2, Digit3, Digit4].
-
+
unpack_length(Binary, LenBytes, Length) ->
<<_, _:LenBytes/bytes, B, _/bitstring>> = Binary,
B_v = B band 16#7F,
@@ -183,4 +180,3 @@ unpack_length(Binary, LenBytes, Length) ->
0 ->
{NewLength, LenBytes + 1}
end.
-

0 comments on commit 4ebda22

Please sign in to comment.
Something went wrong with that request. Please try again.