1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 18:30:54 +00:00
Beego/logs/alils/request.go

63 lines
1.5 KiB
Go
Raw Normal View History

2016-12-27 01:58:42 +00:00
package alils
import (
"bytes"
"crypto/md5"
"fmt"
"net/http"
)
// request sends a request to SLS.
func request(project *LogProject, method, uri string, headers map[string]string,
body []byte) (resp *http.Response, err error) {
// The caller should provide 'x-sls-bodyrawsize' header
if _, ok := headers["x-sls-bodyrawsize"]; !ok {
err = fmt.Errorf("Can't find 'x-sls-bodyrawsize' header")
return
}
// SLS public request headers
headers["Host"] = project.Name + "." + project.Endpoint
headers["Date"] = nowRFC1123()
headers["x-sls-apiversion"] = version
headers["x-sls-signaturemethod"] = signatureMethod
if body != nil {
bodyMD5 := fmt.Sprintf("%X", md5.Sum(body))
headers["Content-MD5"] = bodyMD5
if _, ok := headers["Content-Type"]; !ok {
err = fmt.Errorf("Can't find 'Content-Type' header")
return
}
}
// Calc Authorization
2017-04-30 14:41:23 +00:00
// Authorization = "SLS <AccessKeyID>:<Signature>"
2016-12-27 01:58:42 +00:00
digest, err := signature(project, method, uri, headers)
if err != nil {
return
}
2017-04-30 14:41:23 +00:00
auth := fmt.Sprintf("SLS %v:%v", project.AccessKeyID, digest)
2016-12-27 01:58:42 +00:00
headers["Authorization"] = auth
// Initialize http request
reader := bytes.NewReader(body)
urlStr := fmt.Sprintf("http://%v.%v%v", project.Name, project.Endpoint, uri)
req, err := http.NewRequest(method, urlStr, reader)
if err != nil {
return
}
for k, v := range headers {
req.Header.Add(k, v)
}
// Get ready to do request
resp, err = http.DefaultClient.Do(req)
if err != nil {
return
}
return
}