timeout on stdin
[m6w6/ext-http] / tests / helper / pipeline.inc
1 <?php
2
3 include "server.inc";
4
5 function respond($client, $msg) {
6 $r = new http\Env\Response;
7 $r->setEnvRequest($msg)
8 ->setHeader("X-Req", $msg->getRequestUrl())
9 ->send($client);
10 }
11
12 serve(function($client) {
13 $R = array(STDIN); $W = $E = array();
14 if (!stream_select($R, $W, $E, 10, 0)) {
15 logger("Client %d timed out", (int) $client);
16 return;
17 }
18 $count = trim(fgets(STDIN));
19 logger("Expecting %d messages from client %d", $count, (int) $client);
20 /* the peek message */
21 respond($client, new http\Message($client, false));
22 logger("Handled the peek request of client %d", (int) $client);
23 /* pipelined messages */
24 $req = array();
25 for ($i=0; $i < $count; ++ $i) {
26 $req[] = new http\Message($client, false);
27 logger("Read request no. %d from client %d", $i+1, (int) $client);
28 }
29 foreach ($req as $i => $msg) {
30 respond($client, $msg);
31 logger("Sent response no. %d to client %d", $i+1, (int) $client);
32 }
33 });