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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



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

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

Instance Attribute Details

#bodyObject

Response body, must respond to each.



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

def body
  @body
end

#headersObject

Headers key-value hash



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

def headers
  @headers
end

#statusObject

Status code



11
12
13
# File 'lib/thin/response.rb', line 11

def status
  @status
end

Instance Method Details

#closeObject

Close any resource used by the response



74
75
76
# File 'lib/thin/response.rb', line 74

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:



81
82
83
84
85
86
# File 'lib/thin/response.rb', line 81

def each
  yield head
  @body.each do |chunk|
    yield chunk
  end
end

#headObject

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



37
38
39
# File 'lib/thin/response.rb', line 37

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.



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

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



89
90
91
# File 'lib/thin/response.rb', line 89

def persistent!
  @persistent = true
end

#persistent?Boolean

Persistent connection must be requested as keep-alive from the server and have a Content-Length.

Returns:

  • (Boolean)


95
96
97
# File 'lib/thin/response.rb', line 95

def persistent?
  @persistent && @headers.has_key?(CONTENT_LENGTH)
end