Skip to content

Commit

Permalink
Merge pull request #300 from tencentyun/feature_jojoliang_79461466
Browse files Browse the repository at this point in the history
严格签名修复Content-Length
  • Loading branch information
agin719 authored Feb 20, 2025
2 parents 187b7a4 + 3acf74b commit 2e3ac63
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func TestCVMCredentialTransport(t *testing.T) {
host := strings.TrimLeft(uri, "http://")
req, _ := http.NewRequest("GET", uri, nil)
req.Header.Add("Host", host)
req.Header.Add("x-cos-security-token", token)
expect := newAuthorization(ak, sk, req, authTime, true)
if expect != auth {
t.Errorf("CVMCredentialTransport Authorization error, want:%v, return:%v\n", expect, auth)
Expand Down Expand Up @@ -350,6 +351,7 @@ func TestStsCredentialTransport(t *testing.T) {
host := strings.TrimLeft(uri, "http://")
req, _ := http.NewRequest("GET", uri, nil)
req.Header.Add("Host", host)
req.Header.Add("x-cos-security-token", token)
expect := newAuthorization(ak, sk, req, authTime, true)
if expect != auth {
t.Errorf("StsCredentialTransport Authorization error, want:%v, return:%v\n", expect, auth)
Expand Down
5 changes: 5 additions & 0 deletions cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
var reader io.Reader
contentType := ""
contentMD5 := ""
contentLength := int64(-1)
if body != nil {
// 上传文件
if r, ok := body.(io.Reader); ok {
Expand All @@ -299,6 +300,7 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
contentType = contentTypeXML
reader = bytes.NewReader(b)
contentMD5 = base64.StdEncoding.EncodeToString(calMD5Digest(b))
contentLength = int64(len(b))
}
}

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

0 comments on commit 2e3ac63

Please sign in to comment.