Skip to content

Add support for HTTP rewrites #1062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 16, 2025
Merged

Add support for HTTP rewrites #1062

merged 8 commits into from
Jul 16, 2025

Conversation

bioball
Copy link
Member

@bioball bioball commented May 6, 2025

Follows the design of apple/pkl-evolution#17

@@ -178,6 +178,12 @@ abstract class CliCommand(protected val cliOptions: CliBaseOptions) {
?: settings.http?.proxy?.noProxy
}

private val httpRewrites by lazy {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private val httpRewrites by lazy {
private val httpRewrites: Map<String, String>? by lazy {

I know we are inconsistent in this regard, but I think it's better to have the types always defined for properties unless they are absolute obvious from the value. Even if they are private.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine with me; I'll change the rest of the properties in this class too

this.userAgent = userAgent;
this.requestTimeout = requestTimeout;
this.testPort = testPort;
this.delegate = delegate;
this.rewrites =
rewrites.entrySet().stream()
.sorted(Comparator.comparingInt((entry) -> entry.getKey().length()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorting by length means we'd pick https://foo.bar/ over https://foo.bar/baz when both match (because findRewrite picks the first match), which seems counterintuitive to me.
I'd expect the resolution to be specific -> generic, not the other way around.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, this was a bug

Copy link
Contributor

@stackoverflow stackoverflow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Some minor comments.

)
}
assertThat(ex.message)
.contains("Rewrite rule must have a lowercased hostname, but was 'www.FOO.com'")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we lowercase the host instead of throw?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, true!

.multiple()
.toMap()

private fun OptionCallTransformContext.validateRewrite(rewrite: URI) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this being used? I see you are calling IoUtils.validateRewriteRule which already does validation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this was leftover code. Removed.

if (settings.http() != null) {
var httpClientBuilder = HttpClient.builder();
if (settings.http().proxy() != null) {
var noProxy = settings.http().proxy().noProxy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were we ignoring noProxy rules before? Is this a bugfix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, looks like it is.

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You didn't change this file.

@bioball bioball merged commit 99020bb into apple:main Jul 16, 2025
4 checks passed
@bioball bioball deleted the http-rewrites branch July 16, 2025 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants