@@ -7,14 +7,7 @@ function http_server_skipif($socket_string) {
7
7
if (!stream_socket_server ($ socket_string )) die ('skip stream_socket_server() failed ' );
8
8
}
9
9
10
- /* Minimal HTTP server with predefined responses.
11
- *
12
- * $socket_string is the socket to create and listen on (e.g. tcp://127.0.0.1:1234)
13
- * $files is an array of files containing N responses for N expected requests. Server dies after N requests.
14
- * $output is a stream on which everything sent by clients is written to
15
- */
16
- function http_server ($ socket_string , array $ files , &$ output = null ) {
17
-
10
+ function http_server_init ($ socket_string , &$ output = null ) {
18
11
pcntl_alarm (60 );
19
12
20
13
$ server = stream_socket_server ($ socket_string , $ errno , $ errstr );
@@ -36,6 +29,21 @@ function http_server($socket_string, array $files, &$output = null) {
36
29
return $ pid ;
37
30
}
38
31
32
+ return $ server ;
33
+ }
34
+
35
+ /* Minimal HTTP server with predefined responses.
36
+ *
37
+ * $socket_string is the socket to create and listen on (e.g. tcp://127.0.0.1:1234)
38
+ * $files is an array of files containing N responses for N expected requests. Server dies after N requests.
39
+ * $output is a stream on which everything sent by clients is written to
40
+ */
41
+ function http_server ($ socket_string , array $ files , &$ output = null ) {
42
+
43
+ if (!is_resource ($ server = http_server_init ($ socket_string , $ output ))) {
44
+ return $ server ;
45
+ }
46
+
39
47
foreach ($ files as $ file ) {
40
48
41
49
$ sock = stream_socket_accept ($ server );
@@ -84,6 +92,24 @@ function http_server($socket_string, array $files, &$output = null) {
84
92
exit (0 );
85
93
}
86
94
95
+ function http_server_sleep ($ socket_string , $ micro_seconds = 500000 )
96
+ {
97
+ if (!is_resource ($ server = http_server_init ($ socket_string , $ output ))) {
98
+ return $ server ;
99
+ }
100
+
101
+ $ sock = stream_socket_accept ($ server );
102
+ if (!$ sock ) {
103
+ exit (1 );
104
+ }
105
+
106
+ usleep ($ micro_seconds );
107
+
108
+ fclose ($ sock );
109
+
110
+ exit (0 );
111
+ }
112
+
87
113
function http_server_kill ($ pid ) {
88
114
posix_kill ($ pid , SIGTERM );
89
115
pcntl_waitpid ($ pid , $ status );
0 commit comments