10000 Fix bug #72333 (fwrite() on non-blocking SSL sockets does not work) · php/php-src@17e9fc9 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 17e9fc9

Browse files
committed
Fix bug #72333 (fwrite() on non-blocking SSL sockets does not work)
1 parent 8e45583 commit 17e9fc9

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

ext/openssl/tests/bug72333.phpt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
Bug #72333: fwrite() on non-blocking SSL sockets doesn't work
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("openssl")) die("skip openssl not loaded");
6+
if (!function_exists("proc_open")) die("skip no proc_open");
7+
?>
8+
--FILE--
9+
<?php
10+
$serverCode = <<<'CODE'
11+
$context = stream_context_create(['ssl' => ['local_cert' => __DIR__ . '/bug54992.pem']]);
12+
13+
$flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
14+
$fp = stream_socket_server("ssl://127.0.0.1:10011", $errornum, $errorstr, $flags, $context);
15+
phpt_notify();
16+
$conn = stream_socket_accept($fp);
17+
18+
for ($i = 0; $i < 5; $i++) {
19+
fread($conn, 100000);
20+
usleep(200000);
21+
}
22+
CODE;
23+
24+
$clientCode = <<<'CODE'
25+
$context = stream_context_create(['ssl' => ['verify_peer' => false, 'peer_name' => 'bug54992.local']]);
26+
27+
phpt_wait();
28+
$fp = stream_socket_client("ssl://127.0.0.1:10011", $errornum, $errorstr, 3000, STREAM_CLIENT_CONNECT, $context);
29+
stream_set_blocking($fp, 0);
30+
31+
function blocking_fwrite($fp, $buf) {
32+
$write = [$fp];
33+
$total = 0;
34+
while (stream_select($read, $write, $except, 180)) {
35+
$result = fwrite($fp, $buf);
36+
$total += $result;
37+
if ($total >= strlen($buf)) {
38+
return $total;
39+
}
40+
$buf = substr($buf, $total);
41+
}
42+
}
43+
44+
$str1 = str_repeat("a", 5000000);
45+
blocking_fwrite($fp, $str1);
46+
echo "done";
47+
CODE;
48+
49+
include 'ServerClientTestCase.inc';
50+
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
51+
?>
52+
--EXPECT--
53+
done
54+

ext/openssl/xp_ssl.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,14 @@ static int php_openssl_enable_crypto(php_stream *stream,
18141814

18151815
if (SUCCESS == php_set_sock_blocking(sslsock->s.socket, 0)) {
18161816
sslsock->s.is_blocked = 0;
1817+
SSL_set_mode(
1818+
sslsock->ssl_handle,
1819+
(
1820+
SSL_get_mode(sslsock->ssl_handle) |
1821+
SSL_MODE_ENABLE_PARTIAL_WRITE |
1822+
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
1823+
)
1824+
);
18171825
}
18181826

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

0 commit comments

Comments
 (0)
0