SSE Client: Update Authorization Header

Fixes #2402
This commit is contained in:
yhirose committed 2026-03-21 13:17:28 -04:00
1 parent 45820de332
commit c2bdb1c5c1
3 files changed
+138 -10

No files matched your search

+24 -10
View File
@@ -3660,6 +3660,9 @@ public:
SSEClient &set_reconnect_interval(int ms);
SSEClient &set_max_reconnect_attempts(int n);
// Update headers (thread-safe)
SSEClient &set_headers(const Headers &headers);
// State accessors
bool is_connected() const;
const std::string &last_event_id() const;
@@ -3684,6 +3687,7 @@ private:
Client &client_;
std::string path_;
Headers headers_;
mutable std::mutex headers_mutex_;
// Callbacks
MessageHandler on_message_;
@@ -3992,6 +3996,12 @@ inline SSEClient &SSEClient::set_max_reconnect_attempts(int n) {
return *this;
}
inline SSEClient &SSEClient::set_headers(const Headers &headers) {
std::lock_guard<std::mutex> lock(headers_mutex_);
headers_ = headers;
return *this;
}
inline bool SSEClient::is_connected() const { return connected_.load(); }
inline const std::string &SSEClient::last_event_id() const {
@@ -4070,7 +4080,11 @@ inline void SSEClient::run_event_loop() {
while (running_.load()) {
// Build headers, including Last-Event-ID if we have one
auto request_headers = headers_;
Headers request_headers;
{
std::lock_guard<std::mutex> lock(headers_mutex_);
request_headers = headers_;
}
if (!last_event_id_.empty()) {
request_headers.emplace("Last-Event-ID", last_event_id_);
}
@@ -4089,19 +4103,19 @@ inline void SSEClient::run_event_loop() {
continue;
}
if (result.status() != 200) {
if (result.status() != StatusCode::OK_200) {
connected_.store(false);
// For certain errors, don't reconnect
if (result.status() == 204 || // No Content - server wants us to stop
result.status() == 404 || // Not Found
result.status() == 401 || // Unauthorized
result.status() == 403) { // Forbidden
if (on_error_) { on_error_(Error::Connection); }
if (on_error_) { on_error_(Error::Connection); }
// For certain errors, don't reconnect.
// Note: 401 is intentionally absent so that handlers can refresh
// credentials via set_headers() and let the client reconnect.
if (result.status() == StatusCode::NoContent_204 ||
result.status() == StatusCode::NotFound_404 ||
result.status() == StatusCode::Forbidden_403) {
break;
}
if (on_error_) { on_error_(Error::Connection); }
if (!should_reconnect(reconnect_count)) { break; }
wait_for_reconnect();
reconnect_count++;