Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Proxying Cookies with Drupal

Parent Feed: 

It's the minor things that get you into trouble sometimes. For instance, when I used drupal_http_request to do some proxying of content I ran into a little quirk of Drupal. If you get cookie headers back Drupal will go ahead and combine them all into a comma-separated list. If you look at the code you can plainly see where it does this and the rationale in the Request For Comments (RFC) 2109. The unfortunate part is that RFC 2109 has been made obsolete by RFC 6265 which was published in April 2011. If you blindly take this cookie header and pass it back to the client only the first cookie is actually captured. Whoops. 

Normally I'd just explode it and call it a day. Unfortunately the format for the cookie expiration is "Wdy, DD-Mon-YY HH:MM:SS GMT" (as per section 10.1.2). With just a little regex your problem is solved. It means having to do some annoying workarounds, but here is a solution:

function my_module_proxy_url($url) {
  $result = drupal_http_request($url);  if (200 == $result->code) {
    foreach ($result->headers as $key => $value) {
      if (strcasecmp($key, 'set-cookie')) {
        $cookies = preg_split('/(?<=\S),(?:\S)/', $value);
        foreach ($cookies as $cookie) {
          // Send separately since drupal_add_http_header concatenates with commas
          header($key . ': ' . $cookie, FALSE);
        }
      }
      else {
        drupal_add_http_header($key, $value);
      }
    }
  }
}

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web