2012-10-28 52 views
5

Sinatra是駱駝套的所有標題名稱,導致'P3P'的問題。代碼:如何阻止sinatra重寫我的標題名稱?

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) 

如何從重新寫入cookie名稱停止西納特拉。有我能做的猴子補丁嗎?

回答

5

HTTP header names should be case insensitive,所以理論上這應該不是問題 - P3p應該和P3P一樣好。

在你的情況下,標題是altered by the Webrick server,而不是Sinatra。更改爲其他服務器(如Thin(不會更改標頭))將是最簡單的修復方法(使用Thin over Webrick還有其他好處)。

如果你確實需要繼續使用Webrick,你可以考慮修補WEBrick::HTTPResponse。您必須小心,因爲Webrick會降低標題名稱以處理重複項,因此您可能必須對此進行說明。

相關問題