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.

Files: 
CommentFileSizeAuthor
#18 hook_http_request-786074-16.patch9.27 KBc960657
PASSED: [[SimpleTest]]: [MySQL] 48,825 pass(es).
[ View ]
#16 hook_http_request-786074-15.patch9.32 KBramlev
PASSED: [[SimpleTest]]: [MySQL] 40,560 pass(es).
[ View ]
#4 hook_http_request-3.patch16.49 KBc960657
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch hook_http_request-3.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#2 hook_http_request-2.patch16.43 KBc960657
FAILED: [[SimpleTest]]: [MySQL] 20,265 pass(es), 1 fail(s), and 0 exception(es).
[ View ]
hook_http_request-1.patch10.42 KBc960657
FAILED: [[SimpleTest]]: [MySQL] 20,118 pass(es), 0 fail(s), and 4 exception(es).
[ View ]

Comments

Status:Needs review» Needs work

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

Status:Needs work» Needs review
StatusFileSize
new16.43 KB
FAILED: [[SimpleTest]]: [MySQL] 20,265 pass(es), 1 fail(s), and 0 exception(es).
[ View ]

Updated and with tests added.

Status:Needs review» Needs work

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

Status:Needs work» Needs review
StatusFileSize
new16.49 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch hook_http_request-3.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Make tests work with unclean URLs.

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

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

Subscribing

I'm liking this.

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?

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.

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

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

subscribing...

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.

#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.

StatusFileSize
new9.32 KB
PASSED: [[SimpleTest]]: [MySQL] 40,560 pass(es).
[ View ]

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

Status:Needs work» Needs review

StatusFileSize
new9.27 KB
PASSED: [[SimpleTest]]: [MySQL] 48,825 pass(es).
[ View ]

Reroll.

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

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

Status:Needs review» Closed (duplicate)

Agreed. Closed.