* @brief Handles a buffered message when no newline delimiter is present. */
| 1010 | * @brief Handles a buffered message when no newline delimiter is present. |
| 1011 | */ |
| 1012 | void API::Server::processNoNewlineBuffer(QTcpSocket* socket, ConnectionState& state) |
| 1013 | { |
| 1014 | Q_ASSERT(socket); |
| 1015 | |
| 1016 | auto& buffer = state.buffer; |
| 1017 | const auto trimmed = buffer.trimmed(); |
| 1018 | |
| 1019 | const char firstChar = trimmed.isEmpty() ? '\0' : trimmed.at(0); |
| 1020 | if (firstChar == '{' || firstChar == '[') { |
| 1021 | processBufferedJson(socket, state, trimmed); |
| 1022 | return; |
| 1023 | } |
| 1024 | |
| 1025 | if (buffer.size() > kMaxApiRawBytes) { |
| 1026 | qWarning() << "[API] Raw buffer size limit exceeded:" << state.peerAddress << ":" |
| 1027 | << state.peerPort << "- Buffer size:" << buffer.size() |
| 1028 | << "- Limit:" << kMaxApiRawBytes << "- Disconnecting client"; |
| 1029 | |
| 1030 | disconnectClient( |
| 1031 | socket, state, ErrorCode::ExecutionError, QStringLiteral("Raw payload exceeds size limit")); |
| 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | if (!authorizeDeviceWrite()) { |
| 1036 | buffer.clear(); |
| 1037 | return; |
| 1038 | } |
| 1039 | |
| 1040 | const qint64 written = IO::ConnectionManager::instance().writeData(buffer); |
| 1041 | if (written < 0) [[unlikely]] |
| 1042 | qWarning() << "[API] writeData() failed for raw buffer" |
| 1043 | << "-- data not sent to device"; |
| 1044 | |
| 1045 | buffer.clear(); |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * @brief Attempts to parse buffered data as a complete JSON message. |