forked from apple/servicetalk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initializer API for headers of HTTP proxy CONNECT request
Motivation: After apple#2697 moved HTTP proxy `CONNECT` logic before user-defined `ConnectionFactoryFilter`s, users lost the ability to intercept `CONNECT` requests for the purpose of adding custom headers, like auth, tracing, etc. Modifications: - Introduce `ProxyConfig` and `ProxyConfigBuilder` in `http-api` that can be used to provide additional options for proxy behavior; - Add `SingleAddressHttpClientBuilder.proxyConfig(...)` overload that takes `ProxyConfig`; - Deprecate pre-existing `SingleAddressHttpClientBuilder.proxyAddress()` method, recommend switching to new API; - Enhance `HttpsProxyTest` to verify that new API can be used to send `Proxy-Authorization` header; Result: Users have explicit API to alter HTTP `CONNECT` request headers, if necessary.
- Loading branch information
1 parent
99ea1c5
commit 46ae4e4
Showing
21 changed files
with
305 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
servicetalk-http-api/src/main/java/io/servicetalk/http/api/ProxyConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright © 2023 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.http.api; | ||
|
||
import io.servicetalk.transport.api.ClientSslConfig; | ||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Configuration for a proxy. | ||
* | ||
* @param <A> the type of address | ||
*/ | ||
public interface ProxyConfig<A> { | ||
|
||
/** | ||
* Address of the proxy. | ||
* <p> | ||
* Usually, this is an unresolved proxy address and its type must match the type of address before resolution used | ||
* by {@link SingleAddressHttpClientBuilder}. However, if the client builder was created for a resolved address, | ||
* this address must also be already resolved. Otherwise, a runtime exceptions will occur. | ||
* | ||
* @return address of the proxy | ||
*/ | ||
A address(); | ||
|
||
/** | ||
* An initializer for {@link HttpHeaders} related to | ||
* <a href="https://datatracker.ietf.org/doc/html/rfc9110#section-9.3.6">HTTP/1.1 CONNECT</a> request. | ||
* <p> | ||
* When this {@link ProxyConfig} is used for secure proxy tunnels (when | ||
* {@link SingleAddressHttpClientBuilder#sslConfig(ClientSslConfig) ClientSslConfig} is configured) the tunnel is | ||
* always initialized using {@code HTTP/1.1 CONNECT} request. This {@link Consumer} can be used to set custom | ||
* {@link HttpHeaders} for that request, like {@link HttpHeaderNames#PROXY_AUTHORIZATION proxy-authorization}, | ||
* tracing, or any other header. | ||
* | ||
* @return An initializer for {@link HttpHeaders} related to | ||
* <a href="https://datatracker.ietf.org/doc/html/rfc9110#section-9.3.6">HTTP/1.1 CONNECT</a> request | ||
*/ | ||
Consumer<HttpHeaders> connectRequestHeadersInitializer(); | ||
|
||
/** | ||
* Returns a {@link ProxyConfig} for the specified {@code address}. | ||
* <p> | ||
* All other {@link ProxyConfig} options will use their default values applied by {@link ProxyConfigBuilder}. | ||
* | ||
* @param address Address of the proxy | ||
* @param <A> the type of address | ||
* @return a {@link ProxyConfig} for the specified {@code address} | ||
* @see ProxyConfigBuilder | ||
*/ | ||
static <A> ProxyConfig<A> of(A address) { | ||
return new ProxyConfigBuilder<>(address).build(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
servicetalk-http-api/src/main/java/io/servicetalk/http/api/ProxyConfigBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright © 2023 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.http.api; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Builder for {@link ProxyConfig}. | ||
* | ||
* @param <A> the type of address | ||
*/ | ||
public final class ProxyConfigBuilder<A> { | ||
|
||
private final A address; | ||
private Consumer<HttpHeaders> connectRequestHeadersInitializer = __ -> { }; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param address Proxy address | ||
* @see ProxyConfig#address() | ||
*/ | ||
public ProxyConfigBuilder(final A address) { | ||
this.address = requireNonNull(address); | ||
} | ||
|
||
/** | ||
* Sets an initializer for {@link HttpHeaders} related to | ||
* <a href="https://datatracker.ietf.org/doc/html/rfc9110#section-9.3.6">HTTP/1.1 CONNECT</a> request. | ||
* | ||
* @param connectRequestHeadersInitializer {@link Consumer} that can be used to set custom {@link HttpHeaders} for | ||
* {@code HTTP/1.1 CONNECT} request (auth, tracing, etc.) | ||
* @return {@code this} | ||
* @see ProxyConfig#connectRequestHeadersInitializer() | ||
*/ | ||
public ProxyConfigBuilder<A> connectRequestHeadersInitializer( | ||
final Consumer<HttpHeaders> connectRequestHeadersInitializer) { | ||
this.connectRequestHeadersInitializer = requireNonNull(connectRequestHeadersInitializer); | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds a new {@link ProxyConfig}. | ||
* | ||
* @return a new {@link ProxyConfig}. | ||
*/ | ||
public ProxyConfig<A> build() { | ||
return new DefaultProxyConfig<>(address, connectRequestHeadersInitializer); | ||
} | ||
|
||
private static final class DefaultProxyConfig<A> implements ProxyConfig<A> { | ||
|
||
private final A address; | ||
private final Consumer<HttpHeaders> connectRequestHeadersInitializer; | ||
|
||
private DefaultProxyConfig(final A address, final Consumer<HttpHeaders> connectRequestHeadersInitializer) { | ||
this.address = address; | ||
this.connectRequestHeadersInitializer = connectRequestHeadersInitializer; | ||
} | ||
|
||
@Override | ||
public A address() { | ||
return address; | ||
} | ||
|
||
@Override | ||
public Consumer<HttpHeaders> connectRequestHeadersInitializer() { | ||
return connectRequestHeadersInitializer; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.