Module: Rack::Response::Helpers
- Included in:
- Rack::Response, Raw
- Defined in:
- vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb
Instance Method Summary collapse
- #accepted? ⇒ Boolean
-
#add_header(key, value) ⇒ Object
Add a header that may have multiple values.
- #append(chunk) ⇒ Object protected
- #bad_request? ⇒ Boolean
-
#buffered_body! ⇒ Boolean
protected
Convert the body of this response into an internally buffered Array if possible.
-
#cache!(duration = 3600, directive: "public") ⇒ Object
Specify that the content should be cached.
- #cache_control ⇒ Object
- #cache_control=(value) ⇒ Object
- #client_error? ⇒ Boolean
- #content_length ⇒ Object
-
#content_type ⇒ Object
Get the content type of the response.
-
#content_type=(content_type) ⇒ Object
Set the content type of the response.
- #created? ⇒ Boolean
- #delete_cookie(key, value = {}) ⇒ Object
-
#do_not_cache! ⇒ Object
Specifies that the content shouldn't be cached.
- #etag ⇒ Object
- #etag=(value) ⇒ Object
- #forbidden? ⇒ Boolean
- #include?(header) ⇒ Boolean
- #informational? ⇒ Boolean
- #invalid? ⇒ Boolean
- #location ⇒ Object
- #location=(location) ⇒ Object
- #media_type ⇒ Object
- #media_type_params ⇒ Object
- #method_not_allowed? ⇒ Boolean
- #moved_permanently? ⇒ Boolean
- #no_content? ⇒ Boolean
- #not_acceptable? ⇒ Boolean
- #not_found? ⇒ Boolean
- #ok? ⇒ Boolean
- #precondition_failed? ⇒ Boolean
- #redirect? ⇒ Boolean
- #redirection? ⇒ Boolean
- #request_timeout? ⇒ Boolean
- #server_error? ⇒ Boolean
- #set_cookie(key, value) ⇒ Object
- #set_cookie_header ⇒ Object
- #set_cookie_header=(value) ⇒ Object
- #successful? ⇒ Boolean
- #unauthorized? ⇒ Boolean
- #unprocessable? ⇒ Boolean
Instance Method Details
#accepted? ⇒ Boolean
191 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 191 def accepted?; status == 202; end |
#add_header(key, value) ⇒ Object
Add a header that may have multiple values.
Example: response.add_header 'vary', 'accept-encoding' response.add_header 'vary', 'cookie'
assert_equal 'accept-encoding,cookie', response.get_header('vary')
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 219 def add_header(key, value) raise ArgumentError unless key.is_a?(String) if value.nil? return get_header(key) end value = value.to_s if header = get_header(key) if header.is_a?(Array) header << value else set_header(key, [header, value]) end else set_header(key, value) end end |
#append(chunk) ⇒ Object (protected)
359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 359 def append(chunk) chunk = chunk.dup unless chunk.frozen? @body << chunk if @length @length += chunk.bytesize elsif @buffered @length = chunk.bytesize end return chunk end |
#bad_request? ⇒ Boolean
194 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 194 def bad_request?; status == 400; end |
#buffered_body! ⇒ Boolean (protected)
Convert the body of this response into an internally buffered Array if possible.
@buffered
is a ternary value which indicates whether the body is buffered. It can be:
nil
- The body has not been buffered yet.true
- The body is buffered as an Array instance.false
- The body is not buffered and cannot be buffered.
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 332 def buffered_body! if @buffered.nil? if @body.is_a?(Array) # The user supplied body was an array: @body = @body.compact @length = @body.sum{|part| part.bytesize} @buffered = true elsif @body.respond_to?(:each) # Turn the user supplied body into a buffered array: body = @body @body = Array.new @buffered = true body.each do |part| @writer.call(part.to_s) end body.close if body.respond_to?(:close) else # We don't know how to buffer the user-supplied body: @buffered = false end end return @buffered end |
#cache!(duration = 3600, directive: "public") ⇒ Object
Specify that the content should be cached.
307 308 309 310 311 312 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 307 def cache!(duration = 3600, directive: "public") unless headers[CACHE_CONTROL] =~ /no-cache/ set_header CACHE_CONTROL, "#{directive}, max-age=#{duration}" set_header EXPIRES, (Time.now + duration).httpdate end end |
#cache_control ⇒ Object
290 291 292 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 290 def cache_control get_header CACHE_CONTROL end |
#cache_control=(value) ⇒ Object
294 295 296 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 294 def cache_control=(value) set_header CACHE_CONTROL, value end |
#client_error? ⇒ Boolean
186 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 186 def client_error?; status >= 400 && status < 500; end |
#content_length ⇒ Object
257 258 259 260 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 257 def content_length cl = get_header CONTENT_LENGTH cl ? cl.to_i : cl end |
#content_type ⇒ Object
Get the content type of the response.
240 241 242 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 240 def content_type get_header CONTENT_TYPE end |
#content_type=(content_type) ⇒ Object
Set the content type of the response.
245 246 247 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 245 def content_type=(content_type) set_header CONTENT_TYPE, content_type end |
#created? ⇒ Boolean
190 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 190 def created?; status == 201; end |
#delete_cookie(key, value = {}) ⇒ Object
274 275 276 277 278 279 280 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 274 def (key, value = {}) set_header(SET_COOKIE, Utils.( get_header(SET_COOKIE), key, value ) ) end |
#do_not_cache! ⇒ Object
Specifies that the content shouldn't be cached. Overrides cache!
if already called.
299 300 301 302 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 299 def do_not_cache! set_header CACHE_CONTROL, "no-cache, must-revalidate" set_header EXPIRES, Time.now.httpdate end |
#etag ⇒ Object
314 315 316 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 314 def etag get_header ETAG end |
#etag=(value) ⇒ Object
318 319 320 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 318 def etag=(value) set_header ETAG, value end |
#forbidden? ⇒ Boolean
196 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 196 def forbidden?; status == 403; end |
#include?(header) ⇒ Boolean
206 207 208 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 206 def include?(header) has_header?(header) end |
#informational? ⇒ Boolean
183 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 183 def informational?; status >= 100 && status < 200; end |
#invalid? ⇒ Boolean
181 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 181 def invalid?; status < 100 || status >= 600; end |
#location ⇒ Object
262 263 264 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 262 def location get_header "location" end |
#location=(location) ⇒ Object
266 267 268 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 266 def location=(location) set_header "location", location end |
#media_type ⇒ Object
249 250 251 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 249 def media_type MediaType.type(content_type) end |
#media_type_params ⇒ Object
253 254 255 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 253 def media_type_params MediaType.params(content_type) end |
#method_not_allowed? ⇒ Boolean
198 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 198 def method_not_allowed?; status == 405; end |
#moved_permanently? ⇒ Boolean
193 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 193 def moved_permanently?; status == 301; end |
#no_content? ⇒ Boolean
192 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 192 def no_content?; status == 204; end |
#not_acceptable? ⇒ Boolean
199 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 199 def not_acceptable?; status == 406; end |
#not_found? ⇒ Boolean
197 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 197 def not_found?; status == 404; end |
#ok? ⇒ Boolean
189 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 189 def ok?; status == 200; end |
#precondition_failed? ⇒ Boolean
201 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 201 def precondition_failed?; status == 412; end |
#redirect? ⇒ Boolean
204 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 204 def redirect?; [301, 302, 303, 307, 308].include? status; end |
#redirection? ⇒ Boolean
185 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 185 def redirection?; status >= 300 && status < 400; end |
#request_timeout? ⇒ Boolean
200 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 200 def request_timeout?; status == 408; end |
#server_error? ⇒ Boolean
187 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 187 def server_error?; status >= 500 && status < 600; end |
#set_cookie(key, value) ⇒ Object
270 271 272 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 270 def (key, value) add_header SET_COOKIE, Utils.(key, value) end |
#set_cookie_header ⇒ Object
282 283 284 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 282 def get_header SET_COOKIE end |
#set_cookie_header=(value) ⇒ Object
286 287 288 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 286 def (value) set_header SET_COOKIE, value end |
#successful? ⇒ Boolean
184 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 184 def successful?; status >= 200 && status < 300; end |
#unauthorized? ⇒ Boolean
195 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 195 def ; status == 401; end |
#unprocessable? ⇒ Boolean
202 |
# File 'vendor/bundler/ruby/3.4.0/bundler/gems/rack-40d68f842b5f/lib/rack/response.rb', line 202 def unprocessable?; status == 422; end |