aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-02-20 08:25:05 +0100
committerLoïc Hoguin <[email protected]>2012-02-20 08:25:05 +0100
commitf51493ee376f0ea9eef137f19e6fd0e16e199b8e (patch)
treef5aba326ddb41d128062182fb3c104ca7bc9d680 /src
parente7b6e2a402922724ffd668161ed1b65533b5c034 (diff)
downloadcowboy-f51493ee376f0ea9eef137f19e6fd0e16e199b8e.tar.gz
cowboy-f51493ee376f0ea9eef137f19e6fd0e16e199b8e.tar.bz2
cowboy-f51493ee376f0ea9eef137f19e6fd0e16e199b8e.zip
Add 'Expect' header parsing
At the same time renaming cowboy_http:content_type_params/3 to cowboy_http:params/2 (with a default Acc of []) as this code isn't useful only for content types.
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_http.erl78
-rw-r--r--src/cowboy_http_req.erl5
-rw-r--r--src/cowboy_multipart.erl6
3 files changed, 58 insertions, 31 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index 32b0ca9..6c5a6ea 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -17,8 +17,8 @@
-module(cowboy_http).
%% Parsing.
--export([list/2, nonempty_list/2, content_type/1, content_type_params/3,
- media_range/2, conneg/2, language_range/2, entity_tag_match/1,
+-export([list/2, nonempty_list/2, content_type/1, media_range/2, conneg/2,
+ language_range/2, entity_tag_match/1, expectation/2, params/2,
http_date/1, rfc1123_date/1, rfc850_date/1, asctime_date/1,
whitespace/2, digits/1, token/2, token_ci/2, quoted_string/2]).
@@ -98,33 +98,9 @@ list(Data, Fun, Acc) ->
content_type(Data) ->
media_type(Data,
fun (Rest, Type, SubType) ->
- content_type_params(Rest,
- fun (Params) -> {Type, SubType, Params} end, [])
- end).
-
--spec content_type_params(binary(), fun(), list({binary(), binary()}))
- -> any().
-content_type_params(Data, Fun, Acc) ->
- whitespace(Data,
- fun (<< $;, Rest/binary >>) -> content_type_param(Rest, Fun, Acc);
- (<<>>) -> Fun(lists:reverse(Acc));
- (_Rest) -> {error, badarg}
- end).
-
--spec content_type_param(binary(), fun(), list({binary(), binary()}))
- -> any().
-content_type_param(Data, Fun, Acc) ->
- whitespace(Data,
- fun (Rest) ->
- token_ci(Rest,
- fun (_Rest2, <<>>) -> {error, badarg};
- (<< $=, Rest2/binary >>, Attr) ->
- word(Rest2,
- fun (Rest3, Value) ->
- content_type_params(Rest3, Fun,
- [{Attr, Value}|Acc])
- end);
- (_Rest2, _Attr) -> {error, badarg}
+ params(Rest,
+ fun (<<>>, Params) -> {Type, SubType, Params};
+ (_Rest2, _) -> {error, badarg}
end)
end).
@@ -319,6 +295,50 @@ opaque_tag(Data, Fun, Strength) ->
(Rest, OpaqueTag) -> Fun(Rest, {Strength, OpaqueTag})
end).
+%% @doc Parse an expectation.
+-spec expectation(binary(), fun()) -> any().
+expectation(Data, Fun) ->
+ token_ci(Data,
+ fun (_Rest, <<>>) -> {error, badarg};
+ (<< $=, Rest/binary >>, Expectation) ->
+ word(Rest,
+ fun (Rest2, ExtValue) ->
+ params(Rest2, fun (Rest3, ExtParams) ->
+ Fun(Rest3, {Expectation, ExtValue, ExtParams})
+ end)
+ end);
+ (Rest, Expectation) ->
+ Fun(Rest, Expectation)
+ end).
+
+%% @doc Parse a list of parameters (a=b;c=d).
+-spec params(binary(), fun()) -> any().
+params(Data, Fun) ->
+ params(Data, Fun, []).
+
+-spec params(binary(), fun(), [{binary(), binary()}]) -> any().
+params(Data, Fun, Acc) ->
+ whitespace(Data,
+ fun (<< $;, Rest/binary >>) -> param(Rest, Fun, Acc);
+ (Rest) -> Fun(Rest, lists:reverse(Acc))
+ end).
+
+-spec param(binary(), fun(), [{binary(), binary()}]) -> any().
+param(Data, Fun, Acc) ->
+ whitespace(Data,
+ fun (Rest) ->
+ token_ci(Rest,
+ fun (_Rest2, <<>>) -> {error, badarg};
+ (<< $=, Rest2/binary >>, Attr) ->
+ word(Rest2,
+ fun (Rest3, Value) ->
+ params(Rest3, Fun,
+ [{Attr, Value}|Acc])
+ end);
+ (_Rest2, _Attr) -> {error, badarg}
+ end)
+ end).
+
%% @doc Parse an HTTP date (RFC1123, RFC850 or asctime date).
%% @end
%%
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl
index 92d96ad..e3efd2b 100644
--- a/src/cowboy_http_req.erl
+++ b/src/cowboy_http_req.erl
@@ -271,6 +271,11 @@ parse_header(Name, Req, Default) when Name =:= 'Content-Type' ->
fun (Value) ->
cowboy_http:content_type(Value)
end);
+parse_header(Name, Req, Default) when Name =:= 'Expect' ->
+ parse_header(Name, Req, Default,
+ fun (Value) ->
+ cowboy_http:nonempty_list(Value, fun cowboy_http:expectation/2)
+ end);
parse_header(Name, Req, Default)
when Name =:= 'If-Match'; Name =:= 'If-None-Match' ->
parse_header(Name, Req, Default,
diff --git a/src/cowboy_multipart.erl b/src/cowboy_multipart.erl
index b7aeb54..2428b52 100644
--- a/src/cowboy_multipart.erl
+++ b/src/cowboy_multipart.erl
@@ -45,8 +45,10 @@ content_disposition(Data) ->
cowboy_http:token_ci(Data,
fun (_Rest, <<>>) -> {error, badarg};
(Rest, Disposition) ->
- cowboy_http:content_type_params(Rest,
- fun (Params) -> {Disposition, Params} end, [])
+ cowboy_http:params(Rest,
+ fun (<<>>, Params) -> {Disposition, Params};
+ (_Rest2, _) -> {error, badarg}
+ end)
end).
%% Internal.