|
|
@@ -90,24 +90,30 @@ func (u *URI) Get(key string) string {
|
|
|
if v == "" {
|
|
|
v = u.URL.Query().Get(key)
|
|
|
}
|
|
|
- return StringDecode(v)
|
|
|
+ return v
|
|
|
}
|
|
|
|
|
|
-// GetHex returns the first value in the uri with the give n key, it will return
|
|
|
-// empty nil if that field is not present.
|
|
|
-func (u *URI) GetHex(key string) ([]byte, error) {
|
|
|
- v := u.Values.Get(key)
|
|
|
+// GetEncoded returns the first value in the uri with the give n key, it will
|
|
|
+// return empty nil if that field is not present or is empty. If the return
|
|
|
+// value is hex encoded it will decode it and return it.
|
|
|
+func (u *URI) GetEncoded(key string) []byte {
|
|
|
+ v := u.Get(key)
|
|
|
if v == "" {
|
|
|
- v = u.URL.Query().Get(key)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ if len(v)%2 == 0 {
|
|
|
+ if b, err := hex.DecodeString(v); err == nil {
|
|
|
+ return b
|
|
|
+ }
|
|
|
}
|
|
|
- return HexDecode(v)
|
|
|
+ return []byte(v)
|
|
|
}
|
|
|
|
|
|
// Pin returns the pin encoded in the url. It will read the pin from the
|
|
|
// pin-value or the pin-source attributes.
|
|
|
func (u *URI) Pin() string {
|
|
|
if value := u.Get("pin-value"); value != "" {
|
|
|
- return StringDecode(value)
|
|
|
+ return value
|
|
|
}
|
|
|
if path := u.Get("pin-source"); path != "" {
|
|
|
if b, err := readFile(path); err == nil {
|
|
|
@@ -128,89 +134,3 @@ func readFile(path string) ([]byte, error) {
|
|
|
}
|
|
|
return b, nil
|
|
|
}
|
|
|
-
|
|
|
-// PercentEncode encodes the given bytes using the percent encoding described in
|
|
|
-// RFC3986 (https://tools.ietf.org/html/rfc3986).
|
|
|
-func PercentEncode(b []byte) string {
|
|
|
- buf := new(strings.Builder)
|
|
|
- for _, v := range b {
|
|
|
- buf.WriteString("%" + hex.EncodeToString([]byte{v}))
|
|
|
- }
|
|
|
- return buf.String()
|
|
|
-}
|
|
|
-
|
|
|
-// PercentDecode decodes the given string using the percent encoding described
|
|
|
-// in RFC3986 (https://tools.ietf.org/html/rfc3986).
|
|
|
-func PercentDecode(s string) ([]byte, error) {
|
|
|
- if len(s)%3 != 0 {
|
|
|
- return nil, errors.Errorf("error parsing %s: wrong length", s)
|
|
|
- }
|
|
|
-
|
|
|
- var first string
|
|
|
- buf := new(bytes.Buffer)
|
|
|
- for i, r := range s {
|
|
|
- mod := i % 3
|
|
|
- rr := string(r)
|
|
|
- switch mod {
|
|
|
- case 0:
|
|
|
- if r != '%' {
|
|
|
- return nil, errors.Errorf("error parsing %s: expected %% and found %s in position %d", s, rr, i)
|
|
|
- }
|
|
|
- case 1:
|
|
|
- if !isHex(r) {
|
|
|
- return nil, errors.Errorf("error parsing %s: %s in position %d is not an hexadecimal number", s, rr, i)
|
|
|
- }
|
|
|
- first = string(r)
|
|
|
- case 2:
|
|
|
- if !isHex(r) {
|
|
|
- return nil, errors.Errorf("error parsing %s: %s in position %d is not an hexadecimal number", s, rr, i)
|
|
|
- }
|
|
|
- b, err := hex.DecodeString(first + rr)
|
|
|
- if err != nil {
|
|
|
- return nil, errors.Wrapf(err, "error parsing %s", s)
|
|
|
- }
|
|
|
- buf.Write(b)
|
|
|
- }
|
|
|
- }
|
|
|
- return buf.Bytes(), nil
|
|
|
-}
|
|
|
-
|
|
|
-// StringDecode returns the string given, but it will use Percent-Encoding if
|
|
|
-// the string is percent encoded.
|
|
|
-func StringDecode(s string) string {
|
|
|
- if strings.HasPrefix(s, "%") {
|
|
|
- if b, err := PercentDecode(s); err == nil {
|
|
|
- return string(b)
|
|
|
- }
|
|
|
- }
|
|
|
- return s
|
|
|
-}
|
|
|
-
|
|
|
-// HexDecode deocdes the string s using Percent-Encoding or regular hex
|
|
|
-// encoding.
|
|
|
-func HexDecode(s string) ([]byte, error) {
|
|
|
- if s == "" {
|
|
|
- return nil, nil
|
|
|
- } else if strings.HasPrefix(s, "%") {
|
|
|
- return PercentDecode(s)
|
|
|
- }
|
|
|
-
|
|
|
- b, err := hex.DecodeString(s)
|
|
|
- if err != nil {
|
|
|
- return nil, errors.Wrapf(err, "error parsing %s", s)
|
|
|
- }
|
|
|
- return b, nil
|
|
|
-}
|
|
|
-
|
|
|
-func isHex(r rune) bool {
|
|
|
- switch {
|
|
|
- case r >= '0' && r <= '9':
|
|
|
- return true
|
|
|
- case r >= 'a' && r <= 'f':
|
|
|
- return true
|
|
|
- case r >= 'A' && r <= 'F':
|
|
|
- return true
|
|
|
- default:
|
|
|
- return false
|
|
|
- }
|
|
|
-}
|