From 5a16e33f91f8581ac54f0853acf5334ac362cb1c Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sat, 20 Aug 2011 13:02:51 +0200 Subject: [PATCH] use 303 for redirects from post for HTTP/1.1 (fixes ajax redirects in Internet Explorer 9) --- lib/sinatra/base.rb | 6 +++++- test/helpers_test.rb | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index a3bff4a1..ad599d87 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -113,7 +113,11 @@ module Sinatra # Halt processing and redirect to the URI provided. def redirect(uri, *args) - status 302 + if env['HTTP_VERSION'] == 'HTTP/1.1' and env["REQUEST_METHOD"] != 'GET' + status 303 + else + status 302 + end # According to RFC 2616 section 14.30, "the field value consists of a # single absolute URI" diff --git a/test/helpers_test.rb b/test/helpers_test.rb index 1e033649..6a2aa197 100644 --- a/test/helpers_test.rb +++ b/test/helpers_test.rb @@ -207,6 +207,22 @@ class HelpersTest < Test::Unit::TestCase assert_equal 'http://example.org:444/foo', response['Location'] end + it 'uses 303 for post requests if request is HTTP 1.1' do + mock_app { post('/') { redirect '/'} } + post '/', {}, 'HTTP_VERSION' => 'HTTP/1.1' + assert_equal 303, status + assert_equal '', body + assert_equal 'http://example.org/', response['Location'] + end + + it 'uses 302 for post requests if request is HTTP 1.0' do + mock_app { post('/') { redirect '/'} } + post '/', {}, 'HTTP_VERSION' => 'HTTP/1.0' + assert_equal 302, status + assert_equal '', body + assert_equal 'http://example.org/', response['Location'] + end + it 'works behind a reverse proxy' do mock_app do get '/' do