You are here

protected function ServicesWebTestCase::curlExec in Services 7.3

Same name and namespace in other branches
  1. 6.3 tests/functional/ServicesWebTestCase.php \ServicesWebTestCase::curlExec()

Performs a cURL exec with the specified options after calling curlConnect().

Parameters

$curl_options: Custom cURL options.

Return value

Content returned from the exec.

Overrides DrupalWebTestCase::curlExec

7 calls to ServicesWebTestCase::curlExec()
ServicesSecurityTests::servicesPostNoCSRFHeader in tests/functional/ServicesSecurityTests.test
Copy of servicesPost method but without CSRF header.
ServicesWebTestCase::servicesDelete in tests/services.test
Perform DELETE request.
ServicesWebTestCase::servicesGet in tests/services.test
Perform GET request.
ServicesWebTestCase::servicesHead in tests/services.test
Perform HEAD request.
ServicesWebTestCase::servicesPost in tests/services.test
Perform POST request.

... See full list

File

tests/services.test, line 783

Class

ServicesWebTestCase

Code

protected function curlExec($curl_options, $redirect = FALSE) {

  // Some Curl options might leave the handle in a state where subsequent
  // request can cause warnings or even weird failures, so to be on the safe
  // side we reinitialize Curl for each request.
  $this
    ->curlClose();
  $this
    ->curlInitialize();
  if (!empty($curl_options[CURLOPT_URL])) {

    // Forward XDebug activation if present.
    if (isset($_COOKIE['XDEBUG_SESSION'])) {
      $options = drupal_parse_url($curl_options[CURLOPT_URL]);
      $options += array(
        'query' => array(),
      );
      $options['query'] += array(
        'XDEBUG_SESSION_START' => $_COOKIE['XDEBUG_SESSION'],
      );
      $curl_options[CURLOPT_URL] = url($options['path'], $options);
    }

    // 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 for Drupal 8, since fixed in curl 7.20.0.
    if (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 webservers (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 for Drupal 8, since fixed in curl 7.20.0.
  if (in_array($status, array(
    300,
    301,
    302,
    303,
    305,
    307,
  )) && $this->redirect_count < variable_get('simpletest_maximum_redirects', 5)) {
    if ($this
      ->drupalGetHeader('location')) {
      $this->redirect_count++;
      $curl_options = array();
      $curl_options[CURLOPT_URL] = $this
        ->drupalGetHeader('location');
      $curl_options[CURLOPT_HTTPGET] = TRUE;
      return $this
        ->curlExec($curl_options, TRUE);
    }
  }
  $this
    ->drupalSetContent($content, isset($original_url) ? $original_url : curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL));

  // Analyze the method for log message.
  $method = '';
  if (!empty($curl_options[CURLOPT_NOBODY])) {
    $method = 'HEAD';
  }
  if (empty($method) && !empty($curl_options[CURLOPT_PUT])) {
    $method = 'PUT';
  }
  if (empty($method) && !empty($curl_options[CURLOPT_CUSTOMREQUEST])) {
    $method = $curl_options[CURLOPT_CUSTOMREQUEST];
  }
  if (empty($method)) {
    $method = empty($curl_options[CURLOPT_POSTFIELDS]) ? 'GET' : 'POST';
  }
  $message_vars = array(
    '!method' => $method,
    '@url' => isset($original_url) ? $original_url : $url,
    '@status' => $status,
    '!length' => format_size(drupal_strlen($this
      ->drupalGetContent())),
  );
  $message = format_string('!method @url returned @status (!length).', $message_vars);
  $this
    ->assertTrue($this
    ->drupalGetContent() !== FALSE, $message, 'Browser');
  return $this
    ->drupalGetContent();
}