You are here

public function ALProfilesAPI::canonicalizeRequest in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 7.2 acquia_lift_profiles/includes/acquia_lift_profiles.classes.inc \ALProfilesAPI::canonicalizeRequest()

Returns the canonical representation of a request.

Parameters

$method: The request method, e.g. 'GET'.

$path: The path of the request, e.g. '/dashboard/rest/[ACCOUNTNAME]/segments'.

array $parameters: An array of request parameters.

array $headers: An array of request headers.

bool $add_extra_headers: Whether to add the extra headers that we know drupal_http_request will add to the request. Set to FALSE if the request will not be handled by drupal_http_request.

Return value

string The canonical representation of the request.

1 call to ALProfilesAPI::canonicalizeRequest()
ALProfilesAPI::getAuthHeader in acquia_lift_profiles/includes/acquia_lift_profiles.classes.inc
Returns a string to use for the 'Authorization' header.

File

acquia_lift_profiles/includes/acquia_lift_profiles.classes.inc, line 249
Provides an agent type for Acquia Lift Profiles

Class

ALProfilesAPI
@file Provides an agent type for Acquia Lift Profiles

Code

public function canonicalizeRequest($method, $url, $parameters = array(), $headers = array(), $add_extra_headers = TRUE) {
  $parsed_url = parse_url($url);
  $str = strtoupper($method) . "\n";

  // Certain headers may get added to the actual request so we need to
  // add them here.
  if ($add_extra_headers && !isset($headers['User-Agent'])) {
    $headers['User-Agent'] = 'Drupal (+http://drupal.org/)';
  }
  if ($add_extra_headers && !isset($headers['Host'])) {
    $headers['Host'] = $parsed_url['host'] . (!empty($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
  }

  // Sort all header names alphabetically.
  $header_names = array_keys($headers);
  uasort($header_names, create_function('$a, $b', 'return strtolower($a) < strtolower($b) ? -1 : 1;'));

  // Add each header (trimmed and lowercased) and value to the string, separated by
  // a colon, and with a new line after each header:value pair.
  foreach ($header_names as $header) {
    if (!in_array($header, $this->headerWhitelist)) {
      continue;
    }
    $str .= trim(strtolower($header)) . ':' . trim($headers[$header]) . "\n";
  }

  // Add the path.
  $str .= $parsed_url['path'];

  // Sort any parameters alphabetically and add them as a querystring to our string.
  if (!empty($parameters)) {
    ksort($parameters);
    $first_param = key($parameters);
    $str .= '?' . $first_param . '=' . array_shift($parameters);
    foreach ($parameters as $key => $value) {
      $str .= '&' . $key . '=' . $value;
    }
  }
  return $str;
}