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 --- 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 ----------------------- 3 files changed, 127 insertions(+), 127 deletions(-) create mode 100644 examples/rest_pastebin/src/toppage_h.erl delete mode 100644 examples/rest_pastebin/src/toppage_handler.erl (limited to 'examples/rest_pastebin') 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) -> <>. -- cgit v1.2.3