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
- PERSISTENT_STATUSES =
[100, 101].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, or the response status must require that the connection remain open.
Constructor Details
Instance Attribute Details
#body ⇒ Object
Response body, must respond to each
.
16 17 18 |
# File 'lib/thin/response.rb', line 16 def body @body end |
#headers ⇒ Object
Headers key-value hash
19 20 21 |
# File 'lib/thin/response.rb', line 19 def headers @headers end |
#status ⇒ Object
Status code
13 14 15 |
# File 'lib/thin/response.rb', line 13 def status @status end |
Instance Method Details
#close ⇒ Object
Close any resource used by the response
76 77 78 |
# File 'lib/thin/response.rb', line 76 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
.
83 84 85 86 87 88 89 90 |
# File 'lib/thin/response.rb', line 83 def each yield head if @body.is_a?(String) yield @body else @body.each { |chunk| yield chunk } end end |
#head ⇒ Object
Top header of the response, containing the status code and response headers.
39 40 41 |
# File 'lib/thin/response.rb', line 39 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.
29 30 31 32 33 34 35 |
# File 'lib/thin/response.rb', line 29 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
93 94 95 |
# File 'lib/thin/response.rb', line 93 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.
100 101 102 |
# File 'lib/thin/response.rb', line 100 def persistent? (@persistent && @headers.has_key?(CONTENT_LENGTH)) || PERSISTENT_STATUSES.include?(@status) end |