1 backdrop_web_test_case.php protected BackdropWebTestCase::curlExec($curl_options, $redirect = FALSE)

Initializes and executes a cURL request.

Parameters

$curl_options: An associative array of cURL options to set, where the keys are constants defined by the cURL library. For a list of valid options, see http://www.php.net/manual/function.curl-setopt.php

$redirect: FALSE if this is an initial request, TRUE if this request is the result of a redirect.

Return value

The content returned from the call to curl_exec().:

See also

curlInitialize()

File

core/modules/simpletest/backdrop_web_test_case.php, line 2029

Class

BackdropWebTestCase
Test case for typical Backdrop tests.

Code

protected function curlExec($curl_options, $redirect = FALSE) {
  $this->curlInitialize();

  // cURL incorrectly handles URLs with a fragment by including the
  // fragment in the request to the server, causing some web servers
  // to reject the request citing "400 - Bad Request". To prevent
  // this, we strip the fragment from the request.
  // TODO: Remove this; fixed in curl 7.20.0.
  if (!empty($curl_options[CURLOPT_URL]) && strpos($curl_options[CURLOPT_URL], '#')) {
    $original_url = $curl_options[CURLOPT_URL];
    $curl_options[CURLOPT_URL] = strtok($curl_options[CURLOPT_URL], '#');
  }

  $url = empty($curl_options[CURLOPT_URL]) ? curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL) : $curl_options[CURLOPT_URL];

  if (!empty($curl_options[CURLOPT_POST])) {
    // This is a fix for the Curl library to prevent Expect: 100-continue
    // headers in POST requests, that may cause unexpected HTTP response
    // codes from some web servers (like lighttpd that returns a 417 error
    // code). It is done by setting an empty "Expect" header field that is
    // not overwritten by Curl.
    $curl_options[CURLOPT_HTTPHEADER][] = 'Expect:';
  }
  curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);

  if (!$redirect) {
    // Reset headers, the session ID and the redirect counter.
    $this->session_id = NULL;
    $this->headers = array();
    $this->redirect_count = 0;
  }

  $content = curl_exec($this->curlHandle);
  $status = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);

  // cURL incorrectly handles URLs with fragments, so instead of
  // letting cURL handle redirects we take of them ourselves to
  // to prevent fragments being sent to the web server as part
  // of the request.
  // TODO: Remove this; fixed in curl 7.20.0.
  if (in_array($status, array(300, 301, 302, 303, 305, 307)) && $this->redirect_count < $this->maximumRedirects) {
    if ($this->backdropGetHeader('location')) {
      $this->redirect_count++;
      $curl_options = array();
      $curl_options[CURLOPT_URL] = $this->backdropGetHeader('location');
      $curl_options[CURLOPT_HTTPGET] = TRUE;
      return $this->curlExec($curl_options, TRUE);
    }
  }

  $this->backdropSetContent($content, isset($original_url) ? $original_url : curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL));
  $message_vars = array(
    '!method' => !empty($curl_options[CURLOPT_NOBODY]) ? 'HEAD' : (empty($curl_options[CURLOPT_POSTFIELDS]) ? 'GET' : 'POST'),
    '@url' => isset($original_url) ? $original_url : $url,
    '@status' => $status,
    '!length' => format_size(strlen($this->backdropGetContent()))
  );
  $message = t('!method @url returned @status (!length).', $message_vars);
  $this->assertTrue($this->backdropGetContent() !== FALSE, $message, t('Browser'));
  return $this->backdropGetContent();
}