diff options
Diffstat (limited to 'examples/file_server')
-rw-r--r-- | examples/file_server/Makefile | 8 | ||||
-rw-r--r-- | examples/file_server/README.asciidoc | 89 | ||||
-rw-r--r-- | examples/file_server/priv/small.mp4 | bin | 0 -> 383631 bytes | |||
-rw-r--r-- | examples/file_server/priv/small.ogv | bin | 0 -> 872453 bytes | |||
-rw-r--r-- | examples/file_server/priv/test.txt | 1 | ||||
-rw-r--r-- | examples/file_server/priv/video.html | 11 | ||||
-rw-r--r-- | examples/file_server/relx.config | 2 | ||||
-rw-r--r-- | examples/file_server/src/directory_handler.erl | 47 | ||||
-rw-r--r-- | examples/file_server/src/directory_lister.erl | 32 | ||||
-rw-r--r-- | examples/file_server/src/file_server_app.erl | 29 | ||||
-rw-r--r-- | examples/file_server/src/file_server_sup.erl | 23 |
11 files changed, 242 insertions, 0 deletions
diff --git a/examples/file_server/Makefile b/examples/file_server/Makefile new file mode 100644 index 0000000..7efa6ef --- /dev/null +++ b/examples/file_server/Makefile @@ -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 diff --git a/examples/file_server/README.asciidoc b/examples/file_server/README.asciidoc new file mode 100644 index 0000000..f6bc61c --- /dev/null +++ b/examples/file_server/README.asciidoc @@ -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)=[]) +---- diff --git a/examples/file_server/priv/small.mp4 b/examples/file_server/priv/small.mp4 Binary files differnew file mode 100644 index 0000000..1fc4788 --- /dev/null +++ b/examples/file_server/priv/small.mp4 diff --git a/examples/file_server/priv/small.ogv b/examples/file_server/priv/small.ogv Binary files differnew file mode 100644 index 0000000..6409d6e --- /dev/null +++ b/examples/file_server/priv/small.ogv diff --git a/examples/file_server/priv/test.txt b/examples/file_server/priv/test.txt new file mode 100644 index 0000000..760cddb --- /dev/null +++ b/examples/file_server/priv/test.txt @@ -0,0 +1 @@ +If you read this then the static file server works! diff --git a/examples/file_server/priv/video.html b/examples/file_server/priv/video.html new file mode 100644 index 0000000..0830ae0 --- /dev/null +++ b/examples/file_server/priv/video.html @@ -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> diff --git a/examples/file_server/relx.config b/examples/file_server/relx.config new file mode 100644 index 0000000..9e365e9 --- /dev/null +++ b/examples/file_server/relx.config @@ -0,0 +1,2 @@ +{release, {file_server_example, "1"}, [file_server]}. +{extended_start_script, true}. diff --git a/examples/file_server/src/directory_handler.erl b/examples/file_server/src/directory_handler.erl new file mode 100644 index 0000000..d16e014 --- /dev/null +++ b/examples/file_server/src/directory_handler.erl @@ -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"]. diff --git a/examples/file_server/src/directory_lister.erl b/examples/file_server/src/directory_lister.erl new file mode 100644 index 0000000..2ca9d30 --- /dev/null +++ b/examples/file_server/src/directory_lister.erl @@ -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]). diff --git a/examples/file_server/src/file_server_app.erl b/examples/file_server/src/file_server_app.erl new file mode 100644 index 0000000..49924ba --- /dev/null +++ b/examples/file_server/src/file_server_app.erl @@ -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. diff --git a/examples/file_server/src/file_server_sup.erl b/examples/file_server/src/file_server_sup.erl new file mode 100644 index 0000000..961eb5e --- /dev/null +++ b/examples/file_server/src/file_server_sup.erl @@ -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}}. |