From 86cb105679ff80214bfc9d979afa27b1eb747307 Mon Sep 17 00:00:00 2001 From: Herman Singh Date: Fri, 7 Sep 2018 13:48:43 -0400 Subject: Rename handler modules to _h --- .../src/chunked_hello_world_app.erl | 2 +- examples/chunked_hello_world/src/toppage_h.erl | 15 +++ .../chunked_hello_world/src/toppage_handler.erl | 15 --- .../src/compress_response_app.erl | 2 +- examples/compress_response/src/toppage_h.erl | 23 ++++ examples/compress_response/src/toppage_handler.erl | 23 ---- examples/cookie/src/cookie_app.erl | 2 +- examples/cookie/src/toppage_h.erl | 21 ++++ examples/cookie/src/toppage_handler.erl | 21 ---- examples/echo_get/src/echo_get_app.erl | 2 +- examples/echo_get/src/toppage_h.erl | 22 ++++ examples/echo_get/src/toppage_handler.erl | 22 ---- examples/echo_post/src/echo_post_app.erl | 2 +- examples/echo_post/src/toppage_h.erl | 29 +++++ examples/echo_post/src/toppage_handler.erl | 29 ----- examples/eventsource/src/eventsource_app.erl | 2 +- examples/eventsource/src/eventsource_h.erl | 25 ++++ examples/eventsource/src/eventsource_handler.erl | 25 ---- examples/file_server/src/directory_h.erl | 47 ++++++++ examples/file_server/src/directory_handler.erl | 47 -------- examples/file_server/src/file_server_app.erl | 2 +- examples/hello_world/src/hello_world_app.erl | 2 +- examples/hello_world/src/toppage_h.erl | 12 ++ examples/hello_world/src/toppage_handler.erl | 12 -- .../rest_basic_auth/src/rest_basic_auth_app.erl | 2 +- examples/rest_basic_auth/src/toppage_h.erl | 28 +++++ examples/rest_basic_auth/src/toppage_handler.erl | 28 ----- .../rest_hello_world/src/rest_hello_world_app.erl | 2 +- examples/rest_hello_world/src/toppage_h.erl | 39 +++++++ examples/rest_hello_world/src/toppage_handler.erl | 39 ------- examples/rest_pastebin/src/rest_pastebin_app.erl | 2 +- examples/rest_pastebin/src/toppage_h.erl | 126 +++++++++++++++++++++ examples/rest_pastebin/src/toppage_handler.erl | 126 --------------------- .../ssl_hello_world/src/ssl_hello_world_app.erl | 2 +- examples/ssl_hello_world/src/toppage_h.erl | 12 ++ examples/ssl_hello_world/src/toppage_handler.erl | 12 -- examples/upload/src/upload_app.erl | 2 +- examples/upload/src/upload_h.erl | 15 +++ examples/upload/src/upload_handler.erl | 15 --- examples/websocket/src/websocket_app.erl | 2 +- examples/websocket/src/ws_h.erl | 24 ++++ examples/websocket/src/ws_handler.erl | 24 ---- 42 files changed, 452 insertions(+), 452 deletions(-) create mode 100644 examples/chunked_hello_world/src/toppage_h.erl delete mode 100644 examples/chunked_hello_world/src/toppage_handler.erl create mode 100644 examples/compress_response/src/toppage_h.erl delete mode 100644 examples/compress_response/src/toppage_handler.erl create mode 100644 examples/cookie/src/toppage_h.erl delete mode 100644 examples/cookie/src/toppage_handler.erl create mode 100644 examples/echo_get/src/toppage_h.erl delete mode 100644 examples/echo_get/src/toppage_handler.erl create mode 100644 examples/echo_post/src/toppage_h.erl delete mode 100644 examples/echo_post/src/toppage_handler.erl create mode 100644 examples/eventsource/src/eventsource_h.erl delete mode 100644 examples/eventsource/src/eventsource_handler.erl create mode 100644 examples/file_server/src/directory_h.erl delete mode 100644 examples/file_server/src/directory_handler.erl create mode 100644 examples/hello_world/src/toppage_h.erl delete mode 100644 examples/hello_world/src/toppage_handler.erl create mode 100644 examples/rest_basic_auth/src/toppage_h.erl delete mode 100644 examples/rest_basic_auth/src/toppage_handler.erl create mode 100644 examples/rest_hello_world/src/toppage_h.erl delete mode 100644 examples/rest_hello_world/src/toppage_handler.erl create mode 100644 examples/rest_pastebin/src/toppage_h.erl delete mode 100644 examples/rest_pastebin/src/toppage_handler.erl create mode 100644 examples/ssl_hello_world/src/toppage_h.erl delete mode 100644 examples/ssl_hello_world/src/toppage_handler.erl create mode 100644 examples/upload/src/upload_h.erl delete mode 100644 examples/upload/src/upload_handler.erl create mode 100644 examples/websocket/src/ws_h.erl delete mode 100644 examples/websocket/src/ws_handler.erl diff --git a/examples/chunked_hello_world/src/chunked_hello_world_app.erl b/examples/chunked_hello_world/src/chunked_hello_world_app.erl index 20abf6e..9c6eb1d 100644 --- a/examples/chunked_hello_world/src/chunked_hello_world_app.erl +++ b/examples/chunked_hello_world/src/chunked_hello_world_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/", toppage_handler, []} + {"/", toppage_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/chunked_hello_world/src/toppage_h.erl b/examples/chunked_hello_world/src/toppage_h.erl new file mode 100644 index 0000000..7ed2964 --- /dev/null +++ b/examples/chunked_hello_world/src/toppage_h.erl @@ -0,0 +1,15 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Chunked hello world handler. +-module(toppage_h). + +-export([init/2]). + +init(Req0, Opts) -> + Req = cowboy_req:stream_reply(200, Req0), + cowboy_req:stream_body("Hello\r\n", nofin, Req), + timer:sleep(1000), + cowboy_req:stream_body("World\r\n", nofin, Req), + timer:sleep(1000), + cowboy_req:stream_body("Chunked!\r\n", fin, Req), + {ok, Req, Opts}. diff --git a/examples/chunked_hello_world/src/toppage_handler.erl b/examples/chunked_hello_world/src/toppage_handler.erl deleted file mode 100644 index 3865698..0000000 --- a/examples/chunked_hello_world/src/toppage_handler.erl +++ /dev/null @@ -1,15 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc Chunked hello world handler. --module(toppage_handler). - --export([init/2]). - -init(Req0, Opts) -> - Req = cowboy_req:stream_reply(200, Req0), - cowboy_req:stream_body("Hello\r\n", nofin, Req), - timer:sleep(1000), - cowboy_req:stream_body("World\r\n", nofin, Req), - timer:sleep(1000), - cowboy_req:stream_body("Chunked!\r\n", fin, Req), - {ok, Req, Opts}. diff --git a/examples/compress_response/src/compress_response_app.erl b/examples/compress_response/src/compress_response_app.erl index c5fdbf6..b5cba52 100644 --- a/examples/compress_response/src/compress_response_app.erl +++ b/examples/compress_response/src/compress_response_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/", toppage_handler, []} + {"/", toppage_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/compress_response/src/toppage_h.erl b/examples/compress_response/src/toppage_h.erl new file mode 100644 index 0000000..477cfcf --- /dev/null +++ b/examples/compress_response/src/toppage_h.erl @@ -0,0 +1,23 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Compress response handler. +-module(toppage_h). + +-export([init/2]). + +init(Req0, Opts) -> + BigBody = +<<"A cowboy is an animal herder who tends cattle on ranches in North America, +traditionally on horseback, and often performs a multitude of other ranch- +related tasks. The historic American cowboy of the late 19th century arose +from the vaquero traditions of northern Mexico and became a figure of special +significance and legend. A subtype, called a wrangler, specifically tends the +horses used to work cattle. In addition to ranch work, some cowboys work for +or participate in rodeos. Cowgirls, first defined as such in the late 19th +century, had a less-well documented historical role, but in the modern world +have established the ability to work at virtually identical tasks and obtained +considerable respect for their achievements. There are also cattle handlers +in many other parts of the world, particularly South America and Australia, +who perform work similar to the cowboy in their respective nations.\n">>, + Req = cowboy_req:reply(200, #{}, BigBody, Req0), + {ok, Req, Opts}. diff --git a/examples/compress_response/src/toppage_handler.erl b/examples/compress_response/src/toppage_handler.erl deleted file mode 100644 index 67b8fea..0000000 --- a/examples/compress_response/src/toppage_handler.erl +++ /dev/null @@ -1,23 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc Compress response handler. --module(toppage_handler). - --export([init/2]). - -init(Req0, Opts) -> - BigBody = -<<"A cowboy is an animal herder who tends cattle on ranches in North America, -traditionally on horseback, and often performs a multitude of other ranch- -related tasks. The historic American cowboy of the late 19th century arose -from the vaquero traditions of northern Mexico and became a figure of special -significance and legend. A subtype, called a wrangler, specifically tends the -horses used to work cattle. In addition to ranch work, some cowboys work for -or participate in rodeos. Cowgirls, first defined as such in the late 19th -century, had a less-well documented historical role, but in the modern world -have established the ability to work at virtually identical tasks and obtained -considerable respect for their achievements. There are also cattle handlers -in many other parts of the world, particularly South America and Australia, -who perform work similar to the cowboy in their respective nations.\n">>, - Req = cowboy_req:reply(200, #{}, BigBody, Req0), - {ok, Req, Opts}. diff --git a/examples/cookie/src/cookie_app.erl b/examples/cookie/src/cookie_app.erl index 8301d17..da086e2 100644 --- a/examples/cookie/src/cookie_app.erl +++ b/examples/cookie/src/cookie_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {'_', toppage_handler, []} + {'_', toppage_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/cookie/src/toppage_h.erl b/examples/cookie/src/toppage_h.erl new file mode 100644 index 0000000..308899b --- /dev/null +++ b/examples/cookie/src/toppage_h.erl @@ -0,0 +1,21 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Cookie handler. +-module(toppage_h). + +-export([init/2]). + +init(Req0, Opts) -> + NewValue = integer_to_list(rand:uniform(1000000)), + Req1 = cowboy_req:set_resp_cookie(<<"server">>, NewValue, + Req0, #{path => <<"/">>}), + #{client := ClientCookie, server := ServerCookie} + = cowboy_req:match_cookies([{client, [], <<>>}, {server, [], <<>>}], Req1), + {ok, Body} = toppage_dtl:render([ + {client, ClientCookie}, + {server, ServerCookie} + ]), + Req = cowboy_req:reply(200, #{ + <<"content-type">> => <<"text/html">> + }, Body, Req1), + {ok, Req, Opts}. diff --git a/examples/cookie/src/toppage_handler.erl b/examples/cookie/src/toppage_handler.erl deleted file mode 100644 index 38952ef..0000000 --- a/examples/cookie/src/toppage_handler.erl +++ /dev/null @@ -1,21 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc Cookie handler. --module(toppage_handler). - --export([init/2]). - -init(Req0, Opts) -> - NewValue = integer_to_list(rand:uniform(1000000)), - Req1 = cowboy_req:set_resp_cookie(<<"server">>, NewValue, - Req0, #{path => <<"/">>}), - #{client := ClientCookie, server := ServerCookie} - = cowboy_req:match_cookies([{client, [], <<>>}, {server, [], <<>>}], Req1), - {ok, Body} = toppage_dtl:render([ - {client, ClientCookie}, - {server, ServerCookie} - ]), - Req = cowboy_req:reply(200, #{ - <<"content-type">> => <<"text/html">> - }, Body, Req1), - {ok, Req, Opts}. diff --git a/examples/echo_get/src/echo_get_app.erl b/examples/echo_get/src/echo_get_app.erl index 39fefd6..c2fd0df 100644 --- a/examples/echo_get/src/echo_get_app.erl +++ b/examples/echo_get/src/echo_get_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/", toppage_handler, []} + {"/", toppage_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/echo_get/src/toppage_h.erl b/examples/echo_get/src/toppage_h.erl new file mode 100644 index 0000000..419b4d4 --- /dev/null +++ b/examples/echo_get/src/toppage_h.erl @@ -0,0 +1,22 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc GET echo handler. +-module(toppage_h). + +-export([init/2]). + +init(Req0, Opts) -> + Method = cowboy_req:method(Req0), + #{echo := Echo} = cowboy_req:match_qs([{echo, [], undefined}], Req0), + Req = echo(Method, Echo, Req0), + {ok, Req, Opts}. + +echo(<<"GET">>, undefined, Req) -> + cowboy_req:reply(400, #{}, <<"Missing echo parameter.">>, Req); +echo(<<"GET">>, Echo, Req) -> + cowboy_req:reply(200, #{ + <<"content-type">> => <<"text/plain; charset=utf-8">> + }, Echo, Req); +echo(_, _, Req) -> + %% Method not allowed. + cowboy_req:reply(405, Req). diff --git a/examples/echo_get/src/toppage_handler.erl b/examples/echo_get/src/toppage_handler.erl deleted file mode 100644 index 280b5b0..0000000 --- a/examples/echo_get/src/toppage_handler.erl +++ /dev/null @@ -1,22 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc GET echo handler. --module(toppage_handler). - --export([init/2]). - -init(Req0, Opts) -> - Method = cowboy_req:method(Req0), - #{echo := Echo} = cowboy_req:match_qs([{echo, [], undefined}], Req0), - Req = echo(Method, Echo, Req0), - {ok, Req, Opts}. - -echo(<<"GET">>, undefined, Req) -> - cowboy_req:reply(400, #{}, <<"Missing echo parameter.">>, Req); -echo(<<"GET">>, Echo, Req) -> - cowboy_req:reply(200, #{ - <<"content-type">> => <<"text/plain; charset=utf-8">> - }, Echo, Req); -echo(_, _, Req) -> - %% Method not allowed. - cowboy_req:reply(405, Req). diff --git a/examples/echo_post/src/echo_post_app.erl b/examples/echo_post/src/echo_post_app.erl index 2c39336..b454740 100644 --- a/examples/echo_post/src/echo_post_app.erl +++ b/examples/echo_post/src/echo_post_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/", toppage_handler, []} + {"/", toppage_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/echo_post/src/toppage_h.erl b/examples/echo_post/src/toppage_h.erl new file mode 100644 index 0000000..1c6446a --- /dev/null +++ b/examples/echo_post/src/toppage_h.erl @@ -0,0 +1,29 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc POST echo handler. +-module(toppage_h). + +-export([init/2]). + +init(Req0, Opts) -> + Method = cowboy_req:method(Req0), + HasBody = cowboy_req:has_body(Req0), + Req = maybe_echo(Method, HasBody, Req0), + {ok, Req, Opts}. + +maybe_echo(<<"POST">>, true, Req0) -> + {ok, PostVals, Req} = cowboy_req:read_urlencoded_body(Req0), + Echo = proplists:get_value(<<"echo">>, PostVals), + echo(Echo, Req); +maybe_echo(<<"POST">>, false, Req) -> + cowboy_req:reply(400, [], <<"Missing body.">>, Req); +maybe_echo(_, _, Req) -> + %% Method not allowed. + cowboy_req:reply(405, Req). + +echo(undefined, Req) -> + cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req); +echo(Echo, Req) -> + cowboy_req:reply(200, #{ + <<"content-type">> => <<"text/plain; charset=utf-8">> + }, Echo, Req). diff --git a/examples/echo_post/src/toppage_handler.erl b/examples/echo_post/src/toppage_handler.erl deleted file mode 100644 index 398a377..0000000 --- a/examples/echo_post/src/toppage_handler.erl +++ /dev/null @@ -1,29 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc POST echo handler. --module(toppage_handler). - --export([init/2]). - -init(Req0, Opts) -> - Method = cowboy_req:method(Req0), - HasBody = cowboy_req:has_body(Req0), - Req = maybe_echo(Method, HasBody, Req0), - {ok, Req, Opts}. - -maybe_echo(<<"POST">>, true, Req0) -> - {ok, PostVals, Req} = cowboy_req:read_urlencoded_body(Req0), - Echo = proplists:get_value(<<"echo">>, PostVals), - echo(Echo, Req); -maybe_echo(<<"POST">>, false, Req) -> - cowboy_req:reply(400, [], <<"Missing body.">>, Req); -maybe_echo(_, _, Req) -> - %% Method not allowed. - cowboy_req:reply(405, Req). - -echo(undefined, Req) -> - cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req); -echo(Echo, Req) -> - cowboy_req:reply(200, #{ - <<"content-type">> => <<"text/plain; charset=utf-8">> - }, Echo, Req). diff --git a/examples/eventsource/src/eventsource_app.erl b/examples/eventsource/src/eventsource_app.erl index b83a276..932306e 100644 --- a/examples/eventsource/src/eventsource_app.erl +++ b/examples/eventsource/src/eventsource_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/eventsource", eventsource_handler, []}, + {"/eventsource", eventsource_h, []}, {"/", cowboy_static, {priv_file, eventsource, "index.html"}} ]} ]), diff --git a/examples/eventsource/src/eventsource_h.erl b/examples/eventsource/src/eventsource_h.erl new file mode 100644 index 0000000..ba5d866 --- /dev/null +++ b/examples/eventsource/src/eventsource_h.erl @@ -0,0 +1,25 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc EventSource emitter. +-module(eventsource_h). + +-export([init/2]). +-export([info/3]). + +init(Req0, Opts) -> + Req = cowboy_req:stream_reply(200, #{ + <<"content-type">> => <<"text/event-stream">> + }, Req0), + erlang:send_after(1000, self(), {message, "Tick"}), + {cowboy_loop, Req, Opts}. + +info({message, Msg}, Req, State) -> + cowboy_req:stream_events(#{ + id => id(), + data => Msg + }, nofin, Req), + erlang:send_after(1000, self(), {message, "Tick"}), + {ok, Req, State}. + +id() -> + integer_to_list(erlang:unique_integer([positive, monotonic]), 16). diff --git a/examples/eventsource/src/eventsource_handler.erl b/examples/eventsource/src/eventsource_handler.erl deleted file mode 100644 index 1276a75..0000000 --- a/examples/eventsource/src/eventsource_handler.erl +++ /dev/null @@ -1,25 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc EventSource emitter. --module(eventsource_handler). - --export([init/2]). --export([info/3]). - -init(Req0, Opts) -> - Req = cowboy_req:stream_reply(200, #{ - <<"content-type">> => <<"text/event-stream">> - }, Req0), - erlang:send_after(1000, self(), {message, "Tick"}), - {cowboy_loop, Req, Opts}. - -info({message, Msg}, Req, State) -> - cowboy_req:stream_events(#{ - id => id(), - data => Msg - }, nofin, Req), - erlang:send_after(1000, self(), {message, "Tick"}), - {ok, Req, State}. - -id() -> - integer_to_list(erlang:unique_integer([positive, monotonic]), 16). diff --git a/examples/file_server/src/directory_h.erl b/examples/file_server/src/directory_h.erl new file mode 100644 index 0000000..3232a7e --- /dev/null +++ b/examples/file_server/src/directory_h.erl @@ -0,0 +1,47 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Directory handler. +-module(directory_h). + +%% 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 = [<<"Index", + "">>, Body, <<"\n">>], + {HTML, Req, Path}. + +links(<<>>, File) -> + ["", File, "
\n"]; +links(Prefix, File) -> + ["", File, "
\n"]. diff --git a/examples/file_server/src/directory_handler.erl b/examples/file_server/src/directory_handler.erl deleted file mode 100644 index d16e014..0000000 --- a/examples/file_server/src/directory_handler.erl +++ /dev/null @@ -1,47 +0,0 @@ -%% 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 = [<<"Index", - "">>, Body, <<"\n">>], - {HTML, Req, Path}. - -links(<<>>, File) -> - ["", File, "
\n"]; -links(Prefix, File) -> - ["", File, "
\n"]. diff --git a/examples/file_server/src/file_server_app.erl b/examples/file_server/src/file_server_app.erl index 3f51855..89d5af0 100644 --- a/examples/file_server/src/file_server_app.erl +++ b/examples/file_server/src/file_server_app.erl @@ -15,7 +15,7 @@ start(_Type, _Args) -> {'_', [ {"/[...]", cowboy_static, {priv_dir, file_server, "", [ {mimetypes, cow_mimetypes, all}, - {dir_handler, directory_handler} + {dir_handler, directory_h} ]}} ]} ]), diff --git a/examples/hello_world/src/hello_world_app.erl b/examples/hello_world/src/hello_world_app.erl index dc1bd17..5922948 100644 --- a/examples/hello_world/src/hello_world_app.erl +++ b/examples/hello_world/src/hello_world_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/", toppage_handler, []} + {"/", toppage_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/hello_world/src/toppage_h.erl b/examples/hello_world/src/toppage_h.erl new file mode 100644 index 0000000..ee62a2f --- /dev/null +++ b/examples/hello_world/src/toppage_h.erl @@ -0,0 +1,12 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Hello world handler. +-module(toppage_h). + +-export([init/2]). + +init(Req0, Opts) -> + Req = cowboy_req:reply(200, #{ + <<"content-type">> => <<"text/plain">> + }, <<"Hello world!">>, Req0), + {ok, Req, Opts}. diff --git a/examples/hello_world/src/toppage_handler.erl b/examples/hello_world/src/toppage_handler.erl deleted file mode 100644 index a5a8458..0000000 --- a/examples/hello_world/src/toppage_handler.erl +++ /dev/null @@ -1,12 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc Hello world handler. --module(toppage_handler). - --export([init/2]). - -init(Req0, Opts) -> - Req = cowboy_req:reply(200, #{ - <<"content-type">> => <<"text/plain">> - }, <<"Hello world!">>, Req0), - {ok, Req, Opts}. diff --git a/examples/rest_basic_auth/src/rest_basic_auth_app.erl b/examples/rest_basic_auth/src/rest_basic_auth_app.erl index b8562bb..d595706 100644 --- a/examples/rest_basic_auth/src/rest_basic_auth_app.erl +++ b/examples/rest_basic_auth/src/rest_basic_auth_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/", toppage_handler, []} + {"/", toppage_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/rest_basic_auth/src/toppage_h.erl b/examples/rest_basic_auth/src/toppage_h.erl new file mode 100644 index 0000000..21b66ab --- /dev/null +++ b/examples/rest_basic_auth/src/toppage_h.erl @@ -0,0 +1,28 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Handler with basic HTTP authorization. +-module(toppage_h). + +-export([init/2]). +-export([content_types_provided/2]). +-export([is_authorized/2]). +-export([to_text/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +is_authorized(Req, State) -> + case cowboy_req:parse_header(<<"authorization">>, Req) of + {basic, User = <<"Alladin">>, <<"open sesame">>} -> + {true, Req, User}; + _ -> + {{false, <<"Basic realm=\"cowboy\"">>}, Req, State} + end. + +content_types_provided(Req, State) -> + {[ + {<<"text/plain">>, to_text} + ], Req, State}. + +to_text(Req, User) -> + {<< "Hello, ", User/binary, "!\n" >>, Req, User}. diff --git a/examples/rest_basic_auth/src/toppage_handler.erl b/examples/rest_basic_auth/src/toppage_handler.erl deleted file mode 100644 index 2e8250f..0000000 --- a/examples/rest_basic_auth/src/toppage_handler.erl +++ /dev/null @@ -1,28 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc Handler with basic HTTP authorization. --module(toppage_handler). - --export([init/2]). --export([content_types_provided/2]). --export([is_authorized/2]). --export([to_text/2]). - -init(Req, Opts) -> - {cowboy_rest, Req, Opts}. - -is_authorized(Req, State) -> - case cowboy_req:parse_header(<<"authorization">>, Req) of - {basic, User = <<"Alladin">>, <<"open sesame">>} -> - {true, Req, User}; - _ -> - {{false, <<"Basic realm=\"cowboy\"">>}, Req, State} - end. - -content_types_provided(Req, State) -> - {[ - {<<"text/plain">>, to_text} - ], Req, State}. - -to_text(Req, User) -> - {<< "Hello, ", User/binary, "!\n" >>, Req, User}. diff --git a/examples/rest_hello_world/src/rest_hello_world_app.erl b/examples/rest_hello_world/src/rest_hello_world_app.erl index e89f633..85bd9b5 100644 --- a/examples/rest_hello_world/src/rest_hello_world_app.erl +++ b/examples/rest_hello_world/src/rest_hello_world_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/", toppage_handler, []} + {"/", toppage_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/rest_hello_world/src/toppage_h.erl b/examples/rest_hello_world/src/toppage_h.erl new file mode 100644 index 0000000..bbff6f7 --- /dev/null +++ b/examples/rest_hello_world/src/toppage_h.erl @@ -0,0 +1,39 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Hello world handler. +-module(toppage_h). + +-export([init/2]). +-export([content_types_provided/2]). +-export([hello_to_html/2]). +-export([hello_to_json/2]). +-export([hello_to_text/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +content_types_provided(Req, State) -> + {[ + {<<"text/html">>, hello_to_html}, + {<<"application/json">>, hello_to_json}, + {<<"text/plain">>, hello_to_text} + ], Req, State}. + +hello_to_html(Req, State) -> + Body = <<" + + + REST Hello World! + + +

REST Hello World as HTML!

+ +">>, + {Body, Req, State}. + +hello_to_json(Req, State) -> + Body = <<"{\"rest\": \"Hello World!\"}">>, + {Body, Req, State}. + +hello_to_text(Req, State) -> + {<<"REST Hello World as text!">>, Req, State}. diff --git a/examples/rest_hello_world/src/toppage_handler.erl b/examples/rest_hello_world/src/toppage_handler.erl deleted file mode 100644 index 7657281..0000000 --- a/examples/rest_hello_world/src/toppage_handler.erl +++ /dev/null @@ -1,39 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc Hello world handler. --module(toppage_handler). - --export([init/2]). --export([content_types_provided/2]). --export([hello_to_html/2]). --export([hello_to_json/2]). --export([hello_to_text/2]). - -init(Req, Opts) -> - {cowboy_rest, Req, Opts}. - -content_types_provided(Req, State) -> - {[ - {<<"text/html">>, hello_to_html}, - {<<"application/json">>, hello_to_json}, - {<<"text/plain">>, hello_to_text} - ], Req, State}. - -hello_to_html(Req, State) -> - Body = <<" - - - REST Hello World! - - -

REST Hello World as HTML!

- -">>, - {Body, Req, State}. - -hello_to_json(Req, State) -> - Body = <<"{\"rest\": \"Hello World!\"}">>, - {Body, Req, State}. - -hello_to_text(Req, State) -> - {<<"REST Hello World as text!">>, Req, State}. diff --git a/examples/rest_pastebin/src/rest_pastebin_app.erl b/examples/rest_pastebin/src/rest_pastebin_app.erl index 31ceff3..0fbfd64 100644 --- a/examples/rest_pastebin/src/rest_pastebin_app.erl +++ b/examples/rest_pastebin/src/rest_pastebin_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/[:paste_id]", toppage_handler, []} + {"/[:paste_id]", toppage_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/rest_pastebin/src/toppage_h.erl b/examples/rest_pastebin/src/toppage_h.erl new file mode 100644 index 0000000..712a875 --- /dev/null +++ b/examples/rest_pastebin/src/toppage_h.erl @@ -0,0 +1,126 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Pastebin handler. +-module(toppage_h). + +%% Standard callbacks. +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_provided/2]). +-export([content_types_accepted/2]). +-export([resource_exists/2]). + +%% Custom callbacks. +-export([create_paste/2]). +-export([paste_html/2]). +-export([paste_text/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +allowed_methods(Req, State) -> + {[<<"GET">>, <<"POST">>], Req, State}. + +content_types_provided(Req, State) -> + {[ + {{<<"text">>, <<"plain">>, []}, paste_text}, + {{<<"text">>, <<"html">>, []}, paste_html} + ], Req, State}. + +content_types_accepted(Req, State) -> + {[{{<<"application">>, <<"x-www-form-urlencoded">>, []}, create_paste}], + Req, State}. + +resource_exists(Req, _State) -> + case cowboy_req:binding(paste_id, Req) of + undefined -> + {true, Req, index}; + PasteID -> + case valid_path(PasteID) and file_exists(PasteID) of + true -> {true, Req, PasteID}; + false -> {false, Req, PasteID} + end + end. + +create_paste(Req, State) -> + PasteID = new_paste_id(), + {ok, [{<<"paste">>, Paste}], Req2} = cowboy_req:read_urlencoded_body(Req), + ok = file:write_file(full_path(PasteID), Paste), + case cowboy_req:method(Req2) of + <<"POST">> -> + {{true, <<$/, PasteID/binary>>}, Req2, State}; + _ -> + {true, Req2, State} + end. + +paste_html(Req, index) -> + {read_file("index.html"), Req, index}; +paste_html(Req, Paste) -> + #{lang := Lang} = cowboy_req:match_qs([{lang, [], plain}], Req), + {format_html(Paste, Lang), Req, Paste}. + +paste_text(Req, index) -> + {read_file("index.txt"), Req, index}; +paste_text(Req, Paste) -> + #{lang := Lang} = cowboy_req:match_qs([{lang, [], plain}], Req), + {format_text(Paste, Lang), Req, Paste}. + +% Private + +read_file(Name) -> + {ok, Binary} = file:read_file(full_path(Name)), + Binary. + +full_path(Name) -> + filename:join([code:priv_dir(rest_pastebin), Name]). + +file_exists(Name) -> + case file:read_file_info(full_path(Name)) of + {ok, _Info} -> true; + {error, _Reason} -> false + end. + +valid_path(<<>>) -> true; +valid_path(<<$., _T/binary>>) -> false; +valid_path(<<$/, _T/binary>>) -> false; +valid_path(<<_Char, T/binary>>) -> valid_path(T). + +new_paste_id() -> + Initial = rand:uniform(62) - 1, + new_paste_id(<>, 7). +new_paste_id(Bin, 0) -> + Chars = <<"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890">>, + << <<(binary_part(Chars, B, 1))/binary>> || <> <= Bin >>; +new_paste_id(Bin, Rem) -> + Next = rand:uniform(62) - 1, + new_paste_id(<>, Rem - 1). + +format_html(Paste, plain) -> + Text = escape_html_chars(read_file(Paste)), + <<"", + "paste", + "
", Text/binary, "
\n">>; +format_html(Paste, Lang) -> + highlight(full_path(Paste), Lang, "html"). + +format_text(Paste, plain) -> + read_file(Paste); +format_text(Paste, Lang) -> + highlight(full_path(Paste), Lang, "ansi"). + +highlight(Path, Lang, Type) -> + Path1 = binary_to_list(Path), + Lang1 = binary_to_list(Lang), + os:cmd(["highlight --syntax=", Lang1, + " --doc-title=paste ", + " --out-format=", Type, + " --include-style ", Path1]). + +% Escape some HTML characters that might make a fuss +escape_html_chars(Bin) -> + << <<(escape_html_char(B))/binary>> || <> <= Bin >>. + +escape_html_char($<) -> <<"<">>; +escape_html_char($>) -> <<">">>; +escape_html_char($&) -> <<"&">>; +escape_html_char(C) -> <>. diff --git a/examples/rest_pastebin/src/toppage_handler.erl b/examples/rest_pastebin/src/toppage_handler.erl deleted file mode 100644 index ddf4c50..0000000 --- a/examples/rest_pastebin/src/toppage_handler.erl +++ /dev/null @@ -1,126 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc Pastebin handler. --module(toppage_handler). - -%% Standard callbacks. --export([init/2]). --export([allowed_methods/2]). --export([content_types_provided/2]). --export([content_types_accepted/2]). --export([resource_exists/2]). - -%% Custom callbacks. --export([create_paste/2]). --export([paste_html/2]). --export([paste_text/2]). - -init(Req, Opts) -> - {cowboy_rest, Req, Opts}. - -allowed_methods(Req, State) -> - {[<<"GET">>, <<"POST">>], Req, State}. - -content_types_provided(Req, State) -> - {[ - {{<<"text">>, <<"plain">>, []}, paste_text}, - {{<<"text">>, <<"html">>, []}, paste_html} - ], Req, State}. - -content_types_accepted(Req, State) -> - {[{{<<"application">>, <<"x-www-form-urlencoded">>, []}, create_paste}], - Req, State}. - -resource_exists(Req, _State) -> - case cowboy_req:binding(paste_id, Req) of - undefined -> - {true, Req, index}; - PasteID -> - case valid_path(PasteID) and file_exists(PasteID) of - true -> {true, Req, PasteID}; - false -> {false, Req, PasteID} - end - end. - -create_paste(Req, State) -> - PasteID = new_paste_id(), - {ok, [{<<"paste">>, Paste}], Req2} = cowboy_req:read_urlencoded_body(Req), - ok = file:write_file(full_path(PasteID), Paste), - case cowboy_req:method(Req2) of - <<"POST">> -> - {{true, <<$/, PasteID/binary>>}, Req2, State}; - _ -> - {true, Req2, State} - end. - -paste_html(Req, index) -> - {read_file("index.html"), Req, index}; -paste_html(Req, Paste) -> - #{lang := Lang} = cowboy_req:match_qs([{lang, [], plain}], Req), - {format_html(Paste, Lang), Req, Paste}. - -paste_text(Req, index) -> - {read_file("index.txt"), Req, index}; -paste_text(Req, Paste) -> - #{lang := Lang} = cowboy_req:match_qs([{lang, [], plain}], Req), - {format_text(Paste, Lang), Req, Paste}. - -% Private - -read_file(Name) -> - {ok, Binary} = file:read_file(full_path(Name)), - Binary. - -full_path(Name) -> - filename:join([code:priv_dir(rest_pastebin), Name]). - -file_exists(Name) -> - case file:read_file_info(full_path(Name)) of - {ok, _Info} -> true; - {error, _Reason} -> false - end. - -valid_path(<<>>) -> true; -valid_path(<<$., _T/binary>>) -> false; -valid_path(<<$/, _T/binary>>) -> false; -valid_path(<<_Char, T/binary>>) -> valid_path(T). - -new_paste_id() -> - Initial = rand:uniform(62) - 1, - new_paste_id(<>, 7). -new_paste_id(Bin, 0) -> - Chars = <<"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890">>, - << <<(binary_part(Chars, B, 1))/binary>> || <> <= Bin >>; -new_paste_id(Bin, Rem) -> - Next = rand:uniform(62) - 1, - new_paste_id(<>, Rem - 1). - -format_html(Paste, plain) -> - Text = escape_html_chars(read_file(Paste)), - <<"", - "paste", - "
", Text/binary, "
\n">>; -format_html(Paste, Lang) -> - highlight(full_path(Paste), Lang, "html"). - -format_text(Paste, plain) -> - read_file(Paste); -format_text(Paste, Lang) -> - highlight(full_path(Paste), Lang, "ansi"). - -highlight(Path, Lang, Type) -> - Path1 = binary_to_list(Path), - Lang1 = binary_to_list(Lang), - os:cmd(["highlight --syntax=", Lang1, - " --doc-title=paste ", - " --out-format=", Type, - " --include-style ", Path1]). - -% Escape some HTML characters that might make a fuss -escape_html_chars(Bin) -> - << <<(escape_html_char(B))/binary>> || <> <= Bin >>. - -escape_html_char($<) -> <<"<">>; -escape_html_char($>) -> <<">">>; -escape_html_char($&) -> <<"&">>; -escape_html_char(C) -> <>. diff --git a/examples/ssl_hello_world/src/ssl_hello_world_app.erl b/examples/ssl_hello_world/src/ssl_hello_world_app.erl index 009ab8f..4a9122f 100644 --- a/examples/ssl_hello_world/src/ssl_hello_world_app.erl +++ b/examples/ssl_hello_world/src/ssl_hello_world_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/", toppage_handler, []} + {"/", toppage_h, []} ]} ]), PrivDir = code:priv_dir(ssl_hello_world), diff --git a/examples/ssl_hello_world/src/toppage_h.erl b/examples/ssl_hello_world/src/toppage_h.erl new file mode 100644 index 0000000..ee62a2f --- /dev/null +++ b/examples/ssl_hello_world/src/toppage_h.erl @@ -0,0 +1,12 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Hello world handler. +-module(toppage_h). + +-export([init/2]). + +init(Req0, Opts) -> + Req = cowboy_req:reply(200, #{ + <<"content-type">> => <<"text/plain">> + }, <<"Hello world!">>, Req0), + {ok, Req, Opts}. diff --git a/examples/ssl_hello_world/src/toppage_handler.erl b/examples/ssl_hello_world/src/toppage_handler.erl deleted file mode 100644 index a5a8458..0000000 --- a/examples/ssl_hello_world/src/toppage_handler.erl +++ /dev/null @@ -1,12 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc Hello world handler. --module(toppage_handler). - --export([init/2]). - -init(Req0, Opts) -> - Req = cowboy_req:reply(200, #{ - <<"content-type">> => <<"text/plain">> - }, <<"Hello world!">>, Req0), - {ok, Req, Opts}. diff --git a/examples/upload/src/upload_app.erl b/examples/upload/src/upload_app.erl index 37929cd..2ed5804 100644 --- a/examples/upload/src/upload_app.erl +++ b/examples/upload/src/upload_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ {"/", cowboy_static, {priv_file, upload, "index.html"}}, - {"/upload", upload_handler, []} + {"/upload", upload_h, []} ]} ]), {ok, _} = cowboy:start_clear(http, [{port, 8080}], #{ diff --git a/examples/upload/src/upload_h.erl b/examples/upload/src/upload_h.erl new file mode 100644 index 0000000..45db8e1 --- /dev/null +++ b/examples/upload/src/upload_h.erl @@ -0,0 +1,15 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Upload handler. +-module(upload_h). + +-export([init/2]). + +init(Req, Opts) -> + {ok, Headers, Req2} = cowboy_req:read_part(Req), + {ok, Data, Req3} = cowboy_req:read_part_body(Req2), + {file, <<"inputfile">>, Filename, ContentType} + = cow_multipart:form_data(Headers), + io:format("Received file ~p of content-type ~p as follow:~n~p~n~n", + [Filename, ContentType, Data]), + {ok, Req3, Opts}. diff --git a/examples/upload/src/upload_handler.erl b/examples/upload/src/upload_handler.erl deleted file mode 100644 index 5dbf08f..0000000 --- a/examples/upload/src/upload_handler.erl +++ /dev/null @@ -1,15 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - -%% @doc Upload handler. --module(upload_handler). - --export([init/2]). - -init(Req, Opts) -> - {ok, Headers, Req2} = cowboy_req:read_part(Req), - {ok, Data, Req3} = cowboy_req:read_part_body(Req2), - {file, <<"inputfile">>, Filename, ContentType} - = cow_multipart:form_data(Headers), - io:format("Received file ~p of content-type ~p as follow:~n~p~n~n", - [Filename, ContentType, Data]), - {ok, Req3, Opts}. diff --git a/examples/websocket/src/websocket_app.erl b/examples/websocket/src/websocket_app.erl index 90e6c8d..92c7edc 100644 --- a/examples/websocket/src/websocket_app.erl +++ b/examples/websocket/src/websocket_app.erl @@ -13,7 +13,7 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ {"/", cowboy_static, {priv_file, websocket, "index.html"}}, - {"/websocket", ws_handler, []}, + {"/websocket", ws_h, []}, {"/static/[...]", cowboy_static, {priv_dir, websocket, "static"}} ]} ]), diff --git a/examples/websocket/src/ws_h.erl b/examples/websocket/src/ws_h.erl new file mode 100644 index 0000000..63055df --- /dev/null +++ b/examples/websocket/src/ws_h.erl @@ -0,0 +1,24 @@ +-module(ws_h). + +-export([init/2]). +-export([websocket_init/1]). +-export([websocket_handle/2]). +-export([websocket_info/2]). + +init(Req, Opts) -> + {cowboy_websocket, Req, Opts}. + +websocket_init(State) -> + erlang:start_timer(1000, self(), <<"Hello!">>), + {ok, State}. + +websocket_handle({text, Msg}, State) -> + {reply, {text, << "That's what she said! ", Msg/binary >>}, State}; +websocket_handle(_Data, State) -> + {ok, State}. + +websocket_info({timeout, _Ref, Msg}, State) -> + erlang:start_timer(1000, self(), <<"How' you doin'?">>), + {reply, {text, Msg}, State}; +websocket_info(_Info, State) -> + {ok, State}. diff --git a/examples/websocket/src/ws_handler.erl b/examples/websocket/src/ws_handler.erl deleted file mode 100644 index 209d4bc..0000000 --- a/examples/websocket/src/ws_handler.erl +++ /dev/null @@ -1,24 +0,0 @@ --module(ws_handler). - --export([init/2]). --export([websocket_init/1]). --export([websocket_handle/2]). --export([websocket_info/2]). - -init(Req, Opts) -> - {cowboy_websocket, Req, Opts}. - -websocket_init(State) -> - erlang:start_timer(1000, self(), <<"Hello!">>), - {ok, State}. - -websocket_handle({text, Msg}, State) -> - {reply, {text, << "That's what she said! ", Msg/binary >>}, State}; -websocket_handle(_Data, State) -> - {ok, State}. - -websocket_info({timeout, _Ref, Msg}, State) -> - erlang:start_timer(1000, self(), <<"How' you doin'?">>), - {reply, {text, Msg}, State}; -websocket_info(_Info, State) -> - {ok, State}. -- cgit v1.2.3