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:
parent
4293a40d9e
commit
88227898ed
26 changed files with 181 additions and 180 deletions
|
@ -21,6 +21,9 @@
|
||||||
* link:eventsource[]:
|
* link:eventsource[]:
|
||||||
eventsource emitter and consumer
|
eventsource emitter and consumer
|
||||||
|
|
||||||
|
* link:file_server[]:
|
||||||
|
file server with directory listing
|
||||||
|
|
||||||
* link:hello_world[]:
|
* link:hello_world[]:
|
||||||
simplest example application
|
simplest example application
|
||||||
|
|
||||||
|
@ -42,15 +45,9 @@
|
||||||
* link:ssl_hello_world[]:
|
* link:ssl_hello_world[]:
|
||||||
simplest SSL application
|
simplest SSL application
|
||||||
|
|
||||||
* link:static_world[]:
|
|
||||||
static file handler
|
|
||||||
|
|
||||||
* link:upload[]:
|
* link:upload[]:
|
||||||
multipart/form-data upload
|
multipart/form-data upload
|
||||||
|
|
||||||
* link:web_server[]:
|
|
||||||
serve files with lists directory entries
|
|
||||||
|
|
||||||
* link:websocket[]:
|
* link:websocket[]:
|
||||||
websocket example
|
websocket example
|
||||||
|
|
||||||
|
|
8
examples/file_server/Makefile
Normal file
8
examples/file_server/Makefile
Normal 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
|
89
examples/file_server/README.asciidoc
Normal file
89
examples/file_server/README.asciidoc
Normal 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)=[])
|
||||||
|
----
|
2
examples/file_server/relx.config
Normal file
2
examples/file_server/relx.config
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
{release, {file_server_example, "1"}, [file_server]}.
|
||||||
|
{extended_start_script, true}.
|
|
@ -27,12 +27,12 @@ resource_exists(Req, {ReqPath, FilePath}) ->
|
||||||
|
|
||||||
content_types_provided(Req, State) ->
|
content_types_provided(Req, State) ->
|
||||||
{[
|
{[
|
||||||
{{<<"application">>, <<"json">>, []}, list_json},
|
{{<<"text">>, <<"html">>, []}, list_html},
|
||||||
{{<<"text">>, <<"html">>, []}, list_html}
|
{{<<"application">>, <<"json">>, []}, list_json}
|
||||||
], Req, State}.
|
], Req, State}.
|
||||||
|
|
||||||
list_json(Req, {Path, Fs}) ->
|
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}.
|
{jsx:encode(Files), Req, Path}.
|
||||||
|
|
||||||
list_html(Req, {Path, Fs}) ->
|
list_html(Req, {Path, Fs}) ->
|
|
@ -5,16 +5,14 @@
|
||||||
|
|
||||||
-export([execute/2]).
|
-export([execute/2]).
|
||||||
|
|
||||||
|
execute(Req, Env=#{handler := cowboy_static}) ->
|
||||||
|
redirect_directory(Req, Env);
|
||||||
execute(Req, Env) ->
|
execute(Req, Env) ->
|
||||||
case lists:keyfind(handler, 1, Env) of
|
{ok, Req, Env}.
|
||||||
{handler, cowboy_static} -> redirect_directory(Req, Env);
|
|
||||||
_H -> {ok, Req, Env}
|
|
||||||
end.
|
|
||||||
|
|
||||||
redirect_directory(Req, Env) ->
|
redirect_directory(Req, Env=#{handler_opts := {_, _, _, Extra}}) ->
|
||||||
Path = cowboy_req:path_info(Req),
|
Path = cowboy_req:path_info(Req),
|
||||||
Path1 = << <<S/binary, $/>> || S <- Path >>,
|
Path1 = << <<S/binary, $/>> || S <- Path >>,
|
||||||
{handler_opts, {_, _, _, Extra}} = lists:keyfind(handler_opts, 1, Env),
|
|
||||||
{dir_handler, DirHandler} = lists:keyfind(dir_handler, 1, Extra),
|
{dir_handler, DirHandler} = lists:keyfind(dir_handler, 1, Extra),
|
||||||
FullPath = resource_path(Path1),
|
FullPath = resource_path(Path1),
|
||||||
case valid_path(Path) and filelib:is_dir(FullPath) of
|
case valid_path(Path) and filelib:is_dir(FullPath) of
|
||||||
|
@ -23,9 +21,7 @@ redirect_directory(Req, Env) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handle_directory(Req, Env, Prefix, Path, DirHandler) ->
|
handle_directory(Req, Env, Prefix, Path, DirHandler) ->
|
||||||
Env1 = lists:keydelete(handler, 1,
|
{ok, Req, Env#{handler => DirHandler, handler_opts => {Prefix, Path}}}.
|
||||||
lists:keydelete(handler_opts, 1, Env)),
|
|
||||||
{ok, Req, [{handler, DirHandler}, {handler_opts, {Prefix, Path}} | Env1]}.
|
|
||||||
|
|
||||||
valid_path([]) -> true;
|
valid_path([]) -> true;
|
||||||
valid_path([<<"..">> | _T]) -> false;
|
valid_path([<<"..">> | _T]) -> false;
|
||||||
|
@ -33,4 +29,4 @@ valid_path([<<"/", _/binary>> | _T]) -> false;
|
||||||
valid_path([_H | Rest]) -> valid_path(Rest).
|
valid_path([_H | Rest]) -> valid_path(Rest).
|
||||||
|
|
||||||
resource_path(Path) ->
|
resource_path(Path) ->
|
||||||
filename:join([code:priv_dir(web_server), Path]).
|
filename:join([code:priv_dir(file_server), Path]).
|
|
@ -1,7 +1,7 @@
|
||||||
%% Feel free to use, reuse and abuse the code in this file.
|
%% Feel free to use, reuse and abuse the code in this file.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
-module(web_server_app).
|
-module(file_server_app).
|
||||||
-behaviour(application).
|
-behaviour(application).
|
||||||
|
|
||||||
%% API.
|
%% API.
|
||||||
|
@ -13,17 +13,17 @@
|
||||||
start(_Type, _Args) ->
|
start(_Type, _Args) ->
|
||||||
Dispatch = cowboy_router:compile([
|
Dispatch = cowboy_router:compile([
|
||||||
{'_', [
|
{'_', [
|
||||||
{"/[...]", cowboy_static, {priv_dir, web_server, "", [
|
{"/[...]", cowboy_static, {priv_dir, file_server, "", [
|
||||||
{mimetypes, cow_mimetypes, all},
|
{mimetypes, cow_mimetypes, all},
|
||||||
{dir_handler, directory_handler}
|
{dir_handler, directory_handler}
|
||||||
]}}
|
]}}
|
||||||
]}
|
]}
|
||||||
]),
|
]),
|
||||||
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
|
{ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{
|
||||||
{env, [{dispatch, Dispatch}]},
|
env => #{dispatch => Dispatch},
|
||||||
{middlewares, [cowboy_router, directory_lister, cowboy_handler]}
|
middlewares => [cowboy_router, directory_lister, cowboy_handler]
|
||||||
]),
|
}),
|
||||||
web_server_sup:start_link().
|
file_server_sup:start_link().
|
||||||
|
|
||||||
stop(_State) ->
|
stop(_State) ->
|
||||||
ok.
|
ok.
|
|
@ -1,7 +1,7 @@
|
||||||
%% Feel free to use, reuse and abuse the code in this file.
|
%% Feel free to use, reuse and abuse the code in this file.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
-module(web_server_sup).
|
-module(file_server_sup).
|
||||||
-behaviour(supervisor).
|
-behaviour(supervisor).
|
||||||
|
|
||||||
%% API.
|
%% API.
|
|
@ -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
|
|
|
@ -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!
|
|
||||||
----
|
|
|
@ -1 +0,0 @@
|
||||||
<h1>Howdy, Pardner</h1>
|
|
|
@ -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>
|
|
|
@ -1,2 +0,0 @@
|
||||||
{release, {static_world_example, "1"}, [static_world]}.
|
|
||||||
{extended_start_script, true}.
|
|
|
@ -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.
|
|
|
@ -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}}.
|
|
|
@ -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
|
|
|
@ -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.
|
@ -1 +0,0 @@
|
||||||
If you read this then the static file server works!
|
|
|
@ -1,2 +0,0 @@
|
||||||
{release, {web_server_example, "1"}, [web_server]}.
|
|
||||||
{extended_start_script, true}.
|
|
|
@ -60,6 +60,26 @@ do_stop(Example) ->
|
||||||
ct:log("~s~n", [element(2, file:read_file(Log))]),
|
ct:log("~s~n", [element(2, file:read_file(Log))]),
|
||||||
ok.
|
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.
|
%% TCP and SSL Hello World.
|
||||||
|
|
||||||
hello_world(Config) ->
|
hello_world(Config) ->
|
||||||
|
@ -83,14 +103,7 @@ ssl_hello_world(Config) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_hello_world(Transport, Protocol, Config) ->
|
do_hello_world(Transport, Protocol, Config) ->
|
||||||
Port = case Transport of
|
{200, _, <<"Hello world!">>} = do_get(Transport, Protocol, "/", Config),
|
||||||
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),
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% Echo GET.
|
%% Echo GET.
|
||||||
|
@ -106,10 +119,7 @@ echo_get(Config) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_echo_get(Transport, Protocol, Config) ->
|
do_echo_get(Transport, Protocol, Config) ->
|
||||||
ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
|
{200, _, <<"this is fun">>} = do_get(Transport, Protocol, "/?echo=this+is+fun", 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),
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% Echo POST.
|
%% Echo POST.
|
||||||
|
@ -146,27 +156,50 @@ rest_hello_world(Config) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_rest_hello_world(Transport, Protocol, Config) ->
|
do_rest_hello_world(Transport, Protocol, Config) ->
|
||||||
<< "<html>", _/bits >> = do_rest_hello_world_get(Transport, Protocol, undefined, Config),
|
<< "<html>", _/bits >> = do_rest_get(Transport, Protocol, "/", undefined, Config),
|
||||||
<< "REST Hello World as text!" >> = do_rest_hello_world_get(Transport, Protocol, <<"text/plain">>, Config),
|
<< "REST Hello World as text!" >> = do_rest_get(Transport, Protocol, "/", <<"text/plain">>, Config),
|
||||||
<< "{\"rest\": \"Hello World!\"}" >> = do_rest_hello_world_get(Transport, Protocol, <<"application/json">>, Config),
|
<< "{\"rest\": \"Hello World!\"}" >> = do_rest_get(Transport, Protocol, "/", <<"application/json">>, Config),
|
||||||
not_acceptable = do_rest_hello_world_get(Transport, Protocol, <<"text/css">>, Config),
|
not_acceptable = do_rest_get(Transport, Protocol, "/", <<"text/css">>, Config),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
do_rest_hello_world_get(Transport, Protocol, Accept, Config) ->
|
do_rest_get(Transport, Protocol, Path, Accept, Config) ->
|
||||||
Port = case Transport of
|
ReqHeaders = case Accept of
|
||||||
tcp -> 8080;
|
|
||||||
ssl -> 8443
|
|
||||||
end,
|
|
||||||
ConnPid = gun_open([{port, Port}, {type, Transport}, {protocol, Protocol}|Config]),
|
|
||||||
Headers = case Accept of
|
|
||||||
undefined -> [];
|
undefined -> [];
|
||||||
_ -> [{<<"accept">>, Accept}]
|
_ -> [{<<"accept">>, Accept}]
|
||||||
end,
|
end,
|
||||||
Ref = gun:get(ConnPid, "/", Headers),
|
case do_get(Transport, Protocol, Path, ReqHeaders, Config) of
|
||||||
case gun:await(ConnPid, Ref) of
|
{200, RespHeaders, Body} ->
|
||||||
{response, nofin, 200, _} ->
|
Accept = case Accept of
|
||||||
{ok, Body} = gun:await_body(ConnPid, Ref),
|
undefined -> undefined;
|
||||||
|
_ ->
|
||||||
|
{_, ContentType} = lists:keyfind(<<"content-type">>, 1, RespHeaders),
|
||||||
|
ContentType
|
||||||
|
end,
|
||||||
Body;
|
Body;
|
||||||
{response, _, 406, _} ->
|
{406, _, _} ->
|
||||||
not_acceptable
|
not_acceptable
|
||||||
end.
|
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.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue