Rainbows! Rack HTTP server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: rainbows-public@bogomips.org
Subject: [PATCH 5/5] http_parser: workaround hijack changes in unicorn 5
Date: Sat, 14 Nov 2015 02:47:25 +0000	[thread overview]
Message-ID: <20151114024725.24139-6-e@80x24.org> (raw)
In-Reply-To: <20151114024725.24139-1-e@80x24.org>

unicorn lost the hijack_setup method in version 5,
so we must recreate it ourselves.
---
 lib/rainbows/coolio/client.rb        |  2 +-
 lib/rainbows/coolio/thread_client.rb |  2 +-
 lib/rainbows/epoll/client.rb         |  2 +-
 lib/rainbows/event_machine/client.rb |  2 +-
 lib/rainbows/http_parser.rb          |  9 +++++++++
 lib/rainbows/process_client.rb       |  4 ++--
 lib/rainbows/response.rb             | 16 +++-------------
 7 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/lib/rainbows/coolio/client.rb b/lib/rainbows/coolio/client.rb
index c05fd3a..ad827f6 100644
--- a/lib/rainbows/coolio/client.rb
+++ b/lib/rainbows/coolio/client.rb
@@ -125,7 +125,7 @@ class Rainbows::Coolio::Client < Coolio::IO
     @env['rack.input'] = input
     @env['REMOTE_ADDR'] = @_io.kgio_addr
     @env['async.callback'] = method(:write_async_response)
-    @hp.hijack_setup(@env, @_io)
+    @hp.hijack_setup(@_io)
     status, headers, body = catch(:async) {
       APP.call(@env.merge!(RACK_DEFAULTS))
     }
diff --git a/lib/rainbows/coolio/thread_client.rb b/lib/rainbows/coolio/thread_client.rb
index 4de705f..a3a2ebf 100644
--- a/lib/rainbows/coolio/thread_client.rb
+++ b/lib/rainbows/coolio/thread_client.rb
@@ -26,7 +26,7 @@ class Rainbows::Coolio::ThreadClient < Rainbows::Coolio::Client
   def app_response
     begin
       @env['REMOTE_ADDR'] = @_io.kgio_addr
-      @hp.hijack_setup(@env, @_io)
+      @hp.hijack_setup(@_io)
       APP.call(@env.merge!(RACK_DEFAULTS))
     rescue => e
       Rainbows::Error.app(e) # we guarantee this does not raise
diff --git a/lib/rainbows/epoll/client.rb b/lib/rainbows/epoll/client.rb
index 2d95a99..6dcbb81 100644
--- a/lib/rainbows/epoll/client.rb
+++ b/lib/rainbows/epoll/client.rb
@@ -65,7 +65,7 @@ module Rainbows::Epoll::Client
   def app_call input # called by on_read()
     @env['rack.input'] = input
     @env['REMOTE_ADDR'] = kgio_addr
-    @hp.hijack_setup(@env, self)
+    @hp.hijack_setup(self)
     status, headers, body = APP.call(@env.merge!(RACK_DEFAULTS))
     return hijacked if @hp.hijacked?
     ev_write_response(status, headers, body, @hp.next?)
diff --git a/lib/rainbows/event_machine/client.rb b/lib/rainbows/event_machine/client.rb
index 7fb88f6..039b7a6 100644
--- a/lib/rainbows/event_machine/client.rb
+++ b/lib/rainbows/event_machine/client.rb
@@ -38,7 +38,7 @@ class Rainbows::EventMachine::Client < EM::Connection
     @env['REMOTE_ADDR'] = @_io.kgio_addr
     @env['async.callback'] = method(:write_async_response)
     @env['async.close'] = EM::DefaultDeferrable.new
-    @hp.hijack_setup(@env, @_io)
+    @hp.hijack_setup(@_io)
     status, headers, body = catch(:async) {
       APP.call(@env.merge!(RACK_DEFAULTS))
     }
diff --git a/lib/rainbows/http_parser.rb b/lib/rainbows/http_parser.rb
index 30a67cb..bcf1dba 100644
--- a/lib/rainbows/http_parser.rb
+++ b/lib/rainbows/http_parser.rb
@@ -17,6 +17,15 @@ class Rainbows::HttpParser < Unicorn::HttpParser
     super
   end
 
+  def hijack_setup(io)
+    @hijack_io = io
+    env['rack.hijack'] = self # avoid allocating a new proc this way
+  end
+
+  def call # for rack.hijack
+    env['rack.hijack_io'] = @hijack_io
+  end
+
   def self.quit
     alias_method :next?, :never!
   end
diff --git a/lib/rainbows/process_client.rb b/lib/rainbows/process_client.rb
index 492b8a6..a39d6cd 100644
--- a/lib/rainbows/process_client.rb
+++ b/lib/rainbows/process_client.rb
@@ -39,7 +39,7 @@ module Rainbows::ProcessClient
 
       set_input(env, hp)
       env['REMOTE_ADDR'] = kgio_addr
-      hp.hijack_setup(env, to_io)
+      hp.hijack_setup(to_io)
       status, headers, body = APP.call(env.merge!(RACK_DEFAULTS))
 
       if 100 == status.to_i
@@ -72,7 +72,7 @@ module Rainbows::ProcessClient
     begin
       set_input(env, hp)
       env['REMOTE_ADDR'] = kgio_addr
-      hp.hijack_setup(env, to_io)
+      hp.hijack_setup(to_io)
       status, headers, body = APP.call(env.merge!(RACK_DEFAULTS))
       if 100 == status.to_i
         write("HTTP/1.1 100 Continue\r\n\r\n".freeze)
diff --git a/lib/rainbows/response.rb b/lib/rainbows/response.rb
index 2e8d2d8..0b5e542 100644
--- a/lib/rainbows/response.rb
+++ b/lib/rainbows/response.rb
@@ -15,18 +15,8 @@ module Rainbows::Response
       Rainbows::HttpParser.keepalive_requests = 0
   end
 
-  # Rack 1.5.0 (protocol version 1.2) adds response hijacking support
-  if ((Rack::VERSION[0] << 8) | Rack::VERSION[1]) >= 0x0102
-    def hijack_prepare(value)
-      value
-    end
-
-    def hijack_socket
-      @hp.env['rack.hijack'].call
-    end
-  else
-    def hijack_prepare(_)
-    end
+  def hijack_socket
+    @hp.env['rack.hijack'].call
   end
 
   # returns the original body on success
@@ -45,7 +35,7 @@ module Rainbows::Response
       when "rack.hijack"
         # this was an illegal key in Rack < 1.5, so it should be
         # OK to silently discard it for those older versions
-        hijack = hijack_prepare(value)
+        hijack = value
         alive = false # No persistent connections for hijacking
       else
         if /\n/ =~ value
-- 
EW


      parent reply	other threads:[~2015-11-14  2:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-14  2:47 [WIP 0/5] updates for unicorn 5 internal changes Eric Wong
2015-11-14  2:47 ` [PATCH 1/5] http_parser: handle keepalive_requests internally Eric Wong
2015-11-14  2:47 ` [PATCH 2/5] kill the moronic Status: header Eric Wong
2015-11-14  2:47 ` [PATCH 3/5] reflect changes in Rack::Utils::HTTP_STATUS_CODES Eric Wong
2015-11-14  2:47 ` [PATCH 4/5] reduce constant lookup dependencies Eric Wong
2015-11-14  2:47 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/rainbows/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151114024725.24139-6-e@80x24.org \
    --to=e@80x24.org \
    --cc=rainbows-public@bogomips.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/rainbows.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).