mirror of https://github.com/scrapy/scrapy.git
Merge pull request #420 from nramirezuy/httpauth-multispider
httpauth mid multispider support removed
This commit is contained in:
commit
9ad736d7ce
|
|
@ -5,23 +5,27 @@ See documentation in docs/topics/downloader-middleware.rst
|
|||
"""
|
||||
|
||||
from w3lib.http import basic_auth_header
|
||||
from scrapy.utils.python import WeakKeyCache
|
||||
|
||||
from scrapy import signals
|
||||
|
||||
|
||||
class HttpAuthMiddleware(object):
|
||||
"""Set Basic HTTP Authorization header
|
||||
(http_user and http_pass spider class attributes)"""
|
||||
|
||||
def __init__(self):
|
||||
self._cache = WeakKeyCache(self._authorization)
|
||||
@classmethod
|
||||
def from_crawler(cls, crawler):
|
||||
o = cls()
|
||||
crawler.signals.connect(o.spider_opened, signal=signals.spider_opened)
|
||||
return o
|
||||
|
||||
def _authorization(self, spider):
|
||||
def spider_opened(self, spider):
|
||||
usr = getattr(spider, 'http_user', '')
|
||||
pwd = getattr(spider, 'http_pass', '')
|
||||
if usr or pwd:
|
||||
return basic_auth_header(usr, pwd)
|
||||
self.auth = basic_auth_header(usr, pwd)
|
||||
|
||||
def process_request(self, request, spider):
|
||||
auth = self._cache[spider]
|
||||
auth = getattr(self, 'auth', None)
|
||||
if auth and 'Authorization' not in request.headers:
|
||||
request.headers['Authorization'] = auth
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ class HttpAuthMiddlewareTest(unittest.TestCase):
|
|||
def setUp(self):
|
||||
self.mw = HttpAuthMiddleware()
|
||||
self.spider = TestSpider('foo')
|
||||
self.mw.spider_opened(self.spider)
|
||||
|
||||
def tearDown(self):
|
||||
del self.mw
|
||||
|
|
|
|||
Loading…
Reference in New Issue