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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
<?php
namespace iplx\Http;
use RuntimeException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* HTTP client that uses cURL
*/
class Client implements ClientInterface
{
/**
* Client version
*
* @var string
*/
const VERSION = '1.0.0';
/**
* Maximum number of internal cURL handles
*
* @var int
*/
const MAX_HANDLES = 4;
/**
* Internal cURL handles
*
* @var array
*/
protected $handles = [];
/**
* Return user agent
*
* @return string
*/
protected function getAgent()
{
$defaultAgent = 'ipl/' . self::VERSION;
$defaultAgent .= ' curl/' . curl_version()['version'];
$defaultAgent .= ' PHP/' . PHP_VERSION;
return $defaultAgent;
}
/**
* Create and return a cURL handle based on the given request
*
* @param RequestInterface $request
* @param array $options
*
* @return Handle
*
* @throws RuntimeException
*/
protected function createHandle(RequestInterface $request, array $options)
{
$headers = [];
foreach ($request->getHeaders() as $name => $values) {
$headers[] = $name . ': ' . implode(', ', $values);
}
$curlOptions = [
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FAILONERROR => true,
CURLOPT_USERAGENT => $this->getAgent()
];
if (isset($options['curl'])) {
$curlOptions += $options['curl'];
}
$curlOptions += [
CURLOPT_CUSTOMREQUEST => $request->getMethod(),
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => false,
CURLOPT_URL => (string) $request->getUri()->withFragment('')
];
if (! $request->hasHeader('Accept')) {
$curlOptions[CURLOPT_HTTPHEADER][] = 'Accept:';
}
if (! $request->hasHeader('Content-Type')) {
$curlOptions[CURLOPT_HTTPHEADER][] = 'Content-Type:';
}
if (! $request->hasHeader('Expect')) {
$curlOptions[CURLOPT_HTTPHEADER][] = 'Expect:';
}
if ($request->getBody()->getSize() !== 0) {
$curlOptions[CURLOPT_UPLOAD] = true;
$body = $request->getBody();
if ($body->isSeekable()) {
$body->seek(0);
}
$curlOptions[CURLOPT_READFUNCTION] = function ($ch, $infile, $length) use ($body) {
return $body->read($length);
};
}
if ($request->getProtocolVersion()) {
$protocolVersion = null;
switch ($request->getProtocolVersion()) {
case '2.0':
if (version_compare(phpversion(), '7.0.7', '<')) {
throw new RuntimeException('You need at least PHP 7.0.7 to use HTTP 2.0');
}
$protocolVersion = CURL_HTTP_VERSION_2;
break;
case '1.1':
$protocolVersion = CURL_HTTP_VERSION_1_1;
break;
default:
$protocolVersion = CURL_HTTP_VERSION_1_0;
}
$curlOptions[CURLOPT_HTTP_VERSION] = $protocolVersion;
}
$handle = new Handle();
$curlOptions[CURLOPT_HEADERFUNCTION] = function($ch, $header) use ($handle) {
$size = strlen($header);
if (! trim($header) || strpos($header, 'HTTP/') === 0) {
return $size;
}
list($key, $value) = explode(': ', $header, 2);
$handle->responseHeaders[$key] = rtrim($value, "\r\n");
return $size;
};
$handle->responseBody = Stream::open();
$curlOptions[CURLOPT_WRITEFUNCTION] = function ($ch, $string) use ($handle) {
return $handle->responseBody->write($string);
};
$ch = ! empty($this->handles) ? array_pop($this->handles) : curl_init();
curl_setopt_array($ch, $curlOptions);
$handle->handle = $ch;
return $handle;
}
/**
* Execute a cURL handle and return the response
*
* @param Handle $handle
*
* @return ResponseInterface
*
* @throws RuntimeException
*/
protected function executeHandle(Handle $handle)
{
$ch = $handle->handle;
$success = curl_exec($ch);
if ($success === false) {
throw new RuntimeException(curl_error($ch));
}
$response = new Response(
curl_getinfo($ch, CURLINFO_HTTP_CODE), $handle->responseHeaders, $handle->responseBody
);
if (count($this->handles) >= self::MAX_HANDLES) {
curl_close($ch);
} else {
curl_reset($ch);
$this->handles[] = $ch;
}
return $response;
}
public function send(RequestInterface $request, array $options = [])
{
$handle = $this->createHandle($request, $options);
$response = $this->executeHandle($handle);
return $response;
}
}
|