From 1d39aeec05108af3adf724266be055057eb27891 Mon Sep 17 00:00:00 2001 From: Liang Tao Date: Fri, 4 Apr 2014 13:53:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E5=AF=B9=E8=BF=87=E6=97=B6?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=9A=84=E8=B0=83=E7=94=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/qiniu/http.rb | 12 ++---------- lib/qiniu/tokens/qbox_token.rb | 2 +- lib/qiniu/utils.rb | 29 +++-------------------------- 3 files changed, 6 insertions(+), 37 deletions(-) mode change 100644 => 100755 lib/qiniu/http.rb diff --git a/lib/qiniu/http.rb b/lib/qiniu/http.rb old mode 100644 new mode 100755 index 917bb5c..a03ff82 --- a/lib/qiniu/http.rb +++ b/lib/qiniu/http.rb @@ -1,8 +1,6 @@ # -*- encoding: utf-8 -*- # vim: sw=2 ts=2 -require 'json' - module Qiniu module HTTP @@ -12,12 +10,6 @@ module Qiniu return 200 <= http_code && http_code <= 299 end # is_response_ok? - def safe_json_parse(data) - JSON.parse(data) - rescue JSON::ParserError - {} - end # safe_json_parse - def generate_query_string(params) if params.is_a?(Hash) total_param = params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) } @@ -71,7 +63,7 @@ module Qiniu content_type = resp_headers["content-type"][0] if !content_type.nil? && content_type == API_RESULT_MIMETYPE then # 如果是JSON格式,则反序列化 - resp_body = safe_json_parse(resp_body) + resp_body = Utils.safe_json_parse(resp_body) end return resp_code, resp_body, resp_headers @@ -119,7 +111,7 @@ module Qiniu content_type = resp_headers["content-type"][0] if !content_type.nil? && content_type == API_RESULT_MIMETYPE then # 如果是JSON格式,则反序列化 - resp_body = safe_json_parse(resp_body) + resp_body = Utils.safe_json_parse(resp_body) end return resp_code, resp_body, resp_headers diff --git a/lib/qiniu/tokens/qbox_token.rb b/lib/qiniu/tokens/qbox_token.rb index bca67c2..186aaa9 100755 --- a/lib/qiniu/tokens/qbox_token.rb +++ b/lib/qiniu/tokens/qbox_token.rb @@ -23,7 +23,7 @@ module Qiniu signature += '?' + query_string if !query_string.nil? && !query_string.empty? signature += "\n" if @params.is_a?(Hash) - params_string = Utils.generate_query_string(@params) + params_string = HTTP.generate_query_string(@params) signature += params_string end signature diff --git a/lib/qiniu/utils.rb b/lib/qiniu/utils.rb index 4b1f024..ad92f1b 100755 --- a/lib/qiniu/utils.rb +++ b/lib/qiniu/utils.rb @@ -31,20 +31,13 @@ module Qiniu {} end - def is_response_ok?(status_code) - status_code/100 == 2 - end - - def response_error(status_code, errmsg) - [status_code, {"error" => errmsg}] - end - def debug(msg) if Config.settings[:enable_debug] Log.logger.debug(msg) end end + ### 已过时,仅作为兼容接口保留 def send_request_with url, data = nil, options = {} options[:method] = Config.settings[:method] unless options[:method] options[:content_type] = Config.settings[:content_type] unless options[:content_type] @@ -69,7 +62,7 @@ module Qiniu response = RestClient.post(url, data, header_options) end code = response.respond_to?(:code) ? response.code.to_i : 0 - unless is_response_ok?(code) + unless HTTP.is_response_ok?(code) raise RequestFailed.new("Request Failed", response) else data = {} @@ -80,6 +73,7 @@ module Qiniu [code, data, raw_headers] end # send_request_with + ### 已过时,仅作为兼容接口保留 def http_request url, data = nil, options = {} retry_times = 0 begin @@ -107,23 +101,6 @@ module Qiniu end end - def upload_multipart_data(url, filepath, action_string, callback_query_string = '', uptoken = nil) - post_data = { - :params => callback_query_string, - :action => action_string, - :file => File.new(filepath, 'rb'), - :multipart => true - } - post_data[:auth] = uptoken unless uptoken.nil? - http_request url, post_data - end - - def generate_query_string(params) - return params if params.is_a?(String) - total_param = params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) } - total_param.join("&") - end - def crc32checksum(filepath) File.open(filepath, "rb") { |f| Zlib.crc32 f.read } end