Class: Thin::Response
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
.
79 80 81 |
# File 'lib/swee/thin/response.rb', line 79 def body @body end |
#headers ⇒ Object
Headers key-value hash
82 83 84 |
# File 'lib/swee/thin/response.rb', line 82 def headers @headers end |
#status ⇒ Object
Status code
76 77 78 |
# File 'lib/swee/thin/response.rb', line 76 def status @status end |
Instance Method Details
#close ⇒ Object
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
.
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 |
#head ⇒ Object
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_output ⇒ Object
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.
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 |