Repository: envoyproxy/envoy
File: source/common/http/sse/sse_parser.cc (C++)

Code before:
      } else {
        // Per SSE spec, multiple data fields are concatenated with newlines.
        parsed_event.data->append("\n");
      }
      parsed_event.data->append(field_value.data(), field_value.size());
    } else if (field_name == "id") {
      // Per SSE spec, if the field value contains U+0000 NULL, the field is ignored.
      // Otherwise, set the last event ID to the field value. If multiple id fields exist,
      // the last one wins.
      if (field_value.find('\0') == absl::string_view::npos) {
        parsed_event.id = std::string(field_value);
      }
    } else if (field_name == "event") {
      // Per SSE spec, the event field sets the event type. If multiple event fields exist,
      // the last one wins.
      parsed_event.event_type = std::string(field_value);
    } else if (field_name == "retry") {
      // Per SSE spec, the retry field must consist of only ASCII digits.
      // If it contains any other character, the field is ignored.
      bool valid = true;
      for (char c : field_value) {
        if (c < '0' || c > '9') {
          valid = false;
          break;
        }
      }
      if (valid && !field_value.empty()) {
        uint64_t value = 0;
        for (char c : field_value) {
          value = value * 10 + static_cast<uint64_t>(c - '0');
          // Prevent overflow: cap at uint32 max
          if (value > std::numeric_limits<uint32_t>::max()) {
            value = std::numeric_limits<uint32_t>::max();
            break;
          }
        }
        parsed_event.retry = static_cast<uint32_t>(value);
      }
    }
  }

  return parsed_event;
}

std::tuple<size_t, size_t, size_t> SseParser::findEventEnd(absl::string_view buffer,
                                                           bool end_stream) {
  size_t consumed = 0;
  size_t event_start = 0;
  absl::string_view remaining = buffer;

  // Per SSE spec: Strip UTF-8 BOM (0xEF 0xBB 0xBF) if present at stream start.

Review comment (refactor):
leverage absl/std utility lib here?

```
bool valid = !field_value.empty() && 
                     std::all_of(field_value.begin(), field_value.end(), absl::ascii_isdigit);
```

Code after:
        parsed_event.data->reserve(event.size());
      } else {
        // Per SSE spec, multiple data fields are concatenated with newlines.
        parsed_event.data->append("\n");
      }
      parsed_event.data->append(field_value.data(), field_value.size());
    } else if (field_name == "id") {
      // Per SSE spec, if the field value contains U+0000 NULL, the field is ignored.
      // Otherwise, set the last event ID to the field value. If multiple id fields exist,
      // the last one wins.
      if (field_value.find('\0') == absl::string_view::npos) {
        parsed_event.id = std::string(field_value);
      }
    } else if (field_name == "event") {
      // Per SSE spec, the event field sets the event type. If multiple event fields exist,
      // the last one wins.
      parsed_event.event_type = std::string(field_value);
    } else if (field_name == "retry") {
      // Per SSE spec, the retry field must consist of only ASCII digits.
      // If it contains any other character, the field is ignored.
      if (!field_value.empty()) {
        uint64_t value = 0;
        bool valid = true;
        for (char c : field_value) {
          if (!absl::ascii_isdigit(c)) {
            valid = false;
            break;
          }
          uint64_t new_value = value * 10 + static_cast<uint64_t>(c - '0');
          if (new_value < value) {
            valid = false;
            break;
          }
          value = new_value;
        }
        if (valid) {
          parsed_event.retry = value;
        }
      }
    }
  }

  return parsed_event;
}

std::tuple<size_t, size_t, size_t> SseParser::findEventEnd(absl::string_view buffer,
                                                           bool end_stream) {
  size_t consumed = 0;
  size_t event_start = 0;
  absl::string_view remaining = buffer;