class Thin::Response
A response sent to the client.
Constants
- BAD_REQUEST
- CLOSE
- CONNECTION
- CONTENT_LENGTH
- ERROR
Error Responses
- KEEP_ALIVE
- PERSISTENT_ERROR
- PERSISTENT_STATUSES
- SERVER
Attributes
Response
body, must respond to each
.
Headers
key-value hash
Status code
Public Class Methods
# File lib/swee/thin/response.rb, line 83 def initialize @headers = Headers.new @status = 200 @persistent = false @skip_body = false end
Public Instance Methods
Close any resource used by the response
# File lib/swee/thin/response.rb, line 142 def close @body.close if @body.respond_to?(:close) end
Yields each chunk of the response. To control the size of each chunk define your own each
method on body
.
# File lib/swee/thin/response.rb, line 149 def each yield head unless @skip_body if @body.is_a?(String) yield @body else @body.each { |chunk| yield chunk } end end end
Top header of the response, containing the status code and response headers.
# File lib/swee/thin/response.rb, line 102 def head "HTTP/1.1 #{@status} #{HTTP_STATUS_CODES[@status.to_i]}\r\n#{headers_output}\r\n" end
Ruby 1.9 doesn't have a String#each anymore. Rack
spec doesn't take care of that yet, for now we just use each
but fallback to each_line
on strings. I wish we could remove that condition. To be reviewed when a new Rack
spec comes out.
# File lib/swee/thin/response.rb, line 125 def headers=(key_value_pairs) key_value_pairs.each do |k, vs| next unless vs if vs.is_a?(Integer) vs = vs.to_s end if vs.is_a?(String) vs.each_line { |v| @headers[k] = v.chomp } else vs.each { |v| @headers[k] = v.chomp } end end if key_value_pairs end
String
representation of the headers to be sent in the response.
# File lib/swee/thin/response.rb, line 92 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
Tell the client the connection should stay open
# File lib/swee/thin/response.rb, line 162 def persistent! @persistent = true end
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.
# File lib/swee/thin/response.rb, line 169 def persistent? (@persistent && @headers.has_key?(CONTENT_LENGTH)) || PERSISTENT_STATUSES.include?(@status) end
# File lib/swee/thin/response.rb, line 173 def skip_body! @skip_body = true end