Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CR to CRLF conversion support for ASCII downloads #494

Conversation

andrewshulgin
Copy link
Contributor

@andrewshulgin andrewshulgin commented Mar 27, 2019

@andrewshulgin
Copy link
Contributor Author

andrewshulgin commented Mar 27, 2019

Please check if this acceptable, and I'll update CHANGES.rst if true.

@giampaolo
Copy link
Owner

Hm... this adds a lot of complexity. If os.sep is "\r" on OSX 9 only I would say it's better to leave this out.

@coveralls
Copy link

coveralls commented Mar 27, 2019

Coverage Status

Coverage remained the same at ?% when pulling 0676df5 on andrew-shulgin:ascii_cr_to_crlf_conversion into 3514646 on giampaolo:master.

@giampaolo
Copy link
Owner

Here's what I would do instead:

--- a/pyftpdlib/handlers.py
+++ b/pyftpdlib/handlers.py
@@ -2835,8 +2835,12 @@ class FTPHandler(AsyncChat):
         """Set current type data type to binary/ascii"""
         type = line.upper().replace(' ', '')
         if type in ("A", "L7"):
-            self.respond("200 Type set to: ASCII.")
-            self._current_type = 'a'
+            if os.sep in ('\n', '\r\n'):
+                self.respond("200 Type set to: ASCII.")
+                self._current_type = 'a'
+            else:
+                self.respond('504 ASCII mode not supported on this platform')
+
         elif type in ("I", "L8"):
             self.respond("200 Type set to: Binary.")
             self._current_type = 'i'

@andrewshulgin
Copy link
Contributor Author

andrewshulgin commented Mar 27, 2019

As I stated in the prevoius PR, the platform-native separator doesn't matter much, because any file can have CR line ending on any platform, e.g. downloaded from the web.

@andrewshulgin
Copy link
Contributor Author

andrewshulgin commented Mar 27, 2019

According to Wikipedia, CR line endings are native to:

  • Commodore 8-bit machines (C64, C128)
  • Acorn BBC
  • ZX Spectrum
  • TRS-80
  • Apple II family
  • Oberon
  • classic Mac OS
  • MIT Lisp Machine
  • OS-9

Thus, any file created on these platforms has CR line endings.
To be honest, it doesn't seem that any of these platforms are widely used nowadays.

@giampaolo
Copy link
Owner

As I stated in the prevoius PR, the platform-native separator doesn't matter much, because any file can have CR line ending on any platform, e.g. downloaded from the web.

But we cannot know that, nor make assumptions. It's RFC-959 which dictates to convert os.sep to \r\n when sending a file in ASCII mode.

Also, one question: you modified FileProducer (meaning RETR, aka file going from server to client). What about the other way around? Does the same problem affect DTPHandler._posix_ascii_data_wrapper?

@andrewshulgin
Copy link
Contributor Author

DTPHandler._posix_ascii_data_wrapper works correctly, because the client must always send CRLF-separated data if ASCII transfer mode is being used.

@andrewshulgin
Copy link
Contributor Author

andrewshulgin commented Mar 27, 2019

But we cannot know that

We can just detect and convert CR or LF line endings on-the-fly; this exact behaviour is implemented in this PR.

@giampaolo
Copy link
Owner

DTPHandler._posix_ascii_data_wrapper works correctly, because the client must always send CRLF-separated data if ASCII transfer mode is being used.

Oh you're right.

We can just detect and convert CR or LF line endings on-the-fly; this exact behaviour is implemented in this PR.

Mm.. but is this really desirable? Couldn't "\r" be legitimate (aka it should not be converted)? It would be interesting to know what proftpd and vsftpd do in this regard.

@andrewshulgin
Copy link
Contributor Author

It would be interesting to know what proftpd and vsftpd do in this regard.

I agree, going to check it soon.

@andrewshulgin
Copy link
Contributor Author

andrewshulgin commented Mar 27, 2019

I checked that and can conclude: both ProFTPd and vsftpd do not convert stand-alone CRs into CRLFs. On Linux, at least.

@lurch
Copy link
Contributor

lurch commented Mar 27, 2019

CR line endings are native to:
ZX Spectrum

Oh noes, you mean I can't run pyftpdlib on my Speccy?! 🙀 🌈 ⌨️ 😰 🤣

pyftpdlib/handlers.py Outdated Show resolved Hide resolved
@lurch
Copy link
Contributor

lurch commented Mar 27, 2019

WRT the unit tests, I wonder if it might make sense to also have tests with \r or \n as the very first character of the string, to make sure any edge-cases get caught? 🤷‍♂️

Note that these review-comments don't imply that I "endorse" this change; that's entirely up to @giampaolo 🙂

@giampaolo
Copy link
Owner

I checked that and can conclude: both ProFTPd and vsftpd do not convert stand-alone CRs into CRLFs. On Linux, at least.

OK, I'm gonna close this out then. Thanks.

@giampaolo giampaolo closed this Mar 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants