rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob f42f36728d1beff431eaf4029085a8f7073712ea 1098 bytes (raw)
$ git show v0.95.1:lib/rainbows/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
39
40
41
42
43
 
# -*- encoding: binary -*-
# :enddoc:
require 'time' # for Time#httpdate

module Rainbows::Response

  CODES = Unicorn::HttpResponse::CODES

  def response_header(response, out)
    status, headers = response
    status = CODES[status.to_i] || status

    headers.each do |key, value|
      next if %r{\A(?:X-Rainbows-|Connection\z|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" })
      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 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])
  end

  # called after forking
  def self.setup(klass)
    require('rainbows/response/body') and
      klass.__send__(:include, Rainbows::Response::Body)
  end
end

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