Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I stop sinatra from re-writing my header names?

Sinatra is camel-casing all the header names, causing issues with 'P3P'. Code:

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

give me

* 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

as you can see this-that gets rewritten to This-That and P3P gets rewritten to P3p. Here is my gem list:

* LOCAL GEMS *

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)

How do I stop sinatra from re-writing the cookie names. Is there a monkey patch I can do?

like image 714
browep Avatar asked Oct 28 '12 16:10

browep


People also ask

What is Sinatra :: Base?

Sinatra::Base is the Sinatra without delegation. Consider the following code with delegation: # app.rb require 'sinatra' get '/' do render :template end.


1 Answers

HTTP header names should be case insensitive, so in theory this shouldn’t be a problem – P3p should work just as well as P3P.

In your case the headers are being altered by the Webrick server, not Sinatra. Changing to another server such as Thin (which doesn’t alter the headers) would be the easiest fix (there are other benefits with using Thin over Webrick as well).

If you really need to keep using Webrick, you could look into patching WEBrick::HTTPResponse. You’d have to be careful, as Webrick downcases the headers names to handle duplicates so you might have to account for that.

like image 61
matt Avatar answered Sep 28 '22 01:09

matt