Skip to content
Permalink
Browse files

Unquote request path before passing to FTPClient, it already escape p…

…aths
  • Loading branch information
dangra committed Jul 1, 2015
1 parent 392233f commit cc00ad20cf3d20f7b8ef1b1ca5b97701fc04bad2
Showing with 18 additions and 3 deletions.
  1. +2 −2 scrapy/core/downloader/handlers/ftp.py
  2. +16 −1 tests/test_downloader_handlers.py
@@ -30,7 +30,7 @@

import re
from io import BytesIO
from six.moves.urllib.parse import urlparse
from six.moves.urllib.parse import urlparse, unquote

from twisted.internet import reactor
from twisted.protocols.ftp import FTPClient, CommandFailed
@@ -73,7 +73,7 @@ def download_request(self, request, spider):
request.meta["ftp_password"],
passive=request.meta.get("ftp_passive", 1))
return creator.connectTCP(parsed_url.hostname, parsed_url.port or 21).addCallback(self.gotClient,
request, parsed_url.path)
request, unquote(parsed_url.path))

def gotClient(self, client, request, filepath):
self.client = client
@@ -511,7 +511,9 @@ def setUp(self):
os.mkdir(self.directory)
userdir = os.path.join(self.directory, self.username)
os.mkdir(userdir)
FilePath(userdir).child('file.txt').setContent("I have the power!")
fp = FilePath(userdir)
fp.child('file.txt').setContent("I have the power!")
fp.child('file with spaces.txt').setContent("Moooooooooo power!")

# setup server
realm = FTPRealm(anonymousRoot=self.directory, userHome=self.directory)
@@ -547,6 +549,19 @@ def _test(r):
self.assertEqual(r.headers, {'Local Filename': [''], 'Size': ['17']})
return self._add_test_callbacks(d, _test)

def test_ftp_download_path_with_spaces(self):
request = Request(
url="ftp://127.0.0.1:%s/file with spaces.txt" % self.portNum,
meta={"ftp_user": self.username, "ftp_password": self.password}
)
d = self.download_handler.download_request(request, None)

def _test(r):
self.assertEqual(r.status, 200)
self.assertEqual(r.body, 'Moooooooooo power!')
self.assertEqual(r.headers, {'Local Filename': [''], 'Size': ['18']})
return self._add_test_callbacks(d, _test)

def test_ftp_download_notexist(self):
request = Request(url="ftp://127.0.0.1:%s/notexist.txt" % self.portNum,
meta={"ftp_user": self.username, "ftp_password": self.password})

0 comments on commit cc00ad2

Please sign in to comment.