aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAli Sabil <[email protected]>2012-03-09 12:13:30 +0100
committerAli Sabil <[email protected]>2012-03-09 12:13:30 +0100
commit32a46f898ad999c0a62707807fb02b6805f42ebd (patch)
treeb5d06f5ef46de4e9e428fd6b202674479df88c32
parentc6c2b31695b6ee5a5d8b10e8b4c34baf510f8f25 (diff)
downloadcowboy-32a46f898ad999c0a62707807fb02b6805f42ebd.tar.gz
cowboy-32a46f898ad999c0a62707807fb02b6805f42ebd.tar.bz2
cowboy-32a46f898ad999c0a62707807fb02b6805f42ebd.zip
Make media type parsing more relaxed
Certain user agents send slightly invalid media types, like the following: "text/html, image/gif, image/jpeg, ; q=.2, */; q=.2" The user agent with which this behavior was observed presented itself with the User-Agent string: "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (FlipboardProxy/0.0.5; +http://flipboard.com/browserproxy)"
-rw-r--r--src/cowboy_http.erl17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index 7a3bc7a..9d727f3 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -156,6 +156,13 @@ media_type(Data, Fun) ->
fun (_Rest2, <<>>) -> {error, badarg};
(Rest2, SubType) -> Fun(Rest2, Type, SubType)
end);
+ %% This is a non-strict parsing clause required by some user agents
+ %% that use * instead of */* in the list of media types.
+ (Rest, <<"*">> = Type) ->
+ token_ci(<<"*", Rest/binary>>,
+ fun (_Rest2, <<>>) -> {error, badarg};
+ (Rest2, SubType) -> Fun(Rest2, Type, SubType)
+ end);
(_Rest, _Type) -> {error, badarg}
end).
@@ -676,6 +683,9 @@ quoted_string(<< C, Rest/binary >>, Fun, Acc) ->
-spec qvalue(binary(), fun()) -> any().
qvalue(<< $0, $., Rest/binary >>, Fun) ->
qvalue(Rest, Fun, 0, 100);
+%% Some user agents use q=.x instead of q=0.x
+qvalue(<< $., Rest/binary >>, Fun) ->
+ qvalue(Rest, Fun, 0, 100);
qvalue(<< $0, Rest/binary >>, Fun) ->
Fun(Rest, 0);
qvalue(<< $1, $., $0, $0, $0, Rest/binary >>, Fun) ->
@@ -894,6 +904,13 @@ media_range_list_test_() ->
[{<<"level">>, <<"1">>}, {<<"quoted">>, <<"hi hi hi">>}]}, 123,
[<<"standalone">>, {<<"complex">>, <<"gits">>}]},
{{<<"text">>, <<"plain">>, []}, 1000, []}
+ ]},
+ {<<"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2">>, [
+ {{<<"text">>, <<"html">>, []}, 1000, []},
+ {{<<"image">>, <<"gif">>, []}, 1000, []},
+ {{<<"image">>, <<"jpeg">>, []}, 1000, []},
+ {{<<"*">>, <<"*">>, []}, 200, []},
+ {{<<"*">>, <<"*">>, []}, 200, []}
]}
],
[{V, fun() -> R = list(V, fun media_range/2) end} || {V, R} <- Tests].