rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 1933218ddec009ce14e2221fd1c3126b6b25fb4e 1012 bytes (raw)
$ git show v0.92.0:lib/rainbows/http_response.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
# -*- encoding: binary -*-
require 'time'

module Rainbows

  class HttpResponse < ::Unicorn::HttpResponse

    def self.header_string(status, headers, out)
      status = CODES[status.to_i] || status

      headers.each do |key, value|
        next if %r{\AX-Rainbows-}i =~ key
        next if SKIP.include?(key.downcase)
        if value =~ /\n/
          # avoiding blank, key-only cookies with /\n+/
          out.concat(value.split(/\n+/).map! { |v| "#{key}: #{v}\r\n" })
        else
          out << "#{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 self.write(socket, rack_response, out = [])
      status, headers, body = rack_response
      out.instance_of?(Array) and
        socket.write(header_string(status, headers, out))

      body.each { |chunk| socket.write(chunk) }
      ensure
        body.respond_to?(:close) and body.close
    end
  end
end

git clone https://yhbt.net/rainbows.git