Skip to content

Commit

Permalink
Fix http after upgraded to rest-client 2.x (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
rockuw authored Jul 23, 2017
1 parent 4091324 commit dadb7be
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
36 changes: 6 additions & 30 deletions lib/aliyun/oss/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,11 @@ def closed?
false
end

def inspect
"@buffer: " + @buffer[0, 32].inspect + "...#{@buffer.size} bytes"
end
end

# RestClient requires the payload to respones to :read(bytes)
# and return a stream.
# We are not doing the real read here, just return a
# readable stream for RestClient playload.rb treats it as:
# def read(bytes=nil)
# @stream.read(bytes)
# end
# alias :to_s :read
# net_http_do_request(http, req, payload ? payload.to_s : nil,
# &@block_response)
class StreamPayload
def initialize(crc_enable = false, init_crc = 0, &block)
@stream = StreamWriter.new(crc_enable, init_crc, &block)
end

def read(bytes = nil)
@stream
end

def close
end

def closed?
false
def inspect
"@buffer: " + @buffer[0, 32].inspect + "...#{@buffer.size} bytes"
end
end

Expand Down Expand Up @@ -290,13 +266,13 @@ def do_request(verb, resources = {}, http_options = {}, &block)
:open_timeout => @config.open_timeout || OPEN_TIMEOUT,
:read_timeout => @config.read_timeout || READ_TIMEOUT
)
response = request.execute do |response, &blk|
if response.code >= 300
e = ServerError.new(response)
response = request.execute do |resp, &blk|
if resp.code >= 300
e = ServerError.new(resp)
logger.error(e.to_s)
raise e
else
response.return!(&blk)
resp.return!(&blk)
end
end

Expand Down
13 changes: 7 additions & 6 deletions lib/aliyun/oss/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def put_object(bucket_name, object_name, opts = {}, &block)
headers[CALLBACK_HEADER] = opts[:callback].serialize
end

payload = HTTP::StreamPayload.new(@config.upload_crc_enable, opts[:init_crc], &block)
payload = HTTP::StreamWriter.new(@config.upload_crc_enable, opts[:init_crc], &block)
r = @http.put(
{:bucket => bucket_name, :object => object_name},
{:headers => headers, :body => payload})
Expand All @@ -547,7 +547,7 @@ def put_object(bucket_name, object_name, opts = {}, &block)
end

if @config.upload_crc_enable && !r.headers[:x_oss_hash_crc64ecma].nil?
data_crc = payload.read.data_crc
data_crc = payload.data_crc
Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'put')
end

Expand Down Expand Up @@ -593,7 +593,8 @@ def append_object(bucket_name, object_name, position, opts = {}, &block)

headers.merge!(to_lower_case(opts[:headers])) if opts.key?(:headers)

payload = HTTP::StreamPayload.new(@config.upload_crc_enable && !opts[:init_crc].nil?, opts[:init_crc], &block)
payload = HTTP::StreamWriter.new(
@config.upload_crc_enable && !opts[:init_crc].nil?, opts[:init_crc], &block)

r = @http.post(
{:bucket => bucket_name, :object => object_name, :sub_res => sub_res},
Expand All @@ -602,7 +603,7 @@ def append_object(bucket_name, object_name, position, opts = {}, &block)
if @config.upload_crc_enable &&
!r.headers[:x_oss_hash_crc64ecma].nil? &&
!opts[:init_crc].nil?
data_crc = payload.read.data_crc
data_crc = payload.data_crc
Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'append')
end

Expand Down Expand Up @@ -1110,13 +1111,13 @@ def upload_part(bucket_name, object_name, txn_id, part_no, &block)

sub_res = {'partNumber' => part_no, 'uploadId' => txn_id}

payload = HTTP::StreamPayload.new(@config.upload_crc_enable, &block)
payload = HTTP::StreamWriter.new(@config.upload_crc_enable, &block)
r = @http.put(
{:bucket => bucket_name, :object => object_name, :sub_res => sub_res},
{:body => payload})

if @config.upload_crc_enable && !r.headers[:x_oss_hash_crc64ecma].nil?
data_crc = payload.read.data_crc
data_crc = payload.data_crc
Aliyun::OSS::Util.crc_check(data_crc, r.headers[:x_oss_hash_crc64ecma], 'put')
end

Expand Down

0 comments on commit dadb7be

Please sign in to comment.