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 ;
@@ -143,12 +142,13 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
143142 GoogleCloudToProdNameResolver (URI targetUri , Args args , Resource <Executor > executorResource ,
144143 NameResolver .Factory nameResolverFactory ) {
145144 this .executorResource = checkNotNull (executorResource , "executorResource" );
146- String query = targetUri .getRawQuery ();
145+ String targetPath = checkNotNull (checkNotNull (targetUri , "targetUri" ).getPath (), "targetPath" );
146+ Uri grpcUri = Uri .create (targetUri .toString ());
147+ String query = grpcUri .getRawQuery ();
147148 this .forceXds = checkForceXds (query );
148149 this .schemeOverride = (forceXds || isOnGcp ) ? "xds" : "dns" ;
149150 String newQuery = stripForceXds (query );
150151
151- String targetPath = checkNotNull (checkNotNull (targetUri , "targetUri" ).getPath (), "targetPath" );
152152 Preconditions .checkArgument (
153153 targetPath .startsWith ("/" ),
154154 "the path component (%s) of the target (%s) must start with '/'" ,
@@ -157,28 +157,16 @@ private static BootstrapInfo getBootstrapInfo(boolean isForcedXds)
157157 authority = GrpcUtil .checkAuthority (targetPath .substring (1 ));
158158 syncContext = checkNotNull (args , "args" ).getSynchronizationContext ();
159159
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 );
160+ Uri .Builder modifiedTargetBuilder = grpcUri .toBuilder ().setScheme (schemeOverride );
161+ if (newQuery != null ) {
162+ modifiedTargetBuilder .setRawQuery (newQuery );
163+ } else {
164+ modifiedTargetBuilder .setQuery (null );
181165 }
166+ if (schemeOverride .equals ("xds" )) {
167+ modifiedTargetBuilder .setRawAuthority (C2P_AUTHORITY );
168+ }
169+ targetUri = URI .create (modifiedTargetBuilder .build ().toString ());
182170
183171 if (schemeOverride .equals ("xds" )) {
184172 args = args .toBuilder ()
@@ -440,9 +428,13 @@ private static boolean checkForceXds(String query) {
440428 return false ;
441429 }
442430 for (String part : Splitter .on ('&' ).split (query )) {
443- if (part .equals ("force-xds" ) || part . startsWith ( "force-xds=" ) ) {
431+ if (part .equals ("force-xds" )) {
444432 return true ;
445433 }
434+ if (part .startsWith ("force-xds=" )) {
435+ String value = part .substring ("force-xds=" .length ());
436+ return !value .equalsIgnoreCase ("false" ) && !value .equals ("0" );
437+ }
446438 }
447439 return false ;
448440 }
0 commit comments