public function forward(Request $request, $url)
{
// change request URL
$request->setUrl($url);
// prepare request and response objects
$this->request = $request;
$this->response = new Response();
$options = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 0, CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_FOLLOWLOCATION => false, CURLOPT_AUTOREFERER => false);
// this is probably a good place to add custom curl options that way other critical options below would overwrite that
$config_options = Config::get('curl', array());
$options = array_merge_custom($options, $config_options);
$options[CURLOPT_HEADERFUNCTION] = array($this, 'header_callback');
$options[CURLOPT_WRITEFUNCTION] = array($this, 'write_callback');
// Notify any listeners that the request is ready to be sent, and this is your last chance to make any modifications.
$this->dispatcher->dispatch('request.before_send', new ProxyEvent(array('request' => $this->request, 'response' => $this->response)));
// We may not even need to send this request if response is already available somewhere (CachePlugin)
if ($this->request->params->has('request.complete')) {
// do nothing?
} else {
// any plugin might have changed our URL by this point
$options[CURLOPT_URL] = $this->request->getUri();
// fill in the rest of cURL options
$options[CURLOPT_HTTPHEADER] = explode("\r\n", $this->request->getRawHeaders());
$options[CURLOPT_CUSTOMREQUEST] = $this->request->getMethod();
$options[CURLOPT_POSTFIELDS] = $this->request->getRawBody();
$ch = curl_init();
curl_setopt_array($ch, $options);
// fetch the status - if exception if throw any at callbacks, then the error will be supressed
$result = @curl_exec($ch);
// there must have been an error if at this point
if (!$result) {
$error = sprintf('(%d) %s', curl_errno($ch), curl_error($ch));
throw new \Exception($error);
}
// we have output waiting in the buffer?
$this->response->setContent($this->output_buffer);
// saves memory I would assume?
$this->output_buffer = null;
}
$this->dispatcher->dispatch('request.complete', new ProxyEvent(array('request' => $this->request, 'response' => $this->response)));
return $this->response;
}