Module: Qiniu::RS::Utils

Extended by:
Utils
Included in:
AccessToken, Auth, DownloadToken, EU, IO, Image, Pub, QboxToken, RS, UP, UploadToken, Utils
Defined in:
lib/qiniu/rs/utils.rb

Instance Method Summary collapse

Instance Method Details

#crc32checksum(filepath) ⇒ Object



126
127
128
# File 'lib/qiniu/rs/utils.rb', line 126

def crc32checksum(filepath)
  File.open(filepath, "rb") { |f| Zlib.crc32 f.read }
end

#debug(msg) ⇒ Object



43
44
45
46
47
# File 'lib/qiniu/rs/utils.rb', line 43

def debug(msg)
    if Config.settings[:enable_debug]
        Log.logger.debug(msg)
    end
end

#encode_entry_uri(bucket, key) ⇒ Object



24
25
26
27
# File 'lib/qiniu/rs/utils.rb', line 24

def encode_entry_uri(bucket, key)
  entry_uri = bucket + ':' + key
  urlsafe_base64_encode(entry_uri)
end

#generate_qbox_signature(url, params, mime = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/qiniu/rs/utils.rb', line 130

def generate_qbox_signature(url, params, mime = nil)
  access_key = Config.settings[:access_key]
  secret_key = Config.settings[:secret_key]
  uri = URI.parse(url)
  signature = uri.path
  query_string = uri.query
  signature += '?' + query_string if !query_string.nil? && !query_string.empty?
  signature += "\n"
  if params.is_a?(Hash)
      params_string = generate_query_string(params)
      signature += params_string
  end
  if mime.is_a?(String) && mime == "application/x-www-form-urlencoded" && params.is_a?(String)
      signature += params
  end
  hmac = HMAC::SHA1.new(secret_key)
  hmac.update(signature)
  encoded_digest = urlsafe_base64_encode(hmac.digest)
  %Q(#{access_key}:#{encoded_digest})
end

#generate_query_string(params) ⇒ Object



120
121
122
123
124
# File 'lib/qiniu/rs/utils.rb', line 120

def generate_query_string(params)
  return params if params.is_a?(String)
  total_param = params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) }
  total_param.join("&")
end

#http_request(url, data = nil, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/qiniu/rs/utils.rb', line 83

def http_request url, data = nil, options = {}
  retry_times = 0
  begin
    retry_times += 1
    send_request_with url, data, options
  rescue Errno::ECONNRESET => err
    if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times]
      retry
    else
      Log.logger.error err
    end
  rescue => e
    Log.logger.warn "#{e.message} => Utils.http_request('#{url}')"
    code = 0
    data = {}
    body = {}
    if e.respond_to? :response
      res = e.response
      code = res.code.to_i if res.respond_to? :code
      body = res.respond_to?(:body) ? res.body : ""
      data = safe_json_parse(body) unless body.empty?
    end
    [code, data]
  end
end

#is_response_ok?(status_code) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/qiniu/rs/utils.rb', line 35

def is_response_ok?(status_code)
    status_code/100 == 2
end

#response_error(status_code, errmsg) ⇒ Object



39
40
41
# File 'lib/qiniu/rs/utils.rb', line 39

def response_error(status_code, errmsg)
    [status_code, {"error" => errmsg}]
end

#safe_json_parse(data) ⇒ Object



29
30
31
32
33
# File 'lib/qiniu/rs/utils.rb', line 29

def safe_json_parse(data)
  JSON.parse(data)
rescue JSON::ParserError
  {}
end

#send_request_with(url, data = nil, options = {}) ⇒ Object



49
50
51
52
53
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
# File 'lib/qiniu/rs/utils.rb', line 49

def send_request_with url, data = nil, options = {}
  options[:method] = Config.settings[:method] unless options[:method]
  options[:content_type] = Config.settings[:content_type] unless options[:content_type]
  header_options = {
    :accept => :json,
    :user_agent => Config.settings[:user_agent]
  }
  auth_token = nil
  if !options[:qbox_signature_token].nil? && !options[:qbox_signature_token].empty?
    auth_token = 'QBox ' + options[:qbox_signature_token]
  elsif !options[:upload_signature_token].nil? && !options[:upload_signature_token].empty?
    auth_token = 'UpToken ' + options[:upload_signature_token]
  elsif options[:access_token]
    auth_token = 'Bearer ' + options[:access_token]
  end
  header_options.merge!('Authorization' => auth_token) unless auth_token.nil?
  case options[:method]
  when :get
    response = RestClient.get(url, header_options)
  when :post
    header_options.merge!(:content_type => options[:content_type])
    response = RestClient.post(url, data, header_options)
  end
  code = response.respond_to?(:code) ? response.code.to_i : 0
  unless is_response_ok?(code)
    raise RequestFailed.new("Request Failed", response)
  else
    data = {}
    body = response.respond_to?(:body) ? response.body : {}
    data = safe_json_parse(body) unless body.empty?
  end
  [code, data]
end

#upload_multipart_data(url, filepath, action_string, callback_query_string = '', uptoken = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/qiniu/rs/utils.rb', line 109

def upload_multipart_data(url, filepath, action_string, callback_query_string = '', uptoken = nil)
    post_data = {
      :params => callback_query_string,
      :action => action_string,
      :file => File.new(filepath, 'rb'),
      :multipart => true
    }
    post_data[:auth] = uptoken unless uptoken.nil?
    http_request url, post_data
end

#urlsafe_base64_decode(encoded_content) ⇒ Object



20
21
22
# File 'lib/qiniu/rs/utils.rb', line 20

def urlsafe_base64_decode encoded_content
  Base64.decode64 encoded_content.gsub('_','/').gsub('-', '+')
end

#urlsafe_base64_encode(content) ⇒ Object



16
17
18
# File 'lib/qiniu/rs/utils.rb', line 16

def urlsafe_base64_encode content
  Base64.encode64(content).strip.gsub('+', '-').gsub('/','_').gsub(/\r?\n/, '')
end