@@ -419,56 +419,43 @@ public static HttpHost extractHost(final URI uri) {
419419        if  (uri  == null ) {
420420            return  null ;
421421        }
422-         HttpHost  target  = null ;
423422        if  (uri .isAbsolute ()) {
424-             int  port  = uri .getPort (); // may be overridden later 
425-             String  host  = uri .getHost ();
426-             if  (host  == null ) { // normal parse failed; let's do it ourselves 
423+             if  (uri .getHost () == null ) { // normal parse failed; let's do it ourselves 
427424                // authority does not seem to care about the valid character-set for host names 
428-                 host  =  uri .getAuthority (); 
429-                 if  ( host  !=  null ) { 
425+                 if  ( uri .getAuthority () !=  null ) { 
426+                      String   content  =  uri . getAuthority (); 
430427                    // Strip off any leading user credentials 
431-                     final  int  at  = host .indexOf ('@' );
432-                     if  (at  >= 0 ) {
433-                         if  (host .length () > at +1  ) {
434-                             host  = host .substring (at +1 );
435-                         } else  {
436-                             host  = null ; // @ on its own 
437-                         }
428+                     int  at  = content .indexOf ('@' );
429+                     if  (at  != -1 ) {
430+                         content  = content .substring (at  + 1 );
438431                    }
439-                     // Extract the port suffix, if present 
440-                     if  (host  != null ) {
441-                         final  int  colon  = host .indexOf (':' );
442-                         if  (colon  >= 0 ) {
443-                             final  int  pos  = colon  + 1 ;
444-                             int  len  = 0 ;
445-                             for  (int  i  = pos ; i  < host .length (); i ++) {
446-                                 if  (Character .isDigit (host .charAt (i ))) {
447-                                     len ++;
448-                                 } else  {
449-                                     break ;
450-                                 }
451-                             }
452-                             if  (len  > 0 ) {
453-                                 try  {
454-                                     port  = Integer .parseInt (host .substring (pos , pos  + len ));
455-                                 } catch  (final  NumberFormatException  ex ) {
456-                                 }
457-                             }
458-                             host  = host .substring (0 , colon );
432+                     final  String  scheme  = uri .getScheme ();
433+                     final  String  hostname ;
434+                     final  int  port ;
435+                     at  = content .indexOf (":" );
436+                     if  (at  != -1 ) {
437+                         hostname  = content .substring (0 , at );
438+                         try  {
439+                             final  String  portText  = content .substring (at  + 1 );
440+                             port  = !TextUtils .isEmpty (portText ) ? Integer .parseInt (portText ) : -1 ;
441+                         } catch  (final  NumberFormatException  ex ) {
442+                             return  null ;
459443                        }
444+                     } else  {
445+                         hostname  = content ;
446+                         port  = -1 ;
447+                     }
448+                     try  {
449+                         return  new  HttpHost (hostname , port , scheme );
450+                     } catch  (final  IllegalArgumentException  ex ) {
451+                         return  null ;
460452                    }
461453                }
462-             }
463-             final  String  scheme  = uri .getScheme ();
464-             if  (!TextUtils .isBlank (host )) {
465-                 try  {
466-                     target  = new  HttpHost (host , port , scheme );
467-                 } catch  (final  IllegalArgumentException  ignore ) {
468-                 }
454+             } else  {
455+                 return  new  HttpHost (uri .getHost (), uri .getPort (), uri .getScheme ());
469456            }
470457        }
471-         return  target ;
458+         return  null ;
472459    }
473460
474461    /** 
0 commit comments