Back to all reviewers

Clone network headers carefully

spring-projects/spring-framework
Based on 3 comments
Java

When working with HTTP or WebSocket headers in networking code, be cautious about header manipulation, cloning, and lifecycle management. Native headers in server implementations are often pooled and may be recycled after initial processing.

Networking Java

Reviewer Prompt

When working with HTTP or WebSocket headers in networking code, be cautious about header manipulation, cloning, and lifecycle management. Native headers in server implementations are often pooled and may be recycled after initial processing.

Specific considerations:

  1. Preserve case-insensitivity of HTTP headers when copying them
  2. For WebSocket handshakes, create a deep copy of headers as they may be needed after the original headers are recycled
  3. Avoid adding duplicate headers when reading after headers are written

Example:

// Incorrect - May lose case-insensitivity of header names
this.headers = new HttpHeaders(new LinkedMultiValueMap<>(original.getHeaders()));

// Correct - Create a proper copy that preserves HTTP header behavior
HttpHeaders headers = new HttpHeaders();
headers.addAll(request.getHeaders());

These practices help prevent subtle bugs related to header handling in network communication.

3
Comments Analyzed
Java
Primary Language
Networking
Category

Source Discussions