From 0f8452cafaa27aeffb103019564c086eacfd34f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 16 Jan 2017 14:22:43 +0100 Subject: Add support for multiple stream handlers The stream handlers can be specified using the protocol option 'stream_handlers'. It defaults to [cowboy_stream_h]. The cowboy_stream_h module currently does not forward the calls to further stream handlers. It feels like an edge case; usually we'd want to put our own handlers between the protocol code and the request process. I am therefore going to focus on other things for now. The various types and specifications for stream handlers have been updated and the cowboy_stream module can now be safely used as a behavior. The interface might change a little more, though. This commit does not include tests or documentation. They will follow separately. --- src/cowboy.erl | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/cowboy.erl') diff --git a/src/cowboy.erl b/src/cowboy.erl index 7bb9f1f..56aef34 100644 --- a/src/cowboy.erl +++ b/src/cowboy.erl @@ -41,26 +41,29 @@ %% doesn't let us do that yet. -spec start_clear(ranch:ref(), non_neg_integer(), ranch_tcp:opts(), opts()) -> {ok, pid()} | {error, any()}. -start_clear(Ref, NbAcceptors, TransOpts0, ProtoOpts) +start_clear(Ref, NbAcceptors, TransOpts0, ProtoOpts0) when is_integer(NbAcceptors), NbAcceptors > 0 -> - TransOpts = [connection_type(ProtoOpts)|TransOpts0], + {TransOpts, ConnectionType} = ensure_connection_type(TransOpts0), + ProtoOpts = ProtoOpts0#{connection_type => ConnectionType}, ranch:start_listener(Ref, NbAcceptors, ranch_tcp, TransOpts, cowboy_clear, ProtoOpts). -spec start_tls(ranch:ref(), non_neg_integer(), ranch_ssl:opts(), opts()) -> {ok, pid()} | {error, any()}. -start_tls(Ref, NbAcceptors, TransOpts0, ProtoOpts) +start_tls(Ref, NbAcceptors, TransOpts0, ProtoOpts0) when is_integer(NbAcceptors), NbAcceptors > 0 -> + {TransOpts1, ConnectionType} = ensure_connection_type(TransOpts0), TransOpts = [ - connection_type(ProtoOpts), {next_protocols_advertised, [<<"h2">>, <<"http/1.1">>]}, {alpn_preferred_protocols, [<<"h2">>, <<"http/1.1">>]} - |TransOpts0], + |TransOpts1], + ProtoOpts = ProtoOpts0#{connection_type => ConnectionType}, ranch:start_listener(Ref, NbAcceptors, ranch_ssl, TransOpts, cowboy_tls, ProtoOpts). --spec connection_type(opts()) -> {connection_type, worker | supervisor}. -connection_type(ProtoOpts) -> - {_, Type} = maps:get(stream_handler, ProtoOpts, {cowboy_stream_h, supervisor}), - {connection_type, Type}. +ensure_connection_type(TransOpts) -> + case proplists:get_value(connection_type, TransOpts) of + undefined -> {[{connection_type, supervisor}|TransOpts], supervisor}; + ConnectionType -> {TransOpts, ConnectionType} + end. -spec stop_listener(ranch:ref()) -> ok | {error, not_found}. stop_listener(Ref) -> -- cgit v1.2.3