Skip to content
Permalink
Browse files

Build prototype of running requests.

  • Loading branch information...
markstory committed Dec 25, 2012
1 parent 086dc2c commit ef79fd4dc7b9cf9f25dc9c12e471b8ea28ca97bd
Showing with 84 additions and 4 deletions.
  1. +67 −3 lib/Cake/Network/Http/Adapter/Stream.php
  2. +17 −1 lib/Cake/Network/Http/Response.php
@@ -13,6 +13,7 @@
*/
namespace Cake\Network\Http\Adapter;
use Cake\Error;
use Cake\Network\Http\Request;
use Cake\Network\Http\Response;
use Cake\Network\Http\FormData;
@@ -46,6 +47,13 @@ class Stream {
*/
protected $_stream;
/**
* Connection error list.
*
* @var array
*/
protected $_connectionErrors = [];
/**
* Send a request and get a response back.
*
@@ -54,10 +62,12 @@ class Stream {
* @return Cake\Network\Http\Response
*/
public function send(Request $request, $options) {
$this->_context = array();
$this->_stream = null;
$this->_context = [];
$this->_connectionErrors = [];
$this->_buildContext($request, $options);
return $this->_send();
return $this->_send($request);
}
/**
@@ -132,7 +142,9 @@ protected function _buildContent(Request $request, $options) {
$type = 'multipart/form-data; boundary="' . $formData->boundary() . '"';
$request->header('Content-Type', $type);
$this->_contextOptions['content'] = (string)$formData;
return;
}
$this->_contextOptions['content'] = $content;
}
/**
@@ -187,7 +199,59 @@ protected function _buildSslContext(Request $request, $options) {
}
}
protected function _send() {
/**
* Open the stream and send the request.
*
* @return void
* @throws Cake\Error\Exception
*/
protected function _send($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;
}
/**
@@ -18,7 +18,23 @@
*/
class Response {
public function __construct() {
protected $_headers;
protected $_content;
public function headers($headers = null) {
if ($headers === null) {
return $this->_headers;
}
$this->_headers = $headers;
return $this;
}
public function content($content) {
if ($content === null) {
return $this->_content;
}
$this->_content = $content;
return $this;
}
}

0 comments on commit ef79fd4

Please sign in to comment.
You can’t perform that action at this time.