Can You Specify The Http Method to Use with Sinatra's Redirect

Can you specify the HTTP method to use with Sinatra's redirect?

Most browsers will reset to GET with a 302 (default) redirect. If you're up against a browser that is actually doing the correct thing and NOT changing the request method, you can force it to change with a 303.

redirect '/login', 303

Source:
http://www.gittr.com/index.php/archive/details-of-sinatras-redirect-helper/

Difference between `redirect` and `redirect to` in Sinatra

The redirect method sends the HTTP header to redirect the client to a given URL, and the argument passed should be a fully qualified URL with a host (e.g. http://example.com/path, not just /path).

The to method converts a path to a full URL for your Sinatra app, allowing the resulting URL to be used in redirect. E.g. to('/path') would become http://yoursinatraapp/path.

Cross Domain Post to Sinatra

Yes, you can, by specifying an origin whitelist in Sinatra's options:

configure do
set :protection, :origin_whitelist => ['http://example.com']
end

Replace http://example.com with the URL where you're sending the POST request from.

How to fix Sinatra redirecting https to http under nginx

In order for Sinatra to correctly assemble the url used for redirects, it needs to be able to determine whether the request is using ssl, so that the redirect can be made using http or https as appropriate.

Obviously the actual call to thin isn't using ssl, as this is being handled by the front end web server, and the proxied request is in the clear. We therefore need a way to tell Sinatra that it should treat the request as secure, even though it isn't actually using ssl.

Ultimately the code that determines whether the request should be treated as secure is in the Rack::Request#ssl? and Rack::Request#scheme methods. The scheme methods examines the env hash to see if one of a number of entries are present. One of these is HTTP_X_FORWARDED_PROTO which corresponds to the X-Forwarded-Proto HTTP header. If this is set, then the value is used as the protocol scheme (http or https).

So if we add this HTTP header to the request when it is proxied from nginx to the back end, Sinatra will be able to correctly determine when to redirect to https. In nginx we can add headers to proxied requests with proxy_set_header, and the scheme is available in the $scheme variable.

So adding the line

proxy_set_header X-Forwarded-Proto $scheme;

to the nginx configuration after the proxy_pass line should make it work.



Related Topics



Leave a reply



Submit