Commit d94d1b10 by 陈雨佳

instance variables => local variables

parent c1c919cb
......@@ -11,12 +11,10 @@ module AliyunLogRubySdk
X_LOG_SIGNATURE_METHOD = 'hmac-sha1'.freeze
def generate_headers(options = {})
@options = options
headers = options[:headers] || {}
host = headers.delete('x-log-host')
@headers = options[:headers] || {}
host = @headers.delete('x-log-host')
@headers = @headers.merge('x-log-apiversion' => X_LOG_API_VERSION,
headers = headers.merge('x-log-apiversion' => X_LOG_API_VERSION,
'x-log-signaturemethod' => X_LOG_SIGNATURE_METHOD,
'Date' => Time.now.httpdate,
'Host' => (host || "#{project_name}.#{endpoint}"))
......@@ -24,38 +22,38 @@ module AliyunLogRubySdk
body = options[:body]
if body.present?
@headers['Content-Length'] = body.bytesize.to_s
@headers['Content-MD5'] = calculate_md5(body)
headers['Content-Length'] = body.bytesize.to_s
headers['Content-MD5'] = calculate_md5(body)
else
@headers['Content-Length'] = '0'
@headers['x-log-bodyrawsize'] = '0'
headers['Content-Length'] = '0'
headers['x-log-bodyrawsize'] = '0'
end
@headers['Authorization'] = authorization
headers['Authorization'] = generate_authorization(options[:verb] || 'GET', headers, options[:resource])
@headers['x-log-date'] = @headers['Date']
headers['x-log-date'] = headers['Date']
@headers
headers
end
private
def authorization
"LOG #{access_key_id}:#{signature}"
def generate_authorization(verb, headers, resource)
"LOG #{access_key_id}:#{generate_signature(verb, headers, resource)}"
end
def signature
Base64.encode64(OpenSSL::HMAC.digest('sha1', access_key_secret, sign_string.encode('utf-8'))).rstrip
def generate_signature(verb, headers, resource)
Base64.encode64(OpenSSL::HMAC.digest('sha1', access_key_secret, generate_sign_string(verb, headers, resource).encode('utf-8'))).rstrip
end
def sign_string
def generate_sign_string(verb, headers, resource)
%W[
#{@options[:verb] || 'GET'}
#{@headers['Content-MD5']}
#{@headers['Content-Type']}
#{@headers['Date']}
#{canonicalized_log_headers}
#{@options[:resource]}
#{verb}
#{headers['Content-MD5']}
#{headers['Content-Type']}
#{headers['Date']}
#{canonicalized_log_headers(headers)}
#{resource}
].join("\n")
end
......@@ -63,8 +61,8 @@ module AliyunLogRubySdk
Digest::MD5.hexdigest(body).upcase
end
def canonicalized_log_headers
@headers.select { |key, _value| key.match(/x-log-*|x-acs-*/) }.map do |key, value|
def canonicalized_log_headers(headers)
headers.select { |key, _value| key.match(/x-log-*|x-acs-*/) }.map do |key, value|
"#{key.downcase}:#{value}"
end.sort.join("\n")
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment