Compare commits

..
12 Commits
Author SHA1 Message Date
Junv 957b537da0 update gems 2016-03-21 16:20:33 +08:00
Junv 4db869a53a Support file steam to upload 2016-03-21 16:16:43 +08:00
Bai Long a07b323d9d Merge pull request #116 from forresty/fetch
add `Qiniu.fetch` method
2015-12-16 10:35:15 +08:00
Bai Long 785d960ac0 Merge pull request #133 from BluntBlade/master
加上对回调请求进行签名验证的逻辑
2015-09-24 13:22:20 +08:00
Liang Tao e308ce1cfd 调整版本号。 2015-09-13 20:50:11 +08:00
Liang Tao 1248008642 调整测试代码。 2015-09-13 20:46:00 +08:00
Liang Tao 57d522ce39 添加验证回调请求的函数。 2015-09-13 20:32:41 +08:00
Bai Long c2b72befb9 Merge pull request #132 from BluntBlade/master
修正对于 application/json; charset=utf-8 的判断。
2015-09-12 10:25:39 +08:00
Liang Tao 2912ed9eea Merge branch '20150326.application_json_utf8' 2015-08-24 18:09:34 +08:00
Liang Tao 52057ff062 恢复 exif 接口的单元测试,可以覆盖 application/json; charset=utf-8 的解析测试。 2015-08-24 18:09:22 +08:00
Liang Tao f53dec3242 修正对于 application/json; charset=utf-8 的判断。 2015-04-14 14:31:19 +08:00
Forrest Ye f1ed9a9783 add Qiniu.fetch method 2015-01-05 15:35:43 +08:00
12 changed files with 98 additions and 26 deletions
+1
View File
@@ -16,3 +16,4 @@ spec/reports
test/tmp test/tmp
test/version_tmp test/version_tmp
tmp tmp
.idea
+4
View File
@@ -1,5 +1,9 @@
## CHANGE LOG ## CHANGE LOG
### V6.5.1
- 为 Qiniu::Auth 添加验证七牛回调请求签名合法性的函数。[https://github.com/qiniu/ruby-sdk/pull/133](https://github.com/qiniu/ruby-sdk/pull/133)
### v6.5.0 ### v6.5.0
- 为 Qiniu::Auth 添加一个异常处理逻辑,在 Access Key 和 Secret Key 未正常设置(nil 值)的情况下给出正确提示。[https://github.com/qiniu/ruby-sdk/pull/126](https://github.com/qiniu/ruby-sdk/pull/126) - 为 Qiniu::Auth 添加一个异常处理逻辑,在 Access Key 和 Secret Key 未正常设置(nil 值)的情况下给出正确提示。[https://github.com/qiniu/ruby-sdk/pull/126](https://github.com/qiniu/ruby-sdk/pull/126)
+8 -5
View File
@@ -1,9 +1,9 @@
PATH PATH
remote: . remote: .
specs: specs:
qiniu (6.5.0) qiniu (6.5.1)
json (~> 1.8) json (~> 1.8)
mime-types (~> 1.19) mime-types (~> 2.4.3)
rest-client (~> 1.7.3) rest-client (~> 1.7.3)
ruby-hmac (~> 0.4) ruby-hmac (~> 0.4)
@@ -12,9 +12,9 @@ GEM
specs: specs:
diff-lcs (1.2.5) diff-lcs (1.2.5)
fakeweb (1.3.0) fakeweb (1.3.0)
json (1.8.2) json (1.8.3)
mime-types (1.25.1) mime-types (2.4.3)
netrc (0.10.3) netrc (0.11.0)
rake (10.4.2) rake (10.4.2)
rest-client (1.7.3) rest-client (1.7.3)
mime-types (>= 1.16, < 3.0) mime-types (>= 1.16, < 3.0)
@@ -42,3 +42,6 @@ DEPENDENCIES
qiniu! qiniu!
rake (>= 0.9) rake (>= 0.9)
rspec (>= 2.11) rspec (>= 2.11)
BUNDLED WITH
1.11.2
+5
View File
@@ -117,6 +117,11 @@ module Qiniu
code == StatusOK code == StatusOK
end end
def fetch(bucket, target_url, key)
code, data = Storage.fetch(bucket, target_url, key)
code == StatusOK
end
def batch(command, bucket, keys) def batch(command, bucket, keys)
code, data = Storage.batch(command, bucket, keys) code, data = Storage.batch(command, bucket, keys)
code == StatusOK ? data : false code == StatusOK ? data : false
+31 -11
View File
@@ -206,11 +206,7 @@ module Qiniu
return authorize_download_url(download_url, args) return authorize_download_url(download_url, args)
end # authorize_download_url_2 end # authorize_download_url_2
def generate_acctoken(url, body = '') def generate_acctoken_sign_with_mac(access_key, secret_key, url, body)
### 提取AK/SK信息
access_key = Config.settings[:access_key]
secret_key = Config.settings[:secret_key]
### 解析URL,生成待签名字符串 ### 解析URL,生成待签名字符串
uri = URI.parse(url) uri = URI.parse(url)
signing_str = uri.path signing_str = uri.path
@@ -232,13 +228,12 @@ module Qiniu
### 生成数字签名 ### 生成数字签名
sign = calculate_hmac_sha1_digest(secret_key, signing_str) sign = calculate_hmac_sha1_digest(secret_key, signing_str)
encoded_sign = Utils.urlsafe_base64_encode(sign) return Utils.urlsafe_base64_encode(sign)
end # generate_acctoken_sign_with_mac
### 生成管理授权凭证 def generate_acctoken(url, body = '')
acctoken = "#{access_key}:#{encoded_sign}" encoded_sign = generate_acctoken_sign_with_mac(Config.settings[:access_key], Config.settings[:secret_key], url, body)
return "#{Config.settings[:access_key]}:#{encoded_sign}"
### 返回管理授权凭证
return acctoken
end # generate_acctoken end # generate_acctoken
def generate_uptoken(put_policy) def generate_uptoken(put_policy)
@@ -259,6 +254,31 @@ module Qiniu
### 返回上传授权凭证 ### 返回上传授权凭证
return uptoken return uptoken
end # generate_uptoken end # generate_uptoken
def authenticate_callback_request(auth_str, url, body = '')
### 提取AK/SK信息
access_key = Config.settings[:access_key]
secret_key = Config.settings[:secret_key]
### 检查签名格式
ak_pos = auth_str.index(access_key)
if ak_pos.nil? then
return false
end
colon_pos = auth_str.index(':', ak_pos + 1)
if colon_pos.nil? || ((ak_pos + access_key.length) != colon_pos) then
return false
end
encoded_sign = generate_acctoken_sign_with_mac(access_key, secret_key, url, body)
sign_pos = auth_str.index(encoded_sign, colon_pos + 1)
if sign_pos.nil? || ((sign_pos + encoded_sign.length) != auth_str.length) then
return false
end
return true
end # authenticate_callback_request
end # class << self end # class << self
end # module Auth end # module Auth
+1
View File
@@ -20,6 +20,7 @@ module Qiniu
:content_type => 'application/x-www-form-urlencoded', :content_type => 'application/x-www-form-urlencoded',
:auth_url => "https://acc.qbox.me/oauth2/token", :auth_url => "https://acc.qbox.me/oauth2/token",
:rs_host => "http://rs.qiniu.com", :rs_host => "http://rs.qiniu.com",
:fetch_host => "http://iovip.qbox.me",
:rsf_host => "http://rsf.qbox.me", :rsf_host => "http://rsf.qbox.me",
:up_host => "http://up.qiniu.com", :up_host => "http://up.qiniu.com",
:pub_host => "http://pu.qbox.me:10200", :pub_host => "http://pu.qbox.me:10200",
+2 -2
View File
@@ -65,7 +65,7 @@ module Qiniu
end end
content_type = resp_headers["content-type"][0] content_type = resp_headers["content-type"][0]
if !content_type.nil? && content_type == API_RESULT_MIMETYPE then if !content_type.nil? && !content_type.downcase.index(API_RESULT_MIMETYPE).nil? then
# 如果是JSON格式,则反序列化 # 如果是JSON格式,则反序列化
resp_body = Utils.safe_json_parse(resp_body) resp_body = Utils.safe_json_parse(resp_body)
end end
@@ -117,7 +117,7 @@ module Qiniu
end end
content_type = resp_headers["content-type"][0] content_type = resp_headers["content-type"][0]
if !content_type.nil? && content_type == API_RESULT_MIMETYPE then if !content_type.nil? && !content_type.downcase.index(API_RESULT_MIMETYPE).nil? then
# 如果是JSON格式,则反序列化 # 如果是JSON格式,则反序列化
resp_body = Utils.safe_json_parse(resp_body) resp_body = Utils.safe_json_parse(resp_body)
end end
+5
View File
@@ -82,6 +82,11 @@ module Qiniu
return HTTP.management_post(url) return HTTP.management_post(url)
end # delete end # delete
def fetch(bucket, target_url, key)
url = Config.settings[:fetch_host] + '/fetch/' + Utils.urlsafe_base64_encode(target_url) + '/to/' + encode_entry_uri(bucket, key)
return HTTP.management_post(url)
end # fetch
def batch(command, bucket, keys) def batch(command, bucket, keys)
execs = [] execs = []
keys.each do |key| keys.each do |key|
+13 -4
View File
@@ -49,13 +49,22 @@ module Qiniu
key = nil, key = nil,
x_vars = nil, x_vars = nil,
opts = {}) opts = {})
file = File.new(local_file, 'rb')
return upload_with_token_2_file_stream(uptoken, file, key, x_vars, opts)
end # upload_with_token_2
def upload_with_token_2_file_stream(uptoken,
file,
key = nil,
x_vars = nil,
opts = {})
### 构造URL ### 构造URL
url = Config.settings[:up_host] url = Config.settings[:up_host]
url[/\/*$/] = '' url[/\/*$/] = ''
url += '/' url += '/'
### 构造HTTP Body ### 构造HTTP Body
file = File.new(local_file, 'rb')
if not opts[:content_type].nil? if not opts[:content_type].nil?
file.define_singleton_method("content_type") do file.define_singleton_method("content_type") do
opts[:content_type] opts[:content_type]
@@ -63,8 +72,8 @@ module Qiniu
end end
post_data = { post_data = {
:file => file, :file => file,
:multipart => true, :multipart => true,
} }
if not uptoken.nil? if not uptoken.nil?
post_data[:token] = uptoken post_data[:token] = uptoken
@@ -78,7 +87,7 @@ module Qiniu
### 发送请求 ### 发送请求
HTTP.api_post(url, post_data) HTTP.api_post(url, post_data)
end # upload_with_token_2 end
### 授权举例 ### 授权举例
# put_policy.bucket | put_policy.key | key | 语义 | 授权 # put_policy.bucket | put_policy.key | key | 语义 | 授权
+1 -1
View File
@@ -4,7 +4,7 @@ module Qiniu
module Version module Version
MAJOR = 6 MAJOR = 6
MINOR = 5 MINOR = 5
PATCH = 0 PATCH = 1
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt> # Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
# #
# Example # Example
+20
View File
@@ -3,6 +3,7 @@
require 'spec_helper' require 'spec_helper'
require 'qiniu/auth' require 'qiniu/auth'
require 'qiniu/config'
require 'qiniu/storage' require 'qiniu/storage'
require 'digest/sha1' require 'digest/sha1'
@@ -69,6 +70,25 @@ module Qiniu
end end
end end
end end
### 测试回调签名
context ".authenticate_callback_request" do
it "should works" do
url = '/test.php'
body = 'name=xxx&size=1234'
false.should == Qiniu::Auth.authenticate_callback_request('ABCD', url, body)
false.should == Qiniu::Auth.authenticate_callback_request(Config.settings[:access_key], url, body)
false.should == Qiniu::Auth.authenticate_callback_request('QBox ' + Config.settings[:access_key] + ':', url, body)
false.should == Qiniu::Auth.authenticate_callback_request('QBox ' + Config.settings[:access_key] + ':????', url, body)
acctoken = Qiniu::Auth.generate_acctoken(url, body)
auth_str = 'QBox ' + acctoken
false.should == Qiniu::Auth.authenticate_callback_request(auth_str + ' ', url, body)
true.should == Qiniu::Auth.authenticate_callback_request(auth_str, url, body)
true.should == Qiniu::Auth.authenticate_callback_request(acctoken, url, body)
end
end
end # module Auth end # module Auth
module Exception_Auth module Exception_Auth
+7 -3
View File
@@ -59,14 +59,18 @@ module Qiniu
end end
end end
=begin
context ".exif" do context ".exif" do
it "should works" do it "should works" do
code, data = Qiniu::Fop::Image.exif(@source_image_url) result = Qiniu.get(@bucket, 'gogopher.jpg')
result["url"].should_not be_empty
puts result.inspect
code, data, headers = Qiniu::Fop::Image.exif(result["url"])
code.should == 200
puts data.inspect puts data.inspect
puts headers.inspect
end end
end end
=end
context ".mogrify_preview_url" do context ".mogrify_preview_url" do
it "should works" do it "should works" do