@@ -15,6 +15,16 @@ import (
1515 "golang.org/x/net/context"
1616)
1717
18+ type requestOption struct {
19+ computeContentHash bool
20+ }
21+
22+ func defaultRequestOption () * requestOption {
23+ return & requestOption {
24+ computeContentHash : true ,
25+ }
26+ }
27+
1828// timeout configs
1929var (
2030 defaultRequestTimeout = 60 * time .Second
@@ -92,12 +102,21 @@ func retryWriteErrorCheck(ctx context.Context, err error) (bool, error) {
92102// mock param only for test, default is []
93103func request (project * LogProject , method , uri string , headers map [string ]string ,
94104 body []byte , mock ... interface {}) (* http.Response , error ) {
105+ return requestWithOption (project , method , uri , headers , body , nil , mock ... )
106+ }
107+
108+ func requestWithOption (project * LogProject , method , uri string , headers map [string ]string ,
109+ body []byte , option * requestOption , mock ... interface {}) (* http.Response , error ) {
95110
96111 var r * http.Response
97112 var slsErr error
98113 var err error
99114 var mockErr * mockErrorRetry
100115
116+ if option == nil {
117+ option = defaultRequestOption ()
118+ }
119+
101120 project .init ()
102121 ctx , cancel := context .WithTimeout (context .Background (), project .retryTimeout )
103122 defer cancel ()
@@ -108,7 +127,7 @@ func request(project *LogProject, method, uri string, headers map[string]string,
108127 err = RetryWithCondition (ctx , backoff .NewExponentialBackOff (), func () (bool , error ) {
109128 if len (mock ) == 0 {
110129 //fmt.Println("real request", project, method, uri, headers, body)
111- r , slsErr = realRequest (ctx , project , method , uri , headers , body )
130+ r , slsErr = realRequest (ctx , project , method , uri , headers , body , option )
112131 //fmt.Println("real request done")
113132 } else {
114133 r , mockErr = nil , mock [0 ].(* mockErrorRetry )
@@ -125,7 +144,7 @@ func request(project *LogProject, method, uri string, headers map[string]string,
125144 } else {
126145 err = RetryWithCondition (ctx , backoff .NewExponentialBackOff (), func () (bool , error ) {
127146 if len (mock ) == 0 {
128- r , slsErr = realRequest (ctx , project , method , uri , headers , body )
147+ r , slsErr = realRequest (ctx , project , method , uri , headers , body , option )
129148 } else {
130149 r , mockErr = nil , mock [0 ].(* mockErrorRetry )
131150 mockErr .RetryCnt --
@@ -149,7 +168,7 @@ func request(project *LogProject, method, uri string, headers map[string]string,
149168// request sends a request to alibaba cloud Log Service.
150169// @note if error is nil, you must call http.Response.Body.Close() to finalize reader
151170func realRequest (ctx context.Context , project * LogProject , method , uri string , headers map [string ]string ,
152- body []byte ) (* http.Response , error ) {
171+ body []byte , option * requestOption ) (* http.Response , error ) {
153172
154173 // The caller should provide 'x-log-bodyrawsize' header
155174 if _ , ok := headers [HTTPHeaderBodyRawSize ]; ! ok {
@@ -194,17 +213,21 @@ func realRequest(ctx context.Context, project *LogProject, method, uri string, h
194213 for k , v := range project .innerHeaders {
195214 headers [k ] = v
196215 }
197- var signer Signer
198- if project .AuthVersion == AuthV4 {
216+ var err error
217+ switch project .AuthVersion {
218+ case AuthV4 :
199219 headers [HTTPHeaderLogDate ] = dateTimeISO8601 ()
200- signer = NewSignerV4 (accessKeyID , accessKeySecret , project .Region )
201- } else if project .AuthVersion == AuthV0 {
202- signer = NewSignerV0 ()
203- } else {
220+ signer := NewSignerV4 (accessKeyID , accessKeySecret , project .Region )
221+ err = signer .SignWithOption (method , uri , headers , body , option .computeContentHash )
222+ case AuthV0 :
223+ signer := NewSignerV0 ()
224+ err = signer .Sign (method , uri , headers , body )
225+ default :
204226 headers [HTTPHeaderDate ] = nowRFC1123 ()
205- signer = NewSignerV1 (accessKeyID , accessKeySecret )
227+ signer := NewSignerV1 (accessKeyID , accessKeySecret )
228+ err = signer .Sign (method , uri , headers , body )
206229 }
207- if err := signer . Sign ( method , uri , headers , body ); err != nil {
230+ if err != nil {
208231 return nil , err
209232 }
210233
0 commit comments