apple/pkl · error · HttpClientException

httpRedirectInvalidUri

httpRedirectInvalidUri

Error message

httpRedirectInvalidUri: ${uri}: ${location}

What it means

Thrown when the Location header of an HTTP redirect cannot be resolved against the current request URI to produce a valid URI. RequestRewritingClient resolves redirect targets strictly; a syntactically invalid Location value makes the redirect unresolvable, so the client aborts instead of following a broken redirect.

Solutions

  1. Inspect the Location header returned by the server and fix it server-side (percent-encode illegal characters)
  2. Pre-validate the redirect Location value with new URI(location) before making the request
  3. Bypass the broken redirect by requesting the intended final URL directly
  4. If you control a proxy in front of the server, fix its header rewriting

Example fix

// before: blindly following redirects that may have bad Location
var client = new RequestRewritingClient(...);
var resp = client.send(request, handler, checker);
// after: validate Location yourself
try { new URI(locationHeader); } catch (URISyntaxException e) { /* handle bad redirect target */ }
Defensive patterns

Strategy: validation

Validate before calling

static boolean isResolvableRedirect(String location) { try { new java.net.URI(location); return true; } catch (java.net.URISyntaxException e) { return false; } }

Try / catch

try { client.send(request, handler, checker); } catch (HttpClientException e) { if (e.getMessage().contains("httpRedirectInvalidUri")) { /* inspect Location header, request final URL directly */ } }

Prevention

When it happens

Trigger: An HTTP 3xx response carries a Location header that is not a valid URI reference (e.g. contains spaces, illegal characters, or is malformed), so currentRequestUri.resolve(location) throws IllegalArgumentException.

Common situations: Misbehaving or misconfigured upstream servers (e.g. emitting Location: /path with spaces), proxies injecting bad headers, or manual/test servers producing non-RFC-compliant redirect headers.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/b44dc077d13b32b4. Report an issue: GitHub.

Appendix: source

Thrown at pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java:138

        return response;
      }
      if (response.body() instanceof Closeable closeable) {
        closeable.close();
      }
      if (redirectCount >= MAX_HTTP_REDIRECTS) {
        throw new HttpClientException(
            ErrorMessages.create("httpTooManyRedirects", MAX_HTTP_REDIRECTS));
      }
      var location = response.headers().firstValue("Location");
      if (location.isEmpty()) {
        throw new HttpClientException(
            ErrorMessages.create("httpRedirectNoLocation", currentRequestUri));
      }
      URI redirectUri;
      try {
        redirectUri = currentRequestUri.resolve(location.get());
      } catch (IllegalArgumentException e) {
        throw new HttpClientException(
            ErrorMessages.create("httpRedirectInvalidUri", currentRequestUri, location.get()));
      }
      if (currentRequestUri.getScheme().equalsIgnoreCase("https")
          && redirectUri.getScheme().equalsIgnoreCase("http")) {
        throw new HttpClientException(
            ErrorMessages.create("httpRedirectCannotDowngrade", currentRequestUri, redirectUri));
      }
      currentRequestUri = rewriteUri(redirectUri);
      currentRequest = rewriteRequest(request, currentRequestUri);
      redirectCount++;
    }
  }

  @Override
  public <T> HttpResponse<T> send(
      HttpRequest request,
      BodyHandler<T> responseBodyHandler,
      HttpRequestChecker httpRequestChecker)

View on GitHub (pinned to f3efcbfc9b)