From 91e4f202d4db4ebcb9a4eb863d4598f3c0f73858 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 9 Jun 2015 22:15:25 +0000 Subject: ensure body is closed during hijack Middlewares such as Rack::Lock (used by Rails) break badly unless the response body is closed on hijack, so we will close it to follow the lead of other popular Rack servers. While it's unclear if there's anybody using rack.hijack besides yahns/proxy_pass we'll try to emulate the behavior of other servers as much as possible. ref: https://github.com/ngauthier/tubesock/issues/10 While we're at it, use DieIfUsed correctly in test_ssl.rb :x --- lib/yahns/http_client.rb | 14 ++++++++++---- lib/yahns/http_response.rb | 5 +++-- 2 files changed, 13 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/yahns/http_client.rb b/lib/yahns/http_client.rb index 7351171..0c656e8 100644 --- a/lib/yahns/http_client.rb +++ b/lib/yahns/http_client.rb @@ -193,9 +193,9 @@ class Yahns::HttpClient < Kgio::Socket # :nodoc: mkinput_preread # keep looping (@state == :body) true else # :lazy, false - r = k.app.call(env = @hs.env) - return :ignore if env.include?(RACK_HIJACK_IO) - http_response_write(*r) + status, headers, body = k.app.call(env = @hs.env) + return :ignore if app_hijacked?(env, body) + http_response_write(status, headers, body) end end @@ -217,7 +217,7 @@ class Yahns::HttpClient < Kgio::Socket # :nodoc: # run the rack app status, headers, body = k.app.call(env.merge!(k.app_defaults)) - return :ignore if env.include?(RACK_HIJACK_IO) + return :ignore if app_hijacked?(env, body) if status.to_i == 100 rv = http_100_response(env) and return rv status, headers, body = k.app.call(env) @@ -298,4 +298,10 @@ class Yahns::HttpClient < Kgio::Socket # :nodoc: shutdown rescue nil return # always drop the connection on uncaught errors end + + def app_hijacked?(env, body) + return false unless env.include?(RACK_HIJACK_IO) + body.close if body.respond_to?(:close) + true + end end diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb index 1be28bc..fabd4b7 100644 --- a/lib/yahns/http_response.rb +++ b/lib/yahns/http_response.rb @@ -69,7 +69,9 @@ module Yahns::HttpResponse # :nodoc: end wbuf = Yahns::Wbuf.new(body, alive, self.class.output_buffer_tmpdir, ret) rv = wbuf.wbuf_write(self, header) - body.each { |chunk| rv = wbuf.wbuf_write(self, chunk) } if body + if body && ! alive.respond_to?(:call) # skip body.each if hijacked + body.each { |chunk| rv = wbuf.wbuf_write(self, chunk) } + end wbuf_maybe(wbuf, rv) end @@ -155,7 +157,6 @@ module Yahns::HttpResponse # :nodoc: buf << kv_str(key, value) when "rack.hijack" hijack = value - body = nil # ensure we do not close body else buf << kv_str(key, value) end -- cgit v1.2.3-24-ge0c7