0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 12:40:25 +00:00

Make cowlib a proper dependency

Start moving a few functions from Cowboy into cowlib.
This commit is contained in:
Loïc Hoguin 2013-09-04 19:24:54 +02:00
parent bd0de074c3
commit 4a30198f90
27 changed files with 38 additions and 45 deletions

View file

@ -21,6 +21,7 @@
kernel,
stdlib,
ranch,
cowlib,
crypto
]},
{mod, {cowboy_app, []}},

View file

@ -54,7 +54,6 @@
%% Internal.
-export([init/4]).
-export([parse_request/3]).
-export([parse_host/3]).
-export([resume/6]).
-type opts() :: [{compress, boolean()}
@ -443,6 +442,9 @@ default_port(ssl) -> 443;
default_port(_) -> 80.
%% Another hurtful block of code. :)
%%
%% Same code as cow_http:parse_fullhost/1, but inline because we
%% really want this to go fast.
parse_host(<< $[, Rest/bits >>, false, <<>>) ->
parse_host(Rest, true, << $[ >>);
parse_host(<<>>, false, Acc) ->
@ -593,24 +595,3 @@ error_terminate(Status, Req, State) ->
terminate(#state{socket=Socket, transport=Transport}) ->
Transport:close(Socket),
ok.
%% Tests.
-ifdef(TEST).
parse_host(RawHost) ->
parse_host(RawHost, false, <<>>).
parse_host_test() ->
{<<"example.org">>, 8080} = parse_host(<<"example.org:8080">>),
{<<"example.org">>, undefined} = parse_host(<<"example.org">>),
{<<"192.0.2.1">>, 8080} = parse_host(<<"192.0.2.1:8080">>),
{<<"192.0.2.1">>, undefined} = parse_host(<<"192.0.2.1">>),
{<<"[2001:db8::1]">>, 8080} = parse_host(<<"[2001:db8::1]:8080">>),
{<<"[2001:db8::1]">>, undefined} = parse_host(<<"[2001:db8::1]">>),
{<<"[::ffff:192.0.2.1]">>, 8080} =
parse_host(<<"[::ffff:192.0.2.1]:8080">>),
{<<"[::ffff:192.0.2.1]">>, undefined} =
parse_host(<<"[::ffff:192.0.2.1]">>).
-endif.

View file

@ -340,11 +340,11 @@ delete_child(Pid, State=#state{children=Children}) ->
request_init(FakeSocket, Peer, OnRequest, OnResponse,
Env, Middlewares, Method, Host, Path, Version, Headers) ->
Version2 = parse_version(Version),
{Host2, Port} = cowboy_protocol:parse_host(Host, false, <<>>),
{Path2, Query} = parse_path(Path, <<>>),
{Host2, Port} = cow_http:parse_fullhost(Host),
{Path2, Qs} = cow_http:parse_fullpath(Path),
Version2 = cow_http:parse_version(Version),
Req = cowboy_req:new(FakeSocket, ?MODULE, Peer,
Method, Path2, Query, Version2, Headers,
Method, Path2, Qs, Version2, Headers,
Host2, Port, <<>>, true, false, OnResponse),
case OnRequest of
undefined ->
@ -357,23 +357,6 @@ request_init(FakeSocket, Peer, OnRequest, OnResponse,
end
end.
parse_version(<<"HTTP/1.1">>) ->
'HTTP/1.1';
parse_version(<<"HTTP/1.0">>) ->
'HTTP/1.0'.
parse_path(<<>>, Path) ->
{Path, <<>>};
parse_path(<< $?, Rest/binary >>, Path) ->
parse_query(Rest, Path, <<>>);
parse_path(<< C, Rest/binary >>, SoFar) ->
parse_path(Rest, << SoFar/binary, C >>).
parse_query(<<>>, Path, Query) ->
{Path, Query};
parse_query(<< C, Rest/binary >>, Path, SoFar) ->
parse_query(Rest, Path, << SoFar/binary, C >>).
-spec execute(cowboy_req:req(), cowboy_middleware:env(), [module()])
-> ok.
execute(Req, _, []) ->