http_response: filter out X-Rainbows-* headers
[rainbows.git] / lib / rainbows / http_response.rb
blob6617666ee335d3f1c6973659feadf86530a994a3
1 # -*- encoding: binary -*-
2 require 'time'
3 require 'rainbows'
5 module Rainbows
7   class HttpResponse < ::Unicorn::HttpResponse
9     def self.write(socket, rack_response, out = [])
10       status, headers, body = rack_response
12       if Array === out
13         status = CODES[status.to_i] || status
15         headers.each do |key, value|
16           next if %r{\AX-Rainbows-}i =~ key
17           next if SKIP.include?(key.downcase)
18           if value =~ /\n/
19             out.concat(value.split(/\n/).map! { |v| "#{key}: #{v}\r\n" })
20           else
21             out << "#{key}: #{value}\r\n"
22           end
23         end
25         socket.write("HTTP/1.1 #{status}\r\n" \
26                      "Date: #{Time.now.httpdate}\r\n" \
27                      "Status: #{status}\r\n" \
28                      "#{out.join('')}\r\n")
29       end
31       body.each { |chunk| socket.write(chunk) }
32       ensure
33         body.respond_to?(:close) and body.close rescue nil
34     end
35   end
36 end