forked from johannesboyne/gofakes3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c68b81a
commit 968673d
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package gofakes3 | ||
|
||
// From: minio/cmd/api-utils.go | ||
// License: AGPL-3.0 | ||
|
||
func shouldEscape(c byte) bool { | ||
if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' { | ||
return false | ||
} | ||
|
||
switch c { | ||
case '-', '_', '.', '/', '*': | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// s3URLEncode is based on Golang's url.QueryEscape() code, | ||
// while considering some S3 exceptions: | ||
// - Avoid encoding '/' and '*' | ||
// - Force encoding of '~' | ||
func URLEncode(s string) string { | ||
spaceCount, hexCount := 0, 0 | ||
for i := 0; i < len(s); i++ { | ||
c := s[i] | ||
if shouldEscape(c) { | ||
if c == ' ' { | ||
spaceCount++ | ||
} else { | ||
hexCount++ | ||
} | ||
} | ||
} | ||
|
||
if spaceCount == 0 && hexCount == 0 { | ||
return s | ||
} | ||
|
||
var buf [64]byte | ||
var t []byte | ||
|
||
required := len(s) + 2*hexCount | ||
if required <= len(buf) { | ||
t = buf[:required] | ||
} else { | ||
t = make([]byte, required) | ||
} | ||
|
||
if hexCount == 0 { | ||
copy(t, s) | ||
for i := 0; i < len(s); i++ { | ||
if s[i] == ' ' { | ||
t[i] = '+' | ||
} | ||
} | ||
return string(t) | ||
} | ||
|
||
j := 0 | ||
for i := 0; i < len(s); i++ { | ||
switch c := s[i]; { | ||
case c == ' ': | ||
t[j] = '+' | ||
j++ | ||
case shouldEscape(c): | ||
t[j] = '%' | ||
t[j+1] = "0123456789ABCDEF"[c>>4] | ||
t[j+2] = "0123456789ABCDEF"[c&15] | ||
j += 3 | ||
default: | ||
t[j] = s[i] | ||
j++ | ||
} | ||
} | ||
return string(t) | ||
} |