yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH 2/2] proxy_pass: send 1.0 requests to upstreams for 1.0 clients
Date: Tue,  7 Apr 2015 03:16:58 +0000	[thread overview]
Message-ID: <1428376618-13665-3-git-send-email-e@80x24.org> (raw)
In-Reply-To: <1428376618-13665-1-git-send-email-e@80x24.org>

We cannot pass trailers from upstreams to HTTP/1.0 clients without
fully-buffering the response body AND trailers before forwarding the
response header.

Of course, one of the reasons yahns exists is to support lazy
buffering, so fully-buffering up front is wasteful and hurts
latency.  So instead, degrade to 1.0 requests to upstreams for
HTTP/1.0 clients, this should prevent upstreams from sending
trailers in the first place.

HTTP/1.0 clients on Rails apps may suffer, but there probably
are not too many HTTP/1.0 clients out there.
---
 lib/yahns/proxy_http_response.rb | 79 ++++++++++++----------------------------
 lib/yahns/proxy_pass.rb          |  8 +++-
 test/test_proxy_pass.rb          | 22 ++++++++---
 3 files changed, 47 insertions(+), 62 deletions(-)

diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index fe0a37b..cbfc17e 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -58,7 +58,6 @@ module Yahns::HttpResponse # :nodoc:
     have_body = !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(si) &&
                 env[REQUEST_METHOD] != HEAD
     flags = MSG_DONTWAIT
-    rechunk = false
     k = self.class
     alive = @hs.next? && k.persistent_connections
 
@@ -69,18 +68,6 @@ module Yahns::HttpResponse # :nodoc:
         next # do not let some upstream headers leak through
       when %r{\AContent-Length\z}i
         flags |= MSG_MORE if have_body && value.to_i > 0
-      when %r{\ATransfer-Encoding\z}i
-        if value =~ /\bchunked\b/i
-          case env['HTTP_VERSION'] # this is the original client request
-          when 'HTTP/1.1'
-            rechunk = true
-          else
-            # too expensive to calculate Content-Length for HTTP/1.0
-            # persistent clients, so we will drop persistence instead
-            alive = false # this should already be implied
-            next
-          end
-        end
       end
 
       res << "#{key}: #{value}\r\n"
@@ -121,18 +108,11 @@ module Yahns::HttpResponse # :nodoc:
 
       else # nasty chunked body
         req_res.proxy_trailers = nil # define to avoid warnings for now
-
-        # Only HTTP/1.1 supports chunked responses, we must translate
-        # otherwise.  Otherwise, we must drop the connection to signal
-        # the end.  We only send HTTP/1.1 requests to the upstream so
-        # Rack/Rails can always send streaming responses.
         buf = ''
         case tmp = tip.shift || req_res.kgio_tryread(0x2000, rbuf)
         when String
           kcar.filter_body(buf, tmp)
-          unless buf.empty?
-            wbuf = proxy_write(wbuf, rechunk ? chunk_out(buf) : buf, alive)
-          end
+          wbuf = proxy_write(wbuf, chunk_out(buf), alive) unless buf.empty?
         when nil # premature EOF
           return proxy_err_response(nil, req_res, nil, wbuf)
         when :wait_readable
@@ -144,22 +124,19 @@ module Yahns::HttpResponse # :nodoc:
         buf = tmp
         req_res.proxy_trailers = [ buf, tlr = [] ]
         rbuf = Thread.current[:yahns_rbuf] = ''
-        if rechunk
-          until kcar.trailers(tlr, buf)
-            case rv = req_res.kgio_tryread(0x2000, rbuf)
-            when String
-              buf << rv
-            when :wait_readable
-              # for ensure:
-              wbuf ||= Yahns::Wbuf.new(nil, alive, k.output_buffer_tmpdir,
-                                       false)
-              return :wait_readable
-            when nil # premature EOF
-              return proxy_err_response(nil, req_res, nil, wbuf)
-            end # no loop here
-          end
-          wbuf = proxy_write(wbuf, trailer_out(tlr), alive)
+        until kcar.trailers(tlr, buf)
+          case rv = req_res.kgio_tryread(0x2000, rbuf)
+          when String
+            buf << rv
+          when :wait_readable
+            # for ensure:
+            wbuf ||= Yahns::Wbuf.new(nil, alive, k.output_buffer_tmpdir, false)
+            return :wait_readable
+          when nil # premature EOF
+            return proxy_err_response(nil, req_res, nil, wbuf)
+          end # no loop here
         end
+        wbuf = proxy_write(wbuf, trailer_out(tlr), alive)
       end
     end
 
@@ -187,12 +164,6 @@ module Yahns::HttpResponse # :nodoc:
       end while len != 0
 
     else # nasty chunked body
