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