Merge branch 'v2.6.x'
[m6w6/ext-http] / tests / client028.phpt
1 --TEST--
2 client curl user handler
3 --SKIPIF--
4 <?php
5 include "skipif.inc";
6 skip_client_test();
7 ?>
8 --FILE--
9 <?php
10 echo "Test\n";
11
12 class UserHandler implements http\Client\Curl\User
13 {
14 private $client;
15 private $run;
16 private $fds = array(
17 "R" => array(),
18 "W" => array()
19 );
20 private $R = array();
21 private $W = array();
22 private $timeout = 1000;
23
24 function __construct(http\Client $client) {
25 $this->client = $client;
26 }
27
28 function init($run) {
29 $this->run = $run;
30 }
31
32 function timer(int $timeout_ms) {
33 echo "T";
34 $this->timeout = $timeout_ms;
35 }
36
37 function socket($socket, int $action) {
38 echo "S";
39
40 switch ($action) {
41 case self::POLL_NONE:
42 break;
43 case self::POLL_REMOVE:
44 if (false !== ($r = array_search($socket, $this->fds["R"], true))) {
45 echo "U";
46 unset($this->fds["R"][$r]);
47 }
48
49 if (false !== ($w = array_search($socket, $this->fds["W"], true))) {
50 echo "U";
51 unset($this->fds["W"][$w]);
52 }
53
54 break;
55
56 default:
57 if ($action & self::POLL_IN) {
58 if (!in_array($socket, $this->fds["R"], true)) {
59 $this->fds["R"][] = $socket;
60 }
61 }
62 if ($action & self::POLL_OUT) {
63 if (!in_array($socket, $this->fds["W"], true)) {
64 $this->fds["W"][] = $socket;
65 }
66 }
67 break;
68 }
69 }
70
71 function once() {
72 echo "O";
73
74 foreach ($this->W as $w) {
75 call_user_func($this->run, $this->client, $w, self::POLL_OUT);
76 }
77 foreach ($this->R as $r) {
78 call_user_func($this->run, $this->client, $r, self::POLL_IN);
79 }
80 return count($this->client);
81 }
82
83 function wait(int $timeout_ms = null) {
84 echo "W";
85
86 if ($timeout_ms === null) {
87 $timeout_ms = $this->timeout;
88 }
89 $ts = floor($timeout_ms / 1000);
90 $tu = ($timeout_ms % 1000) * 1000;
91
92 extract($this->fds);
93
94 if (($wfds = count($R) + count($W))) {
95 $nfds = stream_select($R, $W, $E, $ts, $tu);
96 } else {
97 $nfds = 0;
98 }
99 $this->R = (array) $R;
100 $this->W = (array) $W;
101
102 if ($nfds === false) {
103 return false;
104 }
105 if (!$nfds) {
106 if (!$wfds) {
107 echo "S";
108 time_nanosleep($ts, $tu*1000);
109 }
110 call_user_func($this->run, $this->client);
111 }
112
113 return true;
114 }
115
116 function send() {
117 $this->wait();
118 $this->once();
119 }
120 }
121
122
123 include "helper/server.inc";
124
125 server("proxy.inc", function($port) {
126 $client = new http\Client;
127 $client->configure(array(
128 "use_eventloop" => new UserHandler($client)
129 ));
130 $client->enqueue(new http\Client\Request("GET", "http://localhost:$port/"), function($r) {
131 var_dump($r->getResponseCode());
132 });
133 $client->send();
134 });
135
136 ?>
137 ===DONE===
138 --EXPECTREGEX--
139 Test
140 T[WST]*(O[WST]*)+U+int\(200\)
141 ===DONE===