public function postExecuteRequest(PostExecuteRequest $event)
 {
     $timeElapsed = TimeUtil::getElapsedTime($this->currentStartTime);
     if (!isset($this->currentRequest)) {
         throw new \RuntimeException('Request not set');
     }
     if ($this->currentRequest !== $event->getRequest()) {
         throw new \RuntimeException('Requests differ');
     }
     $this->addStat(new Stat(StatsdDataInterface::STATSD_METRIC_TIMING, $timeElapsed, array('solarium_endpoint' => $this->currentEndpoint->getKey(), 'solarium_request_method' => strtolower($event->getRequest()->getMethod()), 'solarium_response_status' => sprintf('%d', $event->getResponse()->getStatusCode()))));
     $this->currentRequest = null;
     $this->currentStartTime = null;
     $this->currentEndpoint = null;
 }
    public function testToString()
    {
        $options = array('host' => '192.168.0.1', 'port' => 123, 'path' => '/mysolr/', 'core' => 'mycore', 'timeout' => 3, 'username' => 'x', 'password' => 'y');
        $this->endpoint->setOptions($options);
        $this->assertEquals('Solarium\\Core\\Client\\Endpoint::__toString
base uri: http://192.168.0.1:123/mysolr/mycore/
host: 192.168.0.1
port: 123
path: /mysolr
core: mycore
timeout: 3
authentication: Array
(
    [username] => x
    [password] => y
)
', (string) $this->endpoint);
    }
Esempio n. 3
0
 /**
  * @expectedException \Solarium\Exception\HttpException
  */
 public function testExecuteWithException()
 {
     $endpoint = new Endpoint();
     $endpoint->setPort(-1);
     // this forces an error
     $request = new Request();
     $this->adapter->execute($request, $endpoint);
 }
Esempio n. 4
0
 /**
  *
  * 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;
 }
Esempio n. 5
0
 /**
  * Create a stream context for a request
  *
  * @param  Request  $request
  * @param  Endpoint $endpoint
  * @return resource
  */
 public function createContext($request, $endpoint)
 {
     $method = $request->getMethod();
     $context = stream_context_create(array('http' => array('method' => $method, 'timeout' => $endpoint->getTimeout())));
     if ($method == Request::METHOD_POST) {
         if ($request->getFileUpload()) {
             stream_context_set_option($context, 'http', 'content', file_get_contents($request->getFileUpload()));
             $request->addHeader('Content-Type: multipart/form-data');
         } else {
             $data = $request->getRawData();
             if (null !== $data) {
                 stream_context_set_option($context, 'http', 'content', $data);
                 $request->addHeader('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'])) {
         $request->addHeader('Authorization: Basic ' . base64_encode($authData['username'] . ':' . $authData['password']));
     }
     $headers = $request->getHeaders();
     if (count($headers) > 0) {
         stream_context_set_option($context, 'http', 'header', implode("\r\n", $headers));
     }
     return $context;
 }
Esempio n. 6
0
 public function testSetAndGetEndpoint()
 {
     $endpoint = new Endpoint();
     $endpoint->setKey('master');
     $this->assertEquals($this->plugin, $this->plugin->setEndpoint($endpoint));
     $this->assertEquals($endpoint, $this->plugin->getEndPoint());
 }
Esempio n. 7
0
 public function testCreateContextWithAuthorization()
 {
     $timeout = 13;
     $method = Request::METHOD_HEAD;
     $request = new Request();
     $request->setMethod($method);
     $request->setAuthentication('someone', 'S0M3p455');
     $endpoint = new Endpoint();
     $endpoint->setTimeout($timeout);
     $context = $this->adapter->createContext($request, $endpoint);
     $this->assertEquals(array('http' => array('method' => $method, 'timeout' => $timeout, 'header' => 'Authorization: Basic c29tZW9uZTpTME0zcDQ1NQ==')), stream_context_get_options($context));
 }
Esempio n. 8
0
 /**
  * Create http request options from request.
  *
  * @param  Request  $request
  * @param  Endpoint $endpoint
  * @return array
  */
 protected function createOptions($request, $endpoint)
 {
     // @codeCoverageIgnoreStart
     $options = array('timeout' => $endpoint->getTimeout());
     foreach ($request->getHeaders() as $headerLine) {
         list($header, $value) = explode(':', $headerLine);
         if ($header = trim($header)) {
             $options['headers'][$header] = trim($value);
         }
     }
     return $options;
     // @codeCoverageIgnoreEnd
 }
Esempio n. 9
0
 /**
  * Add an endpoint
  *
  * Supports a endpoint instance or a config array as input.
  * In case of options a new endpoint instance wil be created based on the options.
  *
  * @throws InvalidArgumentException
  * @param  Endpoint|array           $endpoint
  * @return self                     Provides fluent interface
  */
 public function addEndpoint($endpoint)
 {
     if (is_array($endpoint)) {
         $endpoint = new Endpoint($endpoint);
     }
     $key = $endpoint->getKey();
     if (0 === strlen($key)) {
         throw new InvalidArgumentException('An endpoint must have a key value');
     }
     //double add calls for the same endpoint are ignored, but non-unique keys cause an exception
     //@todo add trigger_error with a notice for double add calls?
     if (array_key_exists($key, $this->endpoints) && $this->endpoints[$key] !== $endpoint) {
         throw new InvalidArgumentException('An endpoint must have a unique key');
     } else {
         $this->endpoints[$key] = $endpoint;
         // if no default endpoint is set do so now
         if (null == $this->defaultEndpoint) {
             $this->defaultEndpoint = $key;
         }
     }
     return $this;
 }
Esempio n. 10
0
 /**
  * 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);
 }
Esempio n. 11
0
 public function log(SolariumRequest $request, $response, SolariumEndpoint $endpoint, $duration)
 {
     $this->queries[] = array('request' => $request, 'response' => $response, 'duration' => $duration, 'base_uri' => $endpoint->getBaseUri());
 }