110 lines
3.6 KiB
C++
110 lines
3.6 KiB
C++
#include "google/stream_decoder.h"
|
|||
|
|
|
||
|
|
#include "google/errors.h"
|
||
|
|
|
||
|
|
namespace molo::google {
|
||
|
|
|
||
|
|
using clients::body::Json;
|
||
|
|
|
||
|
|
StreamDecoder::StreamDecoder(std::string service, Sink sink)
|
||
|
|
: service_(std::move(service)), sink_(std::move(sink)) {}
|
||
|
|
|
||
|
|
bool StreamDecoder::emit(const Json& event) {
|
||
|
|
if (!sink_(event)) stopped_ = true;
|
||
|
|
return !stopped_;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool StreamDecoder::dispatch_sse_block(const std::string& block) {
|
||
|
|
std::string data;
|
||
|
|
std::size_t pos = 0;
|
||
|
|
while (pos <= block.size()) {
|
||
|
|
std::size_t eol = block.find('\n', pos);
|
||
|
|
std::string line = block.substr(pos, eol == std::string::npos ? eol : eol - pos);
|
||
|
|
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||
|
|
if (line.rfind("data:", 0) == 0) {
|
||
|
|
std::string v = line.substr(5);
|
||
|
|
if (!v.empty() && v.front() == ' ') v.erase(0, 1);
|
||
|
|
if (!data.empty()) data += "\n";
|
||
|
|
data += v;
|
||
|
|
}
|
||
|
|
if (eol == std::string::npos) break;
|
||
|
|
pos = eol + 1;
|
||
|
|
}
|
||
|
|
if (data.empty() || data == "[DONE]") return true;
|
||
|
|
Json event;
|
||
|
|
try {
|
||
|
|
event = Json::parse(data);
|
||
|
|
} catch (const std::exception& e) {
|
||
|
|
throw GoogleError(502, "BAD_RESPONSE", std::string("bad stream event: ") + e.what(), service_);
|
||
|
|
}
|
||
|
|
if (const Json* err = event.is_object() ? event.find("error") : nullptr; err)
|
||
|
|
throw GoogleError(502, "STREAM_ERROR", err->dump(), service_);
|
||
|
|
return emit(event);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool StreamDecoder::feed(const std::string& chunk) {
|
||
|
|
if (stopped_) return false;
|
||
|
|
total_ += chunk.size();
|
||
|
|
if (total_ > kMaxBytes)
|
||
|
|
throw GoogleError(502, "BAD_RESPONSE", "stream over the byte cap", service_);
|
||
|
|
buffer_ += chunk;
|
||
|
|
if (!decided_) {
|
||
|
|
std::size_t first = buffer_.find_first_not_of(" \t\r\n");
|
||
|
|
if (first == std::string::npos) return true;
|
||
|
|
sse_ = buffer_[first] != '{' && buffer_[first] != '[';
|
||
|
|
decided_ = true;
|
||
|
|
}
|
||
|
|
if (!sse_) return true; // Plain JSON: parsed whole at finish().
|
||
|
|
for (;;) {
|
||
|
|
std::size_t end = buffer_.find("\n\n");
|
||
|
|
std::size_t sep = 2;
|
||
|
|
std::size_t crlf = buffer_.find("\r\n\r\n");
|
||
|
|
if (crlf != std::string::npos && (end == std::string::npos || crlf < end)) {
|
||
|
|
end = crlf;
|
||
|
|
sep = 4;
|
||
|
|
}
|
||
|
|
if (end == std::string::npos) break;
|
||
|
|
std::string block = buffer_.substr(0, end);
|
||
|
|
buffer_.erase(0, end + sep);
|
||
|
|
if (!dispatch_sse_block(block)) return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void StreamDecoder::finish() {
|
||
|
|
if (stopped_ || !decided_) return;
|
||
|
|
if (sse_) {
|
||
|
|
if (buffer_.find_first_not_of(" \t\r\n") != std::string::npos)
|
||
|
|
dispatch_sse_block(buffer_);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
Json doc;
|
||
|
|
try {
|
||
|
|
doc = Json::parse(buffer_);
|
||
|
|
} catch (const std::exception& e) {
|
||
|
|
throw GoogleError(502, "BAD_RESPONSE", e.what(), service_);
|
||
|
|
}
|
||
|
|
if (doc.is_object()) {
|
||
|
|
if (const Json* err = doc.find("error"))
|
||
|
|
throw GoogleError(err->is_object() && err->find("code") && err->find("code")->is_number()
|
||
|
|
? static_cast<int>(err->find("code")->as_int())
|
||
|
|
: 502,
|
||
|
|
err->is_object() && err->find("status") && err->find("status")->is_string()
|
||
|
|
? err->find("status")->as_string()
|
||
|
|
: "UNKNOWN",
|
||
|
|
err->is_object() && err->find("message") && err->find("message")->is_string()
|
||
|
|
? err->find("message")->as_string()
|
||
|
|
: err->dump(),
|
||
|
|
service_);
|
||
|
|
emit(doc);
|
||
|
|
} else if (doc.is_array()) {
|
||
|
|
for (const Json& item : doc.as_array()) {
|
||
|
|
if (item.is_object() && item.find("error"))
|
||
|
|
throw GoogleError(502, "STREAM_ERROR", item.find("error")->dump(), service_);
|
||
|
|
if (!emit(item)) break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::google
|