Browse files

update to cowboy 0.8.3

- change rebar to actually see real errors
- update cowboy_echo example to cowboy 0.8
- cowboy_handler terminate now has 3 arguments instead of 2
    from: terminate(_Req, _Service) ->
    to:   terminate(_Reason, _Req, _Service) ->
- use cowboy_req:peer for ip/port of client
- update cowboy_test_server.erl to cowboy 0.8
- update README.md
- update usage of cowboy_req:transport to cowboy_req:get for cowboy 0.8
- cowboy 0.8 has all cookies lower cased (fixes JsessionidCookie.test_xhr)
  • Loading branch information...
1 parent 0accaee commit 5a9187174d2a428a4df83a7c503fb351eef97108 @gebi gebi committed Apr 8, 2013
Showing with 38 additions and 39 deletions.
  1. +13 −9 README.md
  2. +6 −4 examples/cowboy_echo.erl
  3. +8 −7 examples/cowboy_test_server.erl
  4. +3 −4 rebar.config
  5. +2 −2 src/sockjs_cowboy_handler.erl
  6. +6 −13 src/sockjs_http.erl
View
22 README.md
@@ -28,25 +28,29 @@ like this:
```erlang
main(_) ->
- application:start(sockjs),
- application:start(cowboy),
+ ok = application:start(sockjs),
+ ok = application:start(ranch),
+ ok = application:start(crypto),
+ ok = application:start(cowboy),
SockjsState = sockjs_handler:init_state(
<<"/echo">>, fun service_echo/3, state, []),
- Routes = [{'_', [{[<<"echo">>, '...'],
+ Routes = [{'_', [{<<"/echo/[...]">>,
sockjs_cowboy_handler, SockjsState}]}],
+ Dispatch = cowboy_router:compile(Routes),
- cowboy:start_listener(http, 100,
- cowboy_tcp_transport, [{port, 8081}],
- cowboy_http_protocol, [{dispatch, Routes}]),
+ cowboy:start_http(cowboy_test_http_listener, 100,
+ [{port, 8081}],
+ [{env, [{dispatch, Dispatch}]}]),
receive
_ -> ok
end.
-service_echo(_Conn, init, state) -> {ok, state};
-service_echo(Conn, {recv, Data}, state) -> Conn:send(Data);
-service_echo(_Conn, closed, state) -> {ok, state}.
+service_echo(_Conn, init, state) -> {ok, state};
+service_echo(Conn, {recv, Data}, state) -> Conn:send(Data);
+service_echo(_Conn, {info, _Info}, state) -> {ok, state};
+service_echo(_Conn, closed, state) -> {ok, state}.
```
Dig into the `examples` directory to get working code:
View
10 examples/cowboy_echo.erl
@@ -6,7 +6,7 @@
-export([main/1]).
%% Cowboy callbacks
--export([init/3, handle/2, terminate/2]).
+-export([init/3, handle/2, terminate/3]).
main(_) ->
@@ -19,14 +19,15 @@ main(_) ->
SockjsState = sockjs_handler:init_state(
<<"/echo">>, fun service_echo/3, state, []),
- VhostRoutes = [{[<<"echo">>, '...'], sockjs_cowboy_handler, SockjsState},
+ VhostRoutes = [{<<"/echo/[...]">>, sockjs_cowboy_handler, SockjsState},
{'_', ?MODULE, []}],
Routes = [{'_', VhostRoutes}], % any vhost
+ Dispatch = cowboy_router:compile(Routes),
io:format(" [*] Running at http://localhost:~p~n", [Port]),
cowboy:start_http(cowboy_echo_http_listener, 100,
[{port, Port}],
- [{dispatch, Routes}]),
+ [{env, [{dispatch, Dispatch}]}]),
receive
_ -> ok
end.
@@ -42,11 +43,12 @@ handle(Req, State) ->
Data, Req),
{ok, Req1, State}.
-terminate(_Req, _State) ->
+terminate(_Reason, _Req, _State) ->
ok.
%% --------------------------------------------------------------------------
service_echo(_Conn, init, state) -> {ok, state};
service_echo(Conn, {recv, Data}, state) -> Conn:send(Data);
+service_echo(_Conn, {info, _Info}, state) -> {ok, state};
service_echo(_Conn, closed, state) -> {ok, state}.
View
15 examples/cowboy_test_server.erl
@@ -32,22 +32,23 @@ main(_) ->
<<"/cookie_needed_echo">>, fun service_echo/3, state,
[{cookie_needed, true}]),
- VRoutes = [{[<<"echo">>, '...'], sockjs_cowboy_handler, StateEcho},
- {[<<"close">>, '...'], sockjs_cowboy_handler, StateClose},
- {[<<"amplify">>, '...'], sockjs_cowboy_handler, StateAmplify},
- {[<<"broadcast">>, '...'], sockjs_cowboy_handler, StateBroadcast},
- {[<<"disabled_websocket_echo">>, '...'], sockjs_cowboy_handler,
+ VRoutes = [{<<"/echo/[...]">>, sockjs_cowboy_handler, StateEcho},
+ {<<"/close/[...]">>, sockjs_cowboy_handler, StateClose},
+ {<<"/amplify/[...]">>, sockjs_cowboy_handler, StateAmplify},
+ {<<"/broadcast/[...]">>, sockjs_cowboy_handler, StateBroadcast},
+ {<<"/disabled_websocket_echo/[...]">>, sockjs_cowboy_handler,
StateDWSEcho},
- {[<<"cookie_needed_echo">>, '...'], sockjs_cowboy_handler,
+ {<<"/cookie_needed_echo/[...]">>, sockjs_cowboy_handler,
StateCNEcho},
{'_', ?MODULE, []}],
Routes = [{'_', VRoutes}], % any vhost
+ Dispatch = cowboy_router:compile(Routes),
io:format(" [*] Running at http://localhost:~p~n", [Port]),
cowboy:start_http(cowboy_test_server_http_listener, 100,
[{port, Port}],
- [{dispatch, Routes}]),
+ [{env, [{dispatch, Dispatch}]}]),
receive
_ -> ok
end.
View
7 rebar.config
@@ -4,13 +4,12 @@
{erl_opts, [
%% fail_on_warning,
- bin_opt_info,
- warn_missing_spec,
+ %bin_opt_info,
+ %warn_missing_spec,
debug_info,
warn_export_all
]}.
{deps, [
- {cowboy, ".*",
- {git, "git://github.com/extend/cowboy.git"}}
+ {cowboy, "0.8.3",{git, "https://github.com/extend/cowboy.git", "0.8.3"}}
]}.
View
4 src/sockjs_cowboy_handler.erl
@@ -3,7 +3,7 @@
-behaviour(cowboy_websocket_handler).
%% Cowboy http callbacks
--export([init/3, handle/2, terminate/2]).
+-export([init/3, handle/2, terminate/3]).
%% Cowboy ws callbacks
-export([websocket_init/3, websocket_handle/3,
@@ -25,7 +25,7 @@ handle(Req, Service) ->
{cowboy, Req3} = sockjs_handler:handle_req(Service, {cowboy, Req}),
{ok, Req3, Service}.
-terminate(_Req, _Service) ->
+terminate(_Reason, _Req, _Service) ->
ok.
%% --------------------------------------------------------------------------
View
19 src/sockjs_http.erl
@@ -70,7 +70,7 @@ header(K, {cowboy, Req})->
-spec jsessionid(req()) -> {nonempty_string() | undefined, req()}.
jsessionid({cowboy, Req}) ->
- {C, Req2} = cowboy_req:cookie(<<"JSESSIONID">>, Req),
+ {C, Req2} = cowboy_req:cookie(<<"jsessionid">>, Req),
case C of
_ when is_binary(C) ->
{binary_to_list(C), {cowboy, Req2}};
@@ -93,14 +93,7 @@ peername({cowboy, Req}) ->
-spec sockname(req()) -> {{inet:ip_address(), non_neg_integer()}, req()}.
sockname({cowboy, Req} = R) ->
- {ok, _T, S} = cowboy_req:transport(Req),
- %% Cowboy has peername(), but doesn't have sockname() equivalent.
- {ok, Addr} = case S of
- _ when is_port(S) ->
- inet:sockname(S);
- _ ->
- {ok, {{0,0,0,0}, 0}}
- end,
+ {Addr, _Req} = cowboy_req:peer(Req),
{Addr, R}.
%% --------------------------------------------------------------------------
@@ -133,18 +126,18 @@ enbinary(L) -> [{list_to_binary(K), list_to_binary(V)} || {K, V} <- L].
-spec hook_tcp_close(req()) -> req().
hook_tcp_close(R = {cowboy, Req}) ->
- {ok, T, S} = cowboy_req:transport(Req),
+ [T, S] = cowboy_req:get([transport, socket], Req),
T:setopts(S,[{active,once}]),
R.
-spec unhook_tcp_close(req()) -> req().
unhook_tcp_close(R = {cowboy, Req}) ->
- {ok, T, S} = cowboy_req:transport(Req),
+ [T, S] = cowboy_req:get([transport, socket], Req),
T:setopts(S,[{active,false}]),
R.
-spec abruptly_kill(req()) -> req().
abruptly_kill(R = {cowboy, Req}) ->
- {ok, T, S} = cowboy_req:transport(Req),
- T:close(S),
+ [T, S] = cowboy_req:get([transport, socket], Req),
+ ok = T:close(S),
R.

0 comments on commit 5a91871

Please sign in to comment.