File: client028.phpt

package info (click to toggle)
php-pecl-http 3.1.0%2B2.6.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,060 kB
  • ctags: 3,725
  • sloc: ansic: 37,486; xml: 818; php: 635; pascal: 164; makefile: 2
file content (141 lines) | stat: -rw-r--r-- 2,602 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
--TEST--
client curl user handler
--SKIPIF--
<?php 
include "skipif.inc";
skip_client_test();
?> 
--FILE--
<?php 
echo "Test\n";

class UserHandler implements http\Client\Curl\User
{
	private $client;
	private $run;
	private $fds = array(
			"R" => array(),
			"W" => array()
	);
	private $R = array();
	private $W = array();
	private $timeout = 1000;
	
	function __construct(http\Client $client) {
		$this->client = $client;
	}
	
	function init($run) {
		$this->run = $run;
	}
	
	function timer(int $timeout_ms) {
		echo "T";
		$this->timeout = $timeout_ms;
	}
	
	function socket($socket, int $action) {
		echo "S";
		
		switch ($action) {
		case self::POLL_NONE:
			break;
		case self::POLL_REMOVE:
			if (false !== ($r = array_search($socket, $this->fds["R"], true))) {
				echo "U";
				unset($this->fds["R"][$r]);
			}
			
			if (false !== ($w = array_search($socket, $this->fds["W"], true))) {
				echo "U";
				unset($this->fds["W"][$w]);
			}
			
			break;
			
		default:
			if ($action & self::POLL_IN) {
				if (!in_array($socket, $this->fds["R"], true)) {
					$this->fds["R"][] = $socket;
				}
			}
			if ($action & self::POLL_OUT) {
				if (!in_array($socket, $this->fds["W"], true)) {
					$this->fds["W"][] = $socket;
				}
			}
			break;
		}
	}
	
	function once() {
		echo "O";
		
		foreach ($this->W as $w) {
			call_user_func($this->run, $this->client, $w, self::POLL_OUT);
		}
		foreach ($this->R as $r) {
			call_user_func($this->run, $this->client, $r, self::POLL_IN);
		}
		return count($this->client);
	}
	
	function wait(int $timeout_ms = null) {
		echo "W";
		
		if ($timeout_ms === null) {
			$timeout_ms = $this->timeout;
		}
		$ts = floor($timeout_ms / 1000);
		$tu = ($timeout_ms % 1000) * 1000;
		
		extract($this->fds);
		
		if (($wfds = count($R) + count($W))) {
			$nfds = stream_select($R, $W, $E, $ts, $tu);
		} else {
			$nfds = 0;
		}
		$this->R = (array) $R;
		$this->W = (array) $W;
		
		if ($nfds === false) {
			return false;
		}
		if (!$nfds) {
			if (!$wfds) {
				echo "S";
				time_nanosleep($ts, $tu*1000);
			}
			call_user_func($this->run, $this->client);
		}
			
		return true;
	}
	
	function send() {
		$this->wait();
		$this->once();
	}
}


include "helper/server.inc";

server("proxy.inc", function($port) {
	$client = new http\Client;
	$client->configure(array(
			"use_eventloop" => new UserHandler($client)
	));
	$client->enqueue(new http\Client\Request("GET", "http://localhost:$port/"), function($r) {
		var_dump($r->getResponseCode());
	});
	$client->send();
});

?>
===DONE===
--EXPECTREGEX--
Test
[ST][WST]*([OWST]*)+U+int\(200\)
===DONE===