Implementation:ClickHouse ClickHouse Poco HTTPResponse
| Knowledge Sources | |
|---|---|
| Domains | Networking, HTTP |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Encapsulation of HTTP response messages with status codes, reason phrases, headers, and cookie management.
Description
The Poco `HTTPResponse` class represents an HTTP response message extending `HTTPMessage` to add response-specific properties: status code and reason phrase. It provides comprehensive support for HTTP status codes (100-511), methods to manage response headers (including `Date` and `Set-Cookie`), and cookie extraction.
The class defines enumerations for all standard HTTP status codes from informational (100s) through server errors (500s), with corresponding reason phrases. It supports reading responses from input streams and writing them to output streams, making it suitable for both client and server implementations.
Usage
ClickHouse uses this vendored component to handle HTTP responses when acting as an HTTP client or processing responses in HTTP-related functionality. It's used in conjunction with `HTTPClientSession` to parse and interpret server responses.
Code Reference
Source Location
- Repository: ClickHouse
- File: base/poco/Net/include/Poco/Net/HTTPResponse.h
- Lines: 1-309
Signature
class HTTPResponse : public HTTPMessage {
public:
enum HTTPStatus {
HTTP_CONTINUE = 100,
HTTP_SWITCHING_PROTOCOLS = 101,
HTTP_OK = 200,
HTTP_CREATED = 201,
HTTP_NO_CONTENT = 204,
HTTP_MOVED_PERMANENTLY = 301,
HTTP_FOUND = 302,
HTTP_NOT_MODIFIED = 304,
HTTP_BAD_REQUEST = 400,
HTTP_UNAUTHORIZED = 401,
HTTP_FORBIDDEN = 403,
HTTP_NOT_FOUND = 404,
HTTP_INTERNAL_SERVER_ERROR = 500,
HTTP_NOT_IMPLEMENTED = 501,
HTTP_SERVICE_UNAVAILABLE = 503
// ... and many more status codes
};
HTTPResponse();
HTTPResponse(HTTPStatus status, const std::string& reason);
HTTPResponse(const std::string& version, HTTPStatus status, const std::string& reason);
HTTPResponse(HTTPStatus status);
void setStatus(HTTPStatus status);
HTTPStatus getStatus() const;
void setReason(const std::string& reason);
const std::string& getReason() const;
void setStatusAndReason(HTTPStatus status, const std::string& reason);
void setStatusAndReason(HTTPStatus status);
void setDate(const Poco::Timestamp& dateTime);
Poco::Timestamp getDate() const;
void addCookie(const HTTPCookie& cookie);
void getCookies(std::vector<HTTPCookie>& cookies) const;
void write(std::ostream& ostr) const;
void read(std::istream& istr);
static const std::string& getReasonForStatus(HTTPStatus status);
};
Import
#include <Poco/Net/HTTPResponse.h>
I/O Contract
| Input | Output |
|---|---|
| Status code and optional reason phrase | Configured HTTP response object |
| Input stream with HTTP response | Parsed response with status, headers, cookies |
| Response object with headers | HTTP response text written to output stream |
HTTP Status Code Ranges
| Range | Category | Examples |
|---|---|---|
| 100-199 | Informational | 100 Continue, 101 Switching Protocols |
| 200-299 | Success | 200 OK, 201 Created, 204 No Content |
| 300-399 | Redirection | 301 Moved Permanently, 302 Found, 304 Not Modified |
| 400-499 | Client Error | 400 Bad Request, 401 Unauthorized, 404 Not Found |
| 500-599 | Server Error | 500 Internal Server Error, 503 Service Unavailable |
Usage Examples
// Client-side: receiving response
HTTPClientSession session("www.example.com");
HTTPRequest request(HTTPRequest::HTTP_GET, "/");
session.sendRequest(request);
HTTPResponse response;
std::istream& rs = session.receiveResponse(response);
std::cout << "Status: " << response.getStatus() << std::endl;
std::cout << "Reason: " << response.getReason() << std::endl;
std::cout << "Content-Type: " << response.getContentType() << std::endl;
if (response.getStatus() == HTTPResponse::HTTP_OK) {
// Read response body
std::string body;
Poco::StreamCopier::copyToString(rs, body);
}
// Extract cookies from response
std::vector<HTTPCookie> cookies;
response.getCookies(cookies);
for (const auto& cookie : cookies) {
std::cout << "Cookie: " << cookie.getName()
<< "=" << cookie.getValue() << std::endl;
}
// Server-side: creating response
HTTPResponse serverResponse(HTTPResponse::HTTP_OK);
serverResponse.setContentType("application/json");
serverResponse.setDate(Poco::Timestamp());
HTTPCookie cookie("session_id", "abc123");
cookie.setPath("/");
cookie.setMaxAge(3600);
serverResponse.addCookie(cookie);
std::ostringstream oss;
serverResponse.write(oss);
// Check status category
if (response.getStatus() >= 400) {
std::cerr << "Error response: "
<< HTTPResponse::getReasonForStatus(response.getStatus())
<< std::endl;
}