Class: Aliyun::OSS::HTTP

Inherits:
Object
  • Object
show all
Includes:
Common::Logging
Defined in:
lib/aliyun/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: 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::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.



118
119
120
# File 'lib/aliyun/oss/http.rb', line 118

def initialize(config)
  @config = config
end

Instance Method Details

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



197
198
199
# File 'lib/aliyun/oss/http.rb', line 197

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

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

helper methods



185
186
187
# File 'lib/aliyun/oss/http.rb', line 185

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

#get_request_url(bucket, object) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/aliyun/oss/http.rb', line 122

def get_request_url(bucket, object)
  url = @config.endpoint.dup
  url.query = nil
  url.fragment = nil 
  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



134
135
136
137
138
139
140
# File 'lib/aliyun/oss/http.rb', line 134

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/aliyun/oss/http.rb', line 143

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



201
202
203
# File 'lib/aliyun/oss/http.rb', line 201

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

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



205
206
207
# File 'lib/aliyun/oss/http.rb', line 205

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

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



193
194
195
# File 'lib/aliyun/oss/http.rb', line 193

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

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



189
190
191
# File 'lib/aliyun/oss/http.rb', line 189

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