Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#18 | hook_http_request-786074-16.patch | 9.27 KB | c960657 |
#16 | hook_http_request-786074-15.patch | 9.32 KB | ramlev |
#4 | hook_http_request-3.patch | 16.49 KB | c960657 |
#2 | hook_http_request-2.patch | 16.43 KB | c960657 |
hook_http_request-1.patch | 10.42 KB | c960657 | |
Comments
Comment #2
c960657 CreditAttribution: c960657 commentedUpdated and with tests added.
Comment #4
c960657 CreditAttribution: c960657 commentedMake tests work with unclean URLs.
Comment #5
pwolanin CreditAttribution: pwolanin commentedIt would seem like a good place to move toward an OO plugin/strategy pattern in D8
Comment #6
wojtha CreditAttribution: wojtha commentedSubscribing
Comment #7
RobLoachI'm liking this.
Comment #8
wojtha CreditAttribution: wojtha commentedAs to the
#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:
Comment #9
pwolanin CreditAttribution: pwolanin commentedWe 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.
Comment #10
c960657 CreditAttribution: c960657 commentedCould you elaborate on this? Is there a ticket on file for this?
Comment #11
c960657 CreditAttribution: c960657 commentedThe ticket is #64866: Pluggable architecture for drupal_http_request().
Comment #12
SqyD CreditAttribution: SqyD commentedsubscribing...
Comment #13
webchickI 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.
Comment #14
rasmusluckow CreditAttribution: rasmusluckow commented#4: hook_http_request-3.patch queued for re-testing.
Comment #16
ramlev CreditAttribution: ramlev commentedRerolled the patch, now support current stage of 8.x
Comment #17
ramlev CreditAttribution: ramlev commentedComment #18
c960657 CreditAttribution: c960657 commentedReroll.
#64866: Pluggable architecture for drupal_http_request() definitely wont happen in D8, but this issue may still have a chance.
Comment #19
BerdirGuzzle happened, which support something like this using events/event subscribers. Closed (duplicate) ?
Comment #20
kasperg CreditAttribution: kasperg commentedAgreed. Closed.