diff --git a/lib/aliyun/oss/http.rb b/lib/aliyun/oss/http.rb index 9dde80a..1a60172 100644 --- a/lib/aliyun/oss/http.rb +++ b/lib/aliyun/oss/http.rb @@ -4,6 +4,18 @@ require 'resolv' require 'fiber' +module RestClientRefinements + # Refine rest-client to exclude the 'Content-Length' header when + # 'Transfer-Encoding' is set to 'chuncked' exclusively in Aliyun::OSS:HTTP. This may be a problem for + # some http servers like tengine. + refine RestClient::Payload::Base do + def headers + ({'content-length' => size.to_s} if size) || {} + end + end +end + + module Aliyun module OSS @@ -31,6 +43,7 @@ module OSS # stream << "hello world" # end class HTTP + using RestClientRefinements DEFAULT_CONTENT_TYPE = 'application/octet-stream' DEFAULT_ACCEPT_ENCODING = 'identity' @@ -307,16 +320,3 @@ def get_user_agent end # HTTP end # OSS end # Aliyun - -# Monkey patch rest-client to exclude the 'Content-Length' header when -# 'Transfer-Encoding' is set to 'chuncked'. This may be a problem for -# some http servers like tengine. -module RestClient - module Payload - class Base - def headers - ({'content-length' => size.to_s} if size) || {} - end - end - end -end diff --git a/lib/aliyun/oss/protocol.rb b/lib/aliyun/oss/protocol.rb index 41d1398..01874d3 100644 --- a/lib/aliyun/oss/protocol.rb +++ b/lib/aliyun/oss/protocol.rb @@ -79,7 +79,7 @@ def list_buckets(opts = {}) update_if_exists( more, { :limit => ->(x) { x.to_i }, - :truncated => ->(x) { x.to_bool } + :truncated => ->(x) { Util.to_bool(x) } } ) @@ -413,7 +413,7 @@ def get_bucket_referer(name) doc = parse_xml(r.body) opts = { :allow_empty => - get_node_text(doc.root, 'AllowEmptyReferer', &:to_bool), + get_node_text(doc.root, 'AllowEmptyReferer') { |x| Util.to_bool(x) }, :whitelist => doc.css("RefererList Referer").map(&:text) } @@ -790,7 +790,7 @@ def list_objects(bucket_name, opts = {}) update_if_exists( more, { :limit => ->(x) { x.to_i }, - :truncated => ->(x) { x.to_bool }, + :truncated => ->(x) { Util.to_bool(x) }, :delimiter => ->(x) { decode_key(x, encoding) }, :marker => ->(x) { decode_key(x, encoding) }, :next_marker => ->(x) { decode_key(x, encoding) } @@ -1412,7 +1412,7 @@ def list_multipart_uploads(bucket_name, opts = {}) update_if_exists( more, { :limit => ->(x) { x.to_i }, - :truncated => ->(x) { x.to_bool }, + :truncated => ->(x) { Util.to_bool(x) }, :key_marker => ->(x) { decode_key(x, encoding) }, :next_key_marker => ->(x) { decode_key(x, encoding) } } @@ -1476,7 +1476,7 @@ def list_parts(bucket_name, object_name, txn_id, opts = {}) update_if_exists( more, { :limit => ->(x) { x.to_i }, - :truncated => ->(x) { x.to_bool } + :truncated => ->(x) { Util.to_bool(x) } } ) diff --git a/lib/aliyun/oss/util.rb b/lib/aliyun/oss/util.rb index 7dc595d..a1971a5 100644 --- a/lib/aliyun/oss/util.rb +++ b/lib/aliyun/oss/util.rb @@ -97,17 +97,14 @@ def ensure_bucket_name_valid(name) unless (name =~ %r|^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$|) fail ClientError, "The bucket name is invalid." end - end + end + + def to_bool(string) + return true if string =~ /^true$/i + false + end end # self end # Util end # OSS -end # Aliyun - -# Monkey patch to support #to_bool -class String - def to_bool - return true if self =~ /^true$/i - false - end -end \ No newline at end of file +end # Aliyun \ No newline at end of file