红宝石是否从响应头中删除标题?

时间:2018-07-24 06:39:22

标签: ruby-on-rails ruby

我直接从博客中获取html内容为:

response = Net::HTTP.get_response(uri)

respond_to do |format|
  format.html { render :text => response.body }
end

尽管在博客引擎(WordPress)中,我如何添加头文件Access-Control-Allow-Origin: *,却发现它未在响应中传递。

但是,如果我使用邮差获取页面或直接在浏览器中查看页面,则可以看到标题在那里。

编辑

我可以看到其他标头通过,例如:

cache-control: no-cache, must-revalidate, max-age=0
content-type: text/html; charset=UTF-8
date: Tue, 24 Jul 2018 06:37:57 GMT
expires: Wed, 11 Jan 1984 05:00:00 GMT

有什么主意吗?

3 个答案:

答案 0 :(得分:4)

response.body将返回您身体的一部分,而不是header部分。您可以将响应转换为哈希并检查标题,如下所示:

> url = "https://stackoverflow.com/questions/51492025/does-ruby-strip-headers-from-response"
> uri = URI.parse(url)
> response = Net::HTTP.get_response(uri)
#=> #<Net::HTTPOK 200 OK readbody=true> 
> response.to_hash
#=> {"cache-control"=>["private"], "content-type"=>["text/html; charset=utf-8"], "last-modified"=>["Tue, 24 Jul 2018 07:04:00 GMT"], "x-frame-options"=>["SAMEORIGIN"], "x-request-guid"=>["22a4b6b6-3039-46e2-b4de-c8af7cad6659"], "strict-transport-security"=>["max-age=15552000"], "content-security-policy"=>["upgrade-insecure-requests"], "accept-ranges"=>["bytes", "bytes"], "age"=>["0", "0"], "content-length"=>["31575"], "date"=>["Tue, 24 Jul 2018 07:04:46 GMT"], "via"=>["1.1 varnish"], "connection"=>["keep-alive"], "x-served-by"=>["cache-bom18221-BOM"], "x-cache"=>["MISS"], "x-cache-hits"=>["0"], "x-timer"=>["S1532415886.990199,VS0,VE280"], "vary"=>["Accept-Encoding,Fastly-SSL"], "x-dns-prefetch-control"=>["off"], "set-cookie"=>["prov=a7dfe911-76a1-f1c1-093b-3fc8fe79af65; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly"]}

您可以通过传递标头名称来访问以下特定标头:

> response['Cache-Control']
#=> "private" 

有关更多详细信息,请阅读:https://ruby-doc.org/stdlib-2.5.1/libdoc/net/http/rdoc/Net/HTTP.html

答案 1 :(得分:2)

Net::HTTPResponse(即您的response)混入Net::HTTPHeader中。因此,您可以将单个标头设为response['Access-Control-Allow-Origin'],使用response.each_header对其进行迭代,甚至可以使用response.to_hash将它们全部作为哈希值。

答案 2 :(得分:2)

如果要传递从您要获取的主机提供的标头,则首先需要将博客的响应存储在另一个变量名中。我们称之为blog_response(这是因为response是Rails控制器实例中预先存在的特殊方法名称。)

blog_response = Net::HTTP.get_response(uri)

然后,您需要像这样从blog_response获取您关心的标题:

header_name, header_value = blog_response.each_header.find do |name, value| 
  name =~ /pattern-matching-a-header-name-i-care-about/i  #case insensitive regex matching recommended for http headers
end

然后您需要在呈现响应之前在控制器中设置它们,例如:

response.headers[header_name] = header_value

respond_to do |format|
  format.html { render :text => blog_response.body }
end

此示例显然仅适用于一个标头,但您可以通过在响应中迭代,匹配和设置它们来复制多个标头,如下所示:

blog_response.each_header.select do |name, value| 
  if name =~ /pattern-matching-header-names-i-care-about|some-other-pattern-i-care-about/i  #case insensitive regex matching recommended for http headers
    response.headers[name] = value
  end
end

如果要通过所有标头,只需执行以下操作:

blog_response.each_header do |name, value| 
  response.headers[name] = value
end

respond_to do |format|
  format.html { render :text => blog_response.body }
end