Commit d94d1b10 by 陈雨佳

instance variables => local variables

parent c1c919cb
...@@ -11,51 +11,49 @@ module AliyunLogRubySdk ...@@ -11,51 +11,49 @@ module AliyunLogRubySdk
X_LOG_SIGNATURE_METHOD = 'hmac-sha1'.freeze X_LOG_SIGNATURE_METHOD = 'hmac-sha1'.freeze
def generate_headers(options = {}) def generate_headers(options = {})
@options = options headers = options[:headers] || {}
host = headers.delete('x-log-host')
@headers = options[:headers] || {} headers = headers.merge('x-log-apiversion' => X_LOG_API_VERSION,
host = @headers.delete('x-log-host') 'x-log-signaturemethod' => X_LOG_SIGNATURE_METHOD,
'Date' => Time.now.httpdate,
@headers = @headers.merge('x-log-apiversion' => X_LOG_API_VERSION, 'Host' => (host || "#{project_name}.#{endpoint}"))
'x-log-signaturemethod' => X_LOG_SIGNATURE_METHOD,
'Date' => Time.now.httpdate,
'Host' => (host || "#{project_name}.#{endpoint}"))
body = options[:body] body = options[:body]
if body.present? if body.present?
@headers['Content-Length'] = body.bytesize.to_s headers['Content-Length'] = body.bytesize.to_s
@headers['Content-MD5'] = calculate_md5(body) headers['Content-MD5'] = calculate_md5(body)
else else
@headers['Content-Length'] = '0' headers['Content-Length'] = '0'
@headers['x-log-bodyrawsize'] = '0' headers['x-log-bodyrawsize'] = '0'
end 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 end
private private
def authorization def generate_authorization(verb, headers, resource)
"LOG #{access_key_id}:#{signature}" "LOG #{access_key_id}:#{generate_signature(verb, headers, resource)}"
end end
def signature def generate_signature(verb, headers, resource)
Base64.encode64(OpenSSL::HMAC.digest('sha1', access_key_secret, sign_string.encode('utf-8'))).rstrip Base64.encode64(OpenSSL::HMAC.digest('sha1', access_key_secret, generate_sign_string(verb, headers, resource).encode('utf-8'))).rstrip
end end
def sign_string def generate_sign_string(verb, headers, resource)
%W[ %W[
#{@options[:verb] || 'GET'} #{verb}
#{@headers['Content-MD5']} #{headers['Content-MD5']}
#{@headers['Content-Type']} #{headers['Content-Type']}
#{@headers['Date']} #{headers['Date']}
#{canonicalized_log_headers} #{canonicalized_log_headers(headers)}
#{@options[:resource]} #{resource}
].join("\n") ].join("\n")
end end
...@@ -63,10 +61,10 @@ module AliyunLogRubySdk ...@@ -63,10 +61,10 @@ module AliyunLogRubySdk
Digest::MD5.hexdigest(body).upcase Digest::MD5.hexdigest(body).upcase
end end
def canonicalized_log_headers def canonicalized_log_headers(headers)
@headers.select { |key, _value| key.match(/x-log-*|x-acs-*/) }.map do |key, value| headers.select { |key, _value| key.match(/x-log-*|x-acs-*/) }.map do |key, value|
"#{key.downcase}:#{value}" "#{key.downcase}:#{value}"
end.sort.join("\n") end.sort.join("\n")
end end
end end
end end
\ No newline at end of file
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