|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of ReactGuzzleRing. |
|
5
|
|
|
* |
|
6
|
|
|
** (c) 2014 Cees-Jan Kiewiet |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace WyriHaximus\React\Guzzle\HttpClient; |
|
12
|
|
|
|
|
13
|
|
|
use Clue\React\Buzz\Browser; |
|
14
|
|
|
use Clue\React\Buzz\Io\Sender; |
|
15
|
|
|
use Clue\React\HttpProxy\ProxyConnector as HttpProxyClient; |
|
16
|
|
|
use Clue\React\Socks\Client as SocksProxyClient; |
|
17
|
|
|
use Psr\Http\Message\RequestInterface; |
|
18
|
|
|
use React\Dns\Resolver\Resolver; |
|
19
|
|
|
use React\EventLoop\LoopInterface; |
|
20
|
|
|
use React\HttpClient\Client as HttpClient; |
|
21
|
|
|
use ReflectionObject; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class RequestFactory |
|
25
|
|
|
* |
|
26
|
|
|
* @package WyriHaximus\React\Guzzle\HttpClient |
|
27
|
|
|
*/ |
|
28
|
|
|
class RequestFactory |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
* @param RequestInterface $request |
|
33
|
|
|
* @param array $options |
|
34
|
|
|
* @param $resolver Resolver |
|
35
|
|
|
* @param HttpClient $httpClient |
|
36
|
|
|
* @param LoopInterface $loop |
|
37
|
|
|
* @return \React\Promise\Promise |
|
38
|
|
|
*/ |
|
39
|
|
|
public function create( |
|
40
|
|
|
RequestInterface $request, |
|
41
|
|
|
array $options, |
|
42
|
|
|
Resolver $resolver, |
|
43
|
|
|
HttpClient $httpClient, |
|
44
|
|
|
LoopInterface $loop |
|
45
|
|
|
) { |
|
46
|
6 |
|
return \WyriHaximus\React\futurePromise($loop)->then(function () use ( |
|
47
|
6 |
|
$request, |
|
48
|
6 |
|
$options, |
|
49
|
6 |
|
$resolver, |
|
50
|
6 |
|
$httpClient, |
|
51
|
6 |
|
$loop |
|
52
|
|
|
) { |
|
53
|
6 |
|
$sender = $this->createSender($options, $resolver, $httpClient, $loop); |
|
54
|
6 |
|
return (new Browser($loop, $sender)) |
|
55
|
6 |
|
->withOptions($this->convertOptions($options)) |
|
56
|
6 |
|
->send($request); |
|
57
|
6 |
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $options |
|
62
|
|
|
* @param HttpClient $httpClient |
|
63
|
|
|
* @param LoopInterface $loop |
|
64
|
|
|
* @return Sender |
|
65
|
|
|
*/ |
|
66
|
6 |
|
protected function createSender(array $options, Resolver $resolver, HttpClient $httpClient, LoopInterface $loop) |
|
67
|
|
|
{ |
|
68
|
6 |
|
$connector = $this->getProperty($httpClient, 'connector'); |
|
69
|
|
|
|
|
70
|
6 |
|
if (isset($options['proxy'])) { |
|
71
|
5 |
|
switch (parse_url($options['proxy'], PHP_URL_SCHEME)) { |
|
72
|
5 |
|
case 'http': |
|
73
|
1 |
|
$connector = new HttpProxyClient($options['proxy'], $connector); |
|
74
|
1 |
|
break; |
|
75
|
4 |
|
case 'socks': |
|
76
|
1 |
|
$connector = $this->createSocksProxy( |
|
77
|
1 |
|
$options['proxy'], |
|
78
|
1 |
|
$loop, |
|
79
|
1 |
|
$connector, |
|
80
|
1 |
|
$resolver |
|
81
|
1 |
|
); |
|
82
|
1 |
|
break; |
|
83
|
3 |
|
case 'socks4': |
|
84
|
3 |
|
case 'socks4a': |
|
85
|
2 |
|
$connector = $this->createSocksProxy( |
|
86
|
2 |
|
$options['proxy'], |
|
87
|
2 |
|
$loop, |
|
88
|
2 |
|
$connector, |
|
89
|
2 |
|
$resolver, |
|
90
|
|
|
4 |
|
91
|
2 |
|
); |
|
92
|
2 |
|
break; |
|
93
|
1 |
|
case 'socks5': |
|
94
|
1 |
|
$connector = $this->createSocksProxy( |
|
95
|
1 |
|
$options['proxy'], |
|
96
|
1 |
|
$loop, |
|
97
|
1 |
|
$connector, |
|
98
|
1 |
|
$resolver, |
|
99
|
|
|
5 |
|
100
|
1 |
|
); |
|
101
|
1 |
|
break; |
|
102
|
5 |
|
} |
|
103
|
5 |
|
} |
|
104
|
|
|
|
|
105
|
6 |
|
return Sender::createFromLoopConnectors($loop, $connector); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
4 |
|
protected function createSocksProxy( |
|
109
|
|
|
$url, |
|
110
|
|
|
$loop, |
|
111
|
|
|
$connector, |
|
112
|
|
|
$resolver, |
|
113
|
|
|
$version = null |
|
114
|
|
|
) { |
|
115
|
4 |
|
$proxyClient = new SocksProxyClient( |
|
116
|
4 |
|
$url, |
|
117
|
4 |
|
$loop, |
|
118
|
4 |
|
$connector, |
|
119
|
|
|
$resolver |
|
120
|
4 |
|
); |
|
121
|
4 |
|
if ($version !== null) { |
|
122
|
3 |
|
$proxyClient->setProtocolVersion($version); |
|
123
|
3 |
|
} |
|
124
|
4 |
|
return $proxyClient->createConnector(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param array $options |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
6 |
|
protected function convertOptions(array $options) |
|
132
|
|
|
{ |
|
133
|
6 |
|
return $options; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param object $object |
|
138
|
|
|
* @param string $desiredProperty |
|
139
|
|
|
* @return mixed |
|
140
|
|
|
*/ |
|
141
|
6 |
|
protected function getProperty($object, $desiredProperty) |
|
142
|
|
|
{ |
|
143
|
6 |
|
$reflection = new ReflectionObject($object); |
|
144
|
6 |
|
$property = $reflection->getProperty($desiredProperty); |
|
145
|
6 |
|
$property->setAccessible(true); |
|
146
|
6 |
|
return $property->getValue($object); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|