Skip to content

Commit 3acf74b

Browse files
author
jojoliang
committed
严格签名修复Content-Length
1 parent a05da06 commit 3acf74b

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

auth_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func TestCVMCredentialTransport(t *testing.T) {
126126
host := strings.TrimLeft(uri, "http://")
127127
req, _ := http.NewRequest("GET", uri, nil)
128128
req.Header.Add("Host", host)
129+
req.Header.Add("x-cos-security-token", token)
129130
expect := newAuthorization(ak, sk, req, authTime, true)
130131
if expect != auth {
131132
t.Errorf("CVMCredentialTransport Authorization error, want:%v, return:%v\n", expect, auth)
@@ -350,6 +351,7 @@ func TestStsCredentialTransport(t *testing.T) {
350351
host := strings.TrimLeft(uri, "http://")
351352
req, _ := http.NewRequest("GET", uri, nil)
352353
req.Header.Add("Host", host)
354+
req.Header.Add("x-cos-security-token", token)
353355
expect := newAuthorization(ak, sk, req, authTime, true)
354356
if expect != auth {
355357
t.Errorf("StsCredentialTransport Authorization error, want:%v, return:%v\n", expect, auth)

cos.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
287287
var reader io.Reader
288288
contentType := ""
289289
contentMD5 := ""
290+
contentLength := int64(-1)
290291
if body != nil {
291292
// 上传文件
292293
if r, ok := body.(io.Reader); ok {
@@ -299,6 +300,7 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
299300
contentType = contentTypeXML
300301
reader = bytes.NewReader(b)
301302
contentMD5 = base64.StdEncoding.EncodeToString(calMD5Digest(b))
303+
contentLength = int64(len(b))
302304
}
303305
}
304306

@@ -311,6 +313,9 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
311313
if err != nil {
312314
return
313315
}
316+
if v := req.Header.Get("Content-Length"); v == "" && contentLength >= 0 {
317+
req.Header.Set("Content-Length", strconv.FormatInt(contentLength, 10))
318+
}
314319
if v := req.Header.Get("Content-Length"); req.ContentLength == 0 && v != "" && v != "0" {
315320
req.ContentLength, _ = strconv.ParseInt(v, 10, 64)
316321
}

0 commit comments

Comments
 (0)