/**
  * Execute an HTTP request and return the results
  *
  * This function will throw if no connection to the server can be established or if
  * there is a problem during data exchange with the server.
  *
  * will restore it.
  *
  * @throws Exception
  *
  * @param string $method       - HTTP request method
  * @param string $url          - HTTP URL
  * @param string $data         - data to post in body
  * @param array  $customHeader - any array containing header elements
  *
  * @return HttpResponse
  */
 private function executeRequest($method, $url, $data, $customHeader = array())
 {
     HttpHelper::validateMethod($method);
     $database = $this->getDatabase();
     if ($database === '') {
         $url = '/_db/' . '_system' . $url;
     } else {
         $url = '/_db/' . $database . $url;
     }
     // create request data
     if ($this->_batchRequest === false) {
         if ($this->_captureBatch === true) {
             $this->_options->offsetSet(ConnectionOptions::OPTION_BATCHPART, true);
             $request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customHeader);
             $this->_options->offsetSet(ConnectionOptions::OPTION_BATCHPART, false);
         } else {
             $request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customHeader);
         }
         if ($this->_captureBatch === true) {
             $batchPart = $this->doBatch($method, $request);
             if (!is_null($batchPart)) {
                 return $batchPart;
             }
         }
     } else {
         $this->_batchRequest = false;
         $this->_options->offsetSet(ConnectionOptions::OPTION_BATCH, true);
         $request = HttpHelper::buildRequest($this->_options, $method, $url, $data, $customHeader);
         $this->_options->offsetSet(ConnectionOptions::OPTION_BATCH, false);
     }
     $traceFunc = $this->_options[ConnectionOptions::OPTION_TRACE];
     if ($traceFunc) {
         // call tracer func
         if ($this->_options[ConnectionOptions::OPTION_ENHANCED_TRACE]) {
             list($header) = HttpHelper::parseHttpMessage($request, $url, $method);
             $headers = HttpHelper::parseHeaders($header);
             $traceFunc(new TraceRequest($headers[2], $method, $url, $data));
         } else {
             $traceFunc('send', $request);
         }
     }
     // open the socket. note: this might throw if the connection cannot be established
     $handle = $this->getHandle();
     if ($handle) {
         // send data and get response back
         if ($traceFunc) {
             // only issue syscall if we need it
             $startTime = microtime(true);
         }
         $result = HttpHelper::transfer($handle, $request);
         if ($traceFunc) {
             // only issue syscall if we need it
             $timeTaken = microtime(true) - $startTime;
         }
         $status = socket_get_status($handle);
         if ($status['timed_out']) {
             throw new ClientException('Got a timeout while waiting for the server\'s response', 408);
         }
         if (!$this->_useKeepAlive) {
             // must close the connection
             fclose($handle);
         }
         $response = new HttpResponse($result, $url, $method);
         if ($traceFunc) {
             // call tracer func
             if ($this->_options[ConnectionOptions::OPTION_ENHANCED_TRACE]) {
                 $traceFunc(new TraceResponse($response->getHeaders(), $response->getHttpCode(), $response->getBody(), $timeTaken));
             } else {
                 $traceFunc('receive', $result);
             }
         }
         return $response;
     }
     throw new ClientException('Whoops, this should never happen');
 }
Exemplo n.º 2
0
 /**
  * Splits a http message into its header and body.
  *
  * @param string $httpMessage The http message string.
  * @param string $originUrl The original URL the response is coming from
  * @param string $originMethod The HTTP method that was used when sending data to the origin URL
  *
  * @throws ClientException
  * @return array
  */
 public static function parseHttpMessage($httpMessage, $originUrl = null, $originMethod = null)
 {
     assert(is_string($httpMessage));
     $barrier = HttpHelper::EOL . HttpHelper::EOL;
     $parts = explode($barrier, $httpMessage, 2);
     $parsed = HttpHelper::parseHeaders($parts[0]);
     if ($parsed[0] == 304 || $parsed[0] == 204) {
         return $parts;
     }
     if (!isset($parts[1]) or $parts[1] === null) {
         if ($originUrl !== null && $originMethod !== null) {
             throw new ClientException('Got an invalid response from the server after request to ' . $originMethod . ' ' . $originUrl);
         }
         throw new ClientException('Got an invalid response from the server');
     }
     return $parts;
 }
 /**
  * Set up the response
  *
  * @throws ClientException
  *
  * @param string $responseString - the complete HTTP response as supplied by the server
  * @param string $originUrl The original URL the response is coming from
  * @param string $originMethod The HTTP method that was used when sending data to the origin URL
  */
 public function __construct($responseString, $originUrl = null, $originMethod = null)
 {
     list($this->_header, $this->_body) = HttpHelper::parseHttpMessage($responseString, $originUrl, $originMethod);
     list($this->_httpCode, $this->_result, $this->_headers) = HttpHelper::parseHeaders($this->_header);
 }