dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] proxy_pass: WIP: simplify proxy_http_response
@ 2016-05-04  3:50 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2016-05-04  3:50 UTC (permalink / raw)
  To: spew

---
 lib/yahns/proxy_http_response.rb | 76 ++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 42 deletions(-)

diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index 731bcd6..2e4dc5e 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -131,38 +131,45 @@ def proxy_response_start(res, tip, kcar, req_res)
     tip = tip.empty? ? [] : [ tip ]
 
     if have_body
-      if len = kcar.body_bytes_left
+      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
-          len = kcar.body_bytes_left -= tmp.size
-          wbuf = proxy_write(wbuf, tmp, alive)
-        when nil # premature EOF
-          return proxy_err_response(nil, req_res, nil, wbuf)
-        when :wait_readable
-          return wait_on_upstream(req_res, alive, wbuf)
-        end until len == 0
-
-      elsif kcar.chunked? # nasty chunked body
-        req_res.proxy_trailers = nil # define to avoid warnings for now
-        buf = ''.dup
-        case tmp = tip.shift || req_res.kgio_tryread(0x2000, rbuf)
-        when String
-          kcar.filter_body(buf, tmp)
-          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
-          return wait_on_upstream(req_res, alive, wbuf)
-        end until kcar.body_eof?
+      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?
 
-        buf = tmp
-        req_res.proxy_trailers = [ buf, tlr = [] ]
+      if chunk
+        chunk = rbuf
+        req_res.proxy_trailers = [ chunk, tlr = [] ]
         rbuf = Thread.current[:yahns_rbuf] = ''.dup
-        until kcar.trailers(tlr, buf)
+        until kcar.trailers(tlr, chunk)
           case rv = req_res.kgio_tryread(0x2000, rbuf)
           when String
-            buf << rv
+            chunk << rv
           when :wait_readable
             return wait_on_upstream(req_res, alive, wbuf)
           when nil # premature EOF
@@ -170,21 +177,6 @@ def proxy_response_start(res, tip, kcar, req_res)
           end # no loop here
         end
         wbuf = proxy_write(wbuf, trailer_out(tlr), alive)
-
-      else # no Content-Length or Transfer-Encoding: chunked, wait on EOF!
-
-        case tmp = tip.shift || req_res.kgio_tryread(0x2000, rbuf)
-        when String
-          tmp = chunk_out(tmp) if alive
-          wbuf = proxy_write(wbuf, tmp, alive)
-        when nil
-          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 while true
-
       end
     end
 

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-05-04  3:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-04  3:50 [PATCH] proxy_pass: WIP: simplify proxy_http_response Eric Wong

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).