From b813607d036251dcdb977500b6a75c1a110825c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 14 Feb 2013 17:24:38 +0100 Subject: Update to Cowboy 0.8.0 --- examples/clock/src/clock.erl | 2 ++ examples/clock/src/clock_app.erl | 15 ++++++------ examples/clock/src/toppage_handler.erl | 6 ++--- rebar.config | 2 +- src/bullet_handler.erl | 44 ++++++++++++++++++---------------- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/examples/clock/src/clock.erl b/examples/clock/src/clock.erl index a4a2988..e8547e1 100644 --- a/examples/clock/src/clock.erl +++ b/examples/clock/src/clock.erl @@ -20,5 +20,7 @@ %% API. start() -> + ok = application:start(crypto), + ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(clock). diff --git a/examples/clock/src/clock_app.erl b/examples/clock/src/clock_app.erl index 71de61f..9b3985f 100644 --- a/examples/clock/src/clock_app.erl +++ b/examples/clock/src/clock_app.erl @@ -11,21 +11,20 @@ %% API. start(_Type, _Args) -> - Dispatch = [ + Dispatch = cowboy_router:compile([ {'_', [ - {[], toppage_handler, []}, - {[<<"bullet">>], bullet_handler, [{handler, stream_handler}]}, - {[<<"static">>, '...'], cowboy_http_static, [ + {"/", toppage_handler, []}, + {"/bullet", bullet_handler, [{handler, stream_handler}]}, + {"/static/[...]", cowboy_static, [ {directory, {priv_dir, bullet, []}}, {mimetypes, [ {<<".js">>, [<<"application/javascript">>]} ]} ]} ]} - ], - {ok, _} = cowboy:start_listener(http, 100, - cowboy_tcp_transport, [{port, 8080}], - cowboy_http_protocol, [{dispatch, Dispatch}] + ]), + {ok, _} = cowboy:start_http(http, 100, + [{port, 8080}], [{env, [{dispatch, Dispatch}]}] ), clock_sup:start_link(). diff --git a/examples/clock/src/toppage_handler.erl b/examples/clock/src/toppage_handler.erl index 66c8400..ee482a6 100644 --- a/examples/clock/src/toppage_handler.erl +++ b/examples/clock/src/toppage_handler.erl @@ -5,7 +5,7 @@ -export([init/3]). -export([handle/2]). --export([terminate/2]). +-export([terminate/3]). init(_Transport, Req, []) -> {ok, Req, undefined}. @@ -52,9 +52,9 @@ $(document).ready(function(){ ">>, - {ok, Req2} = cowboy_http_req:reply(200, [{'Content-Type', <<"text/html">>}], + {ok, Req2} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], Body, Req), {ok, Req2, State}. -terminate(_Req, _State) -> +terminate(_Reason, _Req, _State) -> ok. diff --git a/rebar.config b/rebar.config index 6d19803..4c98aa4 100644 --- a/rebar.config +++ b/rebar.config @@ -1,7 +1,7 @@ {cover_enabled, true}. {deps, [ {cowboy, ".*", - {git, "git://github.com/extend/cowboy.git", {tag, "0.6.1"}}} + {git, "git://github.com/extend/cowboy.git", {tag, "0.8.0"}}} ]}. {eunit_opts, [verbose, {report, {eunit_surefire, [{dir, "."}]}}]}. {erl_opts, [ diff --git a/src/bullet_handler.erl b/src/bullet_handler.erl index e8fec71..85b64a1 100644 --- a/src/bullet_handler.erl +++ b/src/bullet_handler.erl @@ -13,13 +13,17 @@ %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -module(bullet_handler). - -behaviour(cowboy_http_handler). --export([init/3, handle/2, info/3, terminate/2]). +-behaviour(cowboy_websocket_handler). --behaviour(cowboy_http_websocket_handler). --export([websocket_init/3, websocket_handle/3, - websocket_info/3, websocket_terminate/3]). +-export([init/3]). +-export([handle/2]). +-export([info/3]). +-export([terminate/3]). +-export([websocket_init/3]). +-export([websocket_handle/3]). +-export([websocket_info/3]). +-export([websocket_terminate/3]). -record(state, { handler :: module(), @@ -31,32 +35,32 @@ %% HTTP. init(Transport, Req, Opts) -> - case cowboy_http_req:header('Upgrade', Req) of + case cowboy_req:header(<<"upgrade">>, Req) of {undefined, Req2} -> - {Method, Req3} = cowboy_http_req:method(Req2), + {Method, Req3} = cowboy_req:method(Req2), init(Transport, Req3, Opts, Method); {Bin, Req2} when is_binary(Bin) -> case cowboy_bstr:to_lower(Bin) of <<"websocket">> -> - {upgrade, protocol, cowboy_http_websocket}; + {upgrade, protocol, cowboy_websocket}; _Any -> - {ok, Req3} = cowboy_http_req:reply(501, [], [], Req2), + {ok, Req3} = cowboy_req:reply(501, [], [], Req2), {shutdown, Req3, undefined} end end. -init(Transport, Req, Opts, 'GET') -> +init(Transport, Req, Opts, <<"GET">>) -> {handler, Handler} = lists:keyfind(handler, 1, Opts), State = #state{handler=Handler}, case Handler:init(Transport, Req, Opts, once) of {ok, Req2, HandlerState} -> - Req3 = cowboy_http_req:compact(Req2), + Req3 = cowboy_req:compact(Req2), {loop, Req3, State#state{handler_state=HandlerState}, ?TIMEOUT, hibernate}; {shutdown, Req2, HandlerState} -> {shutdown, Req2, State#state{handler_state=HandlerState}} end; -init(Transport, Req, Opts, 'POST') -> +init(Transport, Req, Opts, <<"POST">>) -> {handler, Handler} = lists:keyfind(handler, 1, Opts), State = #state{handler=Handler}, case Handler:init(Transport, Req, Opts, false) of @@ -66,22 +70,22 @@ init(Transport, Req, Opts, 'POST') -> {shutdown, Req2, State#state{handler_state=HandlerState}} end; init(_Transport, Req, _Opts, _Method) -> - {ok, Req2} = cowboy_http_req:reply(405, [], [], Req), + {ok, Req2} = cowboy_req:reply(405, [], [], Req), {shutdown, Req2, undefined}. handle(Req, State) -> - {Method, Req2} = cowboy_http_req:method(Req), + {Method, Req2} = cowboy_req:method(Req), handle(Req2, State, Method). handle(Req, State=#state{handler=Handler, handler_state=HandlerState}, 'POST') -> - case cowboy_http_req:body(Req) of + case cowboy_req:body(Req) of {ok, Data, Req2} -> case Handler:stream(Data, Req2, HandlerState) of {ok, Req3, HandlerState2} -> {ok, Req3, State#state{handler_state=HandlerState2}}; {reply, Reply, Req3, HandlerState2} -> - {ok, Req4} = cowboy_http_req:reply(200, [], Reply, Req3), + {ok, Req4} = cowboy_req:reply(200, [], Reply, Req3), {ok, Req4, State#state{handler_state=HandlerState2}} end; {error, _} -> @@ -95,13 +99,13 @@ info(Message, Req, {ok, Req2, HandlerState2} -> {loop, Req2, State#state{handler_state=HandlerState2}, hibernate}; {reply, Data, Req2, HandlerState2} -> - {ok, Req3} = cowboy_http_req:reply(200, [], Data, Req2), + {ok, Req3} = cowboy_req:reply(200, [], Data, Req2), {ok, Req3, State#state{handler_state=HandlerState2}} end. -terminate(_Req, undefined) -> +terminate(_Reason, _Req, undefined) -> ok; -terminate(Req, #state{handler=Handler, handler_state=HandlerState}) -> +terminate(_Reason, Req, #state{handler=Handler, handler_state=HandlerState}) -> Handler:terminate(Req, HandlerState). %% Websocket. @@ -111,7 +115,7 @@ websocket_init(Transport, Req, Opts) -> State = #state{handler=Handler}, case Handler:init(Transport, Req, Opts, true) of {ok, Req2, HandlerState} -> - Req3 = cowboy_http_req:compact(Req2), + Req3 = cowboy_req:compact(Req2), {ok, Req3, State#state{handler_state=HandlerState}, ?TIMEOUT, hibernate}; {shutdown, Req2, _HandlerState} -> -- cgit v1.2.3