Skip to content

Commit 8d5d630

Browse files
committed
Fix urlEncode for go1.20 or newer
1 parent 1a0ac0c commit 8d5d630

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

aliSimple.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11+
"math"
1112
"net/http"
1213
"net/url"
14+
"runtime"
1315
"sort"
16+
"strconv"
1417
"strings"
1518
"sync"
1619
"time"
@@ -184,14 +187,29 @@ func signStr(ins string, sec string) string {
184187
return base64.StdEncoding.EncodeToString(sum)
185188
}
186189

190+
func goVer() float64 {
191+
verStr := runtime.Version()
192+
verStr, _ = strings.CutPrefix(verStr, "go")
193+
verStrs := strings.Split(verStr, ".")
194+
var result float64
195+
for i, v := range verStrs {
196+
tmp, _ := strconv.ParseFloat(v, 32)
197+
result = tmp * (1 / math.Pow10(i))
198+
}
199+
return result
200+
}
201+
187202
func urlEncode(ins string) string {
188203
str0 := ins
189204
str0 = strings.Replace(str0, "+", "%20", -1)
190205
str0 = strings.Replace(str0, "*", "%2A", -1)
191206
str0 = strings.Replace(str0, "%7E", "~", -1)
192207

193208
str0 = url.QueryEscape(str0)
194-
str0 = strings.Replace(str0, "%26", "&", -1)
209+
if goVer() > 1.20 {
210+
str0 = strings.Replace(str0, "%26", "&", -1)
211+
}
212+
195213
return str0
196214
}
197215

aliSimple_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import (
66
"testing"
77
)
88

9+
const AccessKeyID = "<Input your AccessKeyID here>"
10+
const AccessKeySecret = "<Input your AccessKeySecret here>"
11+
912
func Test_URLEncode(t *testing.T) {
1013
s0 := urlEncode("AccessKeyId=testid&Action=DescribeDomainRecords")
1114
if s0 != "AccessKeyId%3Dtestid%26Action%3DDescribeDomainRecords" {
15+
t.Log(s0)
1216
t.Fail()
1317
}
1418
t.Log(s0)
@@ -37,7 +41,10 @@ func Test_AliClintReq(t *testing.T) {
3741
str = cl0.reqStrToSign(str, "GET")
3842

3943
// validate sign string from doc: https://help.aliyun.com/document_detail/29747.html#:~:text=%E9%82%A3%E4%B9%88-,stringtosign
40-
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" {
44+
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" {
45+
t.Error("sign str error")
46+
}
47+
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" {
4148
t.Error("sign str error")
4249
}
4350
t.Log("sign str:" + str + "\n")
@@ -53,8 +60,8 @@ func Test_AppendDupReq(t *testing.T) {
5360
}
5461

5562
var p0 = Provider{
56-
AccKeyID: "<Input your AccessKeyID here>",
57-
AccKeySecret: "<Input your AccessKeySecret here>",
63+
AccKeyID: AccessKeyID,
64+
AccKeySecret: AccessKeySecret,
5865
}
5966

6067
func Test_RequestUrl(t *testing.T) {

0 commit comments

Comments
 (0)