Просмотр исходного кода

Make cow_http:parse_fullpath/1 remove any fragment component

Loïc Hoguin 9 лет назад
Родитель
Сommit
92d75b5e90
1 измененных файлов с 13 добавлено и 7 удалено
  1. 13 7
      src/cow_http.erl

+ 13 - 7
src/cow_http.erl

@@ -196,25 +196,31 @@ horse_parse_headers() ->
 	).
 -endif.
 
-%% @doc Extract path and query string from a binary.
+%% @doc Extract path and query string from a binary,
+%% removing any fragment component.
 
 -spec parse_fullpath(binary()) -> {binary(), binary()}.
 parse_fullpath(Fullpath) ->
 	parse_fullpath(Fullpath, <<>>).
 
-parse_fullpath(<<>>, Path) ->
-	{Path, <<>>};
-parse_fullpath(<< $?, Qs/binary >>, Path) ->
-	{Path, Qs};
-parse_fullpath(<< C, Rest/binary >>, SoFar) ->
-	parse_fullpath(Rest, << SoFar/binary, C >>).
+parse_fullpath(<<>>, Path) -> {Path, <<>>};
+parse_fullpath(<< $#, _/bits >>, Path) -> {Path, <<>>};
+parse_fullpath(<< $?, Qs/bits >>, Path) -> parse_fullpath_query(Qs, Path, <<>>);
+parse_fullpath(<< C, Rest/bits >>, SoFar) -> parse_fullpath(Rest, << SoFar/binary, C >>).
+
+parse_fullpath_query(<<>>, Path, Query) -> {Path, Query};
+parse_fullpath_query(<< $#, _/bits >>, Path, Query) -> {Path, Query};
+parse_fullpath_query(<< C, Rest/bits >>, Path, SoFar) ->
+	parse_fullpath_query(Rest, Path, << SoFar/binary, C >>).
 
 -ifdef(TEST).
 parse_fullpath_test() ->
 	{<<"*">>, <<>>} = parse_fullpath(<<"*">>),
 	{<<"/">>, <<>>} = parse_fullpath(<<"/">>),
+	{<<"/path/to/resource">>, <<>>} = parse_fullpath(<<"/path/to/resource#fragment">>),
 	{<<"/path/to/resource">>, <<>>} = parse_fullpath(<<"/path/to/resource">>),
 	{<<"/">>, <<>>} = parse_fullpath(<<"/?">>),
+	{<<"/">>, <<"q=cowboy">>} = parse_fullpath(<<"/?q=cowboy#fragment">>),
 	{<<"/">>, <<"q=cowboy">>} = parse_fullpath(<<"/?q=cowboy">>),
 	{<<"/path/to/resource">>, <<"q=cowboy">>}
 		= parse_fullpath(<<"/path/to/resource?q=cowboy">>),