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 4/7] proxy_pass: split out body and trailer reading in response
Date: Mon, 16 May 2016 01:43:37 +0000	[thread overview]
Message-ID: <20160516014340.8258-5-e@80x24.org> (raw)
In-Reply-To: <20160516014340.8258-1-e@80x24.org>

Hopefully this increases readability as well and allows
us to make easier adjustments to support new features in
the future.
---
 lib/yahns/proxy_http_response.rb | 105 +++++++++++++++++++++------------------
 1 file changed, 58 insertions(+), 47 deletions(-)

diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index 00915df..52a8aff 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -124,63 +124,74 @@ def proxy_res_headers(res)
     [ alive, wbuf, have_body ]
   end
 
+  def proxy_read_body(tip, kcar, req_res, alive, wbuf)
+    chunk = ''.dup if kcar.chunked?
+    len = kcar.body_bytes_left
+    rbuf = Thread.current[:yahns_rbuf]
+
+    case tmp = tip.shift || req_res.kgio_tryread(0x2000, rbuf)
+    when String
+      if len
+        kcar.body_bytes_left -= tmp.size # progress for body_eof? => true
+      elsif chunk
+        kcar.filter_body(chunk, rbuf = tmp) # progress for body_eof? => true
+        next if chunk.empty? # call req_res.kgio_tryread for more
+        tmp = chunk_out(chunk)
+      elsif alive # HTTP/1.0 upstream, HTTP/1.1 client
+        tmp = chunk_out(tmp)
+      # else # HTTP/1.0 upstream, HTTP/1.0 client, do nothing
+      end
+      wbuf = proxy_write(wbuf, tmp, alive)
+      chunk.clear if chunk
+    when nil # EOF
+      # HTTP/1.1 upstream, unexpected premature EOF:
+      return proxy_err_response(nil, req_res, nil, wbuf) if len || chunk
+
+      # HTTP/1.0 upstream:
+      wbuf = proxy_write(wbuf, "0\r\n\r\n".freeze, true) if alive
+      req_res.shutdown
+      break
+    when :wait_readable
+      return wait_on_upstream(req_res, alive, wbuf)
+    end until kcar.body_eof?
+
+    if chunk
+      # tip is an empty array and becomes trailer storage
+      req_res.proxy_trailers = [ rbuf.dup, tip ]
+      return proxy_read_trailers(kcar, req_res, alive, wbuf)
+    end
+    wbuf ? proxy_busy_mod_blocked(wbuf, wbuf.busy) : proxy_busy_mod_done(alive)
+  end
+
+  def proxy_read_trailers(kcar, req_res, alive, wbuf)
+    chunk, tlr = req_res.proxy_trailers
+    rbuf = Thread.current[:yahns_rbuf]
+
+    until kcar.trailers(tlr, chunk)
+      case rv = req_res.kgio_tryread(0x2000, rbuf)
+      when String
+        chunk << rv
+      when :wait_readable
+        return wait_on_upstream(req_res, alive, wbuf)
+      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)
+    wbuf ? proxy_busy_mod_blocked(wbuf, wbuf.busy) : proxy_busy_mod_done(alive)
+  end
+
   # start streaming the response once upstream is done sending headers to us.
   # returns :wait_readable if we need to read more from req_res
   # returns :ignore if we yield control to the client(self)
   # returns nil if completely done
   def proxy_response_start(res, tip, kcar, req_res)
     alive, wbuf, have_body = proxy_res_headers(res)
-    rbuf = Thread.current[:yahns_rbuf]
     tip = tip.empty? ? [] : [ tip ]
 
     if have_body
       req_res.proxy_trailers = nil # define to avoid uninitialized warnings
-      chunk = ''.dup if kcar.chunked?
-      tlr = nil
-      len = kcar.body_bytes_left
-
-      case tmp = tip.shift || req_res.kgio_tryread(0x2000, rbuf)
-      when String
-        if len
-          kcar.body_bytes_left -= tmp.size # progress for body_eof? => true
-        elsif chunk
-          kcar.filter_body(chunk, rbuf = tmp) # progress for body_eof? => true
-          next if chunk.empty? # read req_res.kgio_tryread for more
-          tmp = chunk_out(chunk)
-        elsif alive # HTTP/1.0 upstream, HTTP/1.1 client
-          tmp = chunk_out(tmp)
-        # else # HTTP/1.0 upstream, HTTP/1.0 client, do nothing
-        end
-        wbuf = proxy_write(wbuf, tmp, alive)
-        chunk.clear if chunk
-      when nil # EOF
-        # HTTP/1.1 upstream, unexpected premature EOF:
-        return proxy_err_response(nil, req_res, nil, wbuf) if len || chunk
-
-        # HTTP/1.0 upstream:
-        wbuf = proxy_write(wbuf, "0\r\n\r\n".freeze, true) if alive
-        req_res.shutdown
-        break
-      when :wait_readable
-        return wait_on_upstream(req_res, alive, wbuf)
-      end until kcar.body_eof?
-
-      if chunk
-        chunk = rbuf
-        req_res.proxy_trailers = [ chunk, tlr = [] ]
-        rbuf = Thread.current[:yahns_rbuf] = ''.dup
-        until kcar.trailers(tlr, chunk)
-          case rv = req_res.kgio_tryread(0x2000, rbuf)
-          when String
-            chunk << rv
-          when :wait_readable
-            return wait_on_upstream(req_res, alive, wbuf)
-          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
+      return proxy_read_body(tip, kcar, req_res, alive, wbuf)
     end
 
     # all done reading response from upstream, req_res will be discarded

  parent reply	other threads:[~2016-05-16  1:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-16  1:43 [PATCH 0/7] proxy_pass cleanups Eric Wong
2016-05-16  1:43 ` [PATCH 1/7] proxy_pass: simplify writing request bodies upstream Eric Wong
2016-05-16  1:43 ` [PATCH 2/7] proxy_pass: hoist out proxy_res_headers method Eric Wong
2016-05-16  1:43 ` [PATCH 3/7] proxy_pass: simplify proxy_http_response Eric Wong
2016-05-16  1:43 ` Eric Wong [this message]
2016-05-16  1:43 ` [PATCH 5/7] proxy_pass: trim down proxy_response_finish, too Eric Wong
2016-05-16  1:43 ` [PATCH 6/7] proxy_pass: split out req_res into a separate file Eric Wong
2016-05-16  1:43 ` [PATCH 7/7] proxy_pass: fix resumes after complete buffering is unblocked Eric Wong
2016-05-16  2:05 ` false-positive spam [Re: [PATCH 0/7] proxy_pass cleanups] Eric Wong

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=20160516014340.8258-5-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).