From 58dc16f48dcf53ba7e7be20ed335b972e9f9e8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Thu, 30 May 2013 18:32:05 -0300 Subject: [PATCH] obey request method when scrapy deploy is redirected to a new endpoint --- scrapy/commands/deploy.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/scrapy/commands/deploy.py b/scrapy/commands/deploy.py index e652e7ff8..822a98c99 100644 --- a/scrapy/commands/deploy.py +++ b/scrapy/commands/deploy.py @@ -67,10 +67,14 @@ class Command(ScrapyCommand): import setuptools except ImportError: raise UsageError("setuptools not installed") + + urllib2.install_opener(urllib2.build_opener(HTTPRedirectHandler)) + if opts.list_targets: for name, target in _get_targets().items(): print "%-20s %s" % (name, target['url']) return + if opts.list_projects: target = _get_target(opts.list_projects) req = urllib2.Request(_url(target, 'listprojects.json')) @@ -230,3 +234,24 @@ def _build_egg(): def _create_default_setup_py(**kwargs): with open('setup.py', 'w') as f: f.write(_SETUP_PY_TEMPLATE % kwargs) + + +class HTTPRedirectHandler(urllib2.HTTPRedirectHandler): + + def redirect_request(self, req, fp, code, msg, headers, newurl): + newurl = newurl.replace(' ', '%20') + if code in (301, 307): + return urllib2.Request(newurl, + data=req.get_data(), + headers=req.headers, + origin_req_host=req.get_origin_req_host(), + unverifiable=True) + elif code in (302, 303): + newheaders = dict((k, v) for k, v in req.headers.items() + if k.lower() not in ("content-length", "content-type")) + return urllib2.Request(newurl, + headers=newheaders, + origin_req_host=req.get_origin_req_host(), + unverifiable=True) + else: + raise urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp)