diff --git a/NEWS b/NEWS index dbf19c60c026..00d30f53fffb 100644 --- a/NEWS +++ b/NEWS @@ -34,6 +34,10 @@ PHP NEWS . Use recommended TYPE, HELP order for OpenMetrics metadata. (Marcel Hernandez) +- FTP: + . Improved performance of downloads in FTP_ASCII mode by writing each + received buffer at once. (Alexander Danilov) + - GD: . Fixed imagecolormatch() and imagejpeg() ValueError messages. (lacatoire) diff --git a/UPGRADING b/UPGRADING index 8f954dd01e1d..9abff13bc649 100644 --- a/UPGRADING +++ b/UPGRADING @@ -238,6 +238,10 @@ PHP 8.7 UPGRADE NOTES . Parsing HTML with Dom\HTMLDocument or innerHTML uses much less memory for small inputs. +- FTP: + . Improved performance of downloads in FTP_ASCII mode by writing each + received buffer at once. + - PDO: . Improved performance of fetching rows with PDO::FETCH_OBJ. diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index c6fd158ddaed..938ed7765707 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -803,6 +803,39 @@ bool ftp_pasv(ftpbuf_t *ftp, int pasv) return true; } +static void ftp_write_ascii_chunk(php_stream *stream, char *buf, size_t len, bool *pending_cr) +{ + const char *src = buf; + const char *end = buf + len; + const char *cr; + char *dst = buf; + + ZEND_ASSERT(len != 0); + + if (*pending_cr && *src != '\n') { + php_stream_putc(stream, '\r'); + } + + *pending_cr = end[-1] == '\r'; + if (*pending_cr) { + end--; + } + + while ((cr = memchr(src, '\r', end - src))) { + bool is_crlf = cr + 1 < end && cr[1] == '\n'; + size_t chunk_len = cr - src + !is_crlf; + + memmove(dst, src, chunk_len); + dst += chunk_len; + src = cr + 1; + } + + memmove(dst, src, end - src); + dst += end - src; + + php_stream_write(stream, buf, dst - buf); +} + bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_t path_len, ftptype_t type, zend_long resumepos) { databuf_t *data = NULL; @@ -857,48 +890,15 @@ bool ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const size_ } if (type == FTPTYPE_ASCII) { -#ifndef PHP_WIN32 - char *s; -#endif - char *ptr = data->buf; - char *e = ptr + rcvd; /* logic depends on the OS EOL * Win32 -> \r\n * Everything Else \n */ #ifdef PHP_WIN32 - php_stream_write(outstream, ptr, (e - ptr)); - ptr = e; + php_stream_write(outstream, data->buf, rcvd); #else - if (pending_cr) { - pending_cr = false; - if (*ptr == '\n') { - php_stream_putc(outstream, '\n'); - ptr++; - } else { - php_stream_putc(outstream, '\r'); - } - } - while (e > ptr && (s = memchr(ptr, '\r', (e - ptr)))) { - if (s + 1 < e) { - if (*(s + 1) == '\n') { - php_stream_write(outstream, ptr, (s - ptr)); - php_stream_putc(outstream, '\n'); - ptr = s + 2; - } else { - php_stream_write(outstream, ptr, (s - ptr) + 1); - ptr = s + 1; - } - } else { - php_stream_write(outstream, ptr, (s - ptr)); - pending_cr = true; - ptr = s + 1; - } - } + ftp_write_ascii_chunk(outstream, data->buf, rcvd, &pending_cr); #endif - if (ptr < e) { - php_stream_write(outstream, ptr, (e - ptr)); - } } else if (rcvd != php_stream_write(outstream, data->buf, rcvd)) { goto bail; } @@ -2096,7 +2096,7 @@ int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const siz ftp->data = data; ftp->stream = outstream; - ftp->lastch = 0; + ftp->pending_cr = false; ftp->nb = true; return (ftp_nb_continue_read(ftp)); @@ -2109,8 +2109,6 @@ int ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, const siz int ftp_nb_continue_read(ftpbuf_t *ftp) { databuf_t *data = NULL; - char *ptr; - char lastch; size_t rcvd; ftptype_t type; @@ -2129,32 +2127,22 @@ int ftp_nb_continue_read(ftpbuf_t *ftp) ftp->in_use = true; type = ftp->type; - lastch = ftp->lastch; if ((rcvd = my_recv(ftp, data->fd, data->buf, FTP_BUFSIZE))) { if (rcvd == (size_t)-1) { goto bail; } if (type == FTPTYPE_ASCII) { - for (ptr = data->buf; rcvd; rcvd--, ptr++) { - if (lastch == '\r' && *ptr != '\n') { - php_stream_putc(ftp->stream, '\r'); - } - if (*ptr != '\r') { - php_stream_putc(ftp->stream, *ptr); - } - lastch = *ptr; - } + ftp_write_ascii_chunk(ftp->stream, data->buf, rcvd, &ftp->pending_cr); } else if (rcvd != php_stream_write(ftp->stream, data->buf, rcvd)) { goto bail; } - ftp->lastch = lastch; ftp->in_use = false; return PHP_FTP_MOREDATA; } - if (type == FTPTYPE_ASCII && lastch == '\r') { + if (type == FTPTYPE_ASCII && ftp->pending_cr) { php_stream_putc(ftp->stream, '\r'); } @@ -2217,7 +2205,7 @@ int ftp_nb_put(ftpbuf_t *ftp, const char *path, const size_t path_len, php_strea } ftp->data = data; ftp->stream = instream; - ftp->lastch = 0; + ftp->pending_cr = false; ftp->nb = true; return (ftp_nb_continue_write(ftp)); diff --git a/ext/ftp/ftp.h b/ext/ftp/ftp.h index 1c8d45460ca6..e3559aff31d5 100644 --- a/ext/ftp/ftp.h +++ b/ext/ftp/ftp.h @@ -71,7 +71,7 @@ typedef struct ftpbuf php_stream *stream; /* output stream for "nonblocking" transfers */ bool nb; /* "nonblocking" transfer in progress */ bool in_use; /* engine transfer in progress; blocks re-entrant ftp_close */ - char lastch; /* last char of previous call */ + bool pending_cr; /* CR held back by an ASCII "nonblocking" download */ bool direction; /* recv = 0 / send = 1 */ bool closestream;/* close or not close stream */ #ifdef HAVE_FTP_SSL diff --git a/ext/ftp/tests/ftp_nb_get_ascii.phpt b/ext/ftp/tests/ftp_nb_get_ascii.phpt new file mode 100644 index 000000000000..3ea37a0434ed --- /dev/null +++ b/ext/ftp/tests/ftp_nb_get_ascii.phpt @@ -0,0 +1,40 @@ +--TEST-- +ftp_nb_get() ASCII mode: CRLF folds to LF, bare CR is preserved +--EXTENSIONS-- +ftp +pcntl +--FILE-- + str_repeat("A", 4095) . "\n" . str_repeat("B", 10), + 'bare_cr' => "line1\nba\rre\nend" . str_repeat("X", 4078) . "\r" . str_repeat("Y", 10), + 'trailing_cr' => "trail\r", +]; + +foreach ($tests as $remote => $expected) { + $ret = ftp_nb_get($ftp, $local, $remote, FTP_ASCII); + while ($ret === FTP_MOREDATA) { + $ret = ftp_nb_continue($ftp); + } + var_dump($ret === FTP_FINISHED, file_get_contents($local) === $expected); +} +?> +--CLEAN-- + +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true)