Class: Thin::Response
- Inherits:
-
Object
- Object
- Thin::Response
- Defined in:
- lib/thin/response.rb
Overview
A response sent to the client.
Defined Under Namespace
Classes: Stream
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
#initialize ⇒ Response
Returns a new instance of Response.
72 73 74 75 76 77 |
# File 'lib/thin/response.rb', line 72 def initialize @headers = Headers.new @status = 200 @persistent = false @skip_body = false end |
Instance Attribute Details
#body ⇒ Object
Response body, must respond to each.
67 68 69 |
# File 'lib/thin/response.rb', line 67 def body @body end |
#headers ⇒ Object
Headers key-value hash
70 71 72 |
# File 'lib/thin/response.rb', line 70 def headers @headers end |
#status ⇒ Object
Status code
64 65 66 |
# File 'lib/thin/response.rb', line 64 def status @status end |
Instance Method Details
#close ⇒ Object
Close any resource used by the response
107 108 109 |
# File 'lib/thin/response.rb', line 107 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.
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/thin/response.rb', line 114 def each(&block) yield head unless @skip_body if @body.is_a?(String) yield @body elsif @body.respond_to?(:each) @body.each { |chunk| yield chunk } else @body.call(Stream.new(block)) end end end |
#head ⇒ Object
Top header of the response, containing the status code and response headers.
91 92 93 |
# File 'lib/thin/response.rb', line 91 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.
81 82 83 84 85 86 87 |
# File 'lib/thin/response.rb', line 81 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
129 130 131 |
# File 'lib/thin/response.rb', line 129 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.
136 137 138 |
# File 'lib/thin/response.rb', line 136 def persistent? (@persistent && @headers.has_key?(CONTENT_LENGTH)) || PERSISTENT_STATUSES.include?(@status) end |
#skip_body! ⇒ Object
140 141 142 |
# File 'lib/thin/response.rb', line 140 def skip_body! @skip_body = true end |