From 2d542310bdb7fcade1439d0f3de108d6db8dc616 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 6 May 2016 01:04:35 +0000 Subject: proxy_pass: trim down proxy_response_finish, too 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(-) (limited to 'lib') 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 @@ module Yahns::HttpResponse # :nodoc: 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) -- cgit v1.2.3-24-ge0c7