// prepare sets up req to be delivered to OSS. func (client *Client) prepare(req *request) error { // Copy so they can be mutated without affecting on retries. headers := copyHeader(req.headers) params := make(url.Values) for k, v := range req.params { params[k] = v } req.params = params req.headers = headers if !req.prepared { req.prepared = true if req.method == "" { req.method = "GET" } if !strings.HasPrefix(req.path, "/") { req.path = "/" + req.path } err := client.setBaseURL(req) if err != nil { return err } } req.headers.Set("Date", util.GetGMTime()) client.signRequest(req) return nil }
func (client *Client) doRequest(req *request) (*http.Response, error) { payload := req.payload if req.headers == nil { req.headers = make(map[string]string) } if req.endpoint == "" { req.endpoint = client.endpoint } contentLength := "0" if payload != nil { contentLength = strconv.Itoa(len(payload)) } req.headers["Content-Type"] = req.contentType req.headers["Content-Length"] = contentLength req.headers["x-log-bodyrawsize"] = contentLength req.headers["Date"] = util.GetGMTime() req.headers["Host"] = req.endpoint req.headers["x-log-apiversion"] = client.version req.headers["x-log-signaturemethod"] = "hmac-sha1" client.signRequest(req, payload) var reader io.Reader if payload != nil { reader = bytes.NewReader(payload) } hreq, err := http.NewRequest(req.method, req.url(), reader) for k, v := range req.headers { if v != "" { hreq.Header.Set(k, v) } } if err != nil { return nil, err } resp, err := client.httpClient.Do(hreq) if err != nil { return nil, err } if resp.StatusCode != 200 && resp.StatusCode != 204 && resp.StatusCode != 206 { return nil, buildError(resp) } return resp, nil }