Support underscore in domain/host name and don't throw exception
----------------------------------------------------------------
Key: HTTPCLIENT-970
URL: https://issues.apache.org/jira/browse/HTTPCLIENT-970
Project: HttpComponents HttpClient
Issue Type: Bug
Components: HttpClient
Affects Versions: 4.1 Alpha2
Reporter: Mingfai Ma
Priority: Minor
Trying to re-open HTTPCLIENT-911
could we use a custom logic to determine host instead of relying on
URI.getHost() in AbstractHttpClient.determineTarget? I propose to code it like
this:
{code}
public abstract class AbstractHttpClient implements HttpClient {
...
private HttpHost determineTarget(HttpUriRequest request) throws
ClientProtocolException {
// A null target may be acceptable if there is a default target.
// Otherwise, the null target is detected in the director.
HttpHost target = null;
URI requestURI = request.getURI();
if (requestURI.isAbsolute()) {
String ssp = requestURI.getSchemeSpecificPart();
ssp = ssp.substring(2, ssp.length()); //remove "//" prefix
int end = ssp.indexOf(':') > 0 ? ssp.indexOf(':') :
ssp.indexOf('/') > 0 ? ssp.indexOf('/') :
ssp.indexOf('?') > 0 ? ssp.indexOf('?') : ssp.length();
String host = ssp.substring(0, end);
...
{code}
The logic basically to look for ':', '/', or '?' to determine the end index of
the host. The following test cases show how it works in different URL:
{code}
@Test void testGetHostName1(){
String ssp = new URI("http://local_host").getSchemeSpecificPart();
ssp = ssp.substring(2, ssp.length()); //remove "//" prefix
int end = ssp.indexOf(':') > 0 ? ssp.indexOf(':') : ssp.indexOf('/') > 0 ?
ssp.indexOf('/') : ssp.indexOf('?') > 0 ? ssp.indexOf('?') : ssp.length();
assertEquals("local_host", ssp.substring(0, end));
}
@Test void testGetHostName2(){
String ssp = new URI("http://local_host:8080/").getSchemeSpecificPart();
ssp = ssp.substring(2, ssp.length()); //remove "//" prefix
int end = ssp.indexOf(':') > 0 ? ssp.indexOf(':') : ssp.indexOf('/') > 0 ?
ssp.indexOf('/') : ssp.indexOf('?') > 0 ? ssp.indexOf('?') : ssp.length();
assertEquals("local_host", ssp.substring(0, end));
}
@Test void testGetHostName3(){
String ssp = new URI("http://local_host/directory").getSchemeSpecificPart();
ssp = ssp.substring(2, ssp.length()); //remove "//" prefix
int end = ssp.indexOf(':') > 0 ? ssp.indexOf(':') : ssp.indexOf('/') > 0 ?
ssp.indexOf('/') : ssp.indexOf('?') > 0 ? ssp.indexOf('?') : ssp.length();
assertEquals("local_host", ssp.substring(0, end));
}
@Test void testGetHostName4(){
String ssp = new URI("http://local_host?k=v").getSchemeSpecificPart();
ssp = ssp.substring(2, ssp.length()); //remove "//" prefix
int end = ssp.indexOf(':') > 0 ? ssp.indexOf(':') : ssp.indexOf('/') > 0 ?
ssp.indexOf('/') : ssp.indexOf('?') > 0 ? ssp.indexOf('?') : ssp.length();
assertEquals("local_host", ssp.substring(0, end));
}
{code}
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]