Skip to content

Commit

Permalink
Merge pull request #45: Add ssh2_send_eof function
Browse files Browse the repository at this point in the history
  • Loading branch information
langemeijer committed Dec 31, 2020
2 parents a471f58 + 9ec2b76 commit ddf6047
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.xml
Expand Up @@ -72,6 +72,7 @@
<file role="test" name="ssh2_exec.phpt"/>
<file role="test" name="ssh2_sftp_001.phpt"/>
<file role="test" name="ssh2_sftp_002.phpt"/>
<file role="test" name="ssh2_send_eof.phpt"/>
<file role="test" name="ssh2_shell.phpt"/>
<file role="test" name="ssh2_skip.inc"/>
<file role="test" name="ssh2_test.inc"/>
Expand Down
1 change: 1 addition & 0 deletions php_ssh2.h
Expand Up @@ -131,6 +131,7 @@ PHP_FUNCTION(ssh2_tunnel);
PHP_FUNCTION(ssh2_scp_recv);
PHP_FUNCTION(ssh2_scp_send);
PHP_FUNCTION(ssh2_fetch_stream);
PHP_FUNCTION(ssh2_send_eof);

/* In ssh2_sftp.c */
PHP_FUNCTION(ssh2_sftp);
Expand Down
5 changes: 5 additions & 0 deletions ssh2.c
Expand Up @@ -1462,6 +1462,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_ssh2_poll, 0, 0, 1)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_ssh2_send_eof, 0, 0, 1)
ZEND_ARG_INFO(0, channel)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_ssh2_sftp, 1)
ZEND_ARG_INFO(0, session)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -1573,6 +1577,7 @@ zend_function_entry ssh2_functions[] = {
PHP_FE(ssh2_scp_send, arginfo_ssh2_scp_send)
PHP_FE(ssh2_fetch_stream, arginfo_ssh2_fetch_stream)
PHP_FE(ssh2_poll, arginfo_ssh2_poll)
PHP_FE(ssh2_send_eof, arginfo_ssh2_send_eof)

/* SFTP Stuff */
PHP_FE(ssh2_sftp, arginfo_ssh2_sftp)
Expand Down
38 changes: 38 additions & 0 deletions ssh2_fopen_wrappers.c
Expand Up @@ -1439,6 +1439,44 @@ PHP_FUNCTION(ssh2_fetch_stream)
}
/* }}} */

/* {{{ proto stream ssh2_send_eof(stream channel)
* Sends EOF to a stream. Primary use is to close stdin of an stdio stream.
*/
PHP_FUNCTION(ssh2_send_eof)
{
php_ssh2_channel_data *data;
php_stream *parent;
zval *zparent;
int ssh2_ret;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zparent) == FAILURE) {
return;
}

php_stream_from_zval(parent, zparent);
if (parent->ops != &php_ssh2_channel_stream_ops) {
php_error_docref(NULL, E_WARNING, "Provided stream is not of type " PHP_SSH2_CHANNEL_STREAM_NAME);
RETURN_FALSE;
}

data = (php_ssh2_channel_data*)parent->abstract;
if (!data) {
php_error_docref(NULL, E_WARNING, "Abstract in stream is null");
RETURN_FALSE;
}

ssh2_ret = libssh2_channel_send_eof(data->channel);
if (ssh2_ret < 0) {
char msg[256];
snprintf(msg, 256, "Couldn't send EOF to channel (Return code %d)", ssh2_ret);
php_error_docref(NULL, E_WARNING, msg);
RETURN_FALSE;
}

RETURN_TRUE;
}
/* }}} */

/*
* Local variables:
* tab-width: 4
Expand Down
32 changes: 32 additions & 0 deletions tests/ssh2_send_eof.phpt
@@ -0,0 +1,32 @@
--TEST--
ssh2_send_eof() - Tests closing standard input
--SKIPIF--
<?php require('ssh2_skip.inc'); ssh2t_needs_auth(); ?>
--FILE--
<?php require('ssh2_test.inc');

$ssh = ssh2_connect(TEST_SSH2_HOSTNAME, TEST_SSH2_PORT);
var_dump(ssh2t_auth($ssh));

$cmd=ssh2_exec($ssh, 'cat' . PHP_EOL);

var_dump($cmd);

stream_set_blocking($cmd, true);

$content = "foo";

fwrite($cmd, $content);
fflush($cmd);
ssh2_send_eof($cmd);

$response = stream_get_contents($cmd);
var_dump($response === $content);
echo $response . PHP_EOL;

--EXPECTF--
bool(true)
resource(%d) of type (stream)
bool(true)
foo

0 comments on commit ddf6047

Please sign in to comment.