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
- 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
-
#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.
- #skip_body! ⇒ Object
Constructor Details
Instance Attribute Details
#body ⇒ Object
Response body, must respond to each
.
21 22 23 |
# File 'lib/thin/response.rb', line 21 def body @body end |
#headers ⇒ Object
Headers key-value hash
24 25 26 |
# File 'lib/thin/response.rb', line 24 def headers @headers end |
#status ⇒ Object
Status code
18 19 20 |
# File 'lib/thin/response.rb', line 18 def status @status end |
Instance Method Details
#close ⇒ Object
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
.
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 |
#head ⇒ Object
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_output ⇒ Object
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.
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 |