about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-06-09 22:15:25 +0000
committerEric Wong <e@80x24.org>2015-06-10 09:20:31 +0000
commit91e4f202d4db4ebcb9a4eb863d4598f3c0f73858 (patch)
tree1090daac95fc83d76afbc1c5aedc26d2a3b41b59 /lib
parent8e5d64502e478c7e93d7168628d97f66417fcabe (diff)
downloadyahns-91e4f202d4db4ebcb9a4eb863d4598f3c0f73858.tar.gz
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
Diffstat (limited to 'lib')
-rw-r--r--lib/yahns/http_client.rb14
-rw-r--r--lib/yahns/http_response.rb5
2 files changed, 13 insertions, 6 deletions
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