Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 3.07 KB | None | 0 0
  1. -module(test_method).
  2. %-compile([export_all, nowarn_export_all]).
  3.  
  4.  
  5. % standard callbacks
  6. -export([init/2, allowed_methods/2, content_types_provided/2, content_types_accepted/2, resource_exists/2]).
  7.  
  8.  
  9. % custom callbacks
  10. -export([method_jsone/2]).
  11. -export([create_jsone/2]).
  12. -export([create_jsone2/4]).
  13. -export([valid_path/1]).
  14.  
  15.  
  16. init(Req, Opts) ->
  17.   {cowboy_rest, Req, Opts}.
  18.  
  19.  
  20. allowed_methods(Req, State) ->
  21.   {[<<"GET">>, <<"POST">>], Req, State}.
  22.   %{[<<"POST">>], Req, State}.
  23.  
  24.  
  25. content_types_provided(Req, State) ->
  26.   {[
  27.     %{ {<<"text">>, <<"plain">>, []}, method_jsone},
  28.     %{ {<<"text">>, <<"html">>, []}, method_jsone},
  29.     %{ {<<"application">>, <<"json">>, []}, method_jsone}
  30.     { {<<"text">>, <<"plain">>, '*'}, method_jsone},
  31.     { {<<"text">>, <<"html">>, '*'}, method_jsone},
  32.     { {<<"application">>, <<"json">>, '*'}, method_jsone}
  33.   ], Req, State}.
  34.  
  35.  
  36. content_types_accepted(Req, State) ->
  37.   { [{ {<<"application">>, <<"x-www-form-urlencoded">>, []}, create_jsone}], Req, State}.
  38.  
  39.  
  40. resource_exists(Req, State) ->
  41.   io:format("resource_exists State: ~p~n", [State]),
  42.   {ok, Params, Req2} = cowboy_req:read_urlencoded_body(Req),
  43.   io:format("Post params: ~p~n",[Params]),
  44.  
  45.   case lists:keyfind(<<"test_id">>, 1, Params) of
  46.     false -> {false, Req2, [{test_id, <<>>} | State]};
  47.     {_, Test_Id} -> {?MODULE:valid_path(Test_Id), Req2, [{test_id, Test_Id} | State]}
  48.   end.
  49.  
  50.  
  51. create_jsone(Req, State) ->
  52.   io:format("create0_jsone: ~p~n", ["7799"]),
  53.   {test_id, Some_Id} = lists:keyfind(test_id, 1, State),
  54.   io:format("create_jsone: ~p~n", [Some_Id]),
  55.   ?MODULE:create_jsone2(cowboy_req:method(Req), Some_Id, Req, State).
  56.  
  57. % helper
  58. create_jsone2(<<"POST">>, Some_Id, Req2, State) ->
  59.   Req3 = cowboy_req:set_resp_body(["Hello world! id: ", Some_Id], Req2),
  60.   {true, Req3, State};
  61. create_jsone2(M, _, Req2, State) ->
  62.   io:format("method: ~p~n", [M]),
  63.   {true, Req2, State}.
  64.  
  65. % cowboy_req:binding(test_id, Req, undefined) % get value from router bind,, https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_req.binding/
  66. % #{lang := Lang} = cowboy_req:match_qs([{lang, [], plain}], Req), % get_params,, https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_req.match_qs/
  67. % {ok, [{<<"paste">>, Value}], Req2} = cowboy_req:read_urlencoded_body(Req), % post_params,, https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_req.read_urlencoded_body/
  68.  
  69. method_jsone(Req, not_found) ->
  70.   { <<"{\"status\":\"error\", \"error_message\":\"invalid data\"}">>, Req, not_found};
  71. method_jsone(Req, Id) ->
  72.   { <<"{\"status\":\"ok\", \"data\":[{\"id\":\"", Id/binary, "\"}]}">>, Req, Id}.
  73.  
  74.  
  75. % %%%%%%%%%%%%%%%%%%%%%%%55
  76. valid_path(<<>>) -> true;
  77. valid_path(<<$., _T/binary>>) -> false;
  78. valid_path(<<$/, _T/binary>>) -> false;
  79. valid_path(<<_Char, T/binary>>) -> valid_path(T).
  80.  
  81.  
  82.  
  83. % Escape some HTML characters that might make a fuss
  84. escape_html_chars(Bin) ->
  85.   << <<(escape_html_char(B))/binary>> || <<B>> <= Bin >>.
  86.  
  87. escape_html_char($<) -> <<"&lt;">>;
  88. escape_html_char($>) -> <<"&gt;">>;
  89. escape_html_char($&) -> <<"&amp;">>;
  90. escape_html_char(C) -> <<C>>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement