Class: Thin::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/thin/response.rb

Overview

A response sent to the client.

Constant Summary collapse

CONNECTION =
'Connection'.freeze
CLOSE =
'close'.freeze
KEEP_ALIVE =
'keep-alive'.freeze
SERVER =
'Server'.freeze
CONTENT_LENGTH =
'Content-Length'.freeze
PERSISTENT_STATUSES =
[100, 101].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



21
22
23
24
25
# File 'lib/thin/response.rb', line 21

def initialize
  @headers    = Headers.new
  @status     = 200
  @persistent = false
end

Instance Attribute Details

#bodyObject

Response body, must respond to each.



16
17
18
# File 'lib/thin/response.rb', line 16

def body
  @body
end

#headersObject

Headers key-value hash



19
20
21
# File 'lib/thin/response.rb', line 19

def headers
  @headers
end

#statusObject

Status code



13
14
15
# File 'lib/thin/response.rb', line 13

def status
  @status
end

Instance Method Details

#closeObject

Close any resource used by the response



76
77
78
# File 'lib/thin/response.rb', line 76

def close
  @body.close if @body.respond_to?(:close)
end

#each {|head| ... } ⇒ Object

Yields each chunk of the response. To control the size of each chunk define your own each method on body.

Yields:



83
84
85
86
87
88
89
90
# File 'lib/thin/response.rb', line 83

def each
  yield head
  if @body.is_a?(String)
    yield @body
  else
    @body.each { |chunk| yield chunk }
  end
end

#headObject

Top header of the response, containing the status code and response headers.



39
40
41
# File 'lib/thin/response.rb', line 39

def head
  "HTTP/1.1 #{@status} #{HTTP_STATUS_CODES[@status.to_i]}\r\n#{headers_output}\r\n"
end

#headers_outputObject

String representation of the headers to be sent in the response.



29
30
31
32
33
34
35
# File 'lib/thin/response.rb', line 29

def headers_output
  # Set default headers
  @headers[CONNECTION] = persistent? ? KEEP_ALIVE : CLOSE
  @headers[SERVER]     = Thin::SERVER

  @headers.to_s
end

#persistent!Object

Tell the client the connection should stay open



93
94
95
# File 'lib/thin/response.rb', line 93

def persistent!
  @persistent = true
end

#persistent?Boolean

Persistent connection must be requested as keep-alive from the server and have a Content-Length, or the response status must require that the connection remain open.

Returns:

  • (Boolean)


100
101
102
# File 'lib/thin/response.rb', line 100

def persistent?
  (@persistent && @headers.has_key?(CONTENT_LENGTH)) || PERSISTENT_STATUSES.include?(@status)
end