Add possibility to alter request headers between redirects
----------------------------------------------------------
Key: HTTPCLIENT-1088
URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1088
Project: HttpComponents HttpClient
Issue Type: Improvement
Components: HttpClient
Affects Versions: 4.1.1
Environment: *
Reporter: Bartosz Firyn
For now there is no possibility to alter headers between consecutive redirects.
In case when user needs this kind of functionality he has to implement his own
redirection mechanism from scratch. However this could be easily done with
standard HTTP client redirection mechanism. I think it is worth to add
alterHeaders(List<Header> headers) method in the DefaultRedirectStrategy class.
This method would be called from DefaultRequestDirector instead of this code:
1023: redirect.setHeaders(orig.getAllHeaders());
So method I mentioned above would look like:
public List<Header> alterHeaders(List<Header> headers) {
return headers;
}
And call in DefaultRequestDirector like this:
1024: List<Header> headers = orig.getAllHeaders();
1025: headers = redirectStrategy.alterHeaders(headers);
1026: redirect.setHeaders(headers);
Why am I asking about this feature? I had a situation when "Host" headers had
to be changed, but I was not able to do that between redirects. Effect of this
issue was that I was not able to login to the service I tried to gain access to
(they check if "Host" header is correctly filled).
For now easy (and really ugly) workaroud is:
Create your own redirect strategy impl:
public class MateRedirectStrategy extends DefaultRedirectStrategy {
@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse
response, HttpContext context) throws ProtocolException {
// ugly W/A for HTTP Client issue
HttpUriRequest redirect = super.getRedirect(request, response,
context);
URI uri = redirect.getURI();
String host = uri.getHost();
((RequestWrapper) request).getOriginal().setHeader("Host",
host);
return redirect;
}
}
And use it in your client:
client.setRedirectStrategy(new MateRedirectStrategy());
Thats all.
Regards
Bartosz Firyn
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]