diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cow_http.erl | 20 |
1 files 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">>), |