yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
* [PATCH] proxy_pass: support backends which rely on EOF to terminate
@ 2015-04-08  6:16 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-04-08  6:16 UTC (permalink / raw)
  To: yahns-public

Not all backends are capable of generating chunked responses
(especially not to HTTP/1.0 clients) nor can they generate
the Content-Length (especially when gzipping), so they'll
close the socket to signal EOF instead.
---
 lib/yahns/proxy_http_response.rb | 34 +++++++++++++++++++++++++++++++---
 test/test_proxy_pass.rb          | 29 +++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index cbfc17e..af8d8cc 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -106,7 +106,7 @@ module Yahns::HttpResponse # :nodoc:
           return :wait_readable # self remains in :ignore, wait on upstream
         end until len == 0
 
-      else # nasty chunked body
+      elsif kcar.chunked? # nasty chunked body
         req_res.proxy_trailers = nil # define to avoid warnings for now
         buf = ''
         case tmp = tip.shift || req_res.kgio_tryread(0x2000, rbuf)
@@ -137,6 +137,21 @@ module Yahns::HttpResponse # :nodoc:
           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
+          wbuf = proxy_write(wbuf, tmp, alive)
+        when nil
+          req_res.shutdown
+          break
+        when :wait_readable
+          # for ensure:
+          wbuf ||= Yahns::Wbuf.new(nil, alive, k.output_buffer_tmpdir, false)
+          return :wait_readable # self remains in :ignore, wait on upstream
+        end while true
+
       end
     end
 
@@ -163,7 +178,7 @@ module Yahns::HttpResponse # :nodoc:
         return :wait_readable # self remains in :ignore, wait on upstream
       end while len != 0
 
-    else # nasty chunked body
+    elsif kcar.chunked? # nasty chunked body
       buf = ''
 
       unless req_res.proxy_trailers
@@ -193,10 +208,23 @@ module Yahns::HttpResponse # :nodoc:
         end # no loop here
       end
       wbuf.wbuf_write(self, trailer_out(tlr))
+
+    else # no Content-Length or Transfer-Encoding: chunked, wait on EOF!
+
+      case tmp = req_res.kgio_tryread(0x2000, rbuf)
+      when String
+        wbuf.wbuf_write(self, tmp)
+      when nil
+        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)
+    proxy_busy_mod_done(wbuf.wbuf_persist) # returns nil
   end
 
   def proxy_busy_mod_done(alive)
diff --git a/test/test_proxy_pass.rb b/test/test_proxy_pass.rb
index e4e97d1..eb866b3 100644
--- a/test/test_proxy_pass.rb
+++ b/test/test_proxy_pass.rb
@@ -65,6 +65,16 @@ class TestProxyPass < Testcase
           io = env['rack.hijack'].call
           io.write(TRUNCATE_BODY)
           io.close
+        when '/eof-body-fast'
+          io = env['rack.hijack'].call
+          io.write("HTTP/1.0 200 OK\r\n\r\neof-body-fast")
+          io.close
+        when '/eof-body-slow'
+          io = env['rack.hijack'].call
+          io.write("HTTP/1.0 200 OK\r\n\r\n")
+          sleep 0.1
+          io.write("eof-body-slow")
+          io.close
         when '/truncate-head'
           io = env['rack.hijack'].call
           io.write(TRUNCATE_HEAD)
@@ -244,6 +254,7 @@ class TestProxyPass < Testcase
       end
     end
 
+    check_eof_body(host, port)
     check_pipelining(host, port)
     check_response_trailer(host, port)
 
@@ -531,4 +542,22 @@ class TestProxyPass < Testcase
     end
     thrs.each { |t| assert_equal(:OK, t.value) }
   end
+
+  def check_eof_body(host, port)
+    Timeout.timeout(60) do
+      s = TCPSocket.new(host, port)
+      s.write("GET /eof-body-fast HTTP/1.0\r\n\r\n")
+      res = s.read
+      assert_match %r{\AHTTP/1\.1 200 OK\r\n}, res
+      assert_match %r{\r\n\r\neof-body-fast\z}, res
+      s.close
+
+      s = TCPSocket.new(host, port)
+      s.write("GET /eof-body-slow HTTP/1.0\r\n\r\n")
+      res = s.read
+      assert_match %r{\AHTTP/1\.1 200 OK\r\n}, res
+      assert_match %r{\r\n\r\neof-body-slow\z}, res
+      s.close
+    end
+  end
 end
-- 
EW


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

only message in thread, other threads:[~2015-04-08  6:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-08  6:16 [PATCH] proxy_pass: support backends which rely on EOF to terminate Eric Wong

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