{"record":{"id":"b44dc077d13b32b4","repo":"apple/pkl","slug":"httpredirectinvaliduri","errorCode":"httpRedirectInvalidUri","errorMessage":"httpRedirectInvalidUri: ${uri}: ${location}","messagePattern":"httpRedirectInvalidUri: (.+?): (.+?)","errorType":"exception","errorClass":"HttpClientException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java","lineNumber":138,"sourceCode":"        return response;\n      }\n      if (response.body() instanceof Closeable closeable) {\n        closeable.close();\n      }\n      if (redirectCount >= MAX_HTTP_REDIRECTS) {\n        throw new HttpClientException(\n            ErrorMessages.create(\"httpTooManyRedirects\", MAX_HTTP_REDIRECTS));\n      }\n      var location = response.headers().firstValue(\"Location\");\n      if (location.isEmpty()) {\n        throw new HttpClientException(\n            ErrorMessages.create(\"httpRedirectNoLocation\", currentRequestUri));\n      }\n      URI redirectUri;\n      try {\n        redirectUri = currentRequestUri.resolve(location.get());\n      } catch (IllegalArgumentException e) {\n        throw new HttpClientException(\n            ErrorMessages.create(\"httpRedirectInvalidUri\", currentRequestUri, location.get()));\n      }\n      if (currentRequestUri.getScheme().equalsIgnoreCase(\"https\")\n          && redirectUri.getScheme().equalsIgnoreCase(\"http\")) {\n        throw new HttpClientException(\n            ErrorMessages.create(\"httpRedirectCannotDowngrade\", currentRequestUri, redirectUri));\n      }\n      currentRequestUri = rewriteUri(redirectUri);\n      currentRequest = rewriteRequest(request, currentRequestUri);\n      redirectCount++;\n    }\n  }\n\n  @Override\n  public <T> HttpResponse<T> send(\n      HttpRequest request,\n      BodyHandler<T> responseBodyHandler,\n      HttpRequestChecker httpRequestChecker)","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/http/RequestRewritingClient.java#L120-L156","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the Location header returned by the server and fix it server-side (percent-encode illegal characters)","Pre-validate the redirect Location value with new URI(location) before making the request","Bypass the broken redirect by requesting the intended final URL directly","If you control a proxy in front of the server, fix its header rewriting"],"exampleFix":"// before: blindly following redirects that may have bad Location\nvar client = new RequestRewritingClient(...);\nvar resp = client.send(request, handler, checker);\n// after: validate Location yourself\ntry { new URI(locationHeader); } catch (URISyntaxException e) { /* handle bad redirect target */ }","handlingStrategy":"validation","validationCode":"static boolean isResolvableRedirect(String location) { try { new java.net.URI(location); return true; } catch (java.net.URISyntaxException e) { return false; } }","typeGuard":null,"tryCatchPattern":"try { client.send(request, handler, checker); } catch (HttpClientException e) { if (e.getMessage().contains(\"httpRedirectInvalidUri\")) { /* inspect Location header, request final URL directly */ } }","preventionTips":["Fix upstream servers/proxies to emit valid Location headers","Percent-encode redirect targets server-side","Test redirect flows against strict URI parsing"],"tags":["http","redirect","uri"],"backgroundTag":"invalid-url","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}