Example #1
0
 /**
  * Makes an HTTP request. This method can be overridden by subclasses if
  * developers want to do fancier things or use something other than curl to
  * make the request.
  *
  * @param Request $request
  * @throws Github\ApiException
  * @return Response
  */
 public function makeRequest(Request $request)
 {
     if (isset($this->memoryCache[$cacheKey = md5(serialize($request))])) {
         return $this->memoryCache[$cacheKey];
     }
     $ch = $this->buildCurlResource($request);
     $result = curl_exec($ch);
     // provide certificate if needed
     if (curl_errno($ch) == CURLE_SSL_CACERT || curl_errno($ch) === CURLE_SSL_CACERT_BADFILE) {
         Debugger::log('Invalid or no certificate authority found, using bundled information', 'github');
         $this->curlOptions[CURLOPT_CAINFO] = CertificateHelper::getCaInfoFile();
         curl_setopt($ch, CURLOPT_CAINFO, CertificateHelper::getCaInfoFile());
         $result = curl_exec($ch);
     }
     // With dual stacked DNS responses, it's possible for a server to
     // have IPv6 enabled but not have IPv6 connectivity.  If this is
     // the case, curl will try IPv4 first and if that fails, then it will
     // fall back to IPv6 and the error EHOSTUNREACH is returned by the operating system.
     if ($result === FALSE && empty($opts[CURLOPT_IPRESOLVE])) {
         $matches = array();
         if (preg_match('/Failed to connect to ([^:].*): Network is unreachable/', curl_error($ch), $matches)) {
             if (strlen(@inet_pton($matches[1])) === 16) {
                 Debugger::log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.', 'github');
                 $this->curlOptions[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
                 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
                 $result = curl_exec($ch);
             }
         }
     }
     $info = curl_getinfo($ch);
     $info['http_code'] = (int) $info['http_code'];
     if (isset($info['request_header'])) {
         list($info['request_header']) = self::parseHeaders($info['request_header']);
     }
     $info['method'] = isset($post['method']) ? $post['method'] : 'GET';
     $info['headers'] = self::parseHeaders(substr($result, 0, $info['header_size']));
     $info['error'] = $result === FALSE ? array('message' => curl_error($ch), 'code' => curl_errno($ch)) : array();
     $request->setHeaders($info['request_header']);
     $response = new Response($request, substr($result, $info['header_size']), $info['http_code'], end($info['headers']), $info);
     if (!$response->isOk()) {
         $e = $response->toException();
         curl_close($ch);
         $this->onError($e, $response);
         throw $e;
     }
     $this->onSuccess($response);
     curl_close($ch);
     return $this->memoryCache[$cacheKey] = $response;
 }