Skip to content

Commit

Permalink
fix redirect count (#391)
Browse files Browse the repository at this point in the history
* fix redirect couunt

* currentRedirectCounter

* format
  • Loading branch information
ohadbitt authored Nov 4, 2024
1 parent d73eeb3 commit 378aa5a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
20 changes: 6 additions & 14 deletions data/src/main/java/com/microsoft/azure/kusto/data/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
import java.util.Optional;

public abstract class BaseClient implements Client, StreamingClient {

private static final int MAX_REDIRECT_COUNT = 1;

// Make logger available to implementations
protected static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Expand Down Expand Up @@ -55,13 +52,8 @@ private String processResponseBody(HttpResponse response) throws DataServiceExce
}
}

protected InputStream postToStreamingOutput(HttpRequest request) throws DataServiceException {
return postToStreamingOutput(request, 0);
}

// Todo: Implement async version of this method
protected InputStream postToStreamingOutput(HttpRequest request, int redirectCount) throws DataServiceException {

protected InputStream postToStreamingOutput(HttpRequest request, int currentRedirectCounter, int maxRedirectCount) throws DataServiceException {
boolean returnInputStream = false;
String errorFromResponse = null;

Expand All @@ -81,14 +73,14 @@ protected InputStream postToStreamingOutput(HttpRequest request, int redirectCou
// Ideal to close here (as opposed to finally) so that if any data can't be flushed, the exception will be properly thrown and handled
httpResponse.close();

if (shouldPostToOriginalUrlDueToRedirect(redirectCount, responseStatusCode)) {
if (shouldPostToOriginalUrlDueToRedirect(currentRedirectCounter, responseStatusCode, maxRedirectCount)) {
Optional<HttpHeader> redirectLocation = Optional.ofNullable(httpResponse.getHeaders().get(HttpHeaderName.LOCATION));
if (redirectLocation.isPresent() && !redirectLocation.get().getValue().equals(request.getUrl().toString())) {
HttpRequest redirectRequest = HttpRequestBuilder
.fromExistingRequest(request)
.withURL(redirectLocation.get().getValue())
.build();
return postToStreamingOutput(redirectRequest, redirectCount + 1);
return postToStreamingOutput(redirectRequest, currentRedirectCounter + 1, maxRedirectCount);
}
}
} catch (IOException ex) {
Expand Down Expand Up @@ -143,16 +135,16 @@ public static DataServiceException createExceptionFromResponse(String url, HttpR
}

private static void closeResourcesIfNeeded(boolean returnInputStream, HttpResponse httpResponse) {
// If we close the resources after returning the InputStream to the user, he won't be able to read from it - used in streaming query
// If we close the resources after returning the InputStream to the user, he won't be able to read from it - used in streaming query
if (!returnInputStream) {
if (httpResponse != null) {
httpResponse.close();
}
}
}

private static boolean shouldPostToOriginalUrlDueToRedirect(int redirectCount, int status) {
return (status == HttpStatus.FOUND || status == HttpStatus.TEMP_REDIRECT) && redirectCount + 1 <= MAX_REDIRECT_COUNT;
private static boolean shouldPostToOriginalUrlDueToRedirect(int redirectCount, int status, int maxRedirectCount) {
return (status == HttpStatus.FOUND || status == HttpStatus.TEMP_REDIRECT) && redirectCount + 1 <= maxRedirectCount;
}

private static String determineActivityId(HttpResponse httpResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ private InputStream executeStreamingQuery(@NotNull KustoRequest kr) throws DataS

// Get the response and trace the call
return MonitoredActivity.invoke(
(SupplierOneException<InputStream, DataServiceException>) () -> postToStreamingOutput(request, kr.getProperties().getRedirectCount()),
(SupplierOneException<InputStream, DataServiceException>) () -> postToStreamingOutput(request, 0, kr.getProperties().getRedirectCount()),
"ClientImpl.executeStreamingQuery", updateAndGetExecuteTracingAttributes(kr.getDatabase(), kr.getProperties()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class ClientRequestProperties implements Serializable, TraceableAttributes {
public static final String OPTION_SERVER_TIMEOUT = "servertimeout";

// If set and positive, indicates the maximum number of HTTP redirects that the client will process. [Integer]
// If set and positive, indicates the maximum number of HTTP redirects that the client will process. [Integer]
public static final String OPTION_CLIENT_MAX_REDIRECT_COUNT = "client_max_redirect_count";
/*
* Matches valid Kusto Timespans: Optionally negative, optional number of days followed by a period, optionally up to 24 as hours followed by a colon,
Expand Down

0 comments on commit 378aa5a

Please sign in to comment.