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
31
# File 'lib/thin/response.rb', line 26

def initialize
  @headers    = Headers.new
  @status     = 200
  @persistent = false
  @skip_body  = 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



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

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:



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/thin/response.rb', line 89

def each
  yield head

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

#headObject

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



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

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.



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

def headers_output
  # Set default headers
  @headers[CONNECTION] = persistent? ? KEEP_ALIVE : CLOSE unless @headers.has_key?(CONNECTION)
  @headers[SERVER]     = Thin::NAME unless @headers.has_key?(SERVER)

  @headers.to_s
end

#persistent!Object

Tell the client the connection should stay open



102
103
104
# File 'lib/thin/response.rb', line 102

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)


109
110
111
# File 'lib/thin/response.rb', line 109

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

#skip_body!Object



113
114
115
# File 'lib/thin/response.rb', line 113

def skip_body!
  @skip_body = true
end