【问题标题】:how do I stop sinatra from re-writing my header names?如何阻止 sinatra 重写我的标题名称?
【发布时间】:2012-10-18 02:56:59
【问题描述】:

Sinatra 对所有标题名称采用驼峰式命名,导致“P3P”出现问题。代码:

require 'rubygems'
require 'sinatra'

configure :production do
  # Configure stuff here you'll want to
  # only be run at Heroku at boot

  # TIP:  You can get you database information
  #       from ENV['DATABASE_URI'] (see /env route below) 
end

# Quick test
get '/track' do
  #response.headers['Cache-Control'] = 'public, max-age=300'
  response.set_cookie("visited",1)
  response['this-that'] = "CP=\"CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE\""
  response['P3P'] = "CP=\"CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE\""
  "SUCCESS"
end

给我

* About to connect() to localhost port 9393 (#0)
*   Trying 127.0.0.1... connected
> GET /track HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:9393
> Accept: */*
> 
< HTTP/1.1 200 OK 
< X-Frame-Options: sameorigin
< X-Xss-Protection: 1; mode=block
< Content-Type: text/html;charset=utf-8
< This-That: CP="CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE"
< P3p: CP="CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE"
< Content-Length: 7
< Server: WEBrick/1.3.1 (Ruby/1.9.2/2012-04-20)
< Date: Sun, 28 Oct 2012 16:26:47 GMT
< Connection: Keep-Alive
< Set-Cookie: visited=1
< 
* Connection #0 to host localhost left intact
* Closing connection #0
SUCCESS

如您所见,this-that 被重写为 This-ThatP3P 被重写为 P3p。这是我的宝石清单:

* 本地宝石 *

addressable (2.3.2)
bundler (1.2.1)
excon (0.16.7)
heroku (2.32.14)
heroku-api (0.3.5)
launchy (2.1.2)
mime-types (1.19)
netrc (0.7.7)
rack (1.4.1)
rack-protection (1.2.0)
rake (0.9.2.2)
rest-client (1.6.7)
rubygems-bundler (1.1.0)
rubyzip (0.9.9)
rvm (1.11.3.5)
shotgun (0.9)
sinatra (1.3.3)
tilt (1.3.3)

如何阻止 sinatra 重写 cookie 名称。有没有我可以做的猴子补丁?

【问题讨论】:

    标签: ruby http-headers sinatra rack p3p


    【解决方案1】:

    HTTP header names should be case insensitive,所以理论上这应该不是问题——P3p 应该和P3P 一样好用。

    在您的情况下,标题是altered by the Webrick server,而不是 Sinatra。更改为其他服务器,例如 Thin(不会更改标头)将是最简单的解决方法(使用 Thin 而不是 Webrick 还有其他好处)。

    如果你真的需要继续使用 Webrick,你可以考虑修补 WEBrick::HTTPResponse。您必须小心,因为 Webrick 将标头名称小写以处理重复项,因此您可能必须考虑到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 2021-11-06
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2014-06-17
      相关资源
      最近更新 更多