Sha256: e94960cf021c021ea73108fa92d39165636090573cac84222a76c285cb9e7438

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Thin
  # A response sent to the client.
  class Response
    CONNECTION     = 'Connection'.freeze
    SERVER         = 'Server'.freeze
    CLOSE          = 'close'.freeze
    
    # Status code
    attr_accessor :status
    
    # Response body, must respond to +each+.
    attr_accessor :body
    
    # Headers key-value hash
    attr_reader   :headers
    
    def initialize
      @headers = Headers.new
      @status  = 200
    end
    
    # String representation of the headers
    # to be sent in the response.
    def headers_output
      @headers[CONNECTION] = CLOSE
      @headers[SERVER] = Thin::SERVER
      
      @headers.to_s
    end
    
    # Top header of the response,
    # containing the status code and response headers.
    def head
      "HTTP/1.1 #{@status} #{HTTP_STATUS_CODES[@status.to_i]}\r\n#{headers_output}\r\n"
    end
    
    def headers=(key_value_pairs)
      key_value_pairs.each do |k, vs|
        vs.each { |v| @headers[k] = v.chomp }
      end
    end
    
    # Close any resource used by the response
    def close
      @body.close if @body.respond_to?(:close)
    end
    
    # Yields each chunk of the response.
    # To controler the size of the chunks sent to the client
    # define your own +each+ method on +body+.
    def each
      yield head
      @body.each do |chunk|
        yield chunk
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thin-0.5.4 lib/thin/response.rb