yahns Ruby server user/dev discussion
 help / color / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH 2/4] response: reduce stack overhead for parameter passing
Date: Wed,  3 Aug 2016 03:19:04 +0000
Message-ID: <20160803031906.14553-3-e@80x24.org> (raw)
In-Reply-To: <20160803031906.14553-1-e@80x24.org>

This makes it easier to add more parameters to
http_response_write and simplifies current callers.
---
 lib/yahns/http_client.rb   | 20 ++++++++++----------
 lib/yahns/http_response.rb |  7 ++++---
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/lib/yahns/http_client.rb b/lib/yahns/http_client.rb
index 873dd73..7a1bac1 100644
--- a/lib/yahns/http_client.rb
+++ b/lib/yahns/http_client.rb
@@ -191,9 +191,9 @@ def r100_done
     else # :lazy, false
       env = @hs.env
       hdr_only = env['REQUEST_METHOD'] == 'HEAD'.freeze
-      status, headers, body = k.app.call(env)
-      return :ignore if app_hijacked?(env, body)
-      http_response_write(status, headers, body, hdr_only)
+      res = k.app.call(env)
+      return :ignore if app_hijacked?(env, res)
+      http_response_write(res, hdr_only)
     end
   end
 
@@ -224,15 +224,15 @@ def app_call(input)
 
     hdr_only = env['REQUEST_METHOD'] == 'HEAD'.freeze
     # run the rack app
-    status, headers, body = k.app.call(env)
-    return :ignore if app_hijacked?(env, body)
-    if status.to_i == 100
+    res = k.app.call(env)
+    return :ignore if app_hijacked?(env, res)
+    if res[0].to_i == 100
       rv = http_100_response(env) and return rv
-      status, headers, body = k.app.call(env)
+      res = k.app.call(env)
     end
 
     # this returns :wait_readable, :wait_writable, :ignore, or nil:
-    http_response_write(status, headers, body, hdr_only)
+    http_response_write(res, hdr_only)
   end
 
   # called automatically by kgio_write
@@ -308,9 +308,9 @@ def handle_error(e)
     return # always drop the connection on uncaught errors
   end
 
-  def app_hijacked?(env, body)
+  def app_hijacked?(env, res)
     return false unless env.include?('rack.hijack_io'.freeze)
-    body.close if body.respond_to?(:close)
+    res[2].close if res && res[2].respond_to?(:close)
     true
   end
 
diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb
index f2d9c62..b157ee4 100644
--- a/lib/yahns/http_response.rb
+++ b/lib/yahns/http_response.rb
@@ -8,8 +8,8 @@
 # Writes a Rack response to your client using the HTTP/1.1 specification.
 # You use it by simply doing:
 #
-#   status, headers, body = rack_app.call(env)
-#   http_response_write(status, headers, body, env['REQUEST_METHOD']=='HEAD')
+#   res = rack_app.call(env)
+#   http_response_write(res, env['REQUEST_METHOD']=='HEAD')
 #
 # Most header correctness (including Content-Length and Content-Type)
 # is the job of Rack, with the exception of the "Date" header.
@@ -120,7 +120,8 @@ def kv_str(buf, key, value)
 
   # writes the rack_response to socket as an HTTP response
   # returns :wait_readable, :wait_writable, :forget, or nil
-  def http_response_write(status, headers, body, hdr_only)
+  def http_response_write(res, hdr_only)
+    status, headers, body = res
     offset = 0
     count = hijack = nil
     k = self.class
-- 
EW


  parent reply index

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-03  3:19 [PATCH 0/4] remove chunked/Content-Length requirement from apps Eric Wong
2016-08-03  3:19 ` [PATCH 1/4] response: drop clients after HTTP responses of unknown length Eric Wong
2016-08-03  3:19 ` Eric Wong [this message]
2016-08-03  3:19 ` [PATCH 3/4] response: support auto-chunking for HTTP/1.1 Eric Wong
2016-08-03  3:19 ` [PATCH 4/4] Revert "document Rack::Chunked/ContentLength semi-requirements" Eric Wong

Reply instructions:

You may reply publically 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=20160803031906.14553-3-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

yahns Ruby server user/dev discussion

Archives are clonable:
	git clone --mirror https://yhbt.net/yahns-public
	git clone --mirror http://ou63pmih66umazou.onion/yahns-public

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.yahns
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.yahns

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git