Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

ensure meta refresh redirections use GET requests even if coming from POSTs

Daniel Grana0d637a67034a
diff --git a/scrapy/contrib/downloadermiddleware/redirect.py b/scrapy/contrib/downloadermiddleware/redirect.py
index b8085f40a..e4acbc8cb 100644
--- a/scrapy/contrib/downloadermiddleware/redirect.py
+++ b/scrapy/contrib/downloadermiddleware/redirect.py
@@ -28,7 +28,9 @@ class RedirectMiddleware(object):
interval, url = get_meta_refresh(response)
if url and interval < self.max_metarefresh_delay:
- redirected = request.replace(url=url)
+ redirected = request.replace(url=url, method='GET', body='')
+ redirected.headers.pop('Content-Type', None)
+ redirected.headers.pop('Content-Length', None)
return self._redirect(redirected, request, spider, 'meta refresh')
return response
diff --git a/scrapy/tests/test_downloadermiddleware_redirect.py b/scrapy/tests/test_downloadermiddleware_redirect.py
index d031e234c..ba7daece8 100644
--- a/scrapy/tests/test_downloadermiddleware_redirect.py
+++ b/scrapy/tests/test_downloadermiddleware_redirect.py
@@ -64,6 +64,7 @@ class RedirectMiddlewareTest(unittest.TestCase):
assert isinstance(req2, Request)
self.assertEqual(req2.url, 'http://example.org/newpage')
+ def test_meta_refresh_with_high_interval(self):
# meta-refresh with high intervals don't trigger redirects
body = """<html>
<head><meta http-equiv="refresh" content="1000;url=http://example.org/newpage" /></head>
@@ -74,6 +75,25 @@ class RedirectMiddlewareTest(unittest.TestCase):
assert rsp is rsp2
+ def test_meta_refresh_trough_posted_request(self):
+ body = """<html>
+ <head><meta http-equiv="refresh" content="5;url=http://example.org/newpage" /></head>
+ </html>"""
+ req = Request(url='http://example.org', method='POST', body='test',
+ headers={'Content-Type': 'text/plain', 'Content-length': '4'})
+ rsp = Response(url='http://example.org', body=body)
+ req2 = self.mw.process_response(req, rsp, self.spider)
+
+ assert isinstance(req2, Request)
+ self.assertEqual(req2.url, 'http://example.org/newpage')
+ self.assertEqual(req2.method, 'GET')
+ assert 'Content-Type' not in req2.headers, \
+ "Content-Type header must not be present in redirected request"
+ assert 'Content-Length' not in req2.headers, \
+ "Content-Length header must not be present in redirected request"
+ assert not req2.body, \
+ "Redirected body must be empty, not '%s'" % req2.body
+
def test_max_redirect_times(self):
self.mw.max_redirect_times = 1
req = Request('http://scrapytest.org/302')