Skip to content

Commit bdcc78b

Browse files
committed
Merge pull request scrapy#1419 from scrapy/httprepr-on-file-scheme
Do not fail representing non-http requests
2 parents 5c4666a + 8d45b3c commit bdcc78b

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

scrapy/utils/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def request_httprepr(request):
7979
parsed = urlparse_cached(request)
8080
path = urlunparse(('', '', parsed.path or '/', parsed.params, parsed.query, ''))
8181
s = to_bytes(request.method) + b" " + to_bytes(path) + b" HTTP/1.1\r\n"
82-
s += b"Host: " + to_bytes(parsed.hostname) + b"\r\n"
82+
s += b"Host: " + to_bytes(parsed.hostname or b'') + b"\r\n"
8383
if request.headers:
8484
s += request.headers.to_string() + b"\r\n"
8585
s += b"\r\n"

tests/test_utils_request.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,10 @@ def test_request_httprepr(self):
7171
r1 = Request("http://www.example.com", method='POST', headers={"Content-type": b"text/html"}, body=b"Some body")
7272
self.assertEqual(request_httprepr(r1), b'POST / HTTP/1.1\r\nHost: www.example.com\r\nContent-Type: text/html\r\n\r\nSome body')
7373

74+
def test_request_httprepr_for_non_http_request(self):
75+
# the representation is not important but it must not fail.
76+
request_httprepr(Request("file:///tmp/foo.txt"))
77+
request_httprepr(Request("ftp://localhost/tmp/foo.txt"))
78+
7479
if __name__ == "__main__":
7580
unittest.main()

0 commit comments

Comments
 (0)