Skip to content

Commit ed8aa5d

Browse files
runzexiaXuanwo
authored andcommitted
Add requset post support (#70)
* add requset post support * fix travis * fix go lint
1 parent 5476913 commit ed8aa5d

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

request/builder.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
// Builder is the request builder for QingCloud service.
3535
type Builder struct {
3636
parsedURL string
37+
parsedForm url.Values
3738
parsedProperties *map[string]string
3839
parsedParams *map[string]string
3940

@@ -56,14 +57,15 @@ func (b *Builder) BuildHTTPRequest(o *data.Operation, i *reflect.Value) (*http.R
5657

5758
func (b *Builder) build() (*http.Request, error) {
5859
httpRequest, err := http.NewRequest(b.operation.RequestMethod, b.parsedURL, nil)
60+
httpRequest.Form = b.parsedForm
5961
if err != nil {
6062
return nil, err
6163
}
6264

6365
logger.Info(fmt.Sprintf(
64-
"Built QingCloud request: [%d] %s",
66+
"Built QingCloud request: [%d] %s \n %s ",
6567
utils.StringToUnixInt(httpRequest.Header.Get("Date"), "RFC 822"),
66-
httpRequest.URL.String()))
68+
httpRequest.URL.String(), b.parsedForm))
6769

6870
return httpRequest, nil
6971
}
@@ -82,6 +84,10 @@ func (b *Builder) parse() error {
8284
if err != nil {
8385
return err
8486
}
87+
err = b.parseRequestForm()
88+
if err != nil {
89+
return err
90+
}
8591

8692
return nil
8793
}
@@ -218,7 +224,7 @@ func (b *Builder) parseRequestURL() error {
218224

219225
b.parsedURL = endpoint + requestURI
220226

221-
if b.parsedParams != nil {
227+
if b.parsedParams != nil && b.operation.RequestMethod == "GET" {
222228
zone := (*b.parsedProperties)["zone"]
223229
if zone != "" {
224230
(*b.parsedParams)["zone"] = zone
@@ -236,3 +242,18 @@ func (b *Builder) parseRequestURL() error {
236242

237243
return nil
238244
}
245+
246+
func (b *Builder) parseRequestForm() error {
247+
if b.parsedParams != nil && b.operation.RequestMethod == "POST" {
248+
var values = make(url.Values)
249+
zone := (*b.parsedProperties)["zone"]
250+
if zone != "" {
251+
(*b.parsedParams)["zone"] = zone
252+
}
253+
for key, value := range *b.parsedParams {
254+
values.Set(key, url.QueryEscape(value))
255+
}
256+
b.parsedForm = values
257+
}
258+
return nil
259+
}

request/signer.go

+35-18
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ type Signer struct {
3636
AccessKeyID string
3737
SecretAccessKey string
3838

39-
BuiltURL string
39+
BuiltURL string
40+
BuiltForm string
4041
}
4142

4243
// WriteSignature calculates signature and write it to http request.
@@ -47,11 +48,12 @@ func (is *Signer) WriteSignature(request *http.Request) error {
4748
}
4849

4950
newRequest, err := http.NewRequest(request.Method,
50-
request.URL.Scheme+"://"+request.URL.Host+is.BuiltURL, nil)
51+
request.URL.Scheme+"://"+request.URL.Host+is.BuiltURL, strings.NewReader(is.BuiltForm))
5152
if err != nil {
5253
return err
5354
}
5455
request.URL = newRequest.URL
56+
request.Body = newRequest.Body
5557

5658
logger.Info(fmt.Sprintf(
5759
"Signed QingCloud request: [%d] %s",
@@ -79,42 +81,52 @@ func (is *Signer) BuildSignature(request *http.Request) (string, error) {
7981
"QingCloud signature: [%d] %s",
8082
utils.StringToUnixInt(request.Header.Get("Date"), "RFC 822"),
8183
signature))
82-
83-
is.BuiltURL += "&signature=" + signature
84+
if request.Method == "GET" {
85+
is.BuiltURL += "&signature=" + signature
86+
} else if request.Method == "POST" {
87+
is.BuiltForm += "&signature=" + signature
88+
}
8489

8590
return signature, nil
8691
}
8792

8893
// BuildStringToSign build the string to sign.
8994
func (is *Signer) BuildStringToSign(request *http.Request) (string, error) {
90-
query := request.URL.Query()
91-
92-
query.Set("access_key_id", is.AccessKeyID)
93-
query.Set("signature_method", "HmacSHA256")
94-
query.Set("signature_version", "1")
95+
if request.Method == "GET" {
96+
return is.BuildStringToSignByValues(request.Header.Get("Date"), request.Method, request.URL.Path, request.URL.Query())
97+
} else if request.Method == "POST" {
98+
return is.BuildStringToSignByValues(request.Header.Get("Date"), request.Method, request.URL.Path, request.Form)
99+
}
100+
return "", fmt.Errorf("Requset Type Not Support For Sign ")
101+
}
102+
// BuildStringToSignByValues build the string to sign.
103+
func (is *Signer) BuildStringToSignByValues(requestDate string, requestMethod string, requestPath string, requestParams url.Values) (string, error) {
104+
requestParams.Set("access_key_id", is.AccessKeyID)
105+
requestParams.Set("signature_method", "HmacSHA256")
106+
requestParams.Set("signature_version", "1")
95107

96108
var timeValue time.Time
97-
if request.Header.Get("Date") != "" {
109+
if requestDate != "" {
98110
var err error
99-
timeValue, err = utils.StringToTime(request.Header.Get("Date"), "RFC 822")
111+
timeValue, err = utils.StringToTime(requestDate, "RFC 822")
100112
if err != nil {
101113
return "", err
102114
}
103115
} else {
104116
timeValue = time.Now()
105117
}
106-
query.Set("time_stamp", utils.TimeToString(timeValue, "ISO 8601"))
118+
requestParams.Set("time_stamp", utils.TimeToString(timeValue, "ISO 8601"))
107119

108120
keys := []string{}
109-
for key := range query {
121+
for key := range requestParams {
110122
keys = append(keys, key)
111123
}
112124

113125
sort.Strings(keys)
114126

115127
parts := []string{}
116128
for _, key := range keys {
117-
values := query[key]
129+
values := requestParams[key]
118130
if len(values) > 0 {
119131
if values[0] != "" {
120132
value := strings.TrimSpace(strings.Join(values, ""))
@@ -131,14 +143,19 @@ func (is *Signer) BuildStringToSign(request *http.Request) (string, error) {
131143

132144
urlParams := strings.Join(parts, "&")
133145

134-
stringToSign := request.Method + "\n" + request.URL.Path + "\n" + urlParams
146+
stringToSign := requestMethod + "\n" + requestPath + "\n" + urlParams
135147

136148
logger.Debug(fmt.Sprintf(
137-
"QingCloud string to sign: [%d] %s",
138-
utils.StringToUnixInt(request.Header.Get("Date"), "RFC 822"),
149+
"QingCloud string to sign: %s",
139150
stringToSign))
140151

141-
is.BuiltURL = request.URL.Path + "?" + urlParams
152+
if requestMethod == "GET" {
153+
is.BuiltURL = requestPath + "?" + urlParams
154+
is.BuiltForm = ""
155+
} else if requestMethod == "POST" {
156+
is.BuiltURL = requestPath
157+
is.BuiltForm = urlParams
158+
}
142159

143160
return stringToSign, nil
144161
}

0 commit comments

Comments
 (0)