func (option *SignOption) init() { if option.initialized { return } option.headersToSignSpecified = len(option.HeadersToSign) > 0 if option.Timestamp == "" { option.Timestamp = util.TimeToUTCString(time.Now()) } if option.ExpirationPeriodInSeconds <= 0 { option.ExpirationPeriodInSeconds = ExpirationPeriodInSeconds } if option.Headers == nil { option.Headers = make(map[string]string, 3) } else { util.MapKeyToLower(option.Headers) } util.SliceToLower(option.HeadersToSign) if !util.Contains(option.HeadersToSign, "host", true) { option.HeadersToSign = append(option.HeadersToSign, "host") } if !option.headersToSignSpecified { option.HeadersToSign = append(option.HeadersToSign, "x-bce-date") option.Headers["x-bce-date"] = option.Timestamp } else if util.Contains(option.HeadersToSign, "date", true) { if !util.MapContains(option.Headers, generateHeaderValidCompareFunc("date")) { option.Headers["date"] = time.Now().Format(time.RFC1123) } else { option.Headers["date"] = util.TimeStringToRFC1123(util.GetMapValue(option.Headers, "date", true)) } } else { if !util.MapContains(option.Headers, generateHeaderValidCompareFunc("x-bce-date")) { option.Headers["x-bce-date"] = option.Timestamp } } option.initialized = true }
func (option *SignOption) AddHeadersToSign(headers ...string) { if option.HeadersToSign == nil { option.HeadersToSign = []string{} option.HeadersToSign = append(option.HeadersToSign, headers...) } else { for _, header := range headers { if !util.Contains(option.HeadersToSign, header, true) { option.HeadersToSign = append(option.HeadersToSign, header) } } } }
func (req *Request) toCanonicalHeaderString(option *SignOption) string { headerMap := make(map[string]string, len(req.Header)) for key, value := range req.Header { if !option.headersToSignSpecified { if isCanonicalHeader(key) { headerMap[key] = value[0] } } else if util.Contains(option.HeadersToSign, key, true) { headerMap[key] = value[0] } } result := util.ToCanonicalHeaderString(headerMap) return result }
func isCanonicalHeader(key string) bool { key = strings.ToLower(key) return util.Contains(canonicalHeaders, key, true) || strings.Index(key, "x-bce-") == 0 }