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

Merge static_world and web_server examples

The new example is called file_server and it's basically
the same as web_server was. The name is clearer than the
original, all examples being "Web servers".

The new example is also tested and the test suite has
been refactored a little.
This commit is contained in:
Loïc Hoguin 2016-06-13 13:23:22 +02:00
parent 4293a40d9e
commit 88227898ed
26 changed files with 181 additions and 180 deletions

View file

@ -21,6 +21,9 @@
* link:eventsource[]:
eventsource emitter and consumer
* link:file_server[]:
file server with directory listing
* link:hello_world[]:
simplest example application
@ -42,15 +45,9 @@
* link:ssl_hello_world[]:
simplest SSL application
* link:static_world[]:
static file handler
* link:upload[]:
multipart/form-data upload
* link:web_server[]:
serve files with lists directory entries
* link:websocket[]:
websocket example

View file

@ -0,0 +1,8 @@
PROJECT = file_server
PROJECT_DESCRIPTION = Cowboy file server example with directory listing
PROJECT_VERSION = 1
DEPS = cowboy jsx
dep_cowboy_commit = master
include ../../erlang.mk

View file

@ -0,0 +1,89 @@
= File server example with directory listing
To try this example, you need GNU `make` and `git` in your PATH.
To build and run the example, use the following command:
[source,bash]
$ make run
Then point your browser to http://localhost:8080
to browse the contents of the `priv` directory.
Interesting examples include:
* http://localhost:8080/test.txt[Plain text file]
* http://localhost:8080/video.html[HTML5 video demo]
== HTTP/1.1 example output
[source,bash]
----
$ curl -i http://localhost:8080/test.txt
HTTP/1.1 200 OK
connection: keep-alive
server: Cowboy
date: Mon, 09 Sep 2013 13:49:50 GMT
content-length: 52
content-type: text/plain
last-modified: Fri, 18 Jan 2013 16:33:31 GMT
If you read this then the static file server works!
----
== HTTP/2 example output
[source,bash]
----
$ nghttp -v http://localhost:8080/test.txt
[ 0.000] Connected
[ 0.000] send SETTINGS frame <length=12, flags=0x00, stream_id=0>
(niv=2)
[SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
[SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=3>
(dep_stream_id=0, weight=201, exclusive=0)
[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=5>
(dep_stream_id=0, weight=101, exclusive=0)
[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=7>
(dep_stream_id=0, weight=1, exclusive=0)
[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=9>
(dep_stream_id=7, weight=1, exclusive=0)
[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=11>
(dep_stream_id=3, weight=1, exclusive=0)
[ 0.000] send HEADERS frame <length=46, flags=0x25, stream_id=13>
; END_STREAM | END_HEADERS | PRIORITY
(padlen=0, dep_stream_id=11, weight=16, exclusive=0)
; Open new stream
:method: GET
:path: /test.txt
:scheme: http
:authority: localhost:8080
accept: */*
accept-encoding: gzip, deflate
user-agent: nghttp2/1.7.1
[ 0.001] recv SETTINGS frame <length=0, flags=0x00, stream_id=0>
(niv=0)
[ 0.001] recv SETTINGS frame <length=0, flags=0x01, stream_id=0>
; ACK
(niv=0)
[ 0.001] send SETTINGS frame <length=0, flags=0x01, stream_id=0>
; ACK
(niv=0)
[ 0.007] recv (stream_id=13) :status: 200
[ 0.007] recv (stream_id=13) content-length: 52
[ 0.007] recv (stream_id=13) content-type: text/plain
[ 0.007] recv (stream_id=13) date: Mon, 13 Jun 2016 11:25:20 GMT
[ 0.007] recv (stream_id=13) etag: "1371478245"
[ 0.007] recv (stream_id=13) last-modified: Tue, 12 Aug 2014 17:00:17 GMT
[ 0.007] recv (stream_id=13) server: Cowboy
[ 0.007] recv HEADERS frame <length=81, flags=0x04, stream_id=13>
; END_HEADERS
(padlen=0)
; First response header
If you read this then the static file server works!
[ 0.007] recv DATA frame <length=52, flags=0x01, stream_id=13>
; END_STREAM
[ 0.007] send GOAWAY frame <length=8, flags=0x00, stream_id=0>
(last_stream_id=0, error_code=NO_ERROR(0x00), opaque_data(0)=[])
----

View file

@ -0,0 +1,2 @@
{release, {file_server_example, "1"}, [file_server]}.
{extended_start_script, true}.

View file

@ -27,12 +27,12 @@ resource_exists(Req, {ReqPath, FilePath}) ->
content_types_provided(Req, State) ->
{[
{{<<"application">>, <<"json">>, []}, list_json},
{{<<"text">>, <<"html">>, []}, list_html}
{{<<"text">>, <<"html">>, []}, list_html},
{{<<"application">>, <<"json">>, []}, list_json}
], Req, State}.
list_json(Req, {Path, Fs}) ->
Files = [[ <<(list_to_binary(F))/binary>> || F <- Fs ]],
Files = [ <<(list_to_binary(F))/binary>> || F <- Fs ],
{jsx:encode(Files), Req, Path}.
list_html(Req, {Path, Fs}) ->

View file

@ -5,16 +5,14 @@
-export([execute/2]).
execute(Req, Env=#{handler := cowboy_static}) ->
redirect_directory(Req, Env);
execute(Req, Env) ->
case lists:keyfind(handler, 1, Env) of
{handler, cowboy_static} -> redirect_directory(Req, Env);
_H -> {ok, Req, Env}
end.
{ok, Req, Env}.
redirect_directory(Req, Env) ->
redirect_directory(Req, Env=#{handler_opts := {_, _, _, Extra}}) ->
Path = cowboy_req:path_info(Req),
Path1 = << <<S/binary, $/>> || S <- Path >>,
{handler_opts, {_, _, _, Extra}} = lists:keyfind(handler_opts, 1, Env),
{dir_handler, DirHandler} = lists:keyfind(dir_handler, 1, Extra),
FullPath = resource_path(Path1),
case valid_path(Path) and filelib:is_dir(FullPath) of
@ -23,9 +21,7 @@ redirect_directory(Req, Env) ->
end.
handle_directory(Req, Env, Prefix, Path, DirHandler) ->
Env1 = lists:keydelete(handler, 1,
lists:keydelete(handler_opts, 1, Env)),
{ok, Req, [{handler, DirHandler}, {handler_opts, {Prefix, Path}} | Env1]}.
{ok, Req, Env#{handler => DirHandler, handler_opts => {Prefix, Path}}}.
valid_path([]) -> true;
valid_path([<<"..">> | _T]) -> false;
@ -33,4 +29,4 @@ valid_path([<<"/", _/binary>> | _T]) -> false;
valid_path([_H | Rest]) -> valid_path(Rest).
resource_path(Path) ->
filename:join([code:priv_dir(web_server), Path]).
filename:join([code:priv_dir(file_server), Path]).

View file

@ -1,7 +1,7 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @private
-module(web_server_app).
-module(file_server_app).
-behaviour(application).
%% API.
@ -13,17 +13,17 @@
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/[...]", cowboy_static, {priv_dir, web_server, "", [
{"/[...]", cowboy_static, {priv_dir, file_server, "", [
{mimetypes, cow_mimetypes, all},
{dir_handler, directory_handler}
]}}
]}
]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
{env, [{dispatch, Dispatch}]},
{middlewares, [cowboy_router, directory_lister, cowboy_handler]}
]),
web_server_sup:start_link().
{ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{
env => #{dispatch => Dispatch},
middlewares => [cowboy_router, directory_lister, cowboy_handler]
}),
file_server_sup:start_link().
stop(_State) ->
ok.

View file

@ -1,7 +1,7 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @private
-module(web_server_sup).
-module(file_server_sup).
-behaviour(supervisor).
%% API.

View file

@ -1,8 +0,0 @@
PROJECT = static_world
PROJECT_DESCRIPTION = Cowboy static file handler example
PROJECT_VERSION = 1
DEPS = cowboy
dep_cowboy_commit = master
include ../../erlang.mk

View file

@ -1,30 +0,0 @@
= Static file handler example
To try this example, you need GNU `make` and `git` in your PATH.
To build and run the example, use the following command:
[source,bash]
$ make run
The example will serve all the files found in the 'priv/'
directory. For example:
* http://localhost:8080/test.txt[Plain text file]
* http://localhost:8080/video.html[HTML5 video demo]
== Example output
[source,bash]
----
$ curl -i http://localhost:8080/test.txt
HTTP/1.1 200 OK
connection: keep-alive
server: Cowboy
date: Mon, 09 Sep 2013 13:49:50 GMT
content-length: 52
content-type: text/plain
last-modified: Fri, 18 Jan 2013 16:33:31 GMT
If you read this then the static file server works!
----

View file

@ -1 +0,0 @@
<h1>Howdy, Pardner</h1>

View file

@ -1,11 +0,0 @@
<!DOCTYPE html>
<html>
<body>
<h1>HTML5 Video Example</h1>
<video controls>
<source src="small.ogv" type="video/ogg"/>
<source src="small.mp4" type="video/mp4"/>
</video>
<p>Videos taken from <a href="http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5/">TechSlides</a></p>
</body>
</html>

View file

@ -1,2 +0,0 @@
{release, {static_world_example, "1"}, [static_world]}.
{extended_start_script, true}.

View file

@ -1,27 +0,0 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @private
-module(static_world_app).
-behaviour(application).
%% API.
-export([start/2]).
-export([stop/1]).
%% API.
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/", cowboy_static, {priv_file, static_world, "index.html"}},
{"/[...]", cowboy_static, {priv_dir, static_world, "",
[{mimetypes, cow_mimetypes, all}]}}
]}
]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
{env, [{dispatch, Dispatch}]}
]),
static_world_sup:start_link().
stop(_State) ->
ok.

View file

@ -1,23 +0,0 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @private
-module(static_world_sup).
-behaviour(supervisor).
%% API.
-export([start_link/0]).
%% supervisor.
-export([init/1]).
%% API.
-spec start_link() -> {ok, pid()}.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%% supervisor.
init([]) ->
Procs = [],
{ok, {{one_for_one, 10, 10}, Procs}}.

View file

@ -1,8 +0,0 @@
PROJECT = web_server
PROJECT_DESCRIPTION = Cowboy static directory indexing example
PROJECT_VERSION = 1
DEPS = cowboy
dep_cowboy_commit = master
include ../../erlang.mk

View file

@ -1,11 +0,0 @@
= Directory indexing example
To try this example, you need GNU `make` and `git` in your PATH.
To build and run the example, use the following command:
[source,bash]
$ make run
Then point your browser to http://localhost:8080
to browse the contents of the `priv` directory.

Binary file not shown.

Binary file not shown.

View file

@ -1 +0,0 @@
If you read this then the static file server works!

View file

@ -1,2 +0,0 @@
{release, {web_server_example, "1"}, [web_server]}.
{extended_start_script, true}.

View file

@ -60,6 +60,26 @@ do_stop(Example) ->
ct:log("~s~n", [element(2, file:read_file(Log))]),
ok.
%% Fetch a response.
do_get(Transport, Protocol, Path, Config) ->
do_get(Transport, Protocol, Path, [], Config).
do_get(Transport, Protocol, Path, ReqHeaders, Config) ->
Port = case Transport of
tcp -> 8080;
ssl -> 8443
end,
ConnPid = gun_open([{port, Port}, {type, Transport}, {protocol, Protocol}|Config]),
Ref = gun:get(ConnPid, Path, ReqHeaders),
case gun:await(ConnPid, Ref) of
{response, nofin, Status, RespHeaders} ->
{ok, Body} = gun:await_body(ConnPid, Ref),
{Status, RespHeaders, Body};
{response, fin, Status, RespHeaders} ->
{Status, RespHeaders, <<>>}
end.
%% TCP and SSL Hello World.
hello_world(Config) ->
@ -83,14 +103,7 @@ ssl_hello_world(Config) ->
end.
do_hello_world(Transport, Protocol, Config) ->
Port = case Transport of
tcp -> 8080;
ssl -> 8443
end,
ConnPid = gun_open([{port, Port}, {type, Transport}, {protocol, Protocol}|Config]),
Ref = gun:get(ConnPid, "/"),
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
{ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
{200, _, <<"Hello world!">>} = do_get(Transport, Protocol, "/", Config),
ok.
%% Echo GET.
@ -106,10 +119,7 @@ echo_get(Config) ->
end.
do_echo_get(Transport, Protocol, Config) ->
ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
Ref = gun:get(ConnPid, "/?echo=this+is+fun"),
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
{ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
{200, _, <<"this is fun">>} = do_get(Transport, Protocol, "/?echo=this+is+fun", Config),
ok.
%% Echo POST.
@ -146,27 +156,50 @@ rest_hello_world(Config) ->
end.
do_rest_hello_world(Transport, Protocol, Config) ->
<< "<html>", _/bits >> = do_rest_hello_world_get(Transport, Protocol, undefined, Config),
<< "REST Hello World as text!" >> = do_rest_hello_world_get(Transport, Protocol, <<"text/plain">>, Config),
<< "{\"rest\": \"Hello World!\"}" >> = do_rest_hello_world_get(Transport, Protocol, <<"application/json">>, Config),
not_acceptable = do_rest_hello_world_get(Transport, Protocol, <<"text/css">>, Config),
<< "<html>", _/bits >> = do_rest_get(Transport, Protocol, "/", undefined, Config),
<< "REST Hello World as text!" >> = do_rest_get(Transport, Protocol, "/", <<"text/plain">>, Config),
<< "{\"rest\": \"Hello World!\"}" >> = do_rest_get(Transport, Protocol, "/", <<"application/json">>, Config),
not_acceptable = do_rest_get(Transport, Protocol, "/", <<"text/css">>, Config),
ok.
do_rest_hello_world_get(Transport, Protocol, Accept, Config) ->
Port = case Transport of
tcp -> 8080;
ssl -> 8443
end,
ConnPid = gun_open([{port, Port}, {type, Transport}, {protocol, Protocol}|Config]),
Headers = case Accept of
do_rest_get(Transport, Protocol, Path, Accept, Config) ->
ReqHeaders = case Accept of
undefined -> [];
_ -> [{<<"accept">>, Accept}]
end,
Ref = gun:get(ConnPid, "/", Headers),
case gun:await(ConnPid, Ref) of
{response, nofin, 200, _} ->
{ok, Body} = gun:await_body(ConnPid, Ref),
case do_get(Transport, Protocol, Path, ReqHeaders, Config) of
{200, RespHeaders, Body} ->
Accept = case Accept of
undefined -> undefined;
_ ->
{_, ContentType} = lists:keyfind(<<"content-type">>, 1, RespHeaders),
ContentType
end,
Body;
{response, _, 406, _} ->
{406, _, _} ->
not_acceptable
end.
%% File server.
file_server(Config) ->
doc("File server example with directory listing."),
try
do_compile_and_start(file_server),
do_file_server(tcp, http, Config),
do_file_server(tcp, http2, Config)
after
do_stop(file_server)
end.
do_file_server(Transport, Protocol, Config) ->
%% Directory.
{200, DirHeaders, <<"<!DOCTYPE html><html>", _/bits >>} = do_get(Transport, Protocol, "/", Config),
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, DirHeaders),
_ = do_rest_get(Transport, Protocol, "/", <<"application/json">>, Config),
%% Files.
{200, _, _} = do_get(Transport, Protocol, "/small.mp4", Config),
{200, _, _} = do_get(Transport, Protocol, "/small.ogv", Config),
{200, _, _} = do_get(Transport, Protocol, "/test.txt", Config),
{200, _, _} = do_get(Transport, Protocol, "/video.html", Config),
ok.