Class: Thin::Response

Inherits:
Object show all
Defined in:
lib/swee/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.



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

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

Instance Attribute Details

#bodyObject

Response body, must respond to each.



79
80
81
# File 'lib/swee/thin/response.rb', line 79

def body
  @body
end

#headersObject

Headers key-value hash



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

def headers
  @headers
end

#statusObject

Status code



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

def status
  @status
end

Instance Method Details

#closeObject

Close any resource used by the response



143
144
145
# File 'lib/swee/thin/response.rb', line 143

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:



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/swee/thin/response.rb', line 150

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.



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

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.



93
94
95
96
97
98
99
# File 'lib/swee/thin/response.rb', line 93

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



163
164
165
# File 'lib/swee/thin/response.rb', line 163

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)


170
171
172
# File 'lib/swee/thin/response.rb', line 170

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

#skip_body!Object



174
175
176
# File 'lib/swee/thin/response.rb', line 174

def skip_body!
  @skip_body = true
end