From 92d75b5e90d837a263f03986ba2657b7fa0a6954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 6 Jun 2016 15:26:58 +0200 Subject: Make cow_http:parse_fullpath/1 remove any fragment component --- src/cow_http.erl | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/cow_http.erl b/src/cow_http.erl index 4b98d81..c76675c 100644 --- a/src/cow_http.erl +++ b/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">>), -- cgit v1.2.3