Пример #1
0
 public function validateCybertipSchema(EntityEnclosingRequest $req)
 {
     $xsd = $this->getConfig()->get('xsd');
     $error = null;
     $prev = libxml_use_internal_errors(true);
     try {
         $body = (string) $req->getBody();
         $dom = new \DOMDocument();
         $dom->loadXML($body);
         $ret = $dom->schemaValidateSource($xsd);
         if (!$ret) {
             $error = self::libxmlErrorExceptionFactory();
         }
     } catch (\Exception $e) {
         $error = $e;
     }
     libxml_use_internal_errors($prev);
     if ($error instanceof \Exception) {
         throw $error;
     } else {
         if ($error) {
             throw new XsdValidateException($error);
         }
     }
 }
 /**
  * @return EntityBody|\Guzzle\Http\EntityBodyInterface|null
  */
 public function getBody()
 {
     if (!$this->body) {
         $body = '';
         $contentType = $this->originalContentType ?: $this->getHeader('Content-Type');
         if ($this->relatedParts) {
             $parts = array_merge(array(new RelatedString($this->originalBody, $contentType)), $this->relatedParts);
             foreach (new MultipartRelatedIterator($parts, '--' . $this->boundary) as $part) {
                 $body .= $part;
             }
             $contentType = sprintf('%s;boundary=%s', self::MULTIPART_RELATED, $this->boundary);
         } else {
             $body = $this->originalBody;
         }
         parent::setBody($body, $contentType);
     }
     return parent::getBody();
 }
 public function testSeeksToBeginningOfRequestBodyWhenRetrying()
 {
     // Create a request with a body
     $request = new EntityEnclosingRequest('PUT', 'http://www.example.com');
     $request->setBody('abc');
     // Set the retry time to be something that will be retried always
     $request->getParams()->set(BackoffPlugin::DELAY_PARAM, 2);
     // Seek to the end of the stream
     $request->getBody()->seek(3);
     $this->assertEquals('', $request->getBody()->read(1));
     // Create a plugin that does not delay when retrying
     $plugin = new BackoffPlugin(new ConstantBackoffStrategy(0));
     $plugin->onRequestPoll($this->getMockEvent($request));
     // Ensure that the stream was seeked to 0
     $this->assertEquals('a', $request->getBody()->read(1));
 }
 public function testDoesNotCloneBody()
 {
     $request = new EntityEnclosingRequest('PUT', 'http://test.com/foo');
     $request->setBody('test');
     $newRequest = clone $request;
     $newRequest->setBody('foo');
     $this->assertInternalType('string', (string) $request->getBody());
 }
Пример #5
0
 /**
  * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestPoll
  */
 public function testSeeksToBeginningOfRequestBodyWhenRetrying()
 {
     // Create a request with a body
     $request = new EntityEnclosingRequest('PUT', 'http://www.example.com');
     $request->setBody('abc');
     // Set the retry time to be something that will be retried always
     $request->getParams()->set('plugins.exponential_backoff.retry_time', 2);
     // Seek to the end of the stream
     $request->getBody()->seek(3);
     $this->assertEquals('', $request->getBody()->read(1));
     // Create a plugin that does not delay when retrying
     $plugin = new ExponentialBackoffPlugin(2, null, array($this, 'delayClosure'));
     $plugin->onRequestPoll($this->getMockEvent($request));
     // Ensure that the stream was seeked to 0
     $this->assertEquals('a', $request->getBody()->read(1));
 }
 /**
  * @covers Guzzle\Http\Plugin\ExponentialBackoffPlugin::onRequestPoll
  */
 public function testSeeksToBeginningOfRequestBodyWhenRetrying()
 {
     // Create a mock curl multi object
     $multi = $this->getMockBuilder('Guzzle\\Http\\Curl\\CurlMulti')->setMethods(array('remove', 'add'))->getMock();
     // Create a request with a body
     $request = new EntityEnclosingRequest('PUT', 'http://www.example.com');
     $request->setBody('abc');
     // Set the retry time to be something that will be retried always
     $request->getParams()->set('plugins.exponential_backoff.retry_time', 2);
     // Seek to the end of the stream
     $request->getBody()->seek(3);
     $this->assertEquals('', $request->getBody()->read(1));
     // Create a plugin that does not delay when retrying
     $plugin = new ExponentialBackoffPlugin(2, null, array($this, 'delayClosure'));
     // Create an event that is expected for the Poll event
     $event = new Event(array('request' => $request, 'curl_multi' => $multi));
     $event->setName(CurlMultiInterface::POLLING_REQUEST);
     $plugin->onRequestPoll($event);
     // Ensure that the stream was seeked to 0
     $this->assertEquals('a', $request->getBody()->read(1));
 }
    /**
     * Overrides default Guzzle usage of curl_multi_exec by a simple curl call
     * and adds some metrics in Newrelic
     *
     * @param EntityEnclosingRequest $request
     * @return Response
     */
    public static function sendRequest( EntityEnclosingRequest $request )
    {
        $ch = curl_init($request->getUrl());
        $curlOptions = array(
            CURLOPT_URL            => $request->getUrl(),
            CURLOPT_TIMEOUT        => \eZINI::instance('merck.ini')->variable('WebService', 'ESBTimeout'),
            CURLOPT_CONNECTTIMEOUT => \eZINI::instance('merck.ini')->variable('WebService', 'ESBConnectTimeout'),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER         => true,
            CURLOPT_USERAGENT      => (string) $request->getHeader('User-Agent'),
            CURLOPT_PORT           => $request->getPort(),
            CURLOPT_HTTPHEADER     => $request->getHeaderLines(),
            CURLOPT_HTTP_VERSION   => $request->getProtocolVersion() === '1.0'
                    ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1,
            // Verifies the authenticity of the peer's certificate
            CURLOPT_SSL_VERIFYPEER => 1,
            // Certificate must indicate that the server is the server to which you meant to connect
            CURLOPT_SSL_VERIFYHOST => 2,
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => (string) $request->getBody(),
            CURLOPT_VERBOSE        => false,
        );
        curl_setopt_array($ch, $curlOptions);

        $start = microtime(true);
        $curlResult = curl_exec( $ch );
        $curlCallTime = ( microtime(true) - $start ) * 1000;

        $newRelicApi = new \klpNrApi();
        $methodLabel =   preg_match('#/(?P<method>[^/?]+)(?:\?.*)?$#', $request->getUrl(), $m)
            ? ucfirst( $m['method'] )
            : 'Unknown';
        $newRelicApi->addParameter( 'ESB/'.$methodLabel.':'.$request->getPort(), $curlCallTime );
        $newRelicApi->setCustomMetric( 'Custom/ESB/'.$methodLabel, $curlCallTime );

        $body               = $curlResult;
        $maxHeaderBlocks    = 5;
        $index              = 0;

        // We have to deal with HTTP/1.1 100 continue headers
        do
        {
            list( $header, $body ) = explode("\r\n\r\n", $body, 2);
            $index++;
        }
        while( $index < $maxHeaderBlocks && strpos($body, 'HTTP/1') === 0 );

        $parsedHeaders = array();
        foreach( explode("\n", $header) as $h )
        {
            list( $key, $val ) = explode(':', $h);
            $parsedHeaders[trim($key)] = trim($val);
        }
        $http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );

        if ( \eZINI::instance('merck.ini')->variable( 'WebService', 'CurlGetInfo' ) == 'enabled' )
        {
            $curlInfo = curl_getinfo($ch);
            $logMessage  = \ClusterTool::clusterIdentifier().'::'.$methodLabel."\n";
            $logMessage .= var_export( $curlInfo, true );

            \eZLog::write($logMessage, 'esbcurldetails.log');
        }

        curl_close($ch);

        $response = new Response( $http_status, $parsedHeaders, $body );
        $response->setRequest($request);
        $request->setResponse($response);

        return $response;
    }