/** * * adapt Request to HttpRequest * * {@link http://us.php.net/manual/en/http.constants.php * HTTP Predefined Constant} * * {@link http://us.php.net/manual/en/http.request.options.php * HttpRequest options} * * @throws InvalidArgumentException * @param Request $request * @param Endpoint $endpoint * @param HttpRequest * @return \HttpRequest */ public function toHttpRequest($request, $endpoint) { $url = $endpoint->getBaseUri() . $request->getUri(); $httpRequest = new \HttpRequest($url); $headers = array(); foreach ($request->getHeaders() as $headerLine) { list($header, $value) = explode(':', $headerLine); if ($header = trim($header)) { $headers[$header] = trim($value); } } // Try endpoint authentication first, fallback to request for backwards compatibility $authData = $endpoint->getAuthentication(); if (empty($authData['username'])) { $authData = $request->getAuthentication(); } if (!empty($authData['username']) && !empty($authData['password'])) { $headers['Authorization'] = 'Basic ' . base64_encode($authData['username'] . ':' . $authData['password']); } switch ($request->getMethod()) { case Request::METHOD_GET: $method = HTTP_METH_GET; break; case Request::METHOD_POST: $method = HTTP_METH_POST; if ($request->getFileUpload()) { $httpRequest->addPostFile('content', $request->getFileUpload(), 'application/octet-stream; charset=binary'); } else { $httpRequest->setBody($request->getRawData()); if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'text/xml; charset=utf-8'; } } break; case Request::METHOD_HEAD: $method = HTTP_METH_HEAD; break; default: throw new InvalidArgumentException('Unsupported method: ' . $request->getMethod()); } $httpRequest->setMethod($method); $httpRequest->setOptions(array('timeout' => $endpoint->getTimeout(), 'connecttimeout' => $endpoint->getTimeout(), 'dns_cache_timeout' => $endpoint->getTimeout())); $httpRequest->setHeaders($headers); return $httpRequest; }
public function testGetBaseUriWithCore() { $this->endpoint->setHost('myserver')->setPath('/mypath')->setPort(123)->setCore('mycore'); $this->assertEquals('http://myserver:123/mypath/mycore/', $this->endpoint->getBaseUri()); }
/** * Create curl handle for a request * * @throws InvalidArgumentException * @param Request $request * @param Endpoint $endpoint * @return resource */ public function createHandle($request, $endpoint) { // @codeCoverageIgnoreStart $uri = $endpoint->getBaseUri() . $request->getUri(); $method = $request->getMethod(); $options = $this->createOptions($request, $endpoint); $handler = curl_init(); curl_setopt($handler, CURLOPT_URL, $uri); curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); if (!ini_get('open_basedir')) { curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true); } curl_setopt($handler, CURLOPT_TIMEOUT, $options['timeout']); curl_setopt($handler, CURLOPT_CONNECTTIMEOUT, $options['timeout']); if ($proxy = $this->getOption('proxy')) { curl_setopt($handler, CURLOPT_PROXY, $proxy); } if (!isset($options['headers']['Content-Type'])) { if ($method == Request::METHOD_GET) { $options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; } else { $options['headers']['Content-Type'] = 'application/xml; charset=utf-8'; } } // Try endpoint authentication first, fallback to request for backwards compatibility $authData = $endpoint->getAuthentication(); if (empty($authData['username'])) { $authData = $request->getAuthentication(); } if (!empty($authData['username']) && !empty($authData['password'])) { curl_setopt($handler, CURLOPT_USERPWD, $authData['username'] . ':' . $authData['password']); curl_setopt($handler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } if (count($options['headers'])) { $headers = array(); foreach ($options['headers'] as $key => $value) { $headers[] = $key . ": " . $value; } curl_setopt($handler, CURLOPT_HTTPHEADER, $headers); } if ($method == Request::METHOD_POST) { curl_setopt($handler, CURLOPT_POST, true); if ($request->getFileUpload()) { if (version_compare(PHP_VERSION, '5.5.0') >= 0) { $curlFile = curl_file_create($request->getFileUpload()); curl_setopt($handler, CURLOPT_POSTFIELDS, array('content' => $curlFile)); } else { curl_setopt($handler, CURLOPT_POSTFIELDS, array('content' => '@' . $request->getFileUpload())); } } else { curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData()); } } elseif ($method == Request::METHOD_GET) { curl_setopt($handler, CURLOPT_HTTPGET, true); } elseif ($method == Request::METHOD_HEAD) { curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'HEAD'); } else { throw new InvalidArgumentException("unsupported method: {$method}"); } return $handler; // @codeCoverageIgnoreEnd }
/** * Execute a Solr request using the Zend_Http_Client instance * * @throws HttpException * @throws OutOfBoundsException * @param Request $request * @param Endpoint $endpoint * @return Response */ public function execute($request, $endpoint) { $client = $this->getZendHttp(); $client->resetParameters(); switch ($request->getMethod()) { case Request::METHOD_GET: $client->setMethod(\Zend_Http_Client::GET); $client->setParameterGet($request->getParams()); break; case Request::METHOD_POST: $client->setMethod(\Zend_Http_Client::POST); if ($request->getFileUpload()) { $this->prepareFileUpload($client, $request); } else { $client->setParameterGet($request->getParams()); $client->setRawData($request->getRawData()); $request->addHeader('Content-Type: text/xml; charset=UTF-8'); } break; case Request::METHOD_HEAD: $client->setMethod(\Zend_Http_Client::HEAD); $client->setParameterGet($request->getParams()); break; default: throw new OutOfBoundsException('Unsupported method: ' . $request->getMethod()); break; } $client->setUri($endpoint->getBaseUri() . $request->getHandler()); $client->setHeaders($request->getHeaders()); $this->timeout = $endpoint->getTimeout(); $response = $client->request(); return $this->prepareResponse($request, $response); }
/** * Create curl handle for a request * * @throws InvalidArgumentException * @param Request $request * @param Endpoint $endpoint * @return resource */ public function createHandle($request, $endpoint) { // @codeCoverageIgnoreStart $uri = $endpoint->getBaseUri() . $request->getUri(); $method = $request->getMethod(); $options = $this->createOptions($request, $endpoint); $handler = curl_init(); curl_setopt($handler, CURLOPT_URL, $uri); curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true); curl_setopt($handler, CURLOPT_TIMEOUT, $options['timeout']); /* * Begin Added for Pantheon * Subclass and jsu override createHandle */ curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false); if ($endpoint->getOption('ssl')['local_cert']) { curl_setopt($handler, CURLOPT_SSLCERT, realpath($endpoint->getOption('ssl')['local_cert'])); } if ($endpoint->getPort()) { curl_setopt($handler, CURLOPT_PORT, $endpoint->getPort()); } // End Added for pantheon if ($proxy = $this->getOption('proxy')) { curl_setopt($handler, CURLOPT_PROXY, $proxy); } if (!isset($options['headers']['Content-Type'])) { $options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; } // Try endpoint authentication first, fallback to request for backwards compatibility $authData = $endpoint->getAuthentication(); if (empty($authData['username'])) { $authData = $request->getAuthentication(); } if (!empty($authData['username']) && !empty($authData['password'])) { curl_setopt($handler, CURLOPT_USERPWD, $authData['username'] . ':' . $authData['password']); curl_setopt($handler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } if (count($options['headers'])) { $headers = array(); foreach ($options['headers'] as $key => $value) { $headers[] = $key . ": " . $value; } curl_setopt($handler, CURLOPT_HTTPHEADER, $headers); } if ($method == Request::METHOD_POST) { curl_setopt($handler, CURLOPT_POST, true); if ($request->getFileUpload()) { curl_setopt($handler, CURLOPT_POSTFIELDS, array('content' => '@' . $request->getFileUpload())); } else { curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData()); } } elseif ($method == Request::METHOD_GET) { curl_setopt($handler, CURLOPT_HTTPGET, true); } elseif ($method == Request::METHOD_HEAD) { curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'HEAD'); } else { throw new InvalidArgumentException("unsupported method: {$method}"); } return $handler; // @codeCoverageIgnoreEnd }
public function log(SolariumRequest $request, $response, SolariumEndpoint $endpoint, $duration) { $this->queries[] = array('request' => $request, 'response' => $response, 'duration' => $duration, 'base_uri' => $endpoint->getBaseUri()); }