Action Dispatch Response¶ ↑
表示由控制器操作生成的 HTTP 响应。使用它来检索响应的当前状态,或自定义响应。它可以表示真实的 HTTP 响应(即要发送回 Web 浏览器的响应),也可以表示 TestResponse(即从集成测试生成的响应)。
当前请求的 Response 对象在控制器中通过 ActionController::Metal#response 暴露。 ActionController::Metal 还提供了一些附加方法,这些方法会委托给 Response 的属性,例如 ActionController::Metal#headers。
Integration 测试可能也需要更详细地检查响应。例如,Integration::RequestHelpers#get 和 Integration::RequestHelpers#post 等方法会为此目的返回 TestResponse(继承自 Response)的实例。
例如,以下演示集成测试会将控制器响应的主体打印到控制台
class DemoControllerTest < ActionDispatch::IntegrationTest def test_print_root_path_to_console get('/') puts response.body end end
- A
- B
- C
- D
- E
- G
- H
- L
- M
- N
- P
- R
- S
- T
- W
- Rack::Response::Helpers
- ActionDispatch::Http::FilterRedirect
- ActionDispatch::Http::Cache::Response
- MonitorMixin
常量
| CONTENT_TYPE | = | "Content-Type" |
| ContentTypeHeader | = | Struct.new :mime_type, :charset |
| Header | = | Headers |
即将弃用 |
||
| Headers | = | ::Rack::Headers |
| NO_CONTENT_CODES | = | [100, 101, 102, 103, 204, 205, 304] |
| NullContentTypeHeader | = | ContentTypeHeader.new nil, nil |
| SET_COOKIE | = | "Set-Cookie" |
Attributes
| [R] | 页眉 | 响应的标头。 header["Content-Type"] # => "text/plain" header["Content-Type"] = "application/json" header["Content-Type"] # => "application/json" 也别名为 headers["Content-Type"] # => "text/plain" headers["Content-Type"] = "application/json" headers["Content-Type"] # => "application/json" 为兼容性也别名为 |
| [R] | headers | 响应的标头。 header["Content-Type"] # => "text/plain" header["Content-Type"] = "application/json" header["Content-Type"] # => "application/json" 也别名为 headers["Content-Type"] # => "text/plain" headers["Content-Type"] = "application/json" headers["Content-Type"] # => "application/json" 为兼容性也别名为 |
| [RW] | request | 响应的请求。 |
| [R] | status | HTTP 状态码。 |
| [R] | stream | 底层主体,作为一个可流式传输的对象。 |
类公共方法
create(status = 200, headers = {}, body = [], default_headers: self.default_headers) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 185 def self.create(status = 200, headers = {}, body = [], default_headers: self.default_headers) headers = merge_default_headers(headers, default_headers) new status, headers, body end
merge_default_headers(original, default) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 190 def self.merge_default_headers(original, default) default.respond_to?(:merge) ? default.merge(original) : original end
new(status = 200, headers = nil, body = []) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 197 def initialize(status = 200, headers = nil, body = []) super() @headers = Headers.new headers&.each do |key, value| @headers[key] = value end self.body, self.status = body, status @cv = new_cond @committed = false @sending = false @sent = false prepare_cache_control! yield self if block_given? end
实例公共方法
abort() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 446 def abort if stream.respond_to?(:abort) stream.abort elsif stream.respond_to?(:close) # `stream.close` should really be reserved for a close from the other direction, # but we must fall back to it for compatibility. stream.close end end
await_commit() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 223 def await_commit synchronize do @cv.wait_until { @committed } end end
await_sent() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 229 def await_sent synchronize { @cv.wait_until { @sent } } end
body() 链接
将响应的内容作为字符串返回。这包含了对 render 的任何调用的内容。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 369 def body if @stream.respond_to?(:to_ary) @stream.to_ary.join elsif @stream.respond_to?(:body) @stream.body else @stream end end
body=(body) 链接
允许您手动设置或覆盖响应主体。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 384 def body=(body) # Prevent ActionController::Metal::Live::Response from committing the response prematurely. synchronize do if body.respond_to?(:to_str) @stream = build_buffer(self, [body]) elsif body.respond_to?(:to_path) @stream = body elsif body.respond_to?(:to_ary) @stream = build_buffer(self, body) else @stream = body end end end
body_parts() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 433 def body_parts parts = [] @stream.each { |x| parts << x } parts end
charset() 链接
响应的字符集。HTML 需要知道您提供内容的编码,因此我们需要将其一起发送。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 339 def charset header_info = parsed_content_type_header header_info.charset || self.class.default_charset end
charset=(charset) 链接
设置 HTTP 字符集。如果参数为 nil,则将字符集设置为 default_charset。
response.charset = 'utf-16' # => 'utf-16' response.charset = nil # => 'utf-8'
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 328 def charset=(charset) content_type = parsed_content_type_header.mime_type if false == charset set_content_type content_type, nil else set_content_type content_type, charset || self.class.default_charset end end
close() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 442 def close stream.close if stream.respond_to?(:close) end
code() 链接
返回一个字符串以确保与 Net::HTTPResponse 兼容。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 350 def code @status.to_s end
commit!() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 233 def commit! synchronize do before_committed @committed = true @cv.broadcast end end
committed?() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 257 def committed?; synchronize { @committed }; end
content_type() 链接
响应的内容类型。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 308 def content_type super.presence end
content_type=(content_type) 链接
设置 HTTP 响应的内容 MIME 类型。例如,在控制器中,您可以这样写:
response.content_type = "text/html"
此方法还接受一个带有 MIME 类型扩展名的符号
response.content_type = :html
如果为此响应定义了字符集(请参阅 charset=),那么字符集信息也将包含在内容类型信息中。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 289 def content_type=(content_type) case content_type when NilClass return when Symbol mime_type = Mime[content_type] raise ArgumentError, "Unknown MIME type #{content_type}" unless mime_type new_header_info = ContentTypeHeader.new(mime_type.to_s) else new_header_info = parse_content_type(content_type.to_s) end prev_header_info = parsed_content_type_header charset = new_header_info.charset || prev_header_info.charset charset ||= self.class.default_charset unless prev_header_info.mime_type set_content_type new_header_info.mime_type, charset end
cookies() 链接
将响应 cookie 转换为 (name => value) 对的 Hash。
assert_equal 'AuthorOfNewPage', r.cookies['author']
来源: 显示 | 在 GitHub 上
delete_header(key) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 221 def delete_header(key); @headers.delete key; end
each(&block) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 91 def each(&block) sending! x = @stream.each(&block) sent! x end
get_header(key) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 219 def get_header(key); @headers[key]; end
has_header?(key) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 218 def has_header?(key); @headers.key? key; end
location 链接
响应的位置。
来源: 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 261
location=(location) 链接
设置响应的位置
来源: 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 266
media_type() 链接
响应的媒体类型。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 313 def media_type parsed_content_type_header.mime_type end
message() 链接
返回当前 HTTP 状态码对应的消息
response.status = 200 response.message # => "OK" response.status = 404 response.message # => "Not Found"
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 362 def message Rack::Utils::HTTP_STATUS_CODES[@status] end
reset_body!() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 429 def reset_body! @stream = build_buffer(self, []) end
response_code() 链接
请求的响应代码。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 345 def response_code @status end
send_file(path) 链接
将存储在 path 的文件作为响应主体发送。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 424 def send_file(path) commit! @stream = FileBody.new(path) end
sending!() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 241 def sending! synchronize do before_sending @sending = true @cv.broadcast end end
sending?() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 256 def sending?; synchronize { @sending }; end
sending_file=(v) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 317 def sending_file=(v) if true == v self.charset = false end end
sent!() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 249 def sent! synchronize do @sent = true @cv.broadcast end end
sent?() 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 258 def sent?; synchronize { @sent }; end
set_header(key, v) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 220 def set_header(key, v); @headers[key] = v; end
status=(status) 链接
设置 HTTP 状态码。
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 273 def status=(status) @status = Response.rack_status_code(status) end
to_a() 链接
将 Response 转换为状态、标头和主体的 Rack 兼容数组。允许显式展开
status, headers, body = *response
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 460 def to_a commit! rack_response @status, @headers.to_hash end
write(string) 链接
来源: 显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/http/response.rb, line 379 def write(string) @stream.write string end