guzzle / guzzle

Guzzle, an extensible PHP HTTP client

Home Page:https://docs.guzzlephp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enhanced on_redirect closure

aimeos opened this issue · comments

Description

Currently, you can pass a closure which is called when redirects are allowed and at least one occurs:
https://docs.guzzlephp.org/en/stable/request-options.html#allow-redirects

The closure can only read the request, response and target URI objects but cannot modify them so it's use is limited. If it can return a modified URI object instead which is used for the next request or NULL if no more request should be made, then custom security policies for redirects could be implemented.

Example

$onRedirect = function(
    RequestInterface $request,
    ResponseInterface $response,
    UriInterface $uri
) {
    return in_array( $uri->getHost(), ['secure.com'] ) ? $uri : null;
};

Additional context

Affected lines:

if (isset($options['allow_redirects']['on_redirect'])) {
($options['allow_redirects']['on_redirect'])(
$request,
$response,
$nextRequest->getUri()
);
}
$promise = $this($nextRequest, $options);

The relevant change would be:

        $uri = $nextRequest->getUri();
        if (isset($options['allow_redirects']['on_redirect'])) {
            $uri =  ($options['allow_redirects']['on_redirect'])(
                $request,
                $response,
                $uri
            );
        }

        if(!$uri) {
            return $response;
        }

        $promise = $this($nextRequest->withUri($uri), $options);

As this would be a breaking change, it can be implemented for Guzzle 8.0 earliest (because if no Uri object is returned by the closures like now, redirects are not followed any more),

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

commented

Any comments for the proposed change?

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.