Class: AliyunSDK::OSS::HTTP

Inherits:
Object
  • Object
show all
Includes:
Common::Logging
Defined in:
lib/aliyun_sdk/oss/http.rb

Overview

HTTP wraps the HTTP functionalities for accessing OSS RESTful API. It handles the OSS-specific protocol elements, and rest-client details for the user, which includes:

  • automatically generate signature for every request

  • parse response headers/body

  • raise exceptions and capture the request id

  • encapsulates streaming upload/download

Examples:

simple get

headers, body = http.get({:bucket => 'bucket'})

streaming download

http.get({:bucket => 'bucket', :object => 'object'}) do |chunk|
  # handle chunk
end

streaming upload

def streaming_upload(&block)
  http.put({:bucket => 'bucket', :object => 'object'},
           {:body => HTTP::StreamPlayload.new(block)})
end

streaming_upload do |stream|
  stream << "hello world"
end

Defined Under Namespace

Classes: StreamPayload, StreamWriter

Constant Summary collapse

DEFAULT_CONTENT_TYPE =
'application/octet-stream'
DEFAULT_ACCEPT_ENCODING =
'identity'
STS_HEADER =
'x-oss-security-token'
OPEN_TIMEOUT =
10
READ_TIMEOUT =
120

Constants included from Common::Logging

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

Instance Method Summary collapse

Methods included from Common::Logging

#logger, set_log_file, set_log_level

Constructor Details

#initialize(config) ⇒ HTTP

Returns a new instance of HTTP.



137
138
139
# File 'lib/aliyun_sdk/oss/http.rb', line 137

def initialize(config)
  @config = config
end

Instance Method Details

#delete(resources = {}, http_options = {}, &block) ⇒ Object



215
216
217
# File 'lib/aliyun_sdk/oss/http.rb', line 215

def delete(resources = {}, http_options = {}, &block)
  do_request('DELETE', resources, http_options, &block)
end

#get(resources = {}, http_options = {}, &block) ⇒ Object

helper methods



203
204
205
# File 'lib/aliyun_sdk/oss/http.rb', line 203

def get(resources = {}, http_options = {}, &block)
  do_request('GET', resources, http_options, &block)
end

#get_request_url(bucket, object) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/aliyun_sdk/oss/http.rb', line 141

def get_request_url(bucket, object)
  url = @config.endpoint.dup
  isIP = !!(url.host =~ Resolv::IPv4::Regex)
  url.host = "#{bucket}." + url.host if bucket && !@config.cname && !isIP
  url.path = '/'
  url.path << "#{bucket}/" if bucket && isIP
  url.path << "#{CGI.escape(object)}" if object

  url.to_s
end

#get_resource_path(bucket, object) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/aliyun_sdk/oss/http.rb', line 152

def get_resource_path(bucket, object)
  res = '/'
  res << "#{bucket}/" if bucket
  res << "#{object}" if object

  res
end

#handle_response(r, &block) ⇒ Object

Handle Net::HTTPRespoonse



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/aliyun_sdk/oss/http.rb', line 161

def handle_response(r, &block)
  # read all body on error
  if r.code.to_i >= 300
    r.read_body
  else
  # streaming read body on success
    encoding = r['content-encoding']
    if encoding == 'gzip'
      stream = StreamWriter.new { |s| r.read_body { |chunk| s << chunk } }
      reader = Zlib::GzipReader.new(stream)
      yield reader.read(16 * 1024) until reader.eof?
    elsif encoding == 'deflate'
      begin
        stream = Zlib::Inflate.new
        # 1.9.x doesn't support streaming inflate
        if RUBY_VERSION < '2.0.0'
          yield stream.inflate(r.read_body)
        else
          r.read_body { |chunk| stream << chunk }
          stream.finish { |chunk| yield chunk }
        end
      rescue Zlib::DataError
        # No luck with Zlib decompression. Let's try with raw deflate,
        # like some broken web servers do.
        stream = Zlib::Inflate.new(-Zlib::MAX_WBITS)
        # 1.9.x doesn't support streaming inflate
        if RUBY_VERSION < '2.0.0'
          yield stream.inflate(r.read_body)
        else
          r.read_body { |chunk| stream << chunk }
          stream.finish { |chunk| yield chunk }
        end
      end
    else
      r.read_body { |chunk| yield chunk }
    end
  end
end

#head(resources = {}, http_options = {}, &block) ⇒ Object



219
220
221
# File 'lib/aliyun_sdk/oss/http.rb', line 219

def head(resources = {}, http_options = {}, &block)
  do_request('HEAD', resources, http_options, &block)
end

#options(resources = {}, http_options = {}, &block) ⇒ Object



223
224
225
# File 'lib/aliyun_sdk/oss/http.rb', line 223

def options(resources = {}, http_options = {}, &block)
  do_request('OPTIONS', resources, http_options, &block)
end

#post(resources = {}, http_options = {}, &block) ⇒ Object



211
212
213
# File 'lib/aliyun_sdk/oss/http.rb', line 211

def post(resources = {}, http_options = {}, &block)
  do_request('POST', resources, http_options, &block)
end

#put(resources = {}, http_options = {}, &block) ⇒ Object



207
208
209
# File 'lib/aliyun_sdk/oss/http.rb', line 207

def put(resources = {}, http_options = {}, &block)
  do_request('PUT', resources, http_options, &block)
end