@@ -250,56 +250,43 @@ public static HttpHost extractHost(final URI uri) {
250250 if (uri == null ) {
251251 return null ;
252252 }
253- HttpHost target = null ;
254253 if (uri .isAbsolute ()) {
255- int port = uri .getPort (); // may be overridden later
256- String host = uri .getHost ();
257- if (host == null ) { // normal parse failed; let's do it ourselves
254+ if (uri .getHost () == null ) { // normal parse failed; let's do it ourselves
258255 // authority does not seem to care about the valid character-set for host names
259- host = uri .getAuthority ();
260- if ( host != null ) {
256+ if ( uri .getAuthority () != null ) {
257+ String content = uri . getAuthority ();
261258 // Strip off any leading user credentials
262- final int at = host .indexOf ('@' );
263- if (at >= 0 ) {
264- if (host .length () > at +1 ) {
265- host = host .substring (at +1 );
266- } else {
267- host = null ; // @ on its own
268- }
259+ int at = content .indexOf ('@' );
260+ if (at != -1 ) {
261+ content = content .substring (at + 1 );
269262 }
270- // Extract the port suffix, if present
271- if (host != null ) {
272- final int colon = host .indexOf (':' );
273- if (colon >= 0 ) {
274- final int pos = colon + 1 ;
275- int len = 0 ;
276- for (int i = pos ; i < host .length (); i ++) {
277- if (Character .isDigit (host .charAt (i ))) {
278- len ++;
279- } else {
280- break ;
281- }
282- }
283- if (len > 0 ) {
284- try {
285- port = Integer .parseInt (host .substring (pos , pos + len ));
286- } catch (final NumberFormatException ex ) {
287- }
288- }
289- host = host .substring (0 , colon );
263+ final String scheme = uri .getScheme ();
264+ final String hostname ;
265+ final int port ;
266+ at = content .indexOf (":" );
267+ if (at != -1 ) {
268+ hostname = content .substring (0 , at );
269+ try {
270+ final String portText = content .substring (at + 1 );
271+ port = !TextUtils .isEmpty (portText ) ? Integer .parseInt (portText ) : -1 ;
272+ } catch (final NumberFormatException ex ) {
273+ return null ;
290274 }
275+ } else {
276+ hostname = content ;
277+ port = -1 ;
278+ }
279+ try {
280+ return new HttpHost (scheme , hostname , port );
281+ } catch (final IllegalArgumentException ex ) {
282+ return null ;
291283 }
292284 }
293- }
294- final String scheme = uri .getScheme ();
295- if (!TextUtils .isBlank (host )) {
296- try {
297- target = new HttpHost (scheme , host , port );
298- } catch (final IllegalArgumentException ignore ) {
299- }
285+ } else {
286+ return new HttpHost (uri .getScheme (), uri .getHost (), uri .getPort ());
300287 }
301288 }
302- return target ;
289+ return null ;
303290 }
304291
305292 /**
0 commit comments