Skip to content
Browse files

make wss:// websockets work over https

  • Loading branch information...
1 parent e724f27 commit ecc6f85090c91c59e6628adb393f0c3bd25cc61d @RJ RJ committed Nov 21, 2010
View
2 examples/websockets/index.html
@@ -24,7 +24,7 @@
function $() { return document.getElementById(arguments[0]); }
function go()
{
- ws = new WebSocket("ws://localhost:8080");
+ ws = new WebSocket("wss://localhost:8080/");
ws.onopen = function(){ $('connstate').innerHTML='CONNECTED'; }
ws.onclose = function(){ $('connstate').innerHTML='CLOSED'; }
ws.onmessage = function(e){ $('msgs').innerHTML = $('msgs').innerHTML + "<pre>"+e.data+"</pre>"; }
View
17 examples/websockets/websockets_active.erl
@@ -14,19 +14,24 @@ start(Options) ->
true
end,
% websocket options
- WsOpts = [ {active, true},
- {origin_validator, OriginValidator},
- {loop, {?MODULE, wsloop_active}} ],
-
+ WsOpts = [ {active, true},
+ {origin_validator, OriginValidator},
+ {loop, {?MODULE, wsloop_active}} ],
+ %
+ Ssl = [ {ssl, true}, {ssl_opts, [ {certfile, "../https/server_cert.pem"},
+ {keyfile, "../https/server_key.pem"}]} ],
+ %
mochiweb_http:start([{name, ?MODULE},
{loop, Loop},
- {websocket_opts, WsOpts} | Options1]).
+ {websocket_opts, WsOpts} | Options1] ++ Ssl).
stop() ->
mochiweb_http:stop(?MODULE).
wsloop_active(Pid) ->
- mochiweb_websocket_delegate:send(Pid, "WELCOME MSG FROM THE SERVER!"),
+
+ Ret = mochiweb_websocket_delegate:send(Pid, "WELCOME MSG FROM THE SERVER!"),
+ io:format("Sent welcome msg: ~p~n",[Ret]),
wsloop_active0(Pid).
wsloop_active0(Pid) ->
View
8 examples/websockets/websockets_passive.erl
@@ -17,9 +17,11 @@ start(Options) ->
WsOpts = [ {active, false},
{origin_validator, OriginValidator},
{loop, {?MODULE, wsloop}} ],
+ Ssl = [ {ssl, true}, {ssl_opts, [ {certfile, "../https/server_cert.pem"},
+ {keyfile, "../https/server_key.pem"}]} ],
mochiweb_http:start([{name, ?MODULE},
{loop, Loop},
- {websocket_opts, WsOpts} | Options1]).
+ {websocket_opts, WsOpts} | Options1] ++ Ssl).
stop() ->
mochiweb_http:stop(?MODULE).
@@ -30,7 +32,9 @@ wsloop(Ws) ->
wsloop0(Ws).
wsloop0(Ws) ->
- case Ws:get_data() of
+ Got = Ws:get_data(),
+ io:format("GOT:~p~n",[Got]),
+ case Got of
closed -> ok;
closing -> ok;
timeout -> timeout;
View
6 src/mochiweb_http.erl
@@ -295,11 +295,15 @@ websocket_init_with_origin_validated(Socket, Path, Headers, _Origin) ->
undefined -> "";
P -> ["Sec-WebSocket-Protocol: ", P, "\r\n"]
end,
+ HttpScheme = case mochiweb_socket:type(Socket) of
+ plain -> "http";
+ ssl -> "https"
+ end,
Data = ["HTTP/1.1 101 Web Socket Protocol Handshake\r\n",
"Upgrade: WebSocket\r\n",
"Connection: Upgrade\r\n",
"Sec-WebSocket-Location: ", Proto,Host,Path, "\r\n",
- "Sec-WebSocket-Origin: http://", Host, "\r\n",
+ "Sec-WebSocket-Origin: ", HttpScheme, "://", Host, "\r\n",
SubProtoHeader,
"\r\n",
<<Sig/binary>>
View
7 src/mochiweb_socket.erl
@@ -5,7 +5,7 @@
-module(mochiweb_socket).
-export([listen/4, accept/1, recv/3, send/2, close/1, port/1, peername/1,
- setopts/2, type/1]).
+ setopts/2, type/1, controlling_process/2]).
-define(ACCEPT_TIMEOUT, 2000).
@@ -77,6 +77,11 @@ setopts({ssl, Socket}, Opts) ->
setopts(Socket, Opts) ->
inet:setopts(Socket, Opts).
+controlling_process({ssl, Socket}, NewOwner) ->
+ ssl:controlling_process(Socket, NewOwner);
+controlling_process(Socket, NewOwner) ->
+ gen_tcp:controlling_process(Socket, NewOwner).
+
type({ssl, _}) ->
ssl;
type(_) ->
View
18 src/mochiweb_websocket_delegate.erl
@@ -38,7 +38,7 @@ start_link(Path, Headers, Destination) ->
gen_server:start_link(?MODULE, [Path, Headers, Destination], []).
go(Pid, Socket) ->
- ok = gen_tcp:controlling_process(Socket, Pid),
+ ok = mochiweb_socket:controlling_process(Socket, Pid),
gen_server:cast(Pid, {go, Socket}).
send(Pid, Msg) ->
@@ -89,16 +89,26 @@ handle_cast({go, Socket}, State) ->
handle_info({'EXIT', _, _}, State) ->
State#state.dest ! closed,
- {stop, normal, State};
-handle_info({tcp_closed, Sock}, State = #state{socket=Sock}) ->
+ {stop, normal, State};
+handle_info({ssl_closed, _Sock}, State) ->
+ State#state.dest ! closed,
+ {stop, normal, State};
+handle_info({tcp_closed, _Sock}, State) ->
State#state.dest ! closed,
{stop, normal, State};
-handle_info({tcp_error, Sock, Reason}, State = #state{socket=Sock}) ->
+handle_info({tcp_error, _Sock, Reason}, State) ->
+ State#state.dest ! {error, Reason},
+ State#state.dest ! closed,
+ {stop, normal, State};
+handle_info({ssl_error, _Sock, Reason}, State) ->
State#state.dest ! {error, Reason},
State#state.dest ! closed,
{stop, normal, State};
handle_info({tcp, Sock, Data}, State = #state{socket=Sock, buffer=Buffer}) ->
NewState = process_data(State#state{buffer= <<Buffer/binary,Data/binary>>}),
+ {noreply, NewState};
+handle_info({ssl, _Sock, Data}, State = #state{buffer=Buffer}) ->
+ NewState = process_data(State#state{buffer= <<Buffer/binary,Data/binary>>}),
{noreply, NewState}.
terminate(_Reason, _State) ->

0 comments on commit ecc6f85

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