Document ftp: fix unicode in NLST

At the nlst command, the paths from the API could have been unicode, in
which case the '\r\n' separator would be sent as '\00\r\00\n' over the
wire (that's utf-16 encoding).
For that, bring the utf-8 conversion functions inside ftpserver.

bzr revid: p_christ@hol.gr-20100708225433-fqpl911kqfsrm1ff
This commit is contained in:
P. Christeas
2010-07-09 01:54:33 +03:00
parent 3580708a49
commit 173be44150
2 changed files with 25 additions and 30 deletions
+1 -22
View File
@@ -34,29 +34,8 @@ def _get_month_name(month):
elif month==11:return 'Nov'
elif month==12:return 'Dec'
def _to_unicode(s):
try:
return s.decode('utf-8')
except UnicodeError:
try:
return s.decode('latin')
except UnicodeError:
try:
return s.encode('ascii')
except UnicodeError:
return s
from ftpserver import _to_decode, _to_unicode
def _to_decode(s):
try:
return s.encode('utf-8')
except UnicodeError:
try:
return s.encode('latin')
except UnicodeError:
try:
return s.decode('ascii')
except UnicodeError:
return s
class abstracted_fs(object):
"""A class used to interact with the file system, providing a high
+24 -8
View File
@@ -225,13 +225,29 @@ def _to_unicode(s):
try:
return s.decode('utf-8')
except UnicodeError:
try:
return s.decode('latin')
except UnicodeError:
try:
return s.encode('ascii')
except UnicodeError:
return s
pass
try:
return s.decode('latin')
except UnicodeError:
pass
try:
return s.encode('ascii')
except UnicodeError:
return s
def _to_decode(s):
try:
return s.encode('utf-8')
except UnicodeError:
pass
try:
return s.encode('latin')
except UnicodeError:
pass
try:
return s.decode('ascii')
except UnicodeError:
return s
# --- library defined exceptions
@@ -2080,7 +2096,7 @@ class FTPHandler(asynchat.async_chat):
data = ''
if listing:
listing.sort()
data = '\r\n'.join(listing) + '\r\n'
data = ''.join([ _to_decode(x) + '\r\n' for x in listing ])
self.log('OK NLST "%s". Transfer starting.' %line)
self.push_dtp_data(data)