Sha256: e2cf626d81dbdf77c7322810ae3c5453f9dcb409010ce1dda713eeede2bb17d9

Contents?: true

Size: 1.97 KB

Versions: 6

Compression:

Stored size: 1.97 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
    
    if Thin.ruby_18?
      
      # Ruby 1.8 implementation.
      # Respects Rack specs.
      def headers=(key_value_pairs)
        key_value_pairs.each do |k, vs|
          vs.each { |v| @headers[k] = v.chomp }
        end
      end

    else

      # Ruby 1.9 doesn't have a String#each anymore.
      # Rack spec doesn't take care of that yet, for now we just use
      # +each+ but fallback to +each_line+ on strings.
      # I wish we could remove that condition.
      # To be reviewed when a new Rack spec comes out.
      def headers=(key_value_pairs)
        key_value_pairs.each do |k, vs|
          if vs.is_a?(String)
            vs.each_line { |v| @headers[k] = v.chomp }
          else
            vs.each { |v| @headers[k] = v.chomp }
          end
        end
      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 control the size of each chunk
    # define your own +each+ method on +body+.
    def each
      yield head
      @body.each do |chunk|
        yield chunk
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
thin-0.6.4-x86-mswin32-60 lib/thin/response.rb
thin-0.6.2 lib/thin/response.rb
thin-0.6.4 lib/thin/response.rb
thin-0.6.2-x86-mswin32-60 lib/thin/response.rb
thin-0.6.3 lib/thin/response.rb
thin-0.6.3-x86-mswin32-60 lib/thin/response.rb