Skip to content

Commit ef79fd4

Browse files
committed
Build prototype of running requests.
1 parent 086dc2c commit ef79fd4

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

lib/Cake/Network/Http/Adapter/Stream.php

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
namespace Cake\Network\Http\Adapter;
1515

16+
use Cake\Error;
1617
use Cake\Network\Http\Request;
1718
use Cake\Network\Http\Response;
1819
use Cake\Network\Http\FormData;
@@ -46,6 +47,13 @@ class Stream {
4647
*/
4748
protected $_stream;
4849

50+
/**
51+
* Connection error list.
52+
*
53+
* @var array
54+
*/
55+
protected $_connectionErrors = [];
56+
4957
/**
5058
* Send a request and get a response back.
5159
*
@@ -54,10 +62,12 @@ class Stream {
5462
* @return Cake\Network\Http\Response
5563
*/
5664
public function send(Request $request, $options) {
57-
$this->_context = array();
65+
$this->_stream = null;
66+
$this->_context = [];
67+
$this->_connectionErrors = [];
5868

5969
$this->_buildContext($request, $options);
60-
return $this->_send();
70+
return $this->_send($request);
6171
}
6272

6373
/**
@@ -132,7 +142,9 @@ protected function _buildContent(Request $request, $options) {
132142
$type = 'multipart/form-data; boundary="' . $formData->boundary() . '"';
133143
$request->header('Content-Type', $type);
134144
$this->_contextOptions['content'] = (string)$formData;
145+
return;
135146
}
147+
$this->_contextOptions['content'] = $content;
136148
}
137149

138150
/**
@@ -187,7 +199,59 @@ protected function _buildSslContext(Request $request, $options) {
187199
}
188200
}
189201

190-
protected function _send() {
202+
/**
203+
* Open the stream and send the request.
204+
*
205+
* @return void
206+
* @throws Cake\Error\Exception
207+
*/
208+
protected function _send($request) {
209+
$url = $request->url();
210+
$this->_open($url);
211+
$content = '';
212+
while (!feof($this->_stream)) {
213+
$content .= fread($this->_stream, 8192);
214+
}
215+
$meta = stream_get_meta_data($this->_stream);
216+
fclose($this->_stream);
217+
218+
if ($meta['timed_out']) {
219+
throw Error\Exception('Connection timed out ' . $url);
220+
}
221+
$headers = $meta['wrapper_data'];
222+
if (isset($meta['wrapper_type']) && $meta['wrapper_type'] === 'curl') {
223+
$headers = $meta['wrapper_data']['headers'];
224+
}
225+
return new Response($headers, $content);
226+
}
227+
228+
/**
229+
* Open the socket and handle any connection errors.
230+
*
231+
* @param string $url The url to connect to.
232+
* @return void
233+
* @throws Cake\Error\Exception
234+
*/
235+
protected function _open($url) {
236+
set_error_handler([$this, '_connectionErrorHandler']);
237+
$this->_stream = fopen($url, 'rb', false, $this->_context);
238+
restore_error_handler();
239+
240+
if (!$this->_stream || !empty($this->_connectionErrors)) {
241+
throw new Error\Exception(implode("\n", $this->_connectionErrors));
242+
}
243+
}
244+
245+
/**
246+
* Local error handler to capture errors triggered during
247+
* stream connection.
248+
*
249+
* @param int $code
250+
* @param string $message
251+
* @return void
252+
*/
253+
protected function _connectionErrorHandler($code, $message) {
254+
$this->_connectionErrors[] = $message;
191255
}
192256

193257
/**

lib/Cake/Network/Http/Response.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,23 @@
1818
*/
1919
class Response {
2020

21-
public function __construct() {
21+
protected $_headers;
22+
protected $_content;
23+
24+
public function headers($headers = null) {
25+
if ($headers === null) {
26+
return $this->_headers;
27+
}
28+
$this->_headers = $headers;
29+
return $this;
30+
}
31+
32+
public function content($content) {
33+
if ($content === null) {
34+
return $this->_content;
35+
}
36+
$this->_content = $content;
37+
return $this;
2238
}
2339

2440
}

0 commit comments

Comments
 (0)