Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify redirect location with hooks ? #201

Closed
Ugo-PK opened this issue Jan 14, 2016 · 0 comments
Closed

Modify redirect location with hooks ? #201

Ugo-PK opened this issue Jan 14, 2016 · 0 comments
Milestone

Comments

@Ugo-PK
Copy link

Ugo-PK commented Jan 14, 2016

I had the following exception appearing on a particular request, Requests_Exception: Only HTTP requests are handled
It was not really an issue with the lib itself, but I encountered a site that would redirect with a relative URI not starting with with a "/". (eg: index.php?controller=AdminOrders&token=)
To solve the problem I had to change the test for URL absolutize in Requests.php parse_response functions.

if (strpos ($location, '/') === 0 ) {

became

if (strpos ($location, '/') === 0 || strpos ($location, 'http') === false) {

(also had to add $options['type'] = Requests::GET; right after, that site also sent you 302 but expected GET afterward)
It would prolly be possible to just absolutize the url without test or add an option to force it.
An option for the redirect 302/303 behavior would be cool too.

I then tried to implement a hook at "requests.before_redirect_check" to be able to use the library without modifying it directly, but it seems the $return->headers['location'] variable can't be overridden/deleted ? When I try to replace it with another string, it just appends it at the end.

public static function hookingCorrections(&$return, $req_headers, $req_data, $options)
{
    var_dump($return->headers['location']);
    if($return->status_code == 302){
        $location = \Requests_IRI::absolutize($return->url, $return->headers['location']) ;
        $return->headers['location'] = $location;
        var_dump($return->headers['location']);
}

The first and last dumps return :
index.php?controller=AdminOrders&token=6d791c08516a9299e2e2b3b5e87662fe&submitFilterorder=1

index.php?controller=AdminOrders&token=6d791c08516a9299e2e2b3b5e87662fe&submitFilterorder=1,http://www.xxxxxxx.com/admin0956/index.php?controller=AdminOrders&token=6d791c08516a9299e2e2b3b5e87662fe&submitFilterorder=1

[EDIT]
I managed to make my hook work :

public static function hookingCorrections(&$return, $req_headers, $req_data, &$options)
    {
        if($return->status_code == 302){
            $location = \Requests_IRI::absolutize($return->url, $return->headers['location']) ;
            $return->headers->offsetUnset("location");
            $return->headers['location'] = $location;
            $options['type'] = \Requests::GET;
        }
    }

I had to modify the hooking register code so the options var would be passed by reference (to be able to modify it), which is not the case in currently.

I suggest that you pass all var by reference in your hooks to make things easier.

$options['hooks']->dispatch('requests.before_redirect_check', array(&$return, $req_headers, $req_data, &$options));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants