/**
  * (non-PHPdoc)
  * @see ObjectStorage_Http_Adapter_Interface::request()
  */
 public function request()
 {
     $curl = curl_init();
     $urlInfo = parse_url($this->uri);
     $requiredHeaders[] = 'Host: ' . $urlInfo['host'];
     $requiredHeaders[] = 'Connection: close';
     $requiredHeaders[] = 'Expect:';
     $this->requestHeaders = array_merge($requiredHeaders, $this->headers);
     // To get around CURL issue. http://curl.haxx.se/mail/lib-2010-08/0171.html
     $requestHeaders = implode("\r\n", $this->requestHeaders);
     curl_setopt($curl, CURLOPT_HEADER, true);
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_URL, $this->uri);
     curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
     curl_setopt($curl, CURLOPT_HTTPHEADER, array($requestHeaders));
     if ($this->connectTimeout !== null) {
         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
     }
     $method = strtoupper($this->method);
     switch ($method) {
         case 'HEAD':
             curl_setopt($curl, CURLOPT_NOBODY, true);
             break;
         case 'PUT':
             curl_setopt($curl, CURLOPT_PUT, true);
             break;
         case 'POST':
             curl_setopt($curl, CURLOPT_POST, true);
             break;
         case 'DELETE':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
     }
     if (in_array($method, array('PUT', 'POST'))) {
         if ($this->fileHandler != null) {
             curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
             curl_setopt($curl, CURLOPT_INFILE, $this->fileHandler);
             curl_setopt($curl, CURLOPT_READFUNCTION, array(&$this, 'readFileCallback'));
         } else {
             if ($this->body != '') {
                 $filePointer = fopen('php://temp/maxmemory:256000', 'w');
                 if (!$filePointer) {
                     throw new ObjectStorage_Exception('could not open temp memory data');
                 }
                 fwrite($filePointer, $this->body);
                 fseek($filePointer, 0);
                 curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
                 curl_setopt($curl, CURLOPT_INFILE, $filePointer);
                 curl_setopt($curl, CURLOPT_INFILESIZE, strlen($this->body));
             }
         }
     }
     $rawResponse = curl_exec($curl);
     if ($rawResponse === false) {
         throw new ObjectStorage_Exception(curl_error($curl));
     }
     $curlInfo = curl_getinfo($curl);
     $rawHeaders = substr($rawResponse, 0, $curlInfo['header_size']);
     $headers = array();
     if ($rawHeaders != '') {
         $headerLines = explode("\n", $rawHeaders);
         foreach ($headerLines as $line) {
             $headerChunk = explode(': ', $line);
             if (count($headerChunk) == 2) {
                 $headers[ucfirst(strtolower($headerChunk[0]))] = trim($headerChunk[1]);
             }
         }
     }
     $body = substr($rawResponse, $curlInfo['header_size']);
     $statusCode = $curlInfo['http_code'];
     curl_close($curl);
     $response = new ObjectStorage_Http_Response();
     $response->setStatusCode($statusCode);
     $response->setHeaders($headers);
     $response->setBody($body);
     return $response;
 }
 /**
  * (non-PHPdoc)
  * @see ObjectStorage_Http_Adapter_Interface::request()
  */
 public function request()
 {
     $urlInfo = parse_url($this->uri);
     if (!isset($urlInfo['port'])) {
         $urlInfo['port'] = $urlInfo['scheme'] == 'https' ? 443 : 80;
     }
     $scheme = $urlInfo['scheme'] == 'https' ? 'ssl://' : '';
     if (($socket = @fsockopen($scheme . $urlInfo['host'], $urlInfo['port'], $errno, $errstr, $this->timeout)) == false) {
         throw new ObjectStorage_Exception($errstr);
     }
     stream_set_timeout($socket, 0, $this->timeout * 1000);
     $statusCode = 0;
     $this->requestHeaders = array();
     $lineBreak = "\r\n";
     $doubleLineBreaks = $lineBreak . $lineBreak;
     $requestData = $responseData = '';
     $this->requestHeaders[] = $this->method . ' ' . $urlInfo['path'] . (isset($urlInfo['query']) ? '?' . $urlInfo['query'] : '') . ' HTTP/1.1';
     $this->requestHeaders[] = 'Host: ' . $urlInfo['host'];
     $this->requestHeaders[] = 'Connection: Close';
     if (count($this->headers) > 0) {
         $this->requestHeaders = array_merge($this->requestHeaders, $this->headers);
     }
     $requestData = implode($lineBreak, $this->requestHeaders) . $doubleLineBreaks;
     fputs($socket, $requestData);
     if (in_array($this->method, array('PUT', 'POST'))) {
         if (is_resource($this->fileHander)) {
             $fseek = 0;
             // Used for debugging purposes
             $readSize = 1024;
             while (!feof($this->fileHander)) {
                 $contents = fread($this->fileHander, $readSize);
                 if (@fwrite($socket, $contents) === false) {
                     throw new ObjectStorage_Exception('Failed to write data to socket when writing about ' . $fseek . ' bytes.');
                 }
                 $fseek += $readSize;
             }
             fclose($this->fileHander);
         } else {
             if ($this->body != '') {
                 fputs($socket, $this->body);
             }
         }
     }
     while (!feof($socket)) {
         $responseData .= fgets($socket, 128);
     }
     fclose($socket);
     $headerEndingPosition = strpos($responseData, $doubleLineBreaks);
     $rawHeaders = substr($responseData, 0, $headerEndingPosition);
     $headers = array();
     if ($rawHeaders != '') {
         $headerLines = explode("\n", $rawHeaders);
         $isFirst = true;
         foreach ($headerLines as $line) {
             if ($isFirst == true) {
                 $statusChunks = explode(' ', $line);
                 $statusCode = (int) $statusChunks[1];
                 $isFirst = false;
             }
             $headerChunk = explode(': ', $line);
             if (count($headerChunk) == 2) {
                 $headers[ucfirst(strtolower($headerChunk[0]))] = trim($headerChunk[1]);
             }
         }
     }
     $body = substr($responseData, $headerEndingPosition + strlen($doubleLineBreaks));
     $response = new ObjectStorage_Http_Response();
     $response->setStatusCode($statusCode);
     $response->setHeaders($headers);
     $response->setBody($body);
     return $response;
 }
 /**
  * (non-PHPdoc)
  * @see ObjectStorage_Http_Adapter_Interface::request()
  */
 public function request()
 {
     $this->requestHeaders = $this->headers;
     $this->client->setConfig(array('timeout', $this->timeout));
     $result = $this->client->request();
     $response = new ObjectStorage_Http_Response();
     $response->setStatusCode($result->getStatus());
     $response->setHeaders($result->getHeaders());
     $response->setBody($result->getBody());
     return $response;
 }