Skip to content

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

Fixes for bug - Cookie retrieval for hosts with port + unittests for the fix

arijitchakrabortyebc136ddb068
diff --git a/scrapy/http/cookies.py b/scrapy/http/cookies.py
index 8ad8de290..6902d29e8 100644
--- a/scrapy/http/cookies.py
+++ b/scrapy/http/cookies.py
@@ -23,6 +23,11 @@ class CookieJar(object):
# the cookiejar implementation iterates through all domains
# instead we restrict to potential matches on the domain
req_host = urlparse_cached(request).netloc
+
+ # Strip port numbers from netloc, if present
+ if ':' in req_host:
+ req_host = req_host.split(':')[0]
+
if not IPV4_RE.search(req_host):
hosts = potential_domain_matches(req_host)
if req_host.find(".") == -1:
diff --git a/scrapy/tests/test_downloadermiddleware_cookies.py b/scrapy/tests/test_downloadermiddleware_cookies.py
index d9ed476f1..19c720a75 100644
--- a/scrapy/tests/test_downloadermiddleware_cookies.py
+++ b/scrapy/tests/test_downloadermiddleware_cookies.py
@@ -113,3 +113,19 @@ class CookiesMiddlewareTest(TestCase):
req4 = Request('http://scrapytest.org/', meta=res2.meta)
assert self.mw.process_request(req4, self.spider) is None
self.assertEquals(req4.headers.get('Cookie'), 'C2=value2; galleta=dulce')
+
+ #cookies from hosts with port
+ req5_1 = Request('http://scrapytest.org:1104/')
+ assert self.mw.process_request(req5_1, self.spider) is None
+
+ headers = {'Set-Cookie': 'C1=value1; path=/'}
+ res5_1 = Response('http://scrapytest.org:1104/', headers=headers, request=req5_1)
+ assert self.mw.process_response(req5_1, res5_1, self.spider) is res5_1
+
+ req5_2 = Request('http://scrapytest.org:1104/some-redirected-path')
+ assert self.mw.process_request(req5_2, self.spider) is None
+ self.assertEquals(req5_2.headers.get('Cookie'), 'C1=value1')
+
+ req5_3 = Request('http://scrapytest.org/some-redirected-path')
+ assert self.mw.process_request(req5_3, self.spider) is None
+ self.assertEquals(req5_3.headers.get('Cookie'), 'C1=value1')