Class: Baidubce::Utils
- Inherits:
-
Object
- Object
- Baidubce::Utils
- Defined in:
- lib/baidubce/utils/utils.rb
Constant Summary collapse
- DEFAULT_CNAME_LIKE_LIST =
[".cdn.bcebos.com"]
Class Method Summary collapse
-
.append_uri(base_uri, *path_components) ⇒ Object
Append path_components to the end of base_uri in order.
- .generate_headers(headers) ⇒ Object
- .generate_response(headers, body, return_body) ⇒ Object
- .get_canonical_querystring(params, for_signature) ⇒ Object
- .get_md5_from_file(file_name, content_length, buf_size = 8192) ⇒ Object
- .is_cname_like_host(host) ⇒ Object
-
.parse_url_host(config, use_backup_endpoint = false) ⇒ Object
parse protocol, host, port from endpoint in config.
- .url_encode_except_slash(path) ⇒ Object
Class Method Details
.append_uri(base_uri, *path_components) ⇒ Object
Append path_components to the end of base_uri in order.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/baidubce/utils/utils.rb', line 49 def self.append_uri(base_uri, *path_components) uri = [base_uri] path_components.reject(&:empty?) path_components.each { |path| uri << path } unless uri.empty? uri[0].gsub!(/([\/]*$)/, '') uri[-1].gsub!(/(^[\/]*)/, '') uri.each { |u| u.gsub!(/(^[\/]*)|([\/]*$)/, '') } end uri.join("/") end |
.generate_headers(headers) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/baidubce/utils/utils.rb', line 106 def self.generate_headers(headers) = {} resp_headers = headers.inject({}) do |ret, (k, v)| key = k.to_s.tr('_', '-') if key.start_with?(Http::BCE_USER_METADATA_PREFIX) key.slice!(Http::BCE_USER_METADATA_PREFIX) [key] = v elsif key.downcase == Http::ETAG.downcase ret[key] = v.delete('\"') elsif key.downcase == Http::CONTENT_LENGTH.downcase ret[key] = v.to_i else ret[key] = v end ret end resp_headers['user-metadata'] = unless .empty? resp_headers end |
.generate_response(headers, body, return_body) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/baidubce/utils/utils.rb', line 97 def self.generate_response(headers, body, return_body) return body if return_body return generate_headers(headers) if body.to_s.empty? ret = JSON.parse(body) return ret rescue JSON::ParserError return body end |
.get_canonical_querystring(params, for_signature) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/baidubce/utils/utils.rb', line 68 def self.get_canonical_querystring(params, for_signature) return '' if params.nil? || params.empty? arr = [] params.each do |key, value| if !for_signature || key.downcase != Http::AUTHORIZATION.downcase value = '' if value.nil? str = ERB::Util.url_encode(key) + "=" + ERB::Util.url_encode(value) arr << str end end arr.sort! arr.join("&") end |
.get_md5_from_file(file_name, content_length, buf_size = 8192) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/baidubce/utils/utils.rb', line 83 def self.get_md5_from_file(file_name, content_length, buf_size=8192) md5 = Digest::MD5.new left_size = content_length File.open(file_name, 'rb') do |io| bytes_to_read = left_size > buf_size ? buf_size : left_size until left_size <= 0 md5.update(io.read(bytes_to_read)) left_size -= bytes_to_read end end md5.base64digest end |
.is_cname_like_host(host) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/baidubce/utils/utils.rb', line 127 def self.is_cname_like_host(host) DEFAULT_CNAME_LIKE_LIST.each do |suffix| return true if host.to_s.downcase.end_with?(suffix) end return false end |
.parse_url_host(config, use_backup_endpoint = false) ⇒ Object
parse protocol, host, port from endpoint in config.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/baidubce/utils/utils.rb', line 29 def self.parse_url_host(config, use_backup_endpoint=false) endpoint = config.endpoint if use_backup_endpoint endpoint = config.backup_endpoint end unless endpoint.include?"://" protocol = config.protocol.downcase raise "Invalid protocol #{protocol}." if protocol != "http" && protocol != 'https' endpoint = sprintf("%s://%s", protocol, endpoint) end parsed_endpoint = URI.parse(endpoint) scheme = parsed_endpoint.scheme.downcase raise "Invalid endpoint #{endpoint}, unsupported scheme #{scheme}." if scheme != "http" && protocol != 'https' host = parsed_endpoint.host port = parsed_endpoint.port host += ":#{port}" unless scheme == 'http' && port == 80 || scheme == 'https' && port == 443 return "#{scheme}://#{host}", host end |
.url_encode_except_slash(path) ⇒ Object
63 64 65 66 |
# File 'lib/baidubce/utils/utils.rb', line 63 def self.url_encode_except_slash(path) encoded_path = ERB::Util.url_encode(path) encoded_path.gsub('%2F', '/') end |