Skip to content

Commit

Permalink
Fix bug #72333 (fwrite() on non-blocking SSL sockets does not work)
Browse files Browse the repository at this point in the history
  • Loading branch information
bukka committed Mar 14, 2017
1 parent 8e45583 commit 17e9fc9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
54 changes: 54 additions & 0 deletions ext/openssl/tests/bug72333.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
Bug #72333: fwrite() on non-blocking SSL sockets doesn't work
--SKIPIF--
<?php
if (!extension_loaded("openssl")) die("skip openssl not loaded");
if (!function_exists("proc_open")) die("skip no proc_open");
?>
--FILE--
<?php
$serverCode = <<<'CODE'
$context = stream_context_create(['ssl' => ['local_cert' => __DIR__ . '/bug54992.pem']]);
$flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
$fp = stream_socket_server("ssl://127.0.0.1:10011", $errornum, $errorstr, $flags, $context);
phpt_notify();
$conn = stream_socket_accept($fp);
for ($i = 0; $i < 5; $i++) {
fread($conn, 100000);
usleep(200000);
}
CODE;

$clientCode = <<<'CODE'
$context = stream_context_create(['ssl' => ['verify_peer' => false, 'peer_name' => 'bug54992.local']]);
phpt_wait();
$fp = stream_socket_client("ssl://127.0.0.1:10011", $errornum, $errorstr, 3000, STREAM_CLIENT_CONNECT, $context);
stream_set_blocking($fp, 0);
function blocking_fwrite($fp, $buf) {
$write = [$fp];
$total = 0;
while (stream_select($read, $write, $except, 180)) {
$result = fwrite($fp, $buf);
$total += $result;
if ($total >= strlen($buf)) {
return $total;
}
$buf = substr($buf, $total);
}
}
$str1 = str_repeat("a", 5000000);
blocking_fwrite($fp, $str1);
echo "done";
CODE;

include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
--EXPECT--
done

8 changes: 8 additions & 0 deletions ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,14 @@ static int php_openssl_enable_crypto(php_stream *stream,

if (SUCCESS == php_set_sock_blocking(sslsock->s.socket, 0)) {
sslsock->s.is_blocked = 0;
SSL_set_mode(
sslsock->ssl_handle,
(
SSL_get_mode(sslsock->ssl_handle) |
SSL_MODE_ENABLE_PARTIAL_WRITE |
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
)
);
}

timeout = sslsock->is_client ? &sslsock->connect_timeout : &sslsock->s.timeout;
Expand Down

0 comments on commit 17e9fc9

Please sign in to comment.