0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Return undefined instead of failing or returning "" when a value doesn't exist.

This commit is contained in:
Loïc Hoguin 2011-03-27 13:11:57 +02:00
parent d0d9b0e8b3
commit 150723ca21

View file

@ -71,13 +71,15 @@ raw_path(Req) ->
{Req#http_req.raw_path, Req}. {Req#http_req.raw_path, Req}.
-spec qs_val(Name::string(), Req::#http_req{}) -spec qs_val(Name::string(), Req::#http_req{})
-> {Value::string() | true, Req::#http_req{}}. -> {Value::string() | true | undefined, Req::#http_req{}}.
qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined}) -> qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined}) ->
QsVals = parse_qs(RawQs), QsVals = parse_qs(RawQs),
qs_val(Name, Req#http_req{qs_vals=QsVals}); qs_val(Name, Req#http_req{qs_vals=QsVals});
qs_val(Name, Req) -> qs_val(Name, Req) ->
{Name, Value} = lists:keyfind(Name, 1, Req#http_req.qs_vals), case lists:keyfind(Name, 1, Req#http_req.qs_vals) of
{Value, Req}. {Name, Value} -> {Value, Req};
false -> {undefined, Req}
end.
-spec qs_val(Name::string(), Default::term(), Req::#http_req{}) -spec qs_val(Name::string(), Default::term(), Req::#http_req{})
-> {Value::string() | term() | true, Req::#http_req{}}. -> {Value::string() | term() | true, Req::#http_req{}}.
@ -101,10 +103,12 @@ raw_qs(Req) ->
{Req#http_req.raw_qs, Req}. {Req#http_req.raw_qs, Req}.
-spec binding(Name::atom(), Req::#http_req{}) -spec binding(Name::atom(), Req::#http_req{})
-> {Value::string(), Req::#http_req{}}. -> {Value::string() | undefined, Req::#http_req{}}.
binding(Name, Req) -> binding(Name, Req) ->
{Name, Value} = lists:keyfind(Name, 1, Req#http_req.bindings), case lists:keyfind(Name, 1, Req#http_req.bindings) of
{Value, Req}. {Name, Value} -> {Value, Req};
false -> {undefined, Req}
end.
-spec binding(Name::atom(), Default::term(), Req::#http_req{}) -spec binding(Name::atom(), Default::term(), Req::#http_req{})
-> {Value::string() | term(), Req::#http_req{}}. -> {Value::string() | term(), Req::#http_req{}}.
@ -118,11 +122,11 @@ bindings(Req) ->
{Req#http_req.bindings, Req}. {Req#http_req.bindings, Req}.
-spec header(Name::atom() | string(), Req::#http_req{}) -spec header(Name::atom() | string(), Req::#http_req{})
-> {Value::string(), Req::#http_req{}}. -> {Value::string() | undefined, Req::#http_req{}}.
header(Name, Req) -> header(Name, Req) ->
case lists:keyfind(Name, 1, Req#http_req.headers) of case lists:keyfind(Name, 1, Req#http_req.headers) of
{Name, Value} -> {Value, Req}; {Name, Value} -> {Value, Req};
false -> {"", Req} false -> {undefined, Req}
end. end.
-spec header(Name::atom() | string(), Default::term(), Req::#http_req{}) -spec header(Name::atom() | string(), Default::term(), Req::#http_req{})