From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS198093 171.25.193.0/24 X-Spam-Status: No, score=-2.2 required=3.0 tests=AWL,BAYES_00, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_XBL shortcircuit=no autolearn=no version=3.3.2 X-Original-To: spew@80x24.org Received: from 80x24.org (tor-exit0-readme.dfri.se [171.25.193.20]) by dcvr.yhbt.net (Postfix) with ESMTP id 2CBB863383B for ; Tue, 30 Jun 2015 00:35:09 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] reflect changes in Rack::Utils::HTTP_STATUS_CODES Date: Tue, 30 Jun 2015 00:35:06 +0000 Message-Id: <1435624506-13542-1-git-send-email-e@80x24.org> List-Id: --- lib/unicorn/http_response.rb | 19 ++++++++----------- test/unit/test_response.rb | 22 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/unicorn/http_response.rb b/lib/unicorn/http_response.rb index 016dac8..ba8c786 100644 --- a/lib/unicorn/http_response.rb +++ b/lib/unicorn/http_response.rb @@ -10,15 +10,10 @@ # 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 +21,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| @@ -48,7 +45,7 @@ module Unicorn::HttpResponse end end end - socket.write(buf << CRLF) + socket.write(buf << "\r\n".freeze) end if hijack diff --git a/test/unit/test_response.rb b/test/unit/test_response.rb index 3478288..92f1749 100644 --- a/test/unit/test_response.rb +++ b/test/unit/test_response.rb @@ -74,9 +74,29 @@ class ResponseTest < Test::Unit::TestCase def test_unknown_status_pass_through out = StringIO.new - http_response_write(out,"666 I AM THE BEAST", {}, [] ) + http_response_write(out,"666 I AM THE BEAST", {}, []) assert ! out.closed? 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