Repository: WordPress/Requests (language: php)

Issue:
$info parameter isn't passed to <transport>.after_request hook on non-blocking requests
The `fsockopen.after_request` and `curl.after_request` hooks don't receive an `$info` parameter when a non-blocking request is sent.

This means the hook signature differs depending on whether the request is blocking or not, and conflicts with the [hook docs](https://github.com/rmccue/Requests/blob/master/docs/hooks.md).

Possible solution: Pass an empty array as the `$info` parameter for these hooks when a non-blocking request is performed.

Since #206.


Discussion / hints:
@johnbillion I've done a full review of the Hooks documentation today, with PR #646 as a result. While this doesn't change the way the hook is being dispatched, it does update the documentation to be more informative regarding this.

If you have the time, would be great if you could look the PR over and give your opinion.
This proposal is being discussed in #646. Copying the relevant parts of that conversation here for visibility:

@schlessera:
> Hmm, still on the fence about this one, as I'm not yet sure whether the documentation for the optional `$info` args is sufficient here. It might be more robust to go with @johnbillion's suggestion and provide an `$info` array in all cases, with an empty one being used for non-blocking requests. I'll think about this some more...

@jrfnl:
> Deciding whether or not to always pass `$info` is a different issue and if/when it is so decided, the documentation can be further updated to reflect that change.
> 
> Generally speaking I'm in favour of John's suggestion and would recommend for hooks to have the same number of arguments every time they are called, however, making this change now _could_ be considered a breaking change as functions which "hook in" very likely will need updating (callbacks may have a different default value set - `null` - for the optional parameter from the empty array we would pass, callbacks may use `isset()` on the result of `function_get_args()` for this parameter, which would break, etc).
 

Fix patch:
diff --git a/docs/hooks.md b/docs/hooks.md
index 922f53257..4b26a3363 100644
--- a/docs/hooks.md
+++ b/docs/hooks.md
@@ -86,12 +86,14 @@ Available Hooks
 
     Alter the raw HTTP response before returning for parsing.
 
-    Parameters: `string &$headers`, `[array &$info]`
+    Parameters: `string &$headers`, `array &$info`
 
-    The optional `$info` parameter contains the associated array as defined in
+    The `$info` parameter contains the associated array as defined in
     the return value for [curl_getinfo()](https://www.php.net/curl-getinfo#refsect1-function.curl-getinfo-returnvalues).
 
-This optional parameter will be present when a blocking request was made (`$options['blocking' = true`) and will not be present when a non-blocking request was made (`$options['blocking' = false`). The callback signature needs to be adapted accordingly.
+    When a non-blocking request was made (`$options['blocking'] = false`), both the `$headers` string as well as the `$info` array will be empty.
+
+    Prior to Requests 2.1.0, the `$info` parameter was omitted for non-blocking requests.
 
 * **`curl.before_multi_add`**
 
@@ -149,12 +151,14 @@ This optional parameter will be present when a blocking request was made (`$opti
 
     Alter the raw HTTP response before returning for parsing.
 
-    Parameters: `string &$headers`, `[array &$info]`
+    Parameters: `string &$headers`, `array &$info`
 
     The optional `$info` parameter contains the associated array as defined
     in the return value for [stream_get_meta_data()](https://www.php.net/stream-get-meta-data#refsect1-function.stream-get-meta-data-returnvalues).
 
-This optional parameter will be present when a blocking request was made (`$options['blocking' = true`) and will not be present when a non-blocking request was made (`$options['blocking' = false`). The callback signature needs to be adapted accordingly.
+    When a non-blocking request was made (`$options['blocking'] = false`), both the `$headers` string as well as the `$info` array will be empty.
+
+    Prior to Requests 2.1.0, the `$info` parameter was omitted for non-blocking requests.
 
 
 Registering Hooks
diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php
index 2c5e3c0eb..bb910f79c 100644
--- a/src/Transport/Curl.php
+++ b/src/Transport/Curl.php
@@ -470,7 +470,8 @@ private function setup_handle($url, $headers, $data, $options) {
 	public function process_response($response, $options) {
 		if ($options['blocking'] === false) {
 			$fake_headers = '';
-			$options['hooks']->dispatch('curl.after_request', [&$fake_headers]);
+			$fake_info    = [];
+			$options['hooks']->dispatch('curl.after_request', [&$fake_headers, &$fake_info]);
 			return false;
 		}
 
diff --git a/src/Transport/Fsockopen.php b/src/Transport/Fsockopen.php
index 00589f83b..c9a16e24d 100644
--- a/src/Transport/Fsockopen.php
+++ b/src/Transport/Fsockopen.php
@@ -254,7 +254,8 @@ public function request($url, $headers = [], $data = [], $options = []) {
 		if (!$options['blocking']) {
 			fclose($socket);
 			$fake_headers = '';
-			$options['hooks']->dispatch('fsockopen.after_request', [&$fake_headers]);
+			$fake_info    = [];
+			$options['hooks']->dispatch('fsockopen.after_request', [&$fake_headers, &$fake_info]);
 			return '';
 		}
 


Test patch: