0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +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

@ -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)=[])
----

Binary file not shown.

Binary file not shown.

View file

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

View file

@ -0,0 +1,11 @@
<!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

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

View file

@ -0,0 +1,47 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @doc Directory handler.
-module(directory_handler).
%% REST Callbacks
-export([init/2]).
-export([allowed_methods/2]).
-export([resource_exists/2]).
-export([content_types_provided/2]).
%% Callback Callbacks
-export([list_json/2]).
-export([list_html/2]).
init(Req, Paths) ->
{cowboy_rest, Req, Paths}.
allowed_methods(Req, State) ->
{[<<"GET">>], Req, State}.
resource_exists(Req, {ReqPath, FilePath}) ->
case file:list_dir(FilePath) of
{ok, Fs} -> {true, Req, {ReqPath, lists:sort(Fs)}};
_Err -> {false, Req, {ReqPath, FilePath}}
end.
content_types_provided(Req, State) ->
{[
{{<<"text">>, <<"html">>, []}, list_html},
{{<<"application">>, <<"json">>, []}, list_json}
], Req, State}.
list_json(Req, {Path, Fs}) ->
Files = [ <<(list_to_binary(F))/binary>> || F <- Fs ],
{jsx:encode(Files), Req, Path}.
list_html(Req, {Path, Fs}) ->
Body = [[ links(Path, F) || F <- [".."|Fs] ]],
HTML = [<<"<!DOCTYPE html><html><head><title>Index</title></head>",
"<body>">>, Body, <<"</body></html>\n">>],
{HTML, Req, Path}.
links(<<>>, File) ->
["<a href='/", File, "'>", File, "</a><br>\n"];
links(Prefix, File) ->
["<a href='/", Prefix, $/, File, "'>", File, "</a><br>\n"].

View file

@ -0,0 +1,32 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(directory_lister).
-behaviour(cowboy_middleware).
-export([execute/2]).
execute(Req, Env=#{handler := cowboy_static}) ->
redirect_directory(Req, Env);
execute(Req, Env) ->
{ok, Req, Env}.
redirect_directory(Req, Env=#{handler_opts := {_, _, _, Extra}}) ->
Path = cowboy_req:path_info(Req),
Path1 = << <<S/binary, $/>> || S <- Path >>,
{dir_handler, DirHandler} = lists:keyfind(dir_handler, 1, Extra),
FullPath = resource_path(Path1),
case valid_path(Path) and filelib:is_dir(FullPath) of
true -> handle_directory(Req, Env, Path1, FullPath, DirHandler);
false -> {ok, Req, Env}
end.
handle_directory(Req, Env, Prefix, Path, DirHandler) ->
{ok, Req, Env#{handler => DirHandler, handler_opts => {Prefix, Path}}}.
valid_path([]) -> true;
valid_path([<<"..">> | _T]) -> false;
valid_path([<<"/", _/binary>> | _T]) -> false;
valid_path([_H | Rest]) -> valid_path(Rest).
resource_path(Path) ->
filename:join([code:priv_dir(file_server), Path]).

View file

@ -0,0 +1,29 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @private
-module(file_server_app).
-behaviour(application).
%% API.
-export([start/2]).
-export([stop/1]).
%% API.
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
{"/[...]", cowboy_static, {priv_dir, file_server, "", [
{mimetypes, cow_mimetypes, all},
{dir_handler, directory_handler}
]}}
]}
]),
{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

@ -0,0 +1,23 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @private
-module(file_server_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}}.