diff options
author | Loïc Hoguin <[email protected]> | 2011-03-29 14:48:40 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2011-03-29 14:48:40 +0200 |
commit | e6e5b1767fe379c3fb9a9c46f2ec2b05bf485ce8 (patch) | |
tree | 76671f60a7f14af16b892e7e31f9bd7ef99b7ea8 | |
parent | 150723ca21e3b23ea0d4341d7775a3d03e78e9eb (diff) | |
parent | 101034842e4aec1efaf4b39ece0608ba4b336794 (diff) | |
download | cowboy-e6e5b1767fe379c3fb9a9c46f2ec2b05bf485ce8.tar.gz cowboy-e6e5b1767fe379c3fb9a9c46f2ec2b05bf485ce8.tar.bz2 cowboy-e6e5b1767fe379c3fb9a9c46f2ec2b05bf485ce8.zip |
Merge remote-tracking branch 'nox/improve-http-req'
-rw-r--r-- | src/cowboy_http_req.erl | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl index 8d558b7..8ea8efc 100644 --- a/src/cowboy_http_req.erl +++ b/src/cowboy_http_req.erl @@ -1,4 +1,5 @@ %% Copyright (c) 2011, Loïc Hoguin <[email protected]> +%% Copyright (c) 2011, Anthony Ramine <[email protected]> %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -72,24 +73,22 @@ raw_path(Req) -> -spec qs_val(Name::string(), Req::#http_req{}) -> {Value::string() | true | undefined, Req::#http_req{}}. -qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined}) -> - QsVals = parse_qs(RawQs), - qs_val(Name, Req#http_req{qs_vals=QsVals}); +%% @equiv qs_val(Name, Req) -> qs_val(Name, Req, undefined) qs_val(Name, Req) -> + qs_val(Name, Req, undefined). + +-spec qs_val(Name::string(), Req::#http_req{}, Default) + -> {Value::string() | true | Default, Req::#http_req{}} + when Default::term(). +qs_val(Name, Req=#http_req{raw_qs=RawQs, qs_vals=undefined}, Default) -> + QsVals = parse_qs(RawQs), + qs_val(Name, Req#http_req{qs_vals=QsVals}, Default); +qs_val(Name, Req, Default) -> case lists:keyfind(Name, 1, Req#http_req.qs_vals) of {Name, Value} -> {Value, Req}; - false -> {undefined, Req} + false -> {Default, Req} end. --spec qs_val(Name::string(), Default::term(), Req::#http_req{}) - -> {Value::string() | term() | true, Req::#http_req{}}. -qs_val(Name, Default, Req=#http_req{raw_qs=RawQs, qs_vals=undefined}) -> - QsVals = parse_qs(RawQs), - qs_val(Name, Default, Req#http_req{qs_vals=QsVals}); -qs_val(Name, Default, Req) -> - Value = proplists:get_value(Name, Req#http_req.qs_vals, Default), - {Value, Req}. - -spec qs_vals(Req::#http_req{}) -> {list({Name::string(), Value::string() | true}), Req::#http_req{}}. qs_vals(Req=#http_req{raw_qs=RawQs, qs_vals=undefined}) -> @@ -104,18 +103,18 @@ raw_qs(Req) -> -spec binding(Name::atom(), Req::#http_req{}) -> {Value::string() | undefined, Req::#http_req{}}. +%% @equiv binding(Name, Req) -> binding(Name, Req, undefined) binding(Name, Req) -> + binding(Name, Req, undefined). + +-spec binding(Name::atom(), Req::#http_req{}, Default) + -> {Value::string() | Default, Req::#http_req{}} when Default::term(). +binding(Name, Req, Default) -> case lists:keyfind(Name, 1, Req#http_req.bindings) of {Name, Value} -> {Value, Req}; - false -> {undefined, Req} + false -> {Default, Req} end. --spec binding(Name::atom(), Default::term(), Req::#http_req{}) - -> {Value::string() | term(), Req::#http_req{}}. -binding(Name, Default, Req) -> - Value = proplists:get_value(Name, Req#http_req.bindings, Default), - {Value, Req}. - -spec bindings(Req::#http_req{}) -> {list({Name::atom(), Value::string()}), Req::#http_req{}}. bindings(Req) -> @@ -123,18 +122,18 @@ bindings(Req) -> -spec header(Name::atom() | string(), Req::#http_req{}) -> {Value::string() | undefined, Req::#http_req{}}. +%% @equiv header(Name, Req) -> header(Name, Req, undefined) header(Name, Req) -> + header(Name, Req, undefined). + +-spec header(Name::atom() | string(), Req::#http_req{}, Default) + -> {Value::string() | Default, Req::#http_req{}} when Default::term(). +header(Name, Req, Default) -> case lists:keyfind(Name, 1, Req#http_req.headers) of {Name, Value} -> {Value, Req}; - false -> {undefined, Req} + false -> {Default, Req} end. --spec header(Name::atom() | string(), Default::term(), Req::#http_req{}) - -> {Value::string() | term(), Req::#http_req{}}. -header(Name, Default, Req) -> - Value = proplists:get_value(Name, Req#http_req.headers, Default), - {Value, Req}. - -spec headers(Req::#http_req{}) -> {list({Name::atom() | string(), Value::string()}), Req::#http_req{}}. headers(Req) -> @@ -148,7 +147,7 @@ headers(Req) -> body(Req) -> {Length, Req2} = cowboy_http_req:header('Content-Length', Req), case Length of - "" -> {error, badarg}; + undefined -> {error, badarg}; _Any -> Length2 = list_to_integer(Length), body(Length2, Req2) |