cmogstored dev/user discussion/issues/patches/etc
 help / color / mirror / code / Atom feed
From: Eric Wong <e@yhbt.net>
To: cmogstored-public@yhbt.net
Subject: [PATCH 2/2] http: favor chunked over Content-Length
Date: Tue, 17 Mar 2020 06:56:52 +0000	[thread overview]
Message-ID: <20200317065652.10324-3-e@yhbt.net> (raw)
In-Reply-To: <20200317065652.10324-1-e@yhbt.net>

RFC 7230 is actually explicit about favoring the
"Transfer-Encoding: chunked" over a Content-Length header
when a client specifies both.
---
 http_parser.rl   | 13 +++++++++++--
 test/http_put.rb | 16 ++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/http_parser.rl b/http_parser.rl
index 0685d27..835ba65 100644
--- a/http_parser.rl
+++ b/http_parser.rl
@@ -62,7 +62,12 @@ static char *skip_header(struct mog_http *http, char *buf, const char *pe)
 
 	content_length = "Content-Length:"i sep
 		(digit+) $ {
-			if (!length_incr(&http->_p.content_len, fc))
+			/*
+			 * RFC 7230 3.3.2, 3.3.3,:
+			 * favor Transfer-Encoding over Content-Length
+			 */
+			if (!http->_p.chunked &&
+					!length_incr(&http->_p.content_len, fc))
 				fbreak;
 		}
 		$! { errno = EINVAL; fbreak; }
@@ -116,7 +121,11 @@ static char *skip_header(struct mog_http *http, char *buf, const char *pe)
 		# "compress" as described in RFC 7230, so reject them, here.
 		"chunked"i
 		$! { errno = EINVAL; fbreak; }
-		eor @ { http->_p.chunked = 1; };
+		eor @ {
+			http->_p.chunked = 1;
+			/* RFC 7230 3.3.2, 3.3.3,: ignore length if chunked */
+			http->_p.content_len = 0;
+		};
 	trailer = "Trailer:"i sep
 		(("Content-MD5"i @ { http->_p.has_md5 = 1; })
 		 | header_name | ',')
diff --git a/test/http_put.rb b/test/http_put.rb
index 0479629..34e2cdb 100644
--- a/test/http_put.rb
+++ b/test/http_put.rb
@@ -226,6 +226,22 @@ def test_put_bad_content_length
     assert( ! File.exist?("#@tmpdir/dev666/foo") )
   end
 
+  def test_put_favors_chunked
+    order = [ "Transfer-Encoding: chunked", "Content-Length: 123" ]
+    %w[a b].each do
+      path = '/dev666/a'
+      req = "PUT #{path} HTTP/1.1\r\n" \
+            "#{order.join("\r\n")}\r\n\r\n" \
+            "a\r\nhelloworld\r\n0\r\n\r\n"
+      order.reverse!
+      @client.write(req)
+      resp = @client.readpartial(4096)
+      assert_match(%r{\AHTTP/1\.1 201 Created\r\n}, resp, "#{path} created")
+      assert(File.exist?("#@tmpdir#{path}"))
+      assert_equal('helloworld', File.read("#@tmpdir#{path}"))
+    end
+  end
+
   def test_content_md5_good
     req = "PUT /dev666/foo HTTP/1.1\r\n" \
           "Host: #@host:#@port\r\n" \

      parent reply	other threads:[~2020-03-17  6:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-17  6:56 [PATCH 0/2] improve RFC 7230 conformance Eric Wong
2020-03-17  6:56 ` [PATCH 1/2] http: reject non-chunked Transfer-Encoding Eric Wong
2020-03-17  6:56 ` Eric Wong [this message]

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/cmogstored/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200317065652.10324-3-e@yhbt.net \
    --to=e@yhbt.net \
    --cc=cmogstored-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/cmogstored.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).