I would like purge to issue a request to purge a resource for both https and http using the X-Forwarded-Proto header. Has anyone tried to add support for custom headers? If not, where would be the best place to start?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ben.bunk’s picture

I decided to take a stab at implementing a solution to this so the attached patch adds a drupal_alter call on the purge_requests array just before passing it on for processing with curl. Below is an implementation of the drupal_alter to duplicate all the purge requests and add the x-forwarded-proto header to the second set although any headers could be added at this point.

/**
 * Implements hook_purge_post_build_alter().
 *
 * Duplicate the requests and in the second set make sure to purge the HTTPS version of the page.
 */
function example_purge_post_build_alter(array &$purge_requests) {
  // 1. Copy the existing array
  $https_purge_requests = array();
  $https_purge_requests = $purge_requests;

  // 2. Add HTTPS x-forwarded-for header to the copy.
  foreach ($https_purge_requests as $key => $request) {
    if (isset($request['headers'])) {
      $https_purge_requests[$key]['headers'][] = 'X-Forwarded-Proto: https';
    }
  }

  // 3. Merge together.
  $purge_requests = array_merge($purge_requests, $https_purge_requests);
}
chriso’s picture

Version: 7.x-2.x-dev » 7.x-1.x-dev
Category: Support request » Feature request
Status: Active » Reviewed & tested by the community

This patch fits the 1.x branch not the 2.x branch -- but I'm using it, together with your exact example since it makes for pain-free purging of Varnish via Pound. Cool stuff, thanks!

It is a bit of a duplicate of #1888866: Allow other modules to add hostnames and headers for purge requests though; that issue remains as a feature request for the 7.x-2.x branch. Marking this 7.x-1.x patch as RTBC.