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] proxy_pass: support backends which rely on EOF to terminate
Date: Wed,  8 Apr 2015 06:16:49 +0000	[thread overview]
Message-ID: <1428473809-23316-1-git-send-email-e@80x24.org> (raw)

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


                 reply	other threads:[~2015-04-08  6:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1428473809-23316-1-git-send-email-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).