Adding a hook to allow modules to alter HTTP requests and responses would allow modules to do a lot of interesting things:

  • implement an HTTP cache, so requests can be served without hitting the network.
  • implement an alternative HTTP client, e.g. based on cURL.
  • allow or disallow requests to hosts based on whitelists or blacklists.
  • specify individual timeouts, inject credentials, alter the query string, alter HTTP request headers (e.g. User-Agent).
  • log network traffic.
  • modify broken responses, e.g. wrong Content-Type headers or malformed XML.

This patch adds hook_http_request_alter() and hook_http_response(). drupal_http_request() is mostly unchanged except for moving the existing parts around, so the whole request object it built, then possibly altered, and then the network connection is made. If there is interest in this patch, I'll write some tests later.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, hook_http_request-1.patch, failed testing.

c960657’s picture

Status: Needs work » Needs review
FileSize
16.43 KB

Updated and with tests added.

Status: Needs review » Needs work

The last submitted patch, hook_http_request-2.patch, failed testing.

c960657’s picture

Status: Needs work » Needs review
FileSize
16.49 KB

Make tests work with unclean URLs.

pwolanin’s picture

Version: 7.x-dev » 8.x-dev

It would seem like a good place to move toward an OO plugin/strategy pattern in D8

wojtha’s picture

Subscribing

RobLoach’s picture

I'm liking this.

wojtha’s picture

As to the

  • implement an alternative HTTP client, e.g. based on cURL.

#1081192-14: Verify peer on HTTPS if cURL available (but be careful of built-in cert bundles in the codebase), comment by Dries, April 16, 2011:

Gosh. I'd _really_ love to get rid of drupal_http_request() and rely on something provided by PHP. Should we make CURL a requirement?

pwolanin’s picture

Status: Needs review » Postponed

We should wait for a more robust plugin system for core, and then have a pluggable http request system.

I think we should postpone this issue until then.

c960657’s picture

We should wait for a more robust plugin system for core,

Could you elaborate on this? Is there a ticket on file for this?

c960657’s picture

SqyD’s picture

subscribing...

webchick’s picture

Status: Postponed » Needs review

I don't see any reason to postpone any issues on the idea that there might one day be a plugin system in core. This being pluggable would just represent another one-off that said plugin system would have to convert.

rasmusluckow’s picture

#4: hook_http_request-3.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, hook_http_request-3.patch, failed testing.

ramlev’s picture

Rerolled the patch, now support current stage of 8.x

ramlev’s picture

Status: Needs work » Needs review
c960657’s picture

Reroll.

#64866: Pluggable architecture for drupal_http_request() definitely wont happen in D8, but this issue may still have a chance.

Berdir’s picture

Guzzle happened, which support something like this using events/event subscribers. Closed (duplicate) ?

kasperg’s picture

Status: Needs review » Closed (duplicate)

Agreed. Closed.