From d5814a59f40a0347d990444d418f995ff10a8109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 5 Sep 2019 18:05:44 +0200 Subject: rest_pastebin example: Add a constraint for lang parameter --- examples/rest_pastebin/src/toppage_h.erl | 12 ++++++++++-- 1 file 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. -- cgit v1.2.3