Skip to content

Commit

Permalink
Fix urlEncode for go1.20 or newer
Browse files Browse the repository at this point in the history
  • Loading branch information
viscropst committed Jun 28, 2023
1 parent 1a0ac0c commit 8d5d630
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
20 changes: 19 additions & 1 deletion aliSimple.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -184,14 +187,29 @@ func signStr(ins string, sec string) string {
return base64.StdEncoding.EncodeToString(sum)
}

func goVer() float64 {
verStr := runtime.Version()
verStr, _ = strings.CutPrefix(verStr, "go")
verStrs := strings.Split(verStr, ".")
var result float64
for i, v := range verStrs {
tmp, _ := strconv.ParseFloat(v, 32)
result = tmp * (1 / math.Pow10(i))
}
return result
}

func urlEncode(ins string) string {
str0 := ins
str0 = strings.Replace(str0, "+", "%20", -1)
str0 = strings.Replace(str0, "*", "%2A", -1)
str0 = strings.Replace(str0, "%7E", "~", -1)

str0 = url.QueryEscape(str0)
str0 = strings.Replace(str0, "%26", "&", -1)
if goVer() > 1.20 {
str0 = strings.Replace(str0, "%26", "&", -1)
}

return str0
}

Expand Down
13 changes: 10 additions & 3 deletions aliSimple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
"testing"
)

const AccessKeyID = "<Input your AccessKeyID here>"
const AccessKeySecret = "<Input your AccessKeySecret here>"

func Test_URLEncode(t *testing.T) {
s0 := urlEncode("AccessKeyId=testid&Action=DescribeDomainRecords")
if s0 != "AccessKeyId%3Dtestid%26Action%3DDescribeDomainRecords" {
t.Log(s0)
t.Fail()
}
t.Log(s0)
Expand Down Expand Up @@ -37,7 +41,10 @@ func Test_AliClintReq(t *testing.T) {
str = cl0.reqStrToSign(str, "GET")

// validate sign string from doc: https://help.aliyun.com/document_detail/29747.html#:~:text=%E9%82%A3%E4%B9%88-,stringtosign
if str != "GET&%2F&AccessKeyId%3Dtestid&Action%3DDescribeDomainRecords&DomainName%3Dexample.com&Format%3DXML&SignatureMethod%3DHMAC-SHA1&SignatureNonce%3Df59ed6a9-83fc-473b-9cc6-99c95df3856e&SignatureVersion%3D1.0&Timestamp%3D2016-03-24T16%253A41%253A54Z&Version%3D2015-01-09" {
if goVer() > 1.20 && str != "GET&%2F&AccessKeyId%3Dtestid&Action%3DDescribeDomainRecords&DomainName%3Dexample.com&Format%3DXML&SignatureMethod%3DHMAC-SHA1&SignatureNonce%3Df59ed6a9-83fc-473b-9cc6-99c95df3856e&SignatureVersion%3D1.0&Timestamp%3D2016-03-24T16%253A41%253A54Z&Version%3D2015-01-09" {
t.Error("sign str error")
}
if goVer() < 1.20 && str != "GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeDomainRecords%26DomainName%3Dexample.com%26Format%3DXML%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3Df59ed6a9-83fc-473b-9cc6-99c95df3856e%26SignatureVersion%3D1.0%26Timestamp%3D2016-03-24T16%253A41%253A54Z%26Version%3D2015-01-09" {
t.Error("sign str error")
}
t.Log("sign str:" + str + "\n")
Expand All @@ -53,8 +60,8 @@ func Test_AppendDupReq(t *testing.T) {
}

var p0 = Provider{
AccKeyID: "<Input your AccessKeyID here>",
AccKeySecret: "<Input your AccessKeySecret here>",
AccKeyID: AccessKeyID,
AccKeySecret: AccessKeySecret,
}

func Test_RequestUrl(t *testing.T) {
Expand Down

0 comments on commit 8d5d630

Please sign in to comment.