From 18063caee0983808e2d48ca71062c2c0af046ed4 Mon Sep 17 00:00:00 2001 From: Sina Samavati Date: Sat, 7 Dec 2013 23:59:27 +0330 Subject: [PATCH] Change coffer_http_util:not_allowed/2 to take a binary instead of a list of binaries --- apps/coffer/src/coffer_http_blob.erl | 3 +-- apps/coffer/src/coffer_http_root.erl | 2 +- apps/coffer/src/coffer_http_stat.erl | 2 +- apps/coffer/src/coffer_http_storage.erl | 2 +- apps/coffer/src/coffer_http_storages.erl | 2 +- apps/coffer/src/coffer_http_util.erl | 21 ++------------------- 6 files changed, 7 insertions(+), 25 deletions(-) diff --git a/apps/coffer/src/coffer_http_blob.erl b/apps/coffer/src/coffer_http_blob.erl index f7c24d4..d4165f0 100644 --- a/apps/coffer/src/coffer_http_blob.erl +++ b/apps/coffer/src/coffer_http_blob.erl @@ -105,8 +105,7 @@ maybe_process(<<"DELETE">>, BlobRef, Storage, Req) -> end; maybe_process(_, _, _, Req) -> - coffer_http_util:not_allowed([<<"HEAD">>, <<"GET">>, <<"PUT">>, - <<"DELETE">>], Req). + coffer_http_util:not_allowed(<<"HEAD, GET, PUT, DELETE">>, Req). terminate(_Reason, _Req, _State) -> ok. diff --git a/apps/coffer/src/coffer_http_root.erl b/apps/coffer/src/coffer_http_root.erl index cc8060e..142c480 100644 --- a/apps/coffer/src/coffer_http_root.erl +++ b/apps/coffer/src/coffer_http_root.erl @@ -27,7 +27,7 @@ maybe_process(<<"GET">>, Req) -> cowboy_req:reply(200, [{<<"Content-Type">>, <<"application/json">>}], Json1, Req1); maybe_process(_, Req) -> - coffer_http_util:not_allowed([<<"GET">>, <<"HEAD">>], Req). + coffer_http_util:not_allowed(<<"GET, HEAD">>, Req). terminate(_Reason, _Req, _State) -> ok. diff --git a/apps/coffer/src/coffer_http_stat.erl b/apps/coffer/src/coffer_http_stat.erl index ce871c6..9bec842 100644 --- a/apps/coffer/src/coffer_http_stat.erl +++ b/apps/coffer/src/coffer_http_stat.erl @@ -37,7 +37,7 @@ handle_stat(<<"POST">>, Storage, Req) -> BlobRefs = [BlobRef || {_, BlobRef} <- KVs], stat_response(BlobRefs, Storage, Req1); handle_stat(_, _, Req) -> - coffer_http_util:not_allowed([<<"GET">>, <<"POST">>], Req). + coffer_http_util:not_allowed(<<"GET, POST">>, Req). terminate(_Reason, _Req, _State) -> ok. diff --git a/apps/coffer/src/coffer_http_storage.erl b/apps/coffer/src/coffer_http_storage.erl index c71e3c3..74b7d74 100644 --- a/apps/coffer/src/coffer_http_storage.erl +++ b/apps/coffer/src/coffer_http_storage.erl @@ -60,7 +60,7 @@ maybe_process(<<"POST">>, Storage, Req) -> coffer_http_util:ok(Req1) end; maybe_process(_, _, Req) -> - coffer_http_util:not_allowed([<<"HEAD">>, <<"GET">>, <<"POST">>], Req). + coffer_http_util:not_allowed(<<"HEAD, GET, POST">>, Req). terminate(_Reason, _Req, _State) -> ok. diff --git a/apps/coffer/src/coffer_http_storages.erl b/apps/coffer/src/coffer_http_storages.erl index ae80a64..68ce5ae 100644 --- a/apps/coffer/src/coffer_http_storages.erl +++ b/apps/coffer/src/coffer_http_storages.erl @@ -26,7 +26,7 @@ maybe_process(<<"GET">>, Req) -> cowboy_req:reply(200, [{<<"Content-Type">>, <<"application/json">>}], Json, Req1); maybe_process(_, Req) -> - coffer_http_util:not_allowed([<<"GET">>], Req). + coffer_http_util:not_allowed(<<"GET">>, Req). terminate(_Reason, _Req, _State) -> ok. diff --git a/apps/coffer/src/coffer_http_util.erl b/apps/coffer/src/coffer_http_util.erl index 6922094..ae397ce 100644 --- a/apps/coffer/src/coffer_http_util.erl +++ b/apps/coffer/src/coffer_http_util.erl @@ -12,26 +12,9 @@ -export([to_json/2]). -export([maybe_prettify_json/2]). +-spec not_allowed(binary(), Req) -> {ok, Req} when Req::cowboy_req:req(). not_allowed(AllowedMethods, Req) -> - AddCommaFunc = fun(Element, Acc) -> - case Acc of - [<<>>] -> - [Element|Acc]; - _ -> - [Element,<<",">>|Acc] - end - end, - ReversedAllowedMethodsWithComaList = lists:foldl( - AddCommaFunc, - [<<"">>], - AllowedMethods - ), - AllowedMethodsWithComaList = lists:reverse( - ReversedAllowedMethodsWithComaList - ), - AllowedMethodsWithComa = iolist_to_binary(AllowedMethodsWithComaList), - Extra = [{<<"Allow">>, AllowedMethodsWithComa}], - error(405, <<"not_allowed">>, Extra, Req). + error(405, <<"not_allowed">>, [{<<"Allow">>, AllowedMethods}], Req). not_found(Req) -> error(404, <<"not found">>, Req).