Hi all,
I have some issues with code of org.apache.http.impl.conn.
ProxySelectorRoutePlanner.
I have setup a Apache2.2 proxy for both http & https.
I have problem with 2 BOLD lines in following method from
ProxySelectorRoutePlanner.
/**
* Determines a proxy for the given target.
*
* @param target the planned target, never <code>null</code>
* @param request the request to be sent, never <code>null</code>
* @param context the context, or <code>null</code>
*
* @return the proxy to use, or <code>null</code> for a direct route
*
* @throws HttpException
* in case of system proxy settings that cannot be handled
*/
protected HttpHost determineProxy(HttpHost target,
HttpRequest request,
HttpContext context)
throws HttpException {
// the proxy selector can be 'unset', so we better deal with null here
ProxySelector psel = this.proxySelector;
if (psel == null)
psel = ProxySelector.getDefault();
if (psel == null)
return null;
URI targetURI = null;
try {
targetURI = new URI(target.toURI());
} catch (URISyntaxException usx) {
throw new HttpException
("Cannot convert host to URI: " + target, usx);
}
List<Proxy> proxies = psel.select(targetURI);
Proxy p = chooseProxy(proxies, target, request, context);
HttpHost result = null;
if (p.type() == Proxy.Type.HTTP) {
// convert the socket address to an HttpHost
if (!(p.address() instanceof InetSocketAddress)) {
throw new HttpException
("Unable to handle non-Inet proxy address: "+p.address());
}
final InetSocketAddress isa = (InetSocketAddress) p.address();
// assume default scheme (http)
result = new HttpHost(getHost(isa), isa.getPort());
}
return result;
}
Consider my configuration in Apache-tomcat\conf\catalina.properties:
http.proxyHost=10.31.81.210
http.proxyPort=80
https.proxyHost=10.31.81.210
https.proxyPort=443
In above code, Ideally if targetURI scheme is http then proxy should be
http://10.31.81.210:80/ & if https then it should https://10.31.81.210:443/
but above code // assume default scheme (http)
Which is giving result as http://10.31.81.210:443 whereas actually it should be
https://10.31.81.210:443.
HttpHost has several constructors where one of them accepts 3 parameters which
has scheme as well.
public HttpHost(final String hostname, int port, final String scheme) {
81 super();
82 if (hostname == null) {
83 throw new IllegalArgumentException("Host name may not be
null");
84 }
85 this.hostname = hostname;
86 this.lcHostname = hostname.toLowerCase(Locale.ENGLISH);
87 if (scheme != null) {
88 this.schemeName = scheme.toLowerCase(Locale.ENGLISH);
89 } else {
90 this.schemeName = DEFAULT_SCHEME_NAME;
91 }
92 this.port = port;
93 }
94
95 /**
96 * Creates a new {@link HttpHost HttpHost}, with default scheme.
97 *
98 * @param hostname the hostname (IP or DNS name)
99 * @param port the port number.
100 * <code>-1</code> indicates the scheme default
port.
101 */
102 public HttpHost(final String hostname, int port) {
103 this(hostname, port, null);
104 }
In determineProxy() code you are assuming the scheme & calling HttpHost(final
String hostname, int port) which internally passes scheme=null and calls
HttpHost(final String hostname, int port, final String scheme) which ultimately
assigns this.schemeName as http even if targetURI scheme is https.
Finally,
I get output of
// assume default scheme (http)
result = new HttpHost(getHost(isa), isa.getPort());
as result=http://10.31.81.210:443 [gives Page cannot be found because scheme &
proxy setting mismatch according to catalina.properties]
Whereas it should be result=https://10.31.81.210:443 [It works because scheme &
proxy match according to catalina.properties]
This could be solved by replacing result = new HttpHost(getHost(isa),
isa.getPort()); by result = new HttpHost(getHost(isa), isa.getPort(),
targetURI.scheme); which is also another constructor of HttpHost.
What is your opinion on it?
Is my doubt correct or Am I missing something ? If correct then what is the
next line of action for me as getting http instead of https is failing my web
app in presence of proxy.
Please Need your help.
Thanking You,
Ameya Bapat.
EMC Corporation.