Class: QcloudCos::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/qcloud_cos/http.rb

Defined Under Namespace

Classes: ResponseCodeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_id, access_key, token: nil) ⇒ Http

Returns a new instance of Http.



10
11
12
13
14
# File 'lib/qcloud_cos/http.rb', line 10

def initialize(access_id, access_key, token: nil)
  @access_id = access_id
  @access_key = access_key
  @token = token
end

Instance Attribute Details

#access_idObject

Returns the value of attribute access_id.



8
9
10
# File 'lib/qcloud_cos/http.rb', line 8

def access_id
  @access_id
end

#access_keyObject

Returns the value of attribute access_key.



8
9
10
# File 'lib/qcloud_cos/http.rb', line 8

def access_key
  @access_key
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/qcloud_cos/http.rb', line 8

def token
  @token
end

Instance Method Details

#compute_auth(method, fullpath, headers = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/qcloud_cos/http.rb', line 47

def compute_auth(method, fullpath, headers = {})
  path, query_string = fullpath.split("?")

  q_sign_time = "#{Time.now.to_i};#{Time.now.to_i + 600}"
  q_header_list = headers.keys.map(&:downcase).sort.join(";")
  q_url_params = query_string.to_s.split("&").map { |s| s.split("=").first }.compact.map(&:downcase).sort.join(";")

  sign_key = OpenSSL::HMAC.hexdigest("SHA1", access_key, q_sign_time)
  http_parameters = query_string.to_s.split("&").map { |s| s.split("=") }.map { |field, value| [field.downcase, value] }.sort_by { |field, _| field.to_s }.map { |field, value| [field, value].compact.join("=") }.join("&")
  http_headers = headers.map { |h, v| [h.downcase, CGI.escape(v)].join("=") }.join("&")
  http_string = "#{method.downcase}\n#{path}\n#{http_parameters}\n#{http_headers}\n"
  string_to_sign = "sha1\n#{q_sign_time}\n#{Digest::SHA1.hexdigest(http_string)}\n"
  signature = OpenSSL::HMAC.hexdigest("SHA1", sign_key, string_to_sign)

  "q-sign-algorithm=sha1&q-ak=#{access_id}&q-sign-time=#{q_sign_time}&q-key-time=#{q_sign_time}&q-header-list=#{q_header_list}&q-url-param-list=#{q_url_params}&q-signature=#{signature}"
end

#delete(url, headers = {}) ⇒ Object



24
25
26
# File 'lib/qcloud_cos/http.rb', line 24

def delete(url, headers = {})
  request(Net::HTTP::Delete, url, nil, headers)
end

#get(url, headers = {}) ⇒ Object



28
29
30
# File 'lib/qcloud_cos/http.rb', line 28

def get(url, headers = {})
  request(Net::HTTP::Get, url, nil, headers)
end

#post(url, body = nil, headers = {}) ⇒ Object



16
17
18
# File 'lib/qcloud_cos/http.rb', line 16

def post(url, body = nil, headers = {})
  request(Net::HTTP::Post, url, body, headers)
end

#put(url, body = nil, headers = {}) ⇒ Object



20
21
22
# File 'lib/qcloud_cos/http.rb', line 20

def put(url, body = nil, headers = {})
  request(Net::HTTP::Put, url, body, headers)
end

#request(request_klass, url, body = nil, headers = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/qcloud_cos/http.rb', line 32

def request(request_klass, url, body = nil, headers = {})
  uri = URI.parse(url)
  headers["x-cos-security-token"] = token if token
  headers["Authorization"] = compute_auth(request_klass::METHOD, uri.request_uri, headers)
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
    request = request_klass.new(uri, headers)
    request.body = body
    http.request(request)
  end

  raise QcloudCos::Http::ResponseCodeError, response if response.code.to_i != 200

  response
end