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 1/2] response: do not set chunked header on bodyless responses
Date: Wed, 14 Dec 2016 06:59:51 +0000	[thread overview]
Message-ID: <20161214065952.28950-2-e@80x24.org> (raw)
In-Reply-To: <20161214065952.28950-1-e@80x24.org>

Setting "Transfer-Encoding: chunked" on responses will confuse
clients which see a 204 response and do not expect a body.
This follows Rack::Chunked behavior, as yahns should function
without Rack::Chunked middleware.

This regression appeared in yahns v1.13.0 (2016-08-05)
---
 lib/yahns/http_response.rb |  4 ++--
 test/test_auto_chunk.rb    | 22 +++++++++++++++++++++-
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb
index a31ab70..bde4b3f 100644
--- a/lib/yahns/http_response.rb
+++ b/lib/yahns/http_response.rb
@@ -165,12 +165,12 @@ def http_response_write(res, opt)
     end
     count ||= clen
 
-    if !term && chunk_ok
+    if !term && chunk_ok && !hdr_only
       term = true
       body = Yahns::ChunkBody.new(body, opt)
       buf << "Transfer-Encoding: chunked\r\n".freeze
     end
-    alive &&= term
+    alive &&= (term || hdr_only)
     buf << (alive ? "Connection: keep-alive\r\n\r\n".freeze
                   : "Connection: close\r\n\r\n".freeze)
     case rv = kgio_syssend(buf, flags)
diff --git a/test/test_auto_chunk.rb b/test/test_auto_chunk.rb
index a97fe26..e1847f6 100644
--- a/test/test_auto_chunk.rb
+++ b/test/test_auto_chunk.rb
@@ -18,7 +18,12 @@ def test_auto_head
         app = Rack::Builder.new do
           use Rack::ContentType, "text/plain"
           run(lambda do |env|
-            [ 200, {}, %w(a b c) ]
+            case env['PATH_INFO']
+            when '/204'
+              [ 204, {}, [] ]
+            else
+              [ 200, {}, %w(a b c) ]
+            end
           end)
         end
         app(:rack, app) { listen "#{host}:#{port}" }
@@ -50,6 +55,21 @@ def test_auto_head
       assert_equal 200, res.code.to_i
       assert_equal 'abc', res.body
     end
+
+    s = TCPSocket.new(host, port)
+    s.write("GET /204 HTTP/1.1\r\nHost: example.com\r\n\r\n")
+    buf = s.readpartial(1024)
+    assert_match %r{\r\n\r\n\z}, buf
+    refute_match %r{^Transfer-Encoding}i, buf
+    assert_match %r{^Connection: keep-alive\r\n}, buf
+    assert_nil IO.select([s], nil, nil, 1), 'connection persists..'
+
+    # maek sure another on the same connection works
+    s.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
+    buf = s.readpartial(1024)
+    assert_match %r{\AHTTP/1\.1 200}, buf
+    assert_match(%r{^Transfer-Encoding: chunked\r\n}, buf)
+    s.close
   ensure
     quit_wait(pid)
   end
-- 
EW


  reply	other threads:[~2016-12-14  6:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-14  6:59 [PATCH 0/2] revenge of the body-less responses Eric Wong
2016-12-14  6:59 ` Eric Wong [this message]
2016-12-14  6:59 ` [PATCH 2/2] proxy_pass: do not chunk on bodyless upstream responses Eric Wong

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=20161214065952.28950-2-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).