yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Cc: e@80x24.org
Subject: [PATCH 1/3] generate response status strings dynamically
Date: Tue, 30 Jun 2015 03:01:51 +0000	[thread overview]
Message-ID: <1435633313-30223-2-git-send-email-e@80x24.org> (raw)
In-Reply-To: <1435633313-30223-1-git-send-email-e@80x24.org>

Rack::Utils::HTTP_STATUS_CODES may be altered by the underlying
application, allow changes to that to be reflected in our responses
and do not rely on the Unicorn::HttpResponse::CODES hash which
will probably go away soon.
---
 lib/yahns/http_response.rb       |  8 +++++---
 lib/yahns/proxy_http_response.rb | 11 ++++++-----
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb
index fabd4b7..b70491d 100644
--- a/lib/yahns/http_response.rb
+++ b/lib/yahns/http_response.rb
@@ -59,7 +59,7 @@ module Yahns::HttpResponse # :nodoc:
   end
 
   def err_response(code)
-    "#{response_start}#{CODES[code]}\r\n\r\n"
+    "#{response_start}#{code} #{Rack::Utils::HTTP_STATUS_CODES[code]}\r\n\r\n"
   end
 
   def response_header_blocked(ret, header, body, alive, offset, count)
@@ -130,7 +130,6 @@ module Yahns::HttpResponse # :nodoc:
   # 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)
-    status = CODES[status.to_i] || status
     offset = 0
     count = hijack = nil
     k = self.class
@@ -138,7 +137,10 @@ module Yahns::HttpResponse # :nodoc:
     flags = MSG_DONTWAIT
 
     if @hs.headers?
-      buf = "#{response_start}#{status}\r\nDate: #{httpdate}\r\n"
+      code = status.to_i
+      msg = Rack::Utils::HTTP_STATUS_CODES[code]
+      buf = "#{response_start}#{msg ? %Q(#{code} #{msg}) : status}\r\n" \
+            "Date: #{httpdate}\r\n"
       headers.each do |key, value|
         case key
         when %r{\ADate\z}i
diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index cbdce6f..61989c2 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -38,7 +38,8 @@ module Yahns::HttpResponse # :nodoc:
     end
     # try to write something, but don't care if we fail
     Integer === code and
-      kgio_trywrite("HTTP/1.1 #{CODES[code]}\r\n\r\n") rescue nil
+      kgio_trywrite("HTTP/1.1 #{code} #{
+                     Rack::Utils::HTTP_STATUS_CODES[code]}\r\n\r\n") rescue nil
 
     shutdown rescue nil
     req_res.shutdown rescue nil
@@ -59,16 +60,16 @@ module Yahns::HttpResponse # :nodoc:
   # returns nil if completely done
   def proxy_response_start(res, tip, kcar, req_res)
     status, headers = res
-    si = status.to_i
-    status = CODES[si] || status
+    code = status.to_i
+    msg = Rack::Utils::HTTP_STATUS_CODES[code]
     env = @hs.env
-    have_body = !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(si) &&
+    have_body = !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(code) &&
                 env[REQUEST_METHOD] != HEAD
     flags = MSG_DONTWAIT
     alive = @hs.next? && self.class.persistent_connections
     response_headers = env['yahns.proxy_pass.response_headers']
 
-    res = "HTTP/1.1 #{status}\r\n"
+    res = "HTTP/1.1 #{msg ? %Q(#{code} #{msg}) : status}\r\n"
     headers.each do |key,value| # n.b.: headers is an Array of 2-element Arrays
       case key
       when /\A(?:Connection|Keep-Alive)\z/i
-- 
EW


  reply	other threads:[~2015-06-30  3:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-30  3:01 [PATCH 0/3] favor newer Rubies for performance Eric Wong
2015-06-30  3:01 ` Eric Wong [this message]
2015-06-30  3:01 ` [PATCH 2/3] reduce constants and optimize for Ruby 2.2+ Eric Wong
2015-06-30  3:01 ` [PATCH 3/3] http_response: reduce bytecode size 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=1435633313-30223-2-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).