Class: Thin::Response
- Inherits:
-
Object
- Object
- Thin::Response
- 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
-
#body ⇒ Object
Response body, must respond to
each
. -
#headers ⇒ Object
Headers key-value hash.
-
#status ⇒ Object
Status code.
Instance Method Summary collapse
-
#close ⇒ Object
Close any resource used by the response.
-
#each {|head| ... } ⇒ Object
Yields each chunk of the response.
-
#head ⇒ Object
Top header of the response, containing the status code and response headers.
-
#headers_output ⇒ Object
String representation of the headers to be sent in the response.
-
#initialize ⇒ Response
constructor
A new instance of Response.
-
#persistent! ⇒ Object
Tell the client the connection should stay open.
-
#persistent? ⇒ Boolean
Persistent connection must be requested as keep-alive from the server and have a Content-Length.
Constructor Details
Instance Attribute Details
#body ⇒ Object
Response body, must respond to each
.
14 15 16 |
# File 'lib/thin/response.rb', line 14 def body @body end |
#headers ⇒ Object
Headers key-value hash
17 18 19 |
# File 'lib/thin/response.rb', line 17 def headers @headers end |
#status ⇒ Object
Status code
11 12 13 |
# File 'lib/thin/response.rb', line 11 def status @status end |
Instance Method Details
#close ⇒ Object
Close any resource used by the response
71 72 73 |
# File 'lib/thin/response.rb', line 71 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
.
78 79 80 81 82 83 |
# File 'lib/thin/response.rb', line 78 def each yield head @body.each do |chunk| yield chunk end end |
#head ⇒ Object
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_output ⇒ Object
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
86 87 88 |
# File 'lib/thin/response.rb', line 86 def persistent! @persistent = true end |
#persistent? ⇒ Boolean
Persistent connection must be requested as keep-alive from the server and have a Content-Length.
92 93 94 |
# File 'lib/thin/response.rb', line 92 def persistent? @persistent && @headers.has_key?(CONTENT_LENGTH) end |