Skip to content

Commit

Permalink
Merge pull request #67 from pichi/improve_uri_decode
Browse files Browse the repository at this point in the history
Export uri_decode and improve performance
  • Loading branch information
tsloughter committed Feb 20, 2019
2 parents b352766 + 0b3ce9d commit cf7b224
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/elli_request.erl
Expand Up @@ -34,6 +34,7 @@
, get_range/1
, to_proplist/1
, is_request/1
, uri_decode/1
]).

-export_type([http_range/0]).
Expand Down Expand Up @@ -275,27 +276,24 @@ is_ref_alive(Ref) ->
is_request(#req{}) -> true;
is_request(_) -> false.

uri_decode(Bin) ->
case binary:match(Bin, [<<"+">>, <<"%">>]) of
nomatch -> Bin;
{Pos, _} ->
<<Prefix:Pos/binary, Rest/binary>> = Bin,
uri_decode(Rest, Prefix)
end.

uri_decode(<<>>, Acc) -> Acc;
uri_decode(<<$+, Rest/binary>>, Acc) ->
uri_decode(Rest, <<Acc/binary, $\s>>);
uri_decode(<<$%, H, L, Rest/binary>>, Acc) ->
uri_decode(Rest, <<Acc/binary, (hex_to_int(H)):4, (hex_to_int(L)):4>>);
uri_decode(<<C, Rest/binary>>, Acc) ->
uri_decode(Rest, <<Acc/binary, C>>).

-compile({inline, [hex_to_int/1]}).

-ifdef(binary_http_uri).
uri_decode(<<$+, Rest/binary>>) ->
<<$ , (uri_decode(Rest))/binary>>;
uri_decode(<<$%, Hex:2/bytes, Rest/binary>>) ->
<<(binary_to_integer(Hex, 16)), (uri_decode(Rest))/binary>>;
uri_decode(<<C, Rest/binary>>) ->
<<C, (uri_decode(Rest))/binary>>;
uri_decode(<<>>) ->
<<>>.
-else.
uri_decode(<<$+, Rest/binary>>) ->
<<$ , (uri_decode(Rest))/binary>>;
uri_decode(<<$%, HexA, HexB, Rest/binary>>) ->
<<(hex_to_int(HexA)*16+hex_to_int(HexB)), (uri_decode(Rest))/binary>>;
uri_decode(<<C, Rest/binary>>) ->
<<C, (uri_decode(Rest))/binary>>;
uri_decode(<<>>) ->
<<>>.

hex_to_int(X) when X >= $0, X =< $9 -> X-$0;
hex_to_int(X) when X >= $a, X =< $f -> X-$a+10;
hex_to_int(X) when X >= $A, X =< $F -> X-$A+10.
-endif.
hex_to_int(X) when X >= $0, X =< $9 -> X - $0;
hex_to_int(X) when X >= $a, X =< $f -> X - ($a-10);
hex_to_int(X) when X >= $A, X =< $F -> X - ($A-10).

0 comments on commit cf7b224

Please sign in to comment.