ext/ftp/tests: add with_data_connection to mock server - #23455
Conversation
47c5a80 to
817ea85
Compare
|
@NickSdot could you take a look at this? |
Looked at it and generally like it. But I am not actually confident enough to say whether it's fine or not. Some thoughts:
I brainstormed those with Codex and we landed on the below; that fixes some of it and Codex says it's fine. But this obviously still changes some old response codes, hence I myself am not sure it's fine or not -- maybe someone else can answer this more competently. Brainstorming Resultfunction with_data_connection(callable $callback): bool
{
global $s, $pasv, $pasv_listener, $host, $port, $ssl, $bug73457;
$passive = !empty($pasv);
$fs = null;
if (!$passive) {
$fs = stream_socket_client("tcp://$host:$port");
} elseif (empty($bug73457)) {
$fs = stream_socket_accept($pasv_listener, 10);
fclose($pasv_listener);
}
$pasv = false;
$pasv_listener = null;
fputs(
$s,
$passive
? "125 Data connection already open; transfer starting.\r\n"
: "150 File status okay; about to open data connection\r\n"
);
if (!$fs) {
fputs($s, "425 Can't open data connection\r\n");
return false;
}
if (!empty($ssl) && !stream_socket_enable_crypto(
$fs,
true,
STREAM_CRYPTO_METHOD_TLS_SERVER
)) {
fclose($fs);
fputs($s, "425 TLS negotiation failed\r\n");
return false;
}
try {
$callback($fs);
} finally {
fclose($fs); // maybe plus defensive check in case callback closed it?
}
fputs($s, "226 Closing data Connection.\r\n");
return true;
} |
|
Thank you.
Yes, I changed this.
This is intentional. The
This was intentional, but I changed it. It was meant to keep the same functionality in STOR. But that functionality only triggers when the test fails, so throwing the exception is better.
This was intentional, so that when a test wants to test a failing stream_socket_client it is not interrupted by warnings. However, since such tests do not (yet) exist it may indeed be better to remove the error supression. |
- Add `with_data_connection` to handle setting up connection to transfer file data or directory listing over. - Move accepting passive connection to with_data_connection - Use fgets instead of fread. FTP is a line-oriented protocol, so fgets is better. - Remove unused $transfer_type - Simplify STOR equality check
8f010b1 to
fd821b9
Compare
FTP uses separate control and data connections. Extract setting up the data connection to a separate function. This was duplicated many times throughout the test server.
Also use fgets instead of fread, because that matches FTP's line-based protocol better.