0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Improve a few types, including cowboy_req:req()

This commit is contained in:
Loïc Hoguin 2018-10-31 11:45:04 +01:00
parent a8335c63df
commit 473e3fb82b
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
7 changed files with 63 additions and 37 deletions

View file

@ -23,8 +23,7 @@
-export([log/2]).
-export([log/4]).
%% @todo Detailed opts.
-type opts() :: map().
-type opts() :: cowboy_http:opts() | cowboy_http2:opts().
-export_type([opts/0]).
-type fields() :: [atom()

View file

@ -39,10 +39,14 @@
max_keepalive => non_neg_integer(),
max_method_length => non_neg_integer(),
max_request_line_length => non_neg_integer(),
metrics_callback => cowboy_metrics_h:metrics_callback(),
middlewares => [module()],
proxy_header => boolean(),
request_timeout => timeout(),
shutdown_timeout => timeout(),
stream_handlers => [module()]
stream_handlers => [module()],
tracer_callback => cowboy_tracer_h:tracer_callback(),
tracer_match_specs => cowboy_tracer_h:tracer_match_specs()
}.
-export_type([opts/0]).
@ -56,7 +60,7 @@
path = undefined :: binary(),
qs = undefined :: binary(),
version = undefined :: cowboy:http_version(),
headers = undefined :: map() | undefined, %% @todo better type than map()
headers = undefined :: cowboy:http_headers() | undefined,
name = undefined :: binary() | undefined
}).
@ -99,7 +103,7 @@
socket :: inet:socket(),
transport :: module(),
proxy_header :: undefined | ranch_proxy_header:proxy_info(),
opts = #{} :: map(),
opts = #{} :: cowboy:opts(),
%% Remote address and port for the connection.
peer = undefined :: {inet:ip_address(), inet:port_number()},

View file

@ -39,11 +39,15 @@
max_encode_table_size => non_neg_integer(),
max_frame_size_received => 16384..16777215,
max_frame_size_sent => 16384..16777215 | infinity,
metrics_callback => cowboy_metrics_h:metrics_callback(),
middlewares => [module()],
preface_timeout => timeout(),
proxy_header => boolean(),
settings_timeout => timeout(),
shutdown_timeout => timeout(),
stream_handlers => [module()]
stream_handlers => [module()],
tracer_callback => cowboy_tracer_h:tracer_callback(),
tracer_match_specs => cowboy_tracer_h:tracer_match_specs()
}.
-export_type([opts/0]).
@ -78,7 +82,7 @@
}).
-spec init(pid(), ranch:ref(), inet:socket(), module(),
ranch_proxy_header:proxy_info(), cowboy:opts()) -> ok.
ranch_proxy_header:proxy_info() | undefined, cowboy:opts()) -> ok.
init(Parent, Ref, Socket, Transport, ProxyHeader, Opts) ->
Peer0 = Transport:peername(Socket),
Sock0 = Transport:sockname(Socket),
@ -108,7 +112,7 @@ init(Parent, Ref, Socket, Transport, ProxyHeader, Opts) ->
end.
-spec init(pid(), ranch:ref(), inet:socket(), module(),
ranch_proxy_header:proxy_info(), cowboy:opts(),
ranch_proxy_header:proxy_info() | undefined, cowboy:opts(),
{inet:ip_address(), inet:port_number()}, {inet:ip_address(), inet:port_number()},
binary() | undefined, binary()) -> ok.
init(Parent, Ref, Socket, Transport, ProxyHeader, Opts, Peer, Sock, Cert, Buffer) ->
@ -125,7 +129,7 @@ init(Parent, Ref, Socket, Transport, ProxyHeader, Opts, Peer, Sock, Cert, Buffer
%% @todo Add an argument for the request body.
-spec init(pid(), ranch:ref(), inet:socket(), module(),
ranch_proxy_header:proxy_info(), cowboy:opts(),
ranch_proxy_header:proxy_info() | undefined, cowboy:opts(),
{inet:ip_address(), inet:port_number()}, {inet:ip_address(), inet:port_number()},
binary() | undefined, binary(), map() | undefined, cowboy_req:req()) -> ok.
init(Parent, Ref, Socket, Transport, ProxyHeader, Opts, Peer, Sock, Cert, Buffer,

View file

@ -106,6 +106,9 @@
}.
-export_type([metrics/0]).
-type metrics_callback() :: fun((metrics()) -> any()).
-export_type([metrics_callback/0]).
-record(state, {
next :: any(),
callback :: fun((metrics()) -> any()),

View file

@ -121,32 +121,45 @@
}.
-export_type([push_opts/0]).
-type req() :: map(). %% @todo #{
% ref := ranch:ref(),
% pid := pid(),
% streamid := cowboy_stream:streamid(),
% peer := {inet:ip_address(), inet:port_number()},
% proxy_header => ...
%
% method := binary(), %% case sensitive
% version := cowboy:http_version() | atom(),
% scheme := binary(), %% <<"http">> or <<"https">>
% host := binary(), %% lowercase; case insensitive
% port := inet:port_number(),
% path := binary(), %% case sensitive
% qs := binary(), %% case sensitive
% headers := cowboy:http_headers(),
%
% host_info => cowboy_router:tokens(),
% path_info => cowboy_router:tokens(),
% bindings => cowboy_router:bindings(),
%
% has_body := boolean(),
% has_read_body => true,
% body_length := undefined | non_neg_integer()
%
%% @todo resp_*
%}.
-type req() :: #{
%% Public interface.
method := binary(),
version := cowboy:http_version() | atom(),
scheme := binary(),
host := binary(),
port := inet:port_number(),
path := binary(),
qs := binary(),
headers := cowboy:http_headers(),
peer := {inet:ip_address(), inet:port_number()},
sock := {inet:ip_address(), inet:port_number()},
cert := binary() | undefined,
%% Private interface.
ref := ranch:ref(),
pid := pid(),
streamid := cowboy_stream:streamid(),
host_info => cowboy_router:tokens(),
path_info => cowboy_router:tokens(),
bindings => cowboy_router:bindings(),
has_body := boolean(),
body_length := non_neg_integer() | undefined,
has_read_body => true,
multipart => {binary(), binary()} | done,
has_sent_resp => headers | true,
resp_cookies => #{iodata() => iodata()},
resp_headers => #{binary() => iodata()},
resp_body => resp_body(),
proxy_header => ranch_proxy_header:proxy_info(),
media_type => {binary(), binary(), [{binary(), binary()}]},
language => binary() | undefined,
charset => binary() | undefined,
websocket_version => 7 | 8 | 13
}.
-export_type([req/0]).
%% Request.

View file

@ -42,6 +42,9 @@
].
-export_type([tracer_match_specs/0]).
-type tracer_callback() :: fun((init | terminate | tuple(), any()) -> any()).
-export_type([tracer_callback/0]).
-spec init(cowboy_stream:streamid(), cowboy_req:req(), cowboy:opts())
-> {cowboy_stream:commands(), any()}.
init(StreamID, Req, Opts) ->

View file

@ -52,12 +52,12 @@ init_per_group(Name = https, Config) ->
init_per_group(Name = http_compress, Config) ->
cowboy_test:init_http(Name, #{
env => #{dispatch => init_dispatch(Config)},
compress => true
stream_handlers => [cowboy_compress_h, cowboy_stream_h]
}, Config);
init_per_group(Name = https_compress, Config) ->
cowboy_test:init_https(Name, #{
env => #{dispatch => init_dispatch(Config)},
compress => true
stream_handlers => [cowboy_compress_h, cowboy_stream_h]
}, Config).
end_per_group(Name, _) ->