Copilot commented on code in PR #8035:
URL: https://github.com/apache/incubator-seata/pull/8035#discussion_r3025996417


##########
namingserver/src/main/java/org/apache/seata/namingserver/filter/ConsoleRemotingFilter.java:
##########
@@ -99,42 +151,113 @@ public void doFilter(ServletRequest servletRequest, 
ServletResponse servletRespo
                                     + request.getRequestURI()
                                     + (request.getQueryString() != null ? "?" 
+ request.getQueryString() : "");
 
-                            // Copy headers from the original request
+                            // Copy headers from the original request, 
stripping hop-by-hop
+                            // headers (RFC 7230 ยง6.1) and Host (so the client 
library sets
+                            // the correct value for the upstream target).
                             HttpHeaders headers = new HttpHeaders();
                             if (node.getRole() == ClusterRole.LEADER) {
                                 headers.add(RAFT_GROUP_HEADER, node.getUnit());
                             }
                             Collections.list(request.getHeaderNames())
-                                    .forEach(headerName -> 
headers.add(headerName, request.getHeader(headerName)));
+                                    .forEach(headerName -> {
+                                        if 
(!HttpHeaders.HOST.equalsIgnoreCase(headerName)
+                                                && 
!HttpHeaders.CONNECTION.equalsIgnoreCase(headerName)
+                                                && 
!"Keep-Alive".equalsIgnoreCase(headerName)
+                                                && 
!HttpHeaders.PROXY_AUTHENTICATE.equalsIgnoreCase(headerName)
+                                                && 
!HttpHeaders.PROXY_AUTHORIZATION.equalsIgnoreCase(headerName)
+                                                && 
!HttpHeaders.TE.equalsIgnoreCase(headerName)
+                                                && 
!HttpHeaders.TRAILER.equalsIgnoreCase(headerName)
+                                                && 
!HttpHeaders.UPGRADE.equalsIgnoreCase(headerName)) {
+                                            headers.add(headerName, 
request.getHeader(headerName));
+                                        }
+                                    });
 
                             // Create the HttpEntity with headers and body
-                            HttpEntity<byte[]> httpEntity = new 
HttpEntity<>(request.getCachedBody(), headers);
                             HttpMethod httpMethod;
                             try {
                                 httpMethod = 
HttpMethod.valueOf(request.getMethod());
                             } catch (IllegalArgumentException ex) {
-                                logger.error("Unsupported HTTP method: {}", 
request.getMethod(), ex);
+                                LOGGER.error("Unsupported HTTP method: {}", 
request.getMethod(), ex);
                                 
response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
                                 return;
                             }
+
+                            // GET/HEAD methods should not have a body; other 
methods may include a body as needed.
+                            HttpEntity<byte[]> httpEntity;
+                            if (HttpMethod.GET.equals(httpMethod) || 
HttpMethod.HEAD.equals(httpMethod)) {
+                                headers.remove(HttpHeaders.CONTENT_LENGTH);
+                                headers.remove(HttpHeaders.TRANSFER_ENCODING);
+                                // headers-only
+                                httpEntity = new HttpEntity<>(headers);
+                            } else {
+                                byte[] body = request.getCachedBody();
+                                if (body == null || body.length == 0) {
+                                    headers.remove(HttpHeaders.CONTENT_LENGTH);
+                                    
headers.remove(HttpHeaders.TRANSFER_ENCODING);
+                                    // headers-only for empty body
+                                    httpEntity = new HttpEntity<>(headers);
+                                } else {

Review Comment:
   For non-GET/HEAD requests with a non-empty body, the original 
`Content-Length` / `Transfer-Encoding` headers are currently preserved if 
present. Since the body is read/cached and then re-sent by `RestTemplate`, 
forwarding a stale or conflicting `Content-Length`/`Transfer-Encoding` can lead 
to upstream/client-library errors. Prefer removing these headers 
unconditionally and letting the HTTP client compute them from the actual body.
   ```suggestion
                                   } else {
                                       // Remove potentially stale 
length/transfer headers and let the client recompute them
                                       
headers.remove(HttpHeaders.CONTENT_LENGTH);
                                       
headers.remove(HttpHeaders.TRANSFER_ENCODING);
   ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to