aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2019-09-05 18:05:44 +0200
committerLoïc Hoguin <[email protected]>2019-09-05 18:05:44 +0200
commitd5814a59f40a0347d990444d418f995ff10a8109 (patch)
treea2d4870d53efa827133bb4187c72b23921225029 /examples
parent094387a08f95c91ec6e297aa5e5786aff0ab0a65 (diff)
downloadcowboy-d5814a59f40a0347d990444d418f995ff10a8109.tar.gz
cowboy-d5814a59f40a0347d990444d418f995ff10a8109.tar.bz2
cowboy-d5814a59f40a0347d990444d418f995ff10a8109.zip
rest_pastebin example: Add a constraint for lang parameter
Diffstat (limited to 'examples')
-rw-r--r--examples/rest_pastebin/src/toppage_h.erl12
1 files changed, 10 insertions, 2 deletions
diff --git a/examples/rest_pastebin/src/toppage_h.erl b/examples/rest_pastebin/src/toppage_h.erl
index 5b130de..784cad0 100644
--- a/examples/rest_pastebin/src/toppage_h.erl
+++ b/examples/rest_pastebin/src/toppage_h.erl
@@ -56,17 +56,25 @@ create_paste(Req, State) ->
paste_html(Req, index) ->
{read_file("index.html"), Req, index};
paste_html(Req, Paste) ->
- #{lang := Lang} = cowboy_req:match_qs([{lang, [], plain}], Req),
+ #{lang := Lang} = cowboy_req:match_qs([{lang, [fun lang_constraint/2], 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),
+ #{lang := Lang} = cowboy_req:match_qs([{lang, [fun lang_constraint/2], plain}], Req),
{format_text(Paste, Lang), Req, Paste}.
% Private
+lang_constraint(forward, Bin) ->
+ case re:run(Bin, "^[a-z0-9_]+$", [{capture, none}]) of
+ match -> {ok, Bin};
+ nomatch -> {error, bad_lang}
+ end;
+lang_constraint(format_error, {bad_lang, _}) ->
+ "Invalid lang parameter.".
+
read_file(Name) ->
{ok, Binary} = file:read_file(full_path(Name)),
Binary.