<!--
title: Avoid duplicate HTTP headers
domain: data-systems
topic: Networking
language: C++
source: ClickHouse/ClickHouse
updated: 2025-08-12
url: https://awesomereviewers.com/reviewers/clickhouse-avoid-duplicate-http-headers/
-->

Ensure HTTP headers are sent only once per response to prevent network protocol violations and connection errors. Sending headers multiple times violates HTTP protocol standards and can cause "Broken pipe" errors and connection failures.

When working with HTTP responses, be careful not to call methods that send headers (like `response.send()`) before the final response is written. The framework typically handles header transmission automatically during the final write operation.

Example of problematic code:
```cpp
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NO_CONTENT);
response.setChunkedTransferEncoding(false);
response.send(); // ❌ Sends headers prematurely
// ... later in code ...
// Headers sent again automatically - causes protocol violation
```

Example of correct code:
```cpp
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NO_CONTENT);
response.setChunkedTransferEncoding(false);
// ✅ Let the framework send headers during final write operation
```

This applies to all HTTP server implementations and is critical for maintaining stable network connections, especially under high load or in distributed environments where connection reliability is essential.
