-
-
Save hnaohiro/4627658 to your computer and use it in GitHub Desktop.
func urlencode(s string) (result string){ | |
for _, c := range(s) { | |
if c <= 0x7f { // single byte | |
result += fmt.Sprintf("%%%X", c) | |
} else if c > 0x1fffff {// quaternary byte | |
result += fmt.Sprintf("%%%X%%%X%%%X%%%X", | |
0xf0 + ((c & 0x1c0000) >> 18), | |
0x80 + ((c & 0x3f000) >> 12), | |
0x80 + ((c & 0xfc0) >> 6), | |
0x80 + (c & 0x3f), | |
) | |
} else if c > 0x7ff { // triple byte | |
result += fmt.Sprintf("%%%X%%%X%%%X", | |
0xe0 + ((c & 0xf000) >> 12), | |
0x80 + ((c & 0xfc0) >> 6), | |
0x80 + (c & 0x3f), | |
) | |
} else { // double byte | |
result += fmt.Sprintf("%%%X%%%X", | |
0xc0 + ((c & 0x7c0) >> 6), | |
0x80 + (c & 0x3f), | |
) | |
} | |
} | |
return result | |
} |
What if you need a path component with a slash? In Perl:
> perl -MURI -E 'use utf8; my $uri = URI->new("http://example.com"); $uri->path_segments(qw(tart user foo/bâr profile)); say $uri->canonical;'
http://example.com/tart/user/foo%2Fb%C3%A2r/profile
This lets me specify each path part separately.
Why is this required? Golang already has QueryEscape()
as well as PathEscape()
functions. I've written about URL Encoding in Golang and URL Decoding in Golang. Also, there are tools to URL Encode online and URL Decode online.
It's needed because Golang does this:
- use
url.PathEscape
and get space as%20
, but leave:
unencoded (should be%3A
), or - use
url.QueryEscape
and get space as+
, but encode:
as%3A
So, if you integrate with something that goes by RFC in a certain manner and encodes %20
and %3A
, you are out of luck.
P.S. Of course, :
is semi-legal and perhaps shouldn't be encoded, but tell that to all kinds of Java/Python libs...
To add a cherry on top: QueryEscape
incorrectly escapes already escaped strings over and over again, which is straight up ridiculous.
It's 2022 and it still does this.
it escapes everything?