hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Oleg Kalnichevski (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (HTTPCLIENT-1088) Add possibility to alter request headers between redirects
Date Fri, 13 May 2011 20:29:47 GMT

    [ https://issues.apache.org/jira/browse/HTTPCLIENT-1088?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13033264#comment-13033264
] 

Oleg Kalnichevski commented on HTTPCLIENT-1088:
-----------------------------------------------

Bartosz

I am not sure I understand the motivation for this feature. The 'host' header usually gets
generated automatically by HttpClient based on the request URI or the virtual host parameter.
Generally one ought not add Host header manually. If the original request did not have a 'host'
header set manually you would not need to change it in the first place.

I suspect you are trying to fix things in the wrong place.

Oleg

> 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
>              Labels: api-change
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> 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: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message