/
Stream.php
269 lines (246 loc) · 6.85 KB
/
Stream.php
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Network\Http\Adapter;
use Cake\Error;
use Cake\Network\Http\Request;
use Cake\Network\Http\Response;
use Cake\Network\Http\FormData;
/**
* Implements sending Cake\Network\Http\Request
* via php's stream API.
*
* This approach and implementation is partly inspired by Aura.Http
*/
class Stream {
/**
* Context resource used by the stream API.
*
* @var resource
*/
protected $_context;
/**
* Array of options/content for the stream context.
*
* @var array
*/
protected $_contextOptions;
/**
* The stream resource.
*
* @var resource
*/
protected $_stream;
/**
* Connection error list.
*
* @var array
*/
protected $_connectionErrors = [];
/**
* Send a request and get a response back.
*
* @param Cake\Network\Http\Request $request The request object to send.
* @param array $options Array of options for the stream.
* @return Cake\Network\Http\Response
*/
public function send(Request $request, $options) {
$this->_stream = null;
$this->_context = [];
$this->_connectionErrors = [];
$this->_buildContext($request, $options);
return $this->_send($request);
}
/**
* Build the stream context out of the request object.
*
* @param Cake\Network\Http\Request $request The request to build context from.
* @param array $options Additional request options.
* @return void
*/
protected function _buildContext(Request $request, $options) {
$this->_buildContent($request, $options);
$this->_buildHeaders($request, $options);
$this->_buildOptions($request, $options);
$url = $request->url();
$scheme = parse_url($url, PHP_URL_SCHEME);
if ($scheme === 'https') {
$this->_buildSslContext($request, $options);
}
$this->_context = stream_context_create([
$scheme => $this->_contextOptions
]);
}
/**
* Build the header context for the request.
*
* Creates cookies & headers.
*
* @param Cake\Network\Http\Request $request The request being sent.
* @param array $options Array of options to use.
* @return void
*/
protected function _buildHeaders(Request $request, $options) {
$headers = [];
foreach ($request->headers() as $name => $value) {
$headers[] = "$name: $value";
}
$cookies = [];
foreach ($request->cookies() as $name => $value) {
$cookies[] = "$name=$value";
}
if ($cookies) {
$headers[] = 'Cookie: ' . implode('; ', $cookies);
}
$this->_contextOptions['header'] = implode("\r\n", $headers);
}
/**
* Builds the request content based on the request object.
*
* If the $request->body() is a string, it will be used as is.
* Array data will be processed with Cake\Network\Http\FormData
*
* @param Cake\Network\Http\Request $request The request being sent.
* @param array $options Array of options to use.
* @return void
*/
protected function _buildContent(Request $request, $options) {
$content = $request->body();
if (empty($content)) {
return;
}
if (is_string($content)) {
$this->_contextOptions['content'] = $content;
return;
}
if (is_array($content)) {
$formData = new FormData();
$formData->addMany($content);
$type = 'multipart/form-data; boundary="' . $formData->boundary() . '"';
$request->header('Content-Type', $type);
$this->_contextOptions['content'] = (string)$formData;
return;
}
$this->_contextOptions['content'] = $content;
}
/**
* Build miscellaneous options for the request.
*
* @param Cake\Network\Http\Request $request The request being sent.
* @param array $options Array of options to use.
* @return void
*/
protected function _buildOptions(Request $request, $options) {
$this->_contextOptions['method'] = $request->method();
$this->_contextOptions['protocol_version'] = $request->version();
$this->_contextOptions['ignore_errors'] = true;
if (isset($options['timeout'])) {
$this->_contextOptions['timeout'] = $options['timeout'];
}
if (!empty($options['redirect'])) {
$this->_contextOptions['max_redirects'] = $options['redirect'];
}
}
/**
* Build SSL options for the request.
*
* @param Cake\Network\Http\Request $request The request being sent.
* @param array $options Array of options to use.
* @return void
*/
protected function _buildSslContext(Request $request, $options) {
$sslOptions = [
'ssl_verify_peer',
'ssl_verify_depth',
'ssl_allow_self_signed',
'ssl_cafile',
'ssl_local_cert',
'ssl_passphrase',
];
if (empty($options['ssl_cafile'])) {
$options['ssl_cafile'] = CAKE . 'Config' . DS . 'cacert.pem';
}
if (!empty($options['ssl_verify_host'])) {
$url = $request->url();
$host = parse_url($url, PHP_URL_HOST);
$this->_contextOptions['CN_match'] = $host;
}
foreach ($sslOptions as $key) {
if (isset($options[$key])) {
$name = substr($key, 4);
$this->_contextOptions[$name] = $options[$key];
}
}
}
/**
* Open the stream and send the request.
*
* @param Request $request
* @return Response The populated response object.
* @throws Cake\Error\Exception
*/
protected function _send(Request $request) {
$url = $request->url();
$this->_open($url);
$content = '';
while (!feof($this->_stream)) {
$content .= fread($this->_stream, 8192);
}
$meta = stream_get_meta_data($this->_stream);
fclose($this->_stream);
if ($meta['timed_out']) {
throw Error\Exception('Connection timed out ' . $url);
}
$headers = $meta['wrapper_data'];
if (isset($meta['wrapper_type']) && $meta['wrapper_type'] === 'curl') {
$headers = $meta['wrapper_data']['headers'];
}
return new Response($headers, $content);
}
/**
* Open the socket and handle any connection errors.
*
* @param string $url The url to connect to.
* @return void
* @throws Cake\Error\Exception
*/
protected function _open($url) {
set_error_handler([$this, '_connectionErrorHandler']);
$this->_stream = fopen($url, 'rb', false, $this->_context);
restore_error_handler();
if (!$this->_stream || !empty($this->_connectionErrors)) {
throw new Error\Exception(implode("\n", $this->_connectionErrors));
}
}
/**
* Local error handler to capture errors triggered during
* stream connection.
*
* @param int $code
* @param string $message
* @return void
*/
protected function _connectionErrorHandler($code, $message) {
$this->_connectionErrors[] = $message;
}
/**
* Get the context options
*
* Useful for debugging and testing context creation.
*
* @return array
*/
public function contextOptions() {
return $this->_contextOptions;
}
}