Skip to content

Commit

Permalink
[ftp] Extend path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
th3hamm0r committed Jul 15, 2024
1 parent b37d53e commit 312f0d1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
6 changes: 3 additions & 3 deletions storages/backends/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _decode_location(self, location):
"""Return splitted configuration data from location."""
splitted_url = re.search(
r"^(?P<scheme>.+)://(?P<user>.+):(?P<passwd>.+)@"
r"(?P<host>.+):(?P<port>\d+)/(?P<path>.*)$",
r"(?P<host>.+):(?P<port>\d+)(?P<path>/.*)?$",
location,
)

Expand All @@ -75,7 +75,7 @@ def _decode_location(self, location):
config["active"] = splitted_url["scheme"] == "aftp"
config["secure"] = splitted_url["scheme"] == "ftps"

config["path"] = splitted_url["path"] or "/"
config["path"] = splitted_url["path"][1:] if splitted_url["path"] else None
config["host"] = splitted_url["host"]
config["user"] = splitted_url["user"]
config["passwd"] = splitted_url["passwd"]
Expand All @@ -102,7 +102,7 @@ def _start_connection(self):
ftp.prot_p()
if self._config["active"]:
ftp.set_pasv(False)
if self._config["path"] != "":
if self._config["path"]:
ftp.cwd(self._config["path"])
self._connection = ftp
return
Expand Down
50 changes: 48 additions & 2 deletions tests/test_ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,39 @@ def test_init_location_from_setting(self, mock_setting):

def test_decode_location(self):
config = self.storage._decode_location(URL)
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": False,
"path": "",
"port": 2121,
"secure": False,
}
self.assertEqual(config, wanted_config)
config = self.storage._decode_location("ftp://foo:b@r@localhost:2121")
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": False,
"path": None,
"port": 2121,
"secure": False,
}
self.assertEqual(config, wanted_config)
config = self.storage._decode_location("ftp://foo:b@r@localhost:2121/test/dir")
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": False,
"path": "test/dir",
"port": 2121,
"secure": False,
}
self.assertEqual(config, wanted_config)
config = self.storage._decode_location("ftp://foo:b@r@localhost:2121//")
wanted_config = {
"passwd": "b@r",
"host": "localhost",
Expand All @@ -59,14 +92,27 @@ def test_decode_location(self):
"secure": False,
}
self.assertEqual(config, wanted_config)
config = self.storage._decode_location(
"ftp://foo:b@r@localhost:2121//root/test/dir"
)
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": False,
"path": "/root/test/dir",
"port": 2121,
"secure": False,
}
self.assertEqual(config, wanted_config)
# Test active FTP
config = self.storage._decode_location("a" + URL)
wanted_config = {
"passwd": "b@r",
"host": "localhost",
"user": "foo",
"active": True,
"path": "/",
"path": "",
"port": 2121,
"secure": False,
}
Expand Down Expand Up @@ -277,7 +323,7 @@ def test_decode_location(self):
"host": "localhost",
"user": "foo",
"active": False,
"path": "/",
"path": "",
"port": 2121,
"secure": True,
}
Expand Down

0 comments on commit 312f0d1

Please sign in to comment.