From 247793793d93793009bfe9f10ac6a5b55fc8e6a3 Mon Sep 17 00:00:00 2001 From: Tianlong Wu Date: Fri, 25 Dec 2015 17:24:24 +0800 Subject: [PATCH 1/3] fix the issue that StreamWriter will read more bytes than wanted --- lib/aliyun/oss/http.rb | 9 +++++++-- spec/aliyun/oss/http_spec.rb | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/aliyun/oss/http.rb b/lib/aliyun/oss/http.rb index 4fe2496..c46e263 100644 --- a/lib/aliyun/oss/http.rb +++ b/lib/aliyun/oss/http.rb @@ -52,8 +52,13 @@ def read(bytes = nil, outbuf = nil) ret = "" loop do if bytes - ret << @buffer.slice!(0, bytes) - break if ret.size >= bytes + piece = @buffer.slice!(0, bytes) + if piece + ret << piece + bytes -= piece.size + end + fail if bytes < 0 + break if bytes == 0 else ret << @buffer @buffer.clear diff --git a/spec/aliyun/oss/http_spec.rb b/spec/aliyun/oss/http_spec.rb index 923e764..4452f21 100644 --- a/spec/aliyun/oss/http_spec.rb +++ b/spec/aliyun/oss/http_spec.rb @@ -58,6 +58,24 @@ module OSS r = s.read expect(r).to eq('') end + + it "should read exactly bytes" do + s = HTTP::StreamWriter.new do |sr| + 100.times { sr << 'x' * 10 } + end + + r = s.read(11) + expect(r.size).to eq(11) + + r = s.read(25) + expect(r.size).to eq(25) + + r = s.read(900) + expect(r.size).to eq(900) + + r = s.read(1000) + expect(r.size).to eq(64) + end end # StreamWriter end # HTTP From 83e30af77237d003c44faecb8ce7910f2f329530 Mon Sep 17 00:00:00 2001 From: Tianlong Wu Date: Fri, 25 Dec 2015 17:25:07 +0800 Subject: [PATCH 2/3] Version => 0.3.5 --- CHANGELOG.md | 4 ++++ lib/aliyun/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5bc7ae..480a441 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Change Log +### v0.3.5 + +- Fix the issue that StreamWriter will read more bytes than wanted + ### v0.3.4 - Fix handling gzip/deflate response diff --git a/lib/aliyun/version.rb b/lib/aliyun/version.rb index 046e387..39f855e 100644 --- a/lib/aliyun/version.rb +++ b/lib/aliyun/version.rb @@ -2,6 +2,6 @@ module Aliyun - VERSION = "0.3.4" + VERSION = "0.3.5" end # Aliyun From 08060cac0f0efb340f0f76d1f4de0557c969cb01 Mon Sep 17 00:00:00 2001 From: Tianlong Wu Date: Fri, 25 Dec 2015 17:32:26 +0800 Subject: [PATCH 3/3] code refine --- lib/aliyun/oss/http.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/aliyun/oss/http.rb b/lib/aliyun/oss/http.rb index c46e263..79271b8 100644 --- a/lib/aliyun/oss/http.rb +++ b/lib/aliyun/oss/http.rb @@ -52,13 +52,13 @@ def read(bytes = nil, outbuf = nil) ret = "" loop do if bytes + fail if bytes < 0 piece = @buffer.slice!(0, bytes) if piece ret << piece bytes -= piece.size + break if bytes == 0 end - fail if bytes < 0 - break if bytes == 0 else ret << @buffer @buffer.clear