Skip to content

Commit 736e3fc

Browse files
committed
Implement GH-8641: [Stream] STREAM_NOTIFY_COMPLETED over HTTP never emitted
This adds support for the completed event. We also have to check for stream->eof in the `stream->readfilters.head` else branch in order to prevent double firing the completed event. The non-else branch already has that check.
1 parent 47ed190 commit 736e3fc

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

ext/openssl/xp_ssl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,9 @@ static ssize_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, si
21402140
if (nr_bytes > 0) {
21412141
php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), nr_bytes, 0);
21422142
}
2143+
if (stream->eof) {
2144+
php_stream_notify_completed(PHP_STREAM_CONTEXT(stream));
2145+
}
21432146

21442147
/* And if we were originally supposed to be blocking, let's reset the socket to that. */
21452148
if (began_blocked && php_set_sock_blocking(sslsock->s.socket, 1) == SUCCESS) {

ext/standard/tests/http/gh8641.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
GH-8641 ([Stream] STREAM_NOTIFY_COMPLETED over HTTP never emitted)
3+
--SKIPIF--
4+
<?php require 'server.inc'; http_server_skipif(); ?>
5+
--INI--
6+
allow_url_fopen=1
7+
--FILE--
8+
<?php
9+
require 'server.inc';
10+
11+
function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max)
12+
{
13+
if ($notification_code === STREAM_NOTIFY_COMPLETED) {
14+
echo $notification_code, ' ', $bytes_transferred, ' ', $bytes_max, PHP_EOL;
15+
}
16+
}
17+
18+
$ctx = stream_context_create();
19+
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));
20+
21+
$responses = array(
22+
"data://text/plain,HTTP/1.0 200 Ok\r\nContent-Length: 11\r\n\r\nHello world",
23+
);
24+
25+
['pid' => $pid, 'uri' => $uri] = http_server($responses, $output);
26+
27+
$f = file_get_contents($uri, 0, $ctx);
28+
29+
http_server_kill($pid);
30+
var_dump($f);
31+
?>
32+
--EXPECTF--
33+
8 11 11
34+
string(11) "Hello world"

main/streams/php_stream_context.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ END_EXTERN_C()
9494
php_stream_notification_notify((context), PHP_STREAM_NOTIFY_PROGRESS, PHP_STREAM_NOTIFY_SEVERITY_INFO, \
9595
NULL, 0, (bsofar), (bmax), NULL); } } while(0)
9696

97+
#define php_stream_notify_completed(context) do { if ((context) && (context)->notifier) { \
98+
php_stream_notification_notify((context), PHP_STREAM_NOTIFY_COMPLETED, PHP_STREAM_NOTIFY_SEVERITY_INFO, \
99+
NULL, 0, (context)->notifier->progress, (context)->notifier->progress_max, NULL); } } while(0)
100+
97101
#define php_stream_notify_progress_init(context, sofar, bmax) do { if ((context) && (context)->notifier) { \
98102
(context)->notifier->progress = (sofar); \
99103
(context)->notifier->progress_max = (bmax); \

main/streams/streams.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ PHPAPI zend_result _php_stream_fill_read_buffer(php_stream *stream, size_t size)
646646

647647
} else {
648648
/* is there enough data in the buffer ? */
649-
if (stream->writepos - stream->readpos < (zend_off_t)size) {
649+
if (!stream->eof && stream->writepos - stream->readpos < (zend_off_t)size) {
650650
ssize_t justread = 0;
651651

652652
/* reduce buffer memory consumption if possible, to avoid a realloc */

main/streams/xp_socket.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ static ssize_t php_sockop_write(php_stream *stream, const char *buf, size_t coun
116116
if (didwrite > 0) {
117117
php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), didwrite, 0);
118118
}
119+
if (stream->eof) {
120+
php_stream_notify_completed(PHP_STREAM_CONTEXT(stream));
121+
}
119122

120123
return didwrite;
121124
}
@@ -204,6 +207,9 @@ static ssize_t php_sockop_read(php_stream *stream, char *buf, size_t count)
204207
if (nr_bytes > 0) {
205208
php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), nr_bytes, 0);
206209
}
210+
if (stream->eof) {
211+
php_stream_notify_completed(PHP_STREAM_CONTEXT(stream));
212+
}
207213

208214
return nr_bytes;
209215
}

0 commit comments

Comments
 (0)