Skip to content

Instantly share code, notes, and snippets.

@bighunter513
Forked from vans163/escape_uri.erl
Created April 19, 2017 02:58
Show Gist options
  • Save bighunter513/9a806139390715f1c8390b29827cec3d to your computer and use it in GitHub Desktop.
Save bighunter513/9a806139390715f1c8390b29827cec3d to your computer and use it in GitHub Desktop.
Erlang escape uri for POST request
%escape uri
escape_uri(S) when is_list(S) ->
escape_uri(unicode:characters_to_binary(S));
escape_uri(<<C:8, Cs/binary>>) when C >= $a, C =< $z ->
[C] ++ escape_uri(Cs);
escape_uri(<<C:8, Cs/binary>>) when C >= $A, C =< $Z ->
[C] ++ escape_uri(Cs);
escape_uri(<<C:8, Cs/binary>>) when C >= $0, C =< $9 ->
[C] ++ escape_uri(Cs);
escape_uri(<<C:8, Cs/binary>>) when C == $. ->
[C] ++ escape_uri(Cs);
escape_uri(<<C:8, Cs/binary>>) when C == $- ->
[C] ++ escape_uri(Cs);
escape_uri(<<C:8, Cs/binary>>) when C == $_ ->
[C] ++ escape_uri(Cs);
escape_uri(<<C:8, Cs/binary>>) ->
escape_byte(C) ++ escape_uri(Cs);
escape_uri(<<>>) ->
"".
escape_byte(C) when C >= 0, C =< 255 ->
[$%, hex_digit(C bsr 4), hex_digit(C band 15)].
hex_digit(N) when N >= 0, N =< 9 ->
N + $0;
hex_digit(N) when N > 9, N =< 15 ->
N + $a - 10.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment