Class: Aliyun::Log::Request

Inherits:
Object
  • Object
show all
Includes:
Common::Logging
Defined in:
lib/aliyun/log/request.rb

Constant Summary

Constants included from Common::Logging

Common::Logging::MAX_NUM_LOG, Common::Logging::ROTATE_SIZE

Instance Method Summary collapse

Methods included from Common::Logging

log_file=, log_level=, #logger, logger, logger=, logger_level

Constructor Details

#initialize(config) ⇒ Request

Returns a new instance of Request.



15
16
17
# File 'lib/aliyun/log/request.rb', line 15

def initialize(config)
  @config = config
end

Instance Method Details

#canonicalized_headers(headers) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/aliyun/log/request.rb', line 139

def canonicalized_headers(headers)
  h = {}
  headers.each do |k, v|
    h[k.downcase] = v if k =~ /x-log-.*/
  end
  h.keys.sort.map do |e|
    h[e]
    "#{e}:#{h[e].gsub(/^\s+/, '')}"
  end.join("\n")
end

#canonicalized_resource(resource = '', query = {}) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/aliyun/log/request.rb', line 150

def canonicalized_resource(resource = '', query = {})
  return resource if query.empty?

  url = URI.parse(resource)
  sort_str = query.keys.sort.map do |e|
    "#{e}=#{query[e]}"
  end.join('&')
  "#{url}?#{sort_str}"
end

#compact_headers(body = nil, is_pb = false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/aliyun/log/request.rb', line 91

def compact_headers(body = nil, is_pb = false)
  headers = {
    'x-log-apiversion' => '0.6.0',
    'x-log-signaturemethod' => 'hmac-sha1',
    'x-log-bodyrawsize' => '0',
    'Date' => DateTime.now.httpdate,
    'User-Agent' => "aliyun-log ruby-#{RUBY_VERSION}/#{RUBY_PLATFORM}"
  }
  return headers if body.nil?

  if is_pb
    compressed = Zlib::Deflate.deflate(body.encode)
    headers['Content-Length'] = compressed.bytesize.to_s
    raise 'content length is larger than 3MB' if headers['Content-Length'].to_i > 3_145_728

    headers['Content-MD5'] = Digest::MD5.hexdigest(compressed).upcase
    headers['Content-Type'] = 'application/x-protobuf'
    headers['x-log-compresstype'] = 'deflate'
    headers['x-log-bodyrawsize'] = body.encode.bytesize.to_s
  else
    headers['Content-Type'] = 'application/json'
    headers['Content-MD5'] = Digest::MD5.hexdigest(body.encode).upcase
    headers['x-log-bodyrawsize'] = body.bytesize.to_s
  end
  headers
end

#delete(resources, payload) ⇒ Object



50
51
52
# File 'lib/aliyun/log/request.rb', line 50

def delete(resources, payload)
  do_request('DELETE', resources, payload)
end

#do_request(verb, resources, payload) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aliyun/log/request.rb', line 54

def do_request(verb, resources, payload)
  resource_path = get_resource_path(resources)
  request_options = {
    method: verb,
    url: get_request_url(resources),
    open_timeout: @config.open_timeout,
    read_timeout: @config.read_timeout
  }
  if verb == 'GET'
    headers = compact_headers
    headers['Authorization'] = signature(verb, resource_path, headers, payload)
    request_options[:headers] = headers
    request_options[:url] = URI.escape(canonicalized_resource(request_options[:url], payload))
  else
    headers = compact_headers(payload, resources[:is_pb])
    headers['Authorization'] = signature(verb, resource_path, headers)
    request_options[:headers] = headers
    payload = Zlib::Deflate.deflate(payload.encode) if resources[:is_pb]
    request_options[:payload] = payload
  end
  request = RestClient::Request.new(request_options)
  response = request.execute do |resp|
    if resp.code >= 300
      e = ServerError.new(resp)
      logger.error(e.to_s)
      raise e
    else
      resp.return!
    end
  end

  logger.debug("Received HTTP response, code: #{response.code}, headers: " \
                "#{response.headers}, body: #{response.body.force_encoding('UTF-8')}")

  response
end

#get(resources, payload = {}) ⇒ Object



38
39
40
# File 'lib/aliyun/log/request.rb', line 38

def get(resources, payload = {})
  do_request('GET', resources, payload)
end

#get_request_url(resources = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/aliyun/log/request.rb', line 30

def get_request_url(resources = {})
  resources ||= {}
  url = URI.parse(@config.endpoint)
  url.host = "#{resources[:project]}." + url.host if resources[:project]
  url.path = get_resource_path(resources)
  url.to_s
end

#get_resource_path(resources = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/aliyun/log/request.rb', line 19

def get_resource_path(resources = {})
  resources ||= {}
  res = '/'
  if resources[:logstore]
    res = "#{res}logstores"
    res = "#{res}/#{resources[:logstore]}" unless resources[:logstore].empty?
  end
  res = "#{res}/#{resources[:action]}" if resources[:action]
  res
end

#post(resources, payload) ⇒ Object



42
43
44
# File 'lib/aliyun/log/request.rb', line 42

def post(resources, payload)
  do_request('POST', resources, payload)
end

#put(resources, payload) ⇒ Object



46
47
48
# File 'lib/aliyun/log/request.rb', line 46

def put(resources, payload)
  do_request('PUT', resources, payload)
end

#signature(verb, resource, headers, query = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/aliyun/log/request.rb', line 118

def signature(verb, resource, headers, query = {})
  sha1_digest = OpenSSL::HMAC.digest(
    OpenSSL::Digest.new('sha1'),
    @config.access_key_secret,
    string_to_sign(verb, resource, headers, query).chomp
  )
  base64_sign = Base64.strict_encode64(sha1_digest)
  "LOG #{@config.access_key_id}:#{base64_sign}"
end

#string_to_sign(verb, resource, headers, query = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/aliyun/log/request.rb', line 128

def string_to_sign(verb, resource, headers, query = {})
  <<~DOC
    #{verb}
    #{headers['Content-MD5']}
    #{headers['Content-Type']}
    #{headers['Date']}
    #{canonicalized_headers(headers)}
    #{canonicalized_resource(resource, query)}
  DOC
end