dragon-zhang commented on code in PR #3792:
URL: https://github.com/apache/shenyu/pull/3792#discussion_r935650056


##########
shenyu-plugin/shenyu-plugin-httpclient/src/main/java/org/apache/shenyu/plugin/httpclient/WebClientPlugin.java:
##########
@@ -47,20 +49,26 @@ public class WebClientPlugin extends 
AbstractHttpClientPlugin<ClientResponse> {
     public WebClientPlugin(final WebClient webClient) {
         this.webClient = webClient;
     }
-
+    
     @Override
     protected Mono<ClientResponse> doRequest(final ServerWebExchange exchange, 
final String httpMethod, final URI uri,
                                              final HttpHeaders httpHeaders, 
final Flux<DataBuffer> body) {
         // springWebflux5.3 mark #exchange() deprecated. because #echange 
maybe make memory leak.
         // https://github.com/spring-projects/spring-framework/issues/25751
         // exchange is deprecated, so change to {@link 
WebClient.RequestHeadersSpec#exchangeToMono(Function)}
-        // exchangeToMono has two important bug:
-        // 1.exchangeToMono can cause NPE when response body is null
-        // 2.download file with exchangeToMono can't open
         return webClient.method(HttpMethod.valueOf(httpMethod)).uri(uri)
                 .headers(headers -> headers.addAll(httpHeaders))
                 .body(BodyInserters.fromDataBuffers(body))
-                .exchange()
+                .exchangeToMono(response -> 
response.bodyToMono(byte[].class).defaultIfEmpty(new byte[0])
+                        .flatMap(bytes -> {
+                            final ClientResponse.Builder builder = 
ClientResponse.create(response.statusCode())
+                                    .headers(headers -> 
headers.addAll(response.headers().asHttpHeaders()));
+                            if (ArrayUtils.isEmpty(bytes)) {
+                                return Mono.just(builder.build());
+                            }
+                            final DataBufferFactory dataBufferFactory = 
exchange.getResponse().bufferFactory();
+                            return 
Mono.just(builder.body(Flux.just(dataBufferFactory.wrap(bytes))).build());

Review Comment:
   Is it necessary to release the `DataBuffer` here ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to