From 4a30198f9068cc989616c8088e4b890bc1de259d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 4 Sep 2013 19:24:54 +0200 Subject: [PATCH] Make cowlib a proper dependency Start moving a few functions from Cowboy into cowlib. --- Makefile | 2 +- examples/basic_auth/src/basic_auth.erl | 1 + .../src/chunked_hello_world.erl | 1 + .../src/compress_response.erl | 1 + examples/cookie/src/cookie.erl | 1 + examples/echo_get/src/echo_get.erl | 1 + examples/echo_post/src/echo_post.erl | 1 + examples/error_hook/src/error_hook.erl | 1 + examples/eventsource/src/eventsource.erl | 1 + examples/hello_world/src/hello_world.erl | 1 + .../src/markdown_middleware.erl | 1 + .../rest_hello_world/src/rest_hello_world.erl | 1 + examples/rest_pastebin/src/rest_pastebin.erl | 1 + .../src/rest_stream_response.erl | 1 + .../ssl_hello_world/src/ssl_hello_world.erl | 1 + examples/static_world/src/static_world.erl | 1 + examples/web_server/src/web_server.erl | 1 + examples/websocket/src/websocket.erl | 1 + guide/getting_started.md | 4 ++- rebar.config | 1 + src/cowboy.app.src | 1 + src/cowboy_protocol.erl | 25 +++---------------- src/cowboy_spdy.erl | 25 +++---------------- test/autobahn_SUITE.erl | 2 ++ test/http_SUITE.erl | 2 ++ test/spdy_SUITE.erl | 2 ++ test/ws_SUITE.erl | 2 ++ 27 files changed, 38 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index d894efb4..e9c2c53e 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ PLT_APPS = crypto public_key ssl # Dependencies. DEPS = cowlib ranch -dep_cowlib = pkg://cowlib master +dep_cowlib = pkg://cowlib 0.1.0 dep_ranch = pkg://ranch 0.8.5 TEST_DEPS = ct_helper gun diff --git a/examples/basic_auth/src/basic_auth.erl b/examples/basic_auth/src/basic_auth.erl index 9294c773..d54c6776 100644 --- a/examples/basic_auth/src/basic_auth.erl +++ b/examples/basic_auth/src/basic_auth.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(basic_auth). diff --git a/examples/chunked_hello_world/src/chunked_hello_world.erl b/examples/chunked_hello_world/src/chunked_hello_world.erl index 78c771f9..d75511fc 100644 --- a/examples/chunked_hello_world/src/chunked_hello_world.erl +++ b/examples/chunked_hello_world/src/chunked_hello_world.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(chunked_hello_world). diff --git a/examples/compress_response/src/compress_response.erl b/examples/compress_response/src/compress_response.erl index ac2636cd..c69a14b1 100644 --- a/examples/compress_response/src/compress_response.erl +++ b/examples/compress_response/src/compress_response.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(compress_response). diff --git a/examples/cookie/src/cookie.erl b/examples/cookie/src/cookie.erl index 455fcb0b..2626b395 100644 --- a/examples/cookie/src/cookie.erl +++ b/examples/cookie/src/cookie.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(cookie). diff --git a/examples/echo_get/src/echo_get.erl b/examples/echo_get/src/echo_get.erl index 6ffff41c..9b1307db 100644 --- a/examples/echo_get/src/echo_get.erl +++ b/examples/echo_get/src/echo_get.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(echo_get). diff --git a/examples/echo_post/src/echo_post.erl b/examples/echo_post/src/echo_post.erl index c7c8ce18..1f2a2ea6 100644 --- a/examples/echo_post/src/echo_post.erl +++ b/examples/echo_post/src/echo_post.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(echo_post). diff --git a/examples/error_hook/src/error_hook.erl b/examples/error_hook/src/error_hook.erl index 3543590f..2cfd5cf2 100644 --- a/examples/error_hook/src/error_hook.erl +++ b/examples/error_hook/src/error_hook.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(error_hook). diff --git a/examples/eventsource/src/eventsource.erl b/examples/eventsource/src/eventsource.erl index 6505e622..bb859910 100644 --- a/examples/eventsource/src/eventsource.erl +++ b/examples/eventsource/src/eventsource.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(eventsource). diff --git a/examples/hello_world/src/hello_world.erl b/examples/hello_world/src/hello_world.erl index d62ff4bf..5db51bf4 100644 --- a/examples/hello_world/src/hello_world.erl +++ b/examples/hello_world/src/hello_world.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(hello_world). diff --git a/examples/markdown_middleware/src/markdown_middleware.erl b/examples/markdown_middleware/src/markdown_middleware.erl index 49c16dc0..6e943501 100644 --- a/examples/markdown_middleware/src/markdown_middleware.erl +++ b/examples/markdown_middleware/src/markdown_middleware.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(markdown_middleware). diff --git a/examples/rest_hello_world/src/rest_hello_world.erl b/examples/rest_hello_world/src/rest_hello_world.erl index e2b7463b..afc221f1 100644 --- a/examples/rest_hello_world/src/rest_hello_world.erl +++ b/examples/rest_hello_world/src/rest_hello_world.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(rest_hello_world). diff --git a/examples/rest_pastebin/src/rest_pastebin.erl b/examples/rest_pastebin/src/rest_pastebin.erl index cf03a711..e5bd1b56 100644 --- a/examples/rest_pastebin/src/rest_pastebin.erl +++ b/examples/rest_pastebin/src/rest_pastebin.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(rest_pastebin). diff --git a/examples/rest_stream_response/src/rest_stream_response.erl b/examples/rest_stream_response/src/rest_stream_response.erl index ef243093..31e9a362 100644 --- a/examples/rest_stream_response/src/rest_stream_response.erl +++ b/examples/rest_stream_response/src/rest_stream_response.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(rest_stream_response). diff --git a/examples/ssl_hello_world/src/ssl_hello_world.erl b/examples/ssl_hello_world/src/ssl_hello_world.erl index 1413b7bc..83c250e9 100644 --- a/examples/ssl_hello_world/src/ssl_hello_world.erl +++ b/examples/ssl_hello_world/src/ssl_hello_world.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(ssl_hello_world). diff --git a/examples/static_world/src/static_world.erl b/examples/static_world/src/static_world.erl index 6425a994..2bed337c 100644 --- a/examples/static_world/src/static_world.erl +++ b/examples/static_world/src/static_world.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(static_world). diff --git a/examples/web_server/src/web_server.erl b/examples/web_server/src/web_server.erl index ae75c3a3..ac3f1b1a 100644 --- a/examples/web_server/src/web_server.erl +++ b/examples/web_server/src/web_server.erl @@ -9,6 +9,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(web_server). diff --git a/examples/websocket/src/websocket.erl b/examples/websocket/src/websocket.erl index da2a91cc..23afe13d 100644 --- a/examples/websocket/src/websocket.erl +++ b/examples/websocket/src/websocket.erl @@ -7,6 +7,7 @@ start() -> ok = application:start(crypto), + ok = application:start(cowlib), ok = application:start(ranch), ok = application:start(cowboy), ok = application:start(websocket). diff --git a/guide/getting_started.md b/guide/getting_started.md index abd807ae..2ad67db0 100644 --- a/guide/getting_started.md +++ b/guide/getting_started.md @@ -3,10 +3,12 @@ Getting started Cowboy does nothing by default. -Cowboy requires the `crypto` and `ranch` applications to be started. +Cowboy requires the `crypto`, `cowlib` and `ranch` applications to be +started. ``` erlang ok = application:start(crypto). +ok = application:start(cowlib). ok = application:start(ranch). ok = application:start(cowboy). ``` diff --git a/rebar.config b/rebar.config index edd39481..d31efe75 100644 --- a/rebar.config +++ b/rebar.config @@ -1,3 +1,4 @@ {deps, [ + {cowlib, ".*", {git, "git://github.com/extend/cowlib.git", "0.1.0"}}, {ranch, ".*", {git, "git://github.com/extend/ranch.git", "0.8.5"}} ]}. diff --git a/src/cowboy.app.src b/src/cowboy.app.src index 0c4a5b11..504e3447 100644 --- a/src/cowboy.app.src +++ b/src/cowboy.app.src @@ -21,6 +21,7 @@ kernel, stdlib, ranch, + cowlib, crypto ]}, {mod, {cowboy_app, []}}, diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index 68a03b17..3c379837 100644 --- a/src/cowboy_protocol.erl +++ b/src/cowboy_protocol.erl @@ -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. diff --git a/src/cowboy_spdy.erl b/src/cowboy_spdy.erl index 3d39ff5b..dfac29cb 100644 --- a/src/cowboy_spdy.erl +++ b/src/cowboy_spdy.erl @@ -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, _, []) -> diff --git a/test/autobahn_SUITE.erl b/test/autobahn_SUITE.erl index 68a9221a..da566678 100644 --- a/test/autobahn_SUITE.erl +++ b/test/autobahn_SUITE.erl @@ -37,6 +37,7 @@ groups() -> init_per_suite(Config) -> application:start(crypto), + application:start(cowlib), application:start(ranch), application:start(cowboy), %% /tmp must be used as the parent directory for the virtualenv because @@ -58,6 +59,7 @@ end_per_suite(_Config) -> os:cmd("deactivate"), application:stop(cowboy), application:stop(ranch), + application:stop(cowlib), application:stop(crypto), ok. diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index f784c507..b5363801 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -196,6 +196,7 @@ groups() -> init_per_suite(Config) -> application:start(crypto), + application:start(cowlib), application:start(ranch), application:start(cowboy), Dir = ?config(priv_dir, Config) ++ "/static", @@ -207,6 +208,7 @@ end_per_suite(Config) -> ct_helper:delete_static_dir(Dir), application:stop(cowboy), application:stop(ranch), + application:stop(cowlib), application:stop(crypto), ok. diff --git a/test/spdy_SUITE.erl b/test/spdy_SUITE.erl index 6c197928..078c214a 100644 --- a/test/spdy_SUITE.erl +++ b/test/spdy_SUITE.erl @@ -43,6 +43,7 @@ groups() -> init_per_suite(Config) -> application:start(crypto), + application:start(cowlib), application:start(ranch), application:start(cowboy), application:start(asn1), @@ -62,6 +63,7 @@ end_per_suite(Config) -> application:stop(asn1), application:stop(cowboy), application:stop(ranch), + application:stop(cowlib), application:stop(crypto), ok. diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl index d2a08882..0a8ca431 100644 --- a/test/ws_SUITE.erl +++ b/test/ws_SUITE.erl @@ -70,6 +70,7 @@ groups() -> init_per_suite(Config) -> application:start(crypto), + application:start(cowlib), application:start(ranch), application:start(cowboy), Config. @@ -77,6 +78,7 @@ init_per_suite(Config) -> end_per_suite(_Config) -> application:stop(cowboy), application:stop(ranch), + application:stop(cowlib), application:stop(crypto), ok.