Skip to content

Commit 95d107e

Browse files
committed
change test to non online
1 parent 3118ca3 commit 95d107e

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed
Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
--TEST--
22
Testing linger `socket` option.
3-
--SKIPIF--
4-
<?php
5-
//if (getenv("SKIP_ONLINE_TESTS")) die('skip online test');
6-
if (!in_array('https', stream_get_wrappers())) die('skip: https wrapper is required');
7-
?>
83
--FILE--
94
<?php
5+
for ($i=0; $i<100; $i++) {
6+
$port = rand(10000, 65000);
7+
/* Setup socket server */
8+
$server = @stream_socket_server("tcp://127.0.0.1:$port");
9+
if ($server) {
10+
break;
11+
}
12+
}
13+
$client = stream_socket_client("tcp://127.0.0.1:$port");
1014
$context = stream_context_create(['socket' => ['linger' => false]]);
11-
var_dump(file_get_contents('https://httpbin.org/get', false, $context) !== false);
15+
$socket = stream_socket_client("tcp://127.0.0.1:$port", $errno, $errstr, 0, STREAM_CLIENT_CONNECT, $context);
16+
var_dump($socket);
1217
$context = stream_context_create(['socket' => ['linger' => PHP_INT_MAX + 1]]);
13-
var_dump(file_get_contents('https://httpbin.org/get', false, $context) !== false);
14-
$context = stream_context_create(['socket' => ['linger' => 3]]);
15-
var_dump(file_get_contents('https://httpbin.org/get', false, $context) !== false);
18+
$socket = stream_socket_client("tcp://127.0.0.1:$port", $errno, $errstr, 0, STREAM_CLIENT_CONNECT, $context);
19+
var_dump($socket);
20+
$context = stream_context_create(['socket' => ['linger' => 5]]);
21+
$socket = stream_socket_client("tcp://127.0.0.1:$port", $errno, $errstr, 1, STREAM_CLIENT_CONNECT, $context);
22+
var_dump($socket);
23+
stream_set_blocking($socket, true);
24+
var_dump(stream_socket_sendto($socket, "data"));
25+
$data = base64_decode("1oIBAAABAAAAAAAAB2V4YW1wbGUDb3JnAAABAAE=");
26+
stream_set_blocking($socket, 0);
27+
stream_socket_sendto($socket, $data);
28+
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
29+
stream_socket_shutdown($server, STREAM_SHUT_RDWR);
1630
?>
1731
--EXPECTF--
18-
Warning: file_get_contents(https://httpbin.org/get): Failed to open stream: Invalid `linger` value in %s on line %d
32+
Warning: stream_socket_client(): Unable to connect to tcp://127.0.0.1:%d (Invalid `linger` value) in %s on line %d
1933
bool(false)
2034

21-
Warning: file_get_contents(https://httpbin.org/get): Failed to open stream: Invalid `linger` value in %s on line %d
35+
Warning: stream_socket_client(): Unable to connect to tcp://127.0.0.1:%d (Invalid `linger` value) in %s on line %d
2236
bool(false)
23-
bool(true)
37+
resource(%d) of type (stream)
38+
int(4)

main/network.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,15 +470,15 @@ php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned po
470470
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&sockoptval, sizeof(sockoptval));
471471
}
472472
#endif
473-
#ifdef SO_LINGER
473+
#ifdef PHP_SO_LINGER
474474
if (sockopts & STREAM_SOCKOP_SO_LINGER) {
475475
ZEND_ASSERT(option != NULL);
476476
long linger = *(long *)option;
477477
struct linger val = {
478478
.l_onoff = (linger > 0),
479479
.l_linger = (int)linger
480480
};
481-
setsockopt(sock, IPPROTO_TCP, SO_LINGER, (char*)&val, sizeof(val));
481+
setsockopt(sock, IPPROTO_TCP, PHP_SO_LINGER, (char*)&val, sizeof(val));
482482
}
483483
#endif
484484

@@ -909,7 +909,7 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
909909
}
910910
}
911911
#endif
912-
#ifdef SO_LINGER
912+
#ifdef PHP_SO_LINGER
913913
{
914914
if (sockopts & STREAM_SOCKOP_SO_LINGER) {
915915
ZEND_ASSERT(option != NULL);
@@ -918,7 +918,7 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
918918
.l_onoff = linger > 0,
919919
.l_linger = (int)linger
920920
};
921-
setsockopt(sock, IPPROTO_TCP, SO_LINGER, (char*)&val, sizeof(val));
921+
setsockopt(sock, IPPROTO_TCP, PHP_SO_LINGER, (char*)&val, sizeof(val));
922922
}
923923
}
924924
#endif

main/php_network.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
# define PHP_IS_TRANSIENT_ERROR(err) (err == EAGAIN)
5555
#endif
5656

57+
#ifdef SO_LINGER
58+
# ifdef SO_LINGER_SEC
59+
# define PHP_SO_LINGER SO_LINGER_SEC
60+
# else
61+
# define PHP_SO_LINGER SO_LINGER
62+
# endif
63+
#endif
64+
5765
#ifdef PHP_WIN32
5866
#define php_socket_errno() WSAGetLastError()
5967
#else

main/streams/xp_socket.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *
754754
}
755755
#endif
756756

757-
#ifdef SO_LINGER
757+
#ifdef PHP_SO_LINGER
758758
if (PHP_STREAM_CONTEXT(stream)
759759
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "linger")) != NULL) {
760760
bool failed;
@@ -857,7 +857,7 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
857857
}
858858
#endif
859859

860-
#ifdef SO_LINGER
860+
#ifdef PHP_SO_LINGER
861861
if (PHP_STREAM_CONTEXT(stream)
862862
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "linger")) != NULL) {
863863
bool failed;

0 commit comments

Comments
 (0)