diff options
author | Eric Wong <normalperson@yhbt.net> | 2009-10-02 20:44:03 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2009-10-02 21:21:28 -0700 |
commit | 37a12997628fcab722512f8a6370b92d44e33529 (patch) | |
tree | 9ced4ceaee3d4d6ce21dd1742f037d1d79a01e61 /lib/rainbows/http_response.rb | |
download | rainbows-37a12997628fcab722512f8a6370b92d44e33529.tar.gz |
No tests yet, but the old "gossamer" and "rainbows" branches seem to be basically working.
Diffstat (limited to 'lib/rainbows/http_response.rb')
-rw-r--r-- | lib/rainbows/http_response.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/rainbows/http_response.rb b/lib/rainbows/http_response.rb new file mode 100644 index 0000000..ebaa4e7 --- /dev/null +++ b/lib/rainbows/http_response.rb @@ -0,0 +1,35 @@ +# -*- encoding: binary -*- +require 'time' +require 'rainbows' + +module Rainbows + + class HttpResponse < ::Unicorn::HttpResponse + + def self.write(socket, rack_response, out = []) + status, headers, body = rack_response + + if Array === out + status = CODES[status.to_i] || status + + headers.each do |key, value| + next if SKIP.include?(key.downcase) + if value =~ /\n/ + out.concat(value.split(/\n/).map! { |v| "#{key}: #{v}\r\n" }) + else + out << "#{key}: #{value}\r\n" + end + end + + socket.write("HTTP/1.1 #{status}\r\n" \ + "Date: #{Time.now.httpdate}\r\n" \ + "Status: #{status}\r\n" \ + "#{out.join('')}\r\n") + end + + body.each { |chunk| socket.write(chunk) } + ensure + body.respond_to?(:close) and body.close rescue nil + end + end +end |