Skip to content

Commit

Permalink
Fixed the issue where slash from path parameter got removed with endp… (
Browse files Browse the repository at this point in the history
#3964)

* Fixed the issue where slash from path parameter got removed with endpoint override that contains a trailing slash

* Not strip slash
  • Loading branch information
zoewangg authored May 17, 2023
1 parent 1a23009 commit ee31237
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,33 @@ public final class AwsEndpointProviderUtils {
String clientEndpointPath = clientEndpoint.getRawPath();

// [client endpoint path]/[request path]
String requestPath = request.getUri().getRawPath();
String requestPath = request.encodedPath();

// [client endpoint path]/[additional path added by resolver]
String resolvedUriPath = resolvedUri.getRawPath();

// our goal is to construct [client endpoint path]/[additional path added by resolver]/[request path], so we
// just need to strip the client endpoint path from the marshalled request path to isolate just the part added
// by the marshaller
String requestPathWithClientPathRemoved = StringUtils.replaceOnce(requestPath, clientEndpointPath, "");
String finalPath = SdkHttpUtils.appendUri(resolvedUriPath, requestPathWithClientPathRemoved);
String finalPath = requestPath;

// If there is an additional path added by resolver, i.e., [additional path added by resolver] not null,
// we need to combine the path
if (!resolvedUriPath.equals(clientEndpointPath)) {
finalPath = combinePath(clientEndpointPath, requestPath, resolvedUriPath);
}

return request.toBuilder().protocol(resolvedUri.getScheme()).host(resolvedUri.getHost()).port(resolvedUri.getPort())
.encodedPath(finalPath).build();
.encodedPath(finalPath).build();
}

/**
* Our goal is to construct [client endpoint path]/[additional path added by resolver]/[request path], so we just need to
* strip the client endpoint path from the marshalled request path to isolate just the part added by the marshaller. Trailing
* slash is removed from client endpoint path before stripping because it could cause the leading slash to be removed from the
* request path: e.g., StringUtils.replaceOnce("/", "//test", "") generates "/test" and the expected result is "//test"
*/
private static String combinePath(String clientEndpointPath, String requestPath, String resolvedUriPath) {
String requestPathWithClientPathRemoved = StringUtils.replaceOnce(requestPath, clientEndpointPath, "");
String finalPath = SdkHttpUtils.appendUri(resolvedUriPath, requestPathWithClientPathRemoved);
return finalPath;
}

public static AwsRequest addHeaders(AwsRequest request, Map<String, List<String>> headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public class SdkExecutionAttribute {
public static final ExecutionAttribute<Boolean> ENDPOINT_OVERRIDDEN = new ExecutionAttribute<>("EndpointOverridden");

/**
* This is the endpointOverride (if {@link #ENDPOINT_OVERRIDDEN} is true), otherwise null.
* This is the endpointOverride (if {@link #ENDPOINT_OVERRIDDEN} is true), otherwise the endpoint generated from regional
* metadata.
*/
public static final ExecutionAttribute<URI> CLIENT_ENDPOINT = new ExecutionAttribute<>("EndpointOverride");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,35 @@ public void setUri_combinesPathsCorrectly() {
.isEqualTo("https://override.example.com/a/b/c");
}

@Test
public void setUri_doubleSlash_combinesPathsCorrectly() {
URI clientEndpoint = URI.create("https://override.example.com/a");
URI requestUri = URI.create("https://override.example.com/a//c");
URI resolvedUri = URI.create("https://override.example.com/a/b");

SdkHttpRequest request = SdkHttpRequest.builder()
.uri(requestUri)
.method(SdkHttpMethod.GET)
.build();

assertThat(AwsEndpointProviderUtils.setUri(request, clientEndpoint, resolvedUri).getUri().toString())
.isEqualTo("https://override.example.com/a/b//c");
}

@Test
public void setUri_withTrailingSlashNoPath_combinesPathsCorrectly() {
URI clientEndpoint = URI.create("https://override.example.com/");
URI requestUri = URI.create("https://override.example.com//a");
URI resolvedUri = URI.create("https://override.example.com/");
SdkHttpRequest request = SdkHttpRequest.builder()
.uri(requestUri)
.method(SdkHttpMethod.GET)
.build();

assertThat(AwsEndpointProviderUtils.setUri(request, clientEndpoint, resolvedUri).getUri().toString())
.isEqualTo("https://override.example.com//a");
}

@Test
public void setHeaders_existingValuesOnOverride_combinesWithNewValues() {
AwsRequest request = AllTypesRequest.builder()
Expand Down

0 comments on commit ee31237

Please sign in to comment.