Skip to content

Commit

Permalink
Check for Leading Slash in Path (#21163)
Browse files Browse the repository at this point in the history
Check for Leading Slash in Path
  • Loading branch information
alzimmermsft authored May 5, 2021
1 parent 153351f commit b396986
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

/**
* HTTP client that plays back {@link NetworkCallRecord NetworkCallRecords}.
*/
public final class PlaybackClient implements HttpClient {
private static final String X_MS_CLIENT_REQUEST_ID = "x-ms-client-request-id";
private static final String X_MS_ENCRYPTION_KEY_SHA256 = "x-ms-encryption-key-sha256";

// Pattern that matches all '//' in a URL that aren't prefixed by 'http:' or 'https:'.
private static final Pattern DOUBLE_SLASH_CLEANER = Pattern.compile("(?<!https?:)\\/\\/");

private final ClientLogger logger = new ClientLogger(PlaybackClient.class);
private final AtomicInteger count = new AtomicInteger(0);
private final Map<String, String> textReplacementRules;
Expand Down Expand Up @@ -61,8 +66,20 @@ private Mono<HttpResponse> playbackHttpResponse(final HttpRequest request) {

final String matchingUrl = removeHost(incomingUrl);

NetworkCallRecord networkCallRecord = recordedData.findFirstAndRemoveNetworkCall(record ->
record.getMethod().equalsIgnoreCase(incomingMethod) && removeHost(record.getUri()).equalsIgnoreCase(matchingUrl));
NetworkCallRecord networkCallRecord = recordedData.findFirstAndRemoveNetworkCall(record -> {
if (!record.getMethod().equalsIgnoreCase(incomingMethod)) {
return false;
}

String removedHostUri = removeHost(record.getUri());

// There is an upcoming change in azure-core to fix a scenario with '//' being used instead of '/'.
// For now both recording formats need to be supported.
String cleanedHostUri = DOUBLE_SLASH_CLEANER.matcher(removedHostUri).replaceAll("/");
String cleanedMatchingUrl = DOUBLE_SLASH_CLEANER.matcher(matchingUrl).replaceAll("/");

return cleanedHostUri.equalsIgnoreCase(cleanedMatchingUrl);
});

count.incrementAndGet();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ private HttpRequest createHttpRequest(SwaggerMethodParser methodParser, Object[]
if (hostPath == null || hostPath.isEmpty() || "/".equals(hostPath) || path.contains("://")) {
urlBuilder.setPath(path);
} else {
urlBuilder.setPath(hostPath + "/" + path);
if (path.startsWith("/")) {
urlBuilder.setPath(hostPath + path);
} else {
urlBuilder.setPath(hostPath + "/" + path);
}
}
}
}
Expand Down

0 comments on commit b396986

Please sign in to comment.