Skip to content

Commit db56827

Browse files
committed
Added Http\Response::isWritable(), fix #3955
1 parent ac6615d commit db56827

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

ext-src/swoole_http_response.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ static PHP_METHOD(swoole_http_response, cookie);
165165
static PHP_METHOD(swoole_http_response, rawcookie);
166166
static PHP_METHOD(swoole_http_response, header);
167167
static PHP_METHOD(swoole_http_response, initHeader);
168+
static PHP_METHOD(swoole_http_response, isWritable);
168169
static PHP_METHOD(swoole_http_response, detach);
169170
static PHP_METHOD(swoole_http_response, create);
170171
/**
@@ -251,6 +252,7 @@ ZEND_END_ARG_INFO()
251252
const zend_function_entry swoole_http_response_methods[] =
252253
{
253254
PHP_ME(swoole_http_response, initHeader, arginfo_swoole_http_void, ZEND_ACC_PUBLIC)
255+
PHP_ME(swoole_http_response, isWritable, arginfo_swoole_http_void, ZEND_ACC_PUBLIC)
254256
PHP_ME(swoole_http_response, cookie, arginfo_swoole_http_response_cookie, ZEND_ACC_PUBLIC)
255257
PHP_MALIAS(swoole_http_response, setCookie, cookie, arginfo_swoole_http_response_cookie, ZEND_ACC_PUBLIC)
256258
PHP_ME(swoole_http_response, rawcookie, arginfo_swoole_http_response_cookie, ZEND_ACC_PUBLIC)
@@ -701,6 +703,14 @@ static PHP_METHOD(swoole_http_response, initHeader) {
701703
RETURN_TRUE;
702704
}
703705

706+
static PHP_METHOD(swoole_http_response, isWritable) {
707+
http_context *ctx = php_swoole_http_response_get_context(ZEND_THIS);
708+
if (!ctx || (ctx->end || ctx->detached)) {
709+
RETURN_FALSE;
710+
}
711+
RETURN_TRUE;
712+
}
713+
704714
static PHP_METHOD(swoole_http_response, end) {
705715
http_context *ctx = php_swoole_http_response_get_and_check_context(ZEND_THIS);
706716
if (UNEXPECTED(!ctx)) {

tests/swoole_http_server/0.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
swoole_http_server: basic functions
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../include/bootstrap.php';
8+
9+
$pm = new ProcessManager;
10+
11+
$html = base64_encode(random_bytes(rand(2048, 65536)));
12+
13+
$pm->parentFunc = function ($pid) use ($pm, $html) {
14+
go(function () use ($pm, $html) {
15+
$data = httpGetBody("http://127.0.0.1:{$pm->getFreePort()}/");
16+
Assert::same($data, $html);
17+
$pm->kill();
18+
});
19+
Swoole\Event::wait();
20+
echo "DONE\n";
21+
};
22+
23+
$pm->childFunc = function () use ($pm, $html) {
24+
$serv = new swoole_http_server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
25+
$serv->set([
26+
'log_file' => '/dev/null',
27+
]);
28+
$serv->on("workerStart", function ($serv) use ($pm) {
29+
$pm->wakeup();
30+
});
31+
$serv->on('request', function ($req, $resp) use ($html) {
32+
Assert::true($resp->isWritable());
33+
$resp->end($html);
34+
Assert::false($resp->isWritable());
35+
});
36+
$serv->start();
37+
};
38+
39+
$pm->childFirst();
40+
$pm->run();
41+
?>
42+
--EXPECT--
43+
DONE

0 commit comments

Comments
 (0)