-
-      # Only HTTP/1.1 supports chunked responses, we must translate
-      # otherwise.  Otherwise, we must drop the connection to signal
-      # the end.  We only send HTTP/1.1 requests to the upstream so
-      # Rack/Rails can always send streaming responses.
-      rechunk = @hs.env['HTTP_VERSION'] == 'HTTP/1.1'.freeze
       buf = ''
 
       unless req_res.proxy_trailers
@@ -200,7 +171,7 @@ module Yahns::HttpResponse # :nodoc:
         case tmp = req_res.kgio_tryread(0x2000, rbuf)
         when String
           kcar.filter_body(buf, tmp)
-          buf.empty? or wbuf.wbuf_write(self, rechunk ? chunk_out(buf) : buf)
+          buf.empty? or wbuf.wbuf_write(self, chunk_out(buf))
         when nil # premature EOF
           return proxy_err_response(nil, req_res, nil, wbuf)
         when :wait_readable
@@ -211,19 +182,17 @@ module Yahns::HttpResponse # :nodoc:
       end
 
       buf, tlr = *req_res.proxy_trailers
-      if rechunk
-        until kcar.trailers(tlr, buf)
-          case rv = req_res.kgio_tryread(0x2000, rbuf)
-          when String
-            buf << rv
-          when :wait_readable
-            return :wait_readable
-          when nil # premature EOF
-            return proxy_err_response(nil, req_res, nil, wbuf)
-          end # no loop here
-        end
-        wbuf.wbuf_write(self, trailer_out(tlr))
+      until kcar.trailers(tlr, buf)
+        case rv = req_res.kgio_tryread(0x2000, rbuf)
+        when String
+          buf << rv
+        when :wait_readable
+          return :wait_readable
+        when nil # premature EOF
+          return proxy_err_response(nil, req_res, nil, wbuf)
+        end # no loop here
       end
+      wbuf.wbuf_write(self, trailer_out(tlr))
     end
 
     busy = wbuf.busy and return proxy_busy_mod_blocked(wbuf, busy)
diff --git a/lib/yahns/proxy_pass.rb b/lib/yahns/proxy_pass.rb
index 3812dbf..e3ba7f0 100644
--- a/lib/yahns/proxy_pass.rb
+++ b/lib/yahns/proxy_pass.rb
@@ -205,7 +205,13 @@ class Yahns::ProxyPass # :nodoc:
     req = @path.gsub(/\$(\w+)/) { req.__send__($1) }
 
     # start the connection asynchronously and early so TCP can do a
-    req = "#{env['REQUEST_METHOD']} #{req} HTTP/1.1\r\n" \
+    case ver = env['HTTP_VERSION']
+    when 'HTTP/1.1' # leave alone, response may be chunked
+    else # no chunking for HTTP/1.0 and HTTP/0.9
+      ver = 'HTTP/1.0'.freeze
+    end
+
+    req = "#{env['REQUEST_METHOD']} #{req} #{ver}\r\n" \
           "X-Forwarded-For: #{env["REMOTE_ADDR"]}\r\n"
 
     # pass most HTTP_* headers through as-is
diff --git a/test/test_proxy_pass.rb b/test/test_proxy_pass.rb
index 943fb35..c1539f8 100644
--- a/test/test_proxy_pass.rb
+++ b/test/test_proxy_pass.rb
@@ -86,13 +86,23 @@ class TestProxyPass < Testcase
           delay = $1.to_f
           chunky = Object.new
           chunky.instance_variable_set(:@delay, delay)
-          def chunky.each
-            sleep @delay
-            yield "3\r\nHI!\r\n"
-            sleep @delay
-            yield "0\r\n\r\n"
+          if env['HTTP_VERSION'] == 'HTTP/1.0'
+            h = [ %w(Content-Type text/pain), %w(Content-Length 3) ]
+            def chunky.each
+              %w(H I !).each do |x|
+                sleep @delay
+                yield x
+              end
+            end
+          else
+            h = [ %w(Content-Type text/pain), %w(Transfer-Encoding chunked) ]
+            def chunky.each
+              sleep @delay
+              yield "3\r\nHI!\r\n"
+              sleep @delay
+              yield "0\r\n\r\n"
+            end
           end
-          h = [ %w(Content-Type text/pain), %w(Transfer-Encoding chunked) ]
           [ 200, h, chunky ]
         else
           [ 200, h, [ "hi\n"] ]
-- 
EW


      parent reply	other threads:[~2015-04-07  3:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-07  3:16 [PATCH 0/2] proxy_pass: fleshing out trailer support Eric Wong
2015-04-07  3:16 ` [PATCH 1/2] proxy_pass: preliminary support for passing trailers Eric Wong
2015-04-07  3:16 ` 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/yahns/README

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

  git send-email \
    --in-reply-to=1428376618-13665-3-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=yahns-public@yhbt.net \
    /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/yahns.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).