Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

[Http] Request infos are lost when there is a redirect #6564

Closed
egeloen opened this issue Aug 12, 2014 · 13 comments
Closed

[Http] Request infos are lost when there is a redirect #6564

egeloen opened this issue Aug 12, 2014 · 13 comments

Comments

@egeloen
Copy link
Contributor

egeloen commented Aug 12, 2014

Hey!

I'm digging into an issue with the http client. When I follow redirects, the request infos (more precisely headers) are lost due to this line https://github.com/zendframework/zf2/blob/master/library/Zend/Http/Client.php#L930 and more precisely this line https://github.com/zendframework/zf2/blob/master/library/Zend/Http/Client.php#L479 which forgot everything about the previous request.

If someone points me the way to fix it, I can provide a PR.

@ltouro
Copy link
Contributor

ltouro commented Aug 12, 2014

You can set the param strictredirects to TRUE on Zend\Http\Client and set the response code to 302.

$response = $this->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;

OR using the Redirect Controller Plugin

$this->redirect->toUrl($url);

@egeloen
Copy link
Contributor Author

egeloen commented Aug 12, 2014

Yes but if the strictredirects is set to false, shouldn't the headers be forwarded anyway?

I understand if I disabled strictredirects (default behavior), it should reflect what a browser do (POST -> GET and resets GET/POST parameters) but IFAIR, the headers are still forwarded. Am I right?

Btw, I'm not speaking about the response headers but the request ones.

@ltouro
Copy link
Contributor

ltouro commented Aug 12, 2014

Let me see if a understood:

You have a Request (made from browser or other type of client, with certain headers) -> ZF2 receives it, process and return Response (302) -> Browser/client get the response, process, sees the redirect code and send a new Request to the target Location;

You need certain headers to be in the last Request. That is it?

In this case, I think the second Request is out of scope of ZF2.

@egeloen
Copy link
Contributor Author

egeloen commented Aug 12, 2014

No, I do a request with the zf2 http client (Zend\Http\Client) and this one doesn't forward the original headers requests in case of redirects (only when strictredirects is false because in this case, it reset parameters of the client).

To resume:

I send the request (with headers) to a server, the server returns a 302, the client resend the request to the location in the response headers but here, it does not forward the original request headers to the location.

@weierophinney
Copy link
Member

@egeloen What you're observing is actually the documented behavior; from http://framework.zend.com/manual/1.12/en/zend.http.client.advanced.html:

According to the HTTP/1.1 RFC, HTTP 301 and 302 responses should be treated by the client by resending the same request to the specified location - using the same request method. However, most clients to not implement this and always use a GET request when redirecting. By default, Zend_Http_Client does the same - when redirecting on a 301 or 302 response, all GET and POST parameters are reset, and a GET request is sent to the new location. This behavior can be changed by setting the 'strictredirects' configuration parameter to boolean TRUE.

As such, not an issue -- this is by design, to keep compatibility with similar clients. Set the strictredirects flag to boolean true if you want the original headers sent.

@weierophinney
Copy link
Member

Sorry, meant to link to http://framework.zend.com/manual/2.3/en/modules/zend.http.client.advanced.html -- but it's the same text, because it's the same behavior in both ZF1 and ZF2.

@egeloen
Copy link
Contributor Author

egeloen commented Aug 12, 2014

@weierophinney Unfortunatelly, I must disagree on the fact zf1 and zf2 behaves same about headers. The zf1 clients keeps the original headers as you can see here. The resetParameters keeps the original headers as it is called with this default value ($clearAll as false). So, currently zf1 forwards original headers whereas zf2 does not. What is the right behavior? (The doc specifies that GET/POST parameters are reset but nothing is said about original headers).

@tfountain
Copy link
Contributor

@egeloen it might help if you could say specifically which request headers you are expecting to be preserved.

@egeloen
Copy link
Contributor Author

egeloen commented Aug 12, 2014

@tfountain There is no specific headers, I just expects zf2 to forward any original request headers.

For example, I do:

$client
    ->setUri('http://my-url.com')
    ->setMethod('GET')
    ->setHeaders(array('user-agent' => 'my user agent'));
    ->send()

Given http://my-url.com returns a 302 to http://my-url.com/redirect, then I expect http://my-url.com/redirect will receives the original user-agent and not the default zf2 one. Obvously, content-type and content-length headers should be resetted as the request type/length have changed (in case of POST request or any request wrapping datas) but nothing more IMO (as it is currently in zf1).

@egeloen
Copy link
Contributor Author

egeloen commented Aug 12, 2014

ping @weierophinney

@Ocramius
Copy link
Member

@egeloen that's probably because a new request is built. Can you look at existing tests and add assertions?

@Ocramius Ocramius reopened this Aug 13, 2014
@Ocramius Ocramius self-assigned this Aug 13, 2014
@seyfer
Copy link

seyfer commented Dec 23, 2015

Any progress here? I guess I'm having the same issue now.

@GeeH
Copy link

GeeH commented Jun 27, 2016

This issue has been closed as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html

@GeeH GeeH closed this as completed Jun 27, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants