yahns Ruby server user/dev discussion
 help / color / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH 5/7] proxy_pass: trim down proxy_response_finish, too
Date: Mon, 16 May 2016 01:43:38 +0000
Message-ID: <20160516014340.8258-6-e@80x24.org> (raw)
In-Reply-To: <20160516014340.8258-1-e@80x24.org>

After the previous commits, we've added more branches
and made the existing response handling more generic;
so we can remove some duplicated logic and increase
review-ability.
---
 lib/yahns/proxy_http_response.rb | 66 ++--------------------------------------
 1 file changed, 3 insertions(+), 63 deletions(-)

diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index 52a8aff..65fd03b 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -202,69 +202,9 @@ def proxy_response_start(res, tip, kcar, req_res)
   end
 
   def proxy_response_finish(kcar, wbuf, req_res)
-    rbuf = Thread.current[:yahns_rbuf]
-    if len = kcar.body_bytes_left # known Content-Length
-
-      case tmp = req_res.kgio_tryread(0x2000, rbuf)
-      when String
-        len = kcar.body_bytes_left -= tmp.size
-        wbuf.wbuf_write(self, tmp)
-      when nil # premature EOF
-        return proxy_err_response(nil, req_res, nil, wbuf)
-      when :wait_readable
-        return :wait_readable # self remains in :ignore, wait on upstream
-      end while len != 0
-
-    elsif kcar.chunked? # nasty chunked response body
-      buf = ''.dup
-
-      unless req_res.proxy_trailers
-        # are we done dechunking the main body, yet?
-        case tmp = req_res.kgio_tryread(0x2000, rbuf)
-        when String
-          kcar.filter_body(buf, tmp)
-          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
-          return :wait_readable # self remains in :ignore, wait on upstream
-        end until kcar.body_eof?
-        req_res.proxy_trailers = [ tmp, [] ] # onto trailers!
-        rbuf = Thread.current[:yahns_rbuf] = ''.dup
-      end
-
-      buf, tlr = *req_res.proxy_trailers
-      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))
-
-    else # no Content-Length or Transfer-Encoding: chunked, wait on EOF!
-
-      alive = wbuf.wbuf_persist
-      case tmp = req_res.kgio_tryread(0x2000, rbuf)
-      when String
-        tmp = chunk_out(tmp) if alive
-        wbuf.wbuf_write(self, tmp)
-      when nil
-        wbuf.wbuf_write(self, "0\r\n\r\n".freeze) if alive
-        req_res.shutdown
-        break
-      when :wait_readable
-        return :wait_readable # self remains in :ignore, wait on upstream
-      end while true
-
-    end
-
-    busy = wbuf.busy and return proxy_busy_mod_blocked(wbuf, busy)
-    proxy_busy_mod_done(wbuf.wbuf_persist) # returns nil to close req_res
+    alive = wbuf.wbuf_persist
+    req_res.proxy_trailers ? proxy_read_trailers(kcar, req_res, alive, wbuf)
+                           : proxy_read_body([], kcar, req_res, alive, wbuf)
   end
 
   def proxy_wait_next(qflags)

  parent reply index

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 ` [PATCH 4/7] proxy_pass: split out body and trailer reading in response Eric Wong
2016-05-16  1:43 ` Eric Wong [this message]
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 publically 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-6-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

yahns Ruby server user/dev discussion

Archives are clonable:
	git clone --mirror https://yhbt.net/yahns-public
	git clone --mirror http://ou63pmih66umazou.onion/yahns-public

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.yahns
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.yahns

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox