Created
December 21, 2015 22:31
-
-
Save vans163/3ad0f67cb9cd04f505a4 to your computer and use it in GitHub Desktop.
Erlang escape uri for POST request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%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