diff options
author | Eric Wong <normalperson@yhbt.net> | 2010-07-19 10:09:48 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2010-07-19 17:04:14 -0700 |
commit | 5ec57e5f5d7df07f563722a12d95845579e86e13 (patch) | |
tree | c2012a7a968d96786fb15d50185fc11468c92e1f /lib/rainbows/response.rb | |
parent | 399bbdb5c5e7cf9e716aacd9f0763813edf52423 (diff) | |
download | rainbows-5ec57e5f5d7df07f563722a12d95845579e86e13.tar.gz |
This will give each concurrency model more control over particular code paths and serving static files.
Diffstat (limited to 'lib/rainbows/response.rb')
-rw-r--r-- | lib/rainbows/response.rb | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/lib/rainbows/response.rb b/lib/rainbows/response.rb index f42f367..13946ca 100644 --- a/lib/rainbows/response.rb +++ b/lib/rainbows/response.rb @@ -5,34 +5,29 @@ require 'time' # for Time#httpdate module Rainbows::Response CODES = Unicorn::HttpResponse::CODES + CRLF = "\r\n" - def response_header(response, out) - status, headers = response - status = CODES[status.to_i] || status + # freeze headers we may set as hash keys for a small speedup + CONNECTION = "Connection".freeze + CLOSE = "close" + KEEP_ALIVE = "keep-alive" + HH = Rack::Utils::HeaderHash + def response_header(status, headers) + status = CODES[status.to_i] || status + rv = "HTTP/1.1 #{status}\r\n" \ + "Date: #{Time.now.httpdate}\r\n" \ + "Status: #{status}\r\n" headers.each do |key, value| - next if %r{\A(?:X-Rainbows-|Connection\z|Date\z|Status\z)}i =~ key + next if %r{\A(?:X-Rainbows-|Date\z|Status\z)}i =~ key if value =~ /\n/ # avoiding blank, key-only cookies with /\n+/ - out.concat(value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" }) + rv << value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" }.join('') else - out << "#{key}: #{value}\r\n" + rv << "#{key}: #{value}\r\n" end end - - "HTTP/1.1 #{status}\r\n" \ - "Date: #{Time.now.httpdate}\r\n" \ - "Status: #{status}\r\n" \ - "#{out.join('')}\r\n" - end - - def write_header(socket, response, out) - out and socket.write(response_header(response, out)) - end - - def write_response(socket, response, out) - write_header(socket, response, out) - write_body(socket, response[2]) + rv << CRLF end # called after forking |