From: Eric Wong <e@80x24.org>
To: unicorn-public@bogomips.org
Cc: e@80x24.org
Subject: [PATCH 1/3] reflect changes in Rack::Utils::HTTP_STATUS_CODES
Date: Tue, 30 Jun 2015 22:51:50 +0000 [thread overview]
Message-ID: <1435704712-4981-2-git-send-email-e@80x24.org> (raw)
In-Reply-To: <1435704712-4981-1-git-send-email-e@80x24.org>
Applications may want to alter the message associated with HTTP
status codes in Rack::Utils::HTTP_STATUS_CODES. Avoid memoizing
status lines ahead-of-time
Note: this introduces a minor performance regression, but ought to
be unnoticeable unless you're running "Hello world"-type apps.
---
lib/unicorn/http_response.rb | 15 +++++++--------
test/unit/test_response.rb | 20 ++++++++++++++++++++
2 files changed, 27 insertions(+), 8 deletions(-)
diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb
index 016dac8..a42303e 100644
--- a/lib/unicorn/http_response.rb
+++ b/lib/unicorn/http_response.rb
@@ -10,15 +10,12 @@
# is the job of Rack, with the exception of the "Date" and "Status" header.
module Unicorn::HttpResponse
- # Every standard HTTP code mapped to the appropriate message.
- CODES = Rack::Utils::HTTP_STATUS_CODES.inject({}) { |hash,(code,msg)|
- hash[code] = "#{code} #{msg}"
- hash
- }
CRLF = "\r\n"
+ # internal API, code will always be common-enough-for-even-old-Rack
def err_response(code, response_start_sent)
- "#{response_start_sent ? '' : 'HTTP/1.1 '}#{CODES[code]}\r\n\r\n"
+ "#{response_start_sent ? '' : 'HTTP/1.1 '}" \
+ "#{code} #{Rack::Utils::HTTP_STATUS_CODES[code]}\r\n\r\n"
end
# writes the rack_response to socket as an HTTP response
@@ -26,9 +23,11 @@ module Unicorn::HttpResponse
response_start_sent=false)
hijack = nil
- http_response_start = response_start_sent ? '' : 'HTTP/1.1 '
if headers
- buf = "#{http_response_start}#{CODES[status.to_i] || status}\r\n" \
+ code = status.to_i
+ msg = Rack::Utils::HTTP_STATUS_CODES[code]
+ start = response_start_sent ? ''.freeze : 'HTTP/1.1 '.freeze
+ buf = "#{start}#{msg ? %Q(#{code} #{msg}) : status}\r\n" \
"Date: #{httpdate}\r\n" \
"Connection: close\r\n"
headers.each do |key, value|
diff --git a/test/unit/test_response.rb b/test/unit/test_response.rb
index 3478288..1e9d74a 100644
--- a/test/unit/test_response.rb
+++ b/test/unit/test_response.rb
@@ -79,4 +79,24 @@ class ResponseTest < Test::Unit::TestCase
headers = out.string.split(/\r\n\r\n/).first.split(/\r\n/)
assert %r{\AHTTP/\d\.\d 666 I AM THE BEAST\z}.match(headers[0])
end
+
+ def test_modified_rack_http_status_codes_late
+ r, w = IO.pipe
+ pid = fork do
+ r.close
+ # Users may want to globally override the status text associated
+ # with an HTTP status code in their app.
+ Rack::Utils::HTTP_STATUS_CODES[200] = "HI"
+ http_response_write(w, 200, {}, [])
+ w.close
+ end
+ w.close
+ assert_equal "HTTP/1.1 200 HI\r\n", r.gets
+ r.read # just drain the pipe
+ pid, status = Process.waitpid2(pid)
+ assert_predicate status, :success?
+ ensure
+ r.close
+ w.close unless w.closed?
+ end
end
--
EW
next prev parent reply other threads:[~2015-06-30 22:52 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-30 22:51 [PATCH 0/3] reflect changes to Rack::Utils::HTTP_STATUS_CODES Eric Wong
2015-06-30 22:51 ` Eric Wong [this message]
2015-06-30 22:51 ` [PATCH 2/3] reduce constants and optimize for Ruby 2.2 Eric Wong
2015-06-30 22:51 ` [PATCH 3/3] http_response: reduce size of multi-line header path 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/unicorn/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1435704712-4981-2-git-send-email-e@80x24.org \
--to=e@80x24.org \
--cc=unicorn-public@bogomips.org \
/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/unicorn.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).