4848import java .io .Reader ;
4949import java .net .HttpURLConnection ;
5050import java .net .URI ;
51- import java .net .URISyntaxException ;
5251import java .net .URL ;
5352import java .nio .charset .StandardCharsets ;
5453import java .util .List ;
@@ -96,6 +95,7 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
9695 }
9796 BootstrapInfo newInfo = InternalGrpcBootstrapperImpl .parseBootstrap (
9897 generateBootstrap ("" , true , true ));
98+ // Avoid setting global when testing
9999 if (httpConnectionProvider == HttpConnectionFactory .INSTANCE ) {
100100 forceXdsBootstrapInfo = newInfo ;
101101 }
@@ -108,6 +108,7 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
108108 }
109109 BootstrapInfo newInfo = InternalGrpcBootstrapperImpl .parseBootstrap (
110110 generateBootstrap ());
111+ // Avoid setting global when testing
111112 if (httpConnectionProvider == HttpConnectionFactory .INSTANCE ) {
112113 bootstrapInfo = newInfo ;
113114 }
@@ -143,12 +144,13 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
143144 GoogleCloudToProdNameResolver (URI targetUri , Args args , Resource <Executor > executorResource ,
144145 NameResolver .Factory nameResolverFactory ) {
145146 this .executorResource = checkNotNull (executorResource , "executorResource" );
146- String query = targetUri .getRawQuery ();
147+ String targetPath = checkNotNull (checkNotNull (targetUri , "targetUri" ).getPath (), "targetPath" );
148+ Uri grpcUri = Uri .create (targetUri .toString ());
149+ String query = grpcUri .getRawQuery ();
147150 this .forceXds = checkForceXds (query );
148151 this .schemeOverride = (forceXds || isOnGcp ) ? "xds" : "dns" ;
149152 String newQuery = stripForceXds (query );
150153
151- String targetPath = checkNotNull (checkNotNull (targetUri , "targetUri" ).getPath (), "targetPath" );
152154 Preconditions .checkArgument (
153155 targetPath .startsWith ("/" ),
154156 "the path component (%s) of the target (%s) must start with '/'" ,
@@ -157,28 +159,16 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
157159 authority = GrpcUtil .checkAuthority (targetPath .substring (1 ));
158160 syncContext = checkNotNull (args , "args" ).getSynchronizationContext ();
159161
160- String rawAuthority = schemeOverride .equals ("xds" )
161- ? C2P_AUTHORITY
162- : targetUri .getRawAuthority ();
163- String rawPath = targetUri .getRawPath ();
164- String rawFragment = targetUri .getRawFragment ();
165- try {
166- StringBuilder uriStr = new StringBuilder ();
167- uriStr .append (schemeOverride ).append (":" );
168- if (rawAuthority != null ) {
169- uriStr .append ("//" ).append (rawAuthority );
170- }
171- uriStr .append (rawPath );
172- if (newQuery != null ) {
173- uriStr .append ("?" ).append (newQuery );
174- }
175- if (rawFragment != null ) {
176- uriStr .append ("#" ).append (rawFragment );
177- }
178- targetUri = new URI (uriStr .toString ());
179- } catch (URISyntaxException e ) {
180- throw new IllegalArgumentException ("Invalid URI" , e );
162+ Uri .Builder modifiedTargetBuilder = grpcUri .toBuilder ().setScheme (schemeOverride );
163+ if (newQuery != null ) {
164+ modifiedTargetBuilder .setRawQuery (newQuery );
165+ } else {
166+ modifiedTargetBuilder .setQuery (null );
181167 }
168+ if (schemeOverride .equals ("xds" )) {
169+ modifiedTargetBuilder .setRawAuthority (C2P_AUTHORITY );
170+ }
171+ targetUri = URI .create (modifiedTargetBuilder .build ().toString ());
182172
183173 if (schemeOverride .equals ("xds" )) {
184174 args = args .toBuilder ()
@@ -440,9 +430,13 @@ private static boolean checkForceXds(String query) {
440430 return false ;
441431 }
442432 for (String part : Splitter .on ('&' ).split (query )) {
443- if (part .equals ("force-xds" ) || part . startsWith ( "force-xds=" ) ) {
433+ if (part .equals ("force-xds" )) {
444434 return true ;
445435 }
436+ if (part .startsWith ("force-xds=" )) {
437+ String value = part .substring ("force-xds=" .length ());
438+ return !value .equalsIgnoreCase ("false" ) && !value .equals ("0" );
439+ }
446440 }
447441 return false ;
448442 }
0 commit comments