|
| 1 | +#include "cpr/sse.h" |
| 2 | + |
| 3 | +#include <charconv> |
| 4 | +#include <utility> |
| 5 | +#include <cstddef> |
| 6 | +#include <functional> |
| 7 | +#include <string> |
| 8 | +#include <string_view> |
| 9 | +#include <system_error> |
| 10 | + |
| 11 | +namespace cpr { |
| 12 | + |
| 13 | +bool ServerSentEventParser::parse(std::string_view data, const std::function<bool(ServerSentEvent&&)>& callback) { |
| 14 | + // Append incoming data to buffer |
| 15 | + buffer_.append(data); |
| 16 | + |
| 17 | + // Process complete lines |
| 18 | + size_t pos = 0; |
| 19 | + while ((pos = buffer_.find('\n')) != std::string::npos) { |
| 20 | + std::string line = buffer_.substr(0, pos); |
| 21 | + buffer_.erase(0, pos + 1); |
| 22 | + |
| 23 | + // Remove trailing \r if present (handles both \n and \r\n) |
| 24 | + if (!line.empty() && line.back() == '\r') { |
| 25 | + line.pop_back(); |
| 26 | + } |
| 27 | + |
| 28 | + if (!processLine(line, callback)) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return true; |
| 34 | +} |
| 35 | + |
| 36 | +void ServerSentEventParser::reset() { |
| 37 | + buffer_.clear(); |
| 38 | + current_event_ = ServerSentEvent(); |
| 39 | +} |
| 40 | + |
| 41 | +bool ServerSentEventParser::processLine(const std::string& line, const std::function<bool(ServerSentEvent&&)>& callback) { |
| 42 | + // Empty line means end of event |
| 43 | + if (line.empty()) { |
| 44 | + return dispatchEvent(callback); |
| 45 | + } |
| 46 | + |
| 47 | + // Lines starting with ':' are comments, ignore them |
| 48 | + if (line[0] == ':') { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + // Find the colon separator |
| 53 | + const size_t colon_pos = line.find(':'); |
| 54 | + |
| 55 | + std::string field; |
| 56 | + std::string value; |
| 57 | + |
| 58 | + if (colon_pos == std::string::npos) { |
| 59 | + // No colon, entire line is the field name |
| 60 | + field = line; |
| 61 | + value = ""; |
| 62 | + } else { |
| 63 | + field = line.substr(0, colon_pos); |
| 64 | + // Skip the colon and optional leading space |
| 65 | + size_t value_start = colon_pos + 1; |
| 66 | + if (value_start < line.size() && line[value_start] == ' ') { |
| 67 | + value_start++; |
| 68 | + } |
| 69 | + value = line.substr(value_start); |
| 70 | + } |
| 71 | + |
| 72 | + // Process the field |
| 73 | + if (field == "event") { |
| 74 | + current_event_.event = value; |
| 75 | + } else if (field == "data") { |
| 76 | + // Multiple data fields are concatenated with newlines |
| 77 | + if (!current_event_.data.empty()) { |
| 78 | + current_event_.data += '\n'; |
| 79 | + } |
| 80 | + current_event_.data += value; |
| 81 | + } else if (field == "id") { |
| 82 | + // Only set id if the value doesn't contain null character |
| 83 | + if (value.find('\0') == std::string::npos) { |
| 84 | + current_event_.id = value; |
| 85 | + } |
| 86 | + } else if (field == "retry") { |
| 87 | + // Parse retry value as integer |
| 88 | + size_t retry_value = 0; |
| 89 | + const std::string_view sv(value); |
| 90 | + auto [ptr, ec] = std::from_chars(sv.begin(), sv.end(), retry_value); |
| 91 | + if (ec == std::errc()) { |
| 92 | + current_event_.retry = retry_value; |
| 93 | + } |
| 94 | + } |
| 95 | + // Unknown fields are ignored per spec |
| 96 | + |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +bool ServerSentEventParser::dispatchEvent(const std::function<bool(ServerSentEvent&&)>& callback) { |
| 101 | + // Don't dispatch if data is empty |
| 102 | + if (current_event_.data.empty()) { |
| 103 | + current_event_ = ServerSentEvent(); |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + // Invoke callback with the current event |
| 108 | + const bool continue_parsing = callback(std::move(current_event_)); |
| 109 | + |
| 110 | + // Reset for next event (but keep event type as "message") |
| 111 | + current_event_ = ServerSentEvent(); |
| 112 | + |
| 113 | + return continue_parsing; |
| 114 | +} |
| 115 | + |
| 116 | +bool ServerSentEventCallback::handleData(std::string_view data) { |
| 117 | + return parser_.parse(data, [this](ServerSentEvent&& event) { return (*this)(std::move(event)); }); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace cpr |
0 commit comments