aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDrew <[email protected]>2014-01-24 02:44:19 -0500
committerDrew <[email protected]>2014-01-24 02:44:19 -0500
commit190938a75eb31981f37b4ef0ac4644df11f78419 (patch)
treef0264138d4dcd93e8e0fd9e5db68f0e1e437be6e
parent0ec713fc4b185c3cd0f6b2e7ec2c5f198361bddd (diff)
downloadcowboy-190938a75eb31981f37b4ef0ac4644df11f78419.tar.gz
cowboy-190938a75eb31981f37b4ef0ac4644df11f78419.tar.bz2
cowboy-190938a75eb31981f37b4ef0ac4644df11f78419.zip
Allows language_range/2 to parse RFC5646 language codes.
-rw-r--r--src/cowboy_http.erl23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index 499173c..ea454d6 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -258,7 +258,7 @@ language_tag(Data, Fun) ->
-spec language_subtag(binary(), fun(), binary(), [binary()]) -> any().
language_subtag(Data, Fun, Tag, Acc) ->
- alpha(Data,
+ alphanumeric(Data,
fun (_Rest, SubTag) when byte_size(SubTag) =:= 0;
byte_size(SubTag) > 8 -> {error, badarg};
(<< $-, Rest/binary >>, SubTag) ->
@@ -647,6 +647,22 @@ alpha(<< C, Rest/binary >>, Fun, Acc)
alpha(Data, Fun, Acc) ->
Fun(Data, Acc).
+-spec alphanumeric(binary(), fun()) -> any().
+alphanumeric(Data, Fun) ->
+ alphanumeric(Data, Fun, <<>>).
+
+-spec alphanumeric(binary(), fun(), binary()) -> any().
+alphanumeric(<<>>, Fun, Acc) ->
+ Fun(<<>>, Acc);
+alphanumeric(<< C, Rest/binary >>, Fun, Acc)
+ when C >= $a andalso C =< $z;
+ C >= $A andalso C =< $Z;
+ C >= $0 andalso C =< $9 ->
+ C2 = cowboy_bstr:char_to_lower(C),
+ alphanumeric(Rest, Fun, << Acc/binary, C2 >>);
+alphanumeric(Data, Fun, Acc) ->
+ Fun(Data, Acc).
+
%% @doc Parse either a token or a quoted string.
-spec word(binary(), fun()) -> any().
word(Data = << $", _/binary >>, Fun) ->
@@ -1065,12 +1081,13 @@ nonempty_language_range_list_test_() ->
{<<"en-gb">>, 800},
{<<"en">>, 700}
]},
- {<<"en, en-US, en-cockney, i-cherokee, x-pig-latin">>, [
+ {<<"en, en-US, en-cockney, i-cherokee, x-pig-latin, es-419">>, [
{<<"en">>, 1000},
{<<"en-us">>, 1000},
{<<"en-cockney">>, 1000},
{<<"i-cherokee">>, 1000},
- {<<"x-pig-latin">>, 1000}
+ {<<"x-pig-latin">>, 1000},
+ {<<"es-419">>, 1000}
]}
],
[{V, fun() -> R = nonempty_list(V, fun language_range/2) end}