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
ERROR =

Error Responses

[500, {'Content-Type' => 'text/plain'}, ['Internal server error']].freeze
PERSISTENT_ERROR =
[500, {'Content-Type' => 'text/plain', 'Connection' => 'keep-alive', 'Content-Length' => "21"}, ['Internal server error']].freeze
BAD_REQUEST =
[400, {'Content-Type' => 'text/plain'}, ['Bad Request']].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



26
27
28
29
30
# File 'lib/thin/response.rb', line 26

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

Instance Attribute Details

#bodyObject

Response body, must respond to each.



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

def body
  @body
end

#headersObject

Headers key-value hash



24
25
26
# File 'lib/thin/response.rb', line 24

def headers
  @headers
end

#statusObject

Status code



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

def status
  @status
end

Instance Method Details

#closeObject

Close any resource used by the response



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

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:



88
89
90
91
92
93
94
95
# File 'lib/thin/response.rb', line 88

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.



44
45
46
# File 'lib/thin/response.rb', line 44

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.



34
35
36
37
38
39
40
# File 'lib/thin/response.rb', line 34

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



98
99
100
# File 'lib/thin/response.rb', line 98

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)


105
106
107
# File 'lib/thin/response.rb', line 105

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