spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Propagate WebClient attributes into underlying HTTP client request where possible

Open Crustack opened this issue 2 years ago • 1 comments

This PR Closes #26208 As proposed by @tstavinoha (https://github.com/spring-projects/spring-framework/issues/26208#issuecomment-739817950) and @rstoyanchev (https://github.com/spring-projects/spring-framework/issues/26208#issuecomment-783324482), this adds the option applyAttributes to the WebClient.Builder. If it is set to true, the ClientRequest.attributes() will be copied over to the underlying http-client library HttpRequest. For this there are different implementations for the various ClientHttpConnector implementations:

  • JettyClientHttpRequest: Attributes are set with the org.eclipse.jetty.client.api.Request#attribute(String, Object) method one by one
  • ReactorClientHttpRequest: A single Map<String, Object> containing all request attributes is set to the Attribute attributes in the reactor.netty.channel.ChannelOperations#channel() (as proposed by https://github.com/spring-projects/spring-framework/issues/26208#issuecomment-783324482)
  • ReactorNetty2ClientHttpRequest: Same as in ReactorClientHttpRequest, but with the netty5 classes
  • HttpComponentsClientHttpRequest: Attributes are set with the org.apache.hc.client5.http.protocol.HttpClientContext#setAttribute(String, Object) method one by one
  • JdkClientHttpRequest: I still need some input on this one. As far as I can see there is no possibility to set request attributes to the java.net.http.HttpRequest?

As suggested by https://github.com/spring-projects/spring-framework/issues/26208#issuecomment-739817950, applyAttributes defaults to true and the user can opt-out when using the Builder to create the Connector.

Crustack avatar Feb 11 '23 14:02 Crustack

@rstoyanchev would love some feedback if you have the time.

Crustack avatar Mar 04 '23 13:03 Crustack