From 19d07496e6c2f7dac9322b805841543ac009f45f Mon Sep 17 00:00:00 2001 From: Kyle King Date: Sat, 5 Mar 2016 00:32:02 -0700 Subject: [PATCH 1/4] fixes #312 --- lib/http/content_type.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/http/content_type.rb b/lib/http/content_type.rb index 20e44571..89bf56b3 100644 --- a/lib/http/content_type.rb +++ b/lib/http/content_type.rb @@ -20,7 +20,8 @@ def mime_type(str) # :nodoc: def charset(str) md = str.to_s.match CHARSET_RE - md && md[1].to_s.strip.gsub(/^"|"$/, "") + md = md && md[1].to_s.strip.gsub(/^"|"$/, "") + Encoding.find md rescue nil end end end From e5d4d1c84157da8b85a6b0fd5f79ccb47a090b3b Mon Sep 17 00:00:00 2001 From: Kyle King Date: Sun, 6 Mar 2016 16:25:52 -0700 Subject: [PATCH 2/4] keep charset a string --- lib/http/content_type.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/http/content_type.rb b/lib/http/content_type.rb index 89bf56b3..981e5d24 100644 --- a/lib/http/content_type.rb +++ b/lib/http/content_type.rb @@ -21,7 +21,7 @@ def mime_type(str) def charset(str) md = str.to_s.match CHARSET_RE md = md && md[1].to_s.strip.gsub(/^"|"$/, "") - Encoding.find md rescue nil + md if Encoding.find(md) rescue nil end end end From a99fe2c75c4478912d34972d0dc4244d37fb2e89 Mon Sep 17 00:00:00 2001 From: Kyle King Date: Sun, 6 Mar 2016 19:08:41 -0700 Subject: [PATCH 3/4] fix rubocop offenses --- lib/http/content_type.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/http/content_type.rb b/lib/http/content_type.rb index 981e5d24..e6470d87 100644 --- a/lib/http/content_type.rb +++ b/lib/http/content_type.rb @@ -20,8 +20,13 @@ def mime_type(str) # :nodoc: def charset(str) md = str.to_s.match CHARSET_RE - md = md && md[1].to_s.strip.gsub(/^"|"$/, "") - md if Encoding.find(md) rescue nil + md &&= md[1].to_s.strip.gsub(/^"|"$/, "") + + begin + md if Encoding.find(md) + rescue ArgumentError + nil + end end end end From 2951f2c09954b4bf267636a4348701669c2e13d9 Mon Sep 17 00:00:00 2001 From: Kyle King Date: Sun, 6 Mar 2016 20:19:26 -0700 Subject: [PATCH 4/4] move encoding check to response body --- lib/http/content_type.rb | 8 +------- lib/http/response/body.rb | 11 +++++++++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/http/content_type.rb b/lib/http/content_type.rb index e6470d87..20e44571 100644 --- a/lib/http/content_type.rb +++ b/lib/http/content_type.rb @@ -20,13 +20,7 @@ def mime_type(str) # :nodoc: def charset(str) md = str.to_s.match CHARSET_RE - md &&= md[1].to_s.strip.gsub(/^"|"$/, "") - - begin - md if Encoding.find(md) - rescue ArgumentError - nil - end + md && md[1].to_s.strip.gsub(/^"|"$/, "") end end end diff --git a/lib/http/response/body.rb b/lib/http/response/body.rb index 7dd97673..089be974 100644 --- a/lib/http/response/body.rb +++ b/lib/http/response/body.rb @@ -35,11 +35,18 @@ def to_s fail StateError, "body is being streamed" unless @streaming.nil? + # see issue 312 + begin + encoding = Encoding.find @encoding + rescue ArgumentError + encoding = Encoding::BINARY + end + begin @streaming = false - @contents = "".force_encoding(@encoding) + @contents = "".force_encoding(encoding) while (chunk = @client.readpartial) - @contents << chunk.force_encoding(@encoding) + @contents << chunk.force_encoding(encoding) end rescue @contents = nil