aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-10-25 10:29:46 +0200
committerLoïc Hoguin <[email protected]>2011-10-25 10:29:46 +0200
commit1a839954bb9f1e9d246680fe8f90304a563d1a9c (patch)
tree582a5632041ddc53e32c2c5ad85cad1699658e87 /src/cowboy_http.erl
parent62230182127a1482e664681523c8ad2029cfac5f (diff)
downloadcowboy-1a839954bb9f1e9d246680fe8f90304a563d1a9c.tar.gz
cowboy-1a839954bb9f1e9d246680fe8f90304a563d1a9c.tar.bz2
cowboy-1a839954bb9f1e9d246680fe8f90304a563d1a9c.zip
Rewrite list/tokens parsing with an added whitespace function
Diffstat (limited to 'src/cowboy_http.erl')
-rw-r--r--src/cowboy_http.erl45
1 files changed, 18 insertions, 27 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index b05611b..8648b86 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -44,53 +44,44 @@ list(Data, Fun) ->
end.
-spec list(binary(), fun(), [binary()]) -> [any()] | {error, badarg}.
-list(<<>>, _Fun, Acc) ->
- Acc;
%% From the RFC:
%% <blockquote>Wherever this construct is used, null elements are allowed,
%% but do not contribute to the count of elements present.
%% That is, "(element), , (element) " is permitted, but counts
%% as only two elements. Therefore, where at least one element is required,
%% at least one non-null element MUST be present.</blockquote>
-list(<< $,, Rest/bits >>, Fun, Acc) ->
- list(Rest, Fun, Acc);
list(Data, Fun, Acc) ->
- Fun(Data,
- fun (R, <<>>) -> list_separator(R,
- fun (D) -> list(D, Fun, Acc) end);
- (R, I) -> list_separator(R,
- fun (D) -> list(D, Fun, [I|Acc]) end)
+ whitespace(Data,
+ fun (<<>>) -> Acc;
+ (<< $,, Rest/bits >>) -> list(Rest, Fun, Acc);
+ (Rest) -> Fun(Rest,
+ fun (D, I) -> whitespace(D,
+ fun (<<>>) -> [I|Acc];
+ (<< $,, R/bits >>) -> list(R, Fun, [I|Acc]);
+ (_Any) -> {error, badarg}
+ end)
+ end)
end).
--spec list_separator(binary(), fun()) -> any().
-list_separator(<<>>, Fun) ->
- Fun(<<>>);
-list_separator(<< $,, Rest/bits >>, Fun) ->
- Fun(Rest);
-list_separator(<< C, Rest/bits >>, Fun)
+%% @doc Skip whitespace.
+-spec whitespace(binary(), fun()) -> any().
+whitespace(<< C, Rest/bits >>, Fun)
when C =:= $\s; C =:= $\t ->
- list_separator(Rest, Fun);
-list_separator(_Data, _Fun) ->
- {error, badarg}.
+ whitespace(Rest, Fun);
+whitespace(Data, Fun) ->
+ Fun(Data).
%% @doc Parse a case-insensitive token.
%%
%% Changes all characters to lowercase.
-spec token_ci(binary(), fun()) -> any().
token_ci(Data, Fun) ->
- token(Data, Fun, ci).
+ token(Data, Fun, ci, <<>>).
%% @doc Parse a token.
-spec token(binary(), fun()) -> any().
token(Data, Fun) ->
- token(Data, Fun, cs).
-
--spec token(binary(), fun(), ci | cs) -> any().
-token(<< C, Rest/bits >>, Fun, Case)
- when C =:= $\s; C =:= $\t ->
- token(Rest, Fun, Case);
-token(Data, Fun, Case) ->
- token(Data, Fun, Case, <<>>).
+ token(Data, Fun, cs, <<>>).
-spec token(binary(), fun(), ci | cs, binary()) -> any().
token(<<>>, Fun, _Case, Acc) ->