|
49 | 49 |
|
50 | 50 | # The standard FTP server control port |
51 | 51 | FTP_PORT = 21 |
| 52 | +# The sizehint parameter passed to readline() calls |
| 53 | +MAXLINE = 8192 |
52 | 54 |
|
53 | 55 |
|
54 | 56 | # Exception raised when an error or invalid response is received |
@@ -96,6 +98,7 @@ class FTP: |
96 | 98 | debugging = 0 |
97 | 99 | host = '' |
98 | 100 | port = FTP_PORT |
| 101 | + maxline = MAXLINE |
99 | 102 | sock = None |
100 | 103 | file = None |
101 | 104 | welcome = None |
@@ -194,7 +197,9 @@ def putcmd(self, line): |
194 | 197 | # Internal: return one line from the server, stripping CRLF. |
195 | 198 | # Raise EOFError if the connection is closed |
196 | 199 | def getline(self): |
197 | | - line = self.file.readline() |
| 200 | + line = self.file.readline(self.maxline + 1) |
| 201 | + if len(line) > self.maxline: |
| 202 | + raise Error("got more than %d bytes" % self.maxline) |
198 | 203 | if self.debugging > 1: |
199 | 204 | print('*get*', self.sanitize(line)) |
200 | 205 | if not line: raise EOFError |
@@ -446,7 +451,9 @@ def retrlines(self, cmd, callback = None): |
446 | 451 | with self.transfercmd(cmd) as conn, \ |
447 | 452 | conn.makefile('r', encoding=self.encoding) as fp: |
448 | 453 | while 1: |
449 | | - line = fp.readline() |
| 454 | + line = fp.readline(self.maxline + 1) |
| 455 | + if len(line) > self.maxline: |
| 456 | + raise Error("got more than %d bytes" % self.maxline) |
450 | 457 | if self.debugging > 2: print('*retr*', repr(line)) |
451 | 458 | if not line: |
452 | 459 | break |
@@ -496,7 +503,9 @@ def storlines(self, cmd, fp, callback=None): |
496 | 503 | self.voidcmd('TYPE A') |
497 | 504 | with self.transfercmd(cmd) as conn: |
498 | 505 | while 1: |
499 | | - buf = fp.readline() |
| 506 | + buf = fp.readline(self.maxline + 1) |
| 507 | + if len(buf) > self.maxline: |
| 508 | + raise Error("got more than %d bytes" % self.maxline) |
500 | 509 | if not buf: break |
501 | 510 | if buf[-2:] != B_CRLF: |
502 | 511 | if buf[-1] in B_CRLF: buf = buf[:-1] |
@@ -773,7 +782,9 @@ def retrlines(self, cmd, callback = None): |
773 | 782 | fp = conn.makefile('r', encoding=self.encoding) |
774 | 783 | with fp, conn: |
775 | 784 | while 1: |
776 | | - line = fp.readline() |
| 785 | + line = fp.readline(self.maxline + 1) |
| 786 | + if len(line) > self.maxline: |
| 787 | + raise Error("got more than %d bytes" % self.maxline) |
777 | 788 | if self.debugging > 2: print('*retr*', repr(line)) |
778 | 789 | if not line: |
779 | 790 | break |
@@ -804,7 +815,9 @@ def storlines(self, cmd, fp, callback=None): |
804 | 815 | self.voidcmd('TYPE A') |
805 | 816 | with self.transfercmd(cmd) as conn: |
806 | 817 | while 1: |
807 | | - buf = fp.readline() |
| 818 | + buf = fp.readline(self.maxline + 1) |
| 819 | + if len(buf) > self.maxline: |
| 820 | + raise Error("got more than %d bytes" % self.maxline) |
808 | 821 | if not buf: break |
809 | 822 | if buf[-2:] != B_CRLF: |
810 | 823 | if buf[-1] in B_CRLF: buf = buf[:-1] |
|
0 commit comments