Skip to content

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

bugfix in request_httprepr() function

Pablo Hoffmanb8aa74ee9ec6
diff --git a/scrapy/tests/test_utils_request.py b/scrapy/tests/test_utils_request.py
index ae09b9e5e..f554c615c 100644
--- a/scrapy/tests/test_utils_request.py
+++ b/scrapy/tests/test_utils_request.py
@@ -63,10 +63,13 @@ class UtilsRequestTest(unittest.TestCase):
def test_request_httprepr(self):
r1 = Request("http://www.example.com")
- self.assertEqual(request_httprepr(r1), 'GET http://www.example.com HTTP/1.1\r\nHost: www.example.com\r\n\r\n')
+ self.assertEqual(request_httprepr(r1), 'GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n')
+
+ r1 = Request("http://www.example.com/some/page.html?arg=1")
+ self.assertEqual(request_httprepr(r1), 'GET /some/page.html?arg=1 HTTP/1.1\r\nHost: www.example.com\r\n\r\n')
r1 = Request("http://www.example.com", method='POST', headers={"Content-type": "text/html"}, body="Some body")
- self.assertEqual(request_httprepr(r1), 'POST http://www.example.com HTTP/1.1\r\nHost: www.example.com\r\nContent-Type: text/html\r\n\r\nSome body')
+ self.assertEqual(request_httprepr(r1), 'POST / HTTP/1.1\r\nHost: www.example.com\r\nContent-Type: text/html\r\n\r\nSome body')
if __name__ == "__main__":
unittest.main()
diff --git a/scrapy/utils/request.py b/scrapy/utils/request.py
index 27a6b5d05..472f31dbd 100644
--- a/scrapy/utils/request.py
+++ b/scrapy/utils/request.py
@@ -6,6 +6,7 @@ scrapy.http.Request objects
import hashlib
import weakref
from base64 import urlsafe_b64encode
+from urlparse import urlunparse
from scrapy.utils.url import canonicalize_url
from scrapy.utils.httpobj import urlparse_cached
@@ -76,9 +77,10 @@ def request_httprepr(request):
bytes that will be send when performing the request (that's controlled
by Twisted).
"""
- hostname = urlparse_cached(request).hostname
- s = "%s %s HTTP/1.1\r\n" % (request.method, request.url)
- s += "Host: %s\r\n" % hostname
+ parsed = urlparse_cached(request)
+ path = urlunparse(('', '', parsed.path or '/', parsed.params, parsed.query, ''))
+ s = "%s %s HTTP/1.1\r\n" % (request.method, path)
+ s += "Host: %s\r\n" % parsed.hostname
if request.headers:
s += request.headers.to_string() + "\r\n"
s += "\r\n"