service/NtfyStream.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "NtfyStream.h" | ||
| 2 | |||
| 3 | #include <QJsonDocument> | ||
| 4 | #include <QJsonObject> | ||
| 5 | |||
| 6 | namespace NtfyStream { | ||
| 7 | |||
| 8 | 13 | Lines feed(QByteArray *buffer, const QByteArray &chunk, int maxFrameBytes, int maxBufferBytes) { | |
| 9 | 13 | Lines lines; | |
| 10 |
1/1✓ Branch 2 → 3 taken 13 times.
|
13 | *buffer += chunk; |
| 11 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 13 times.
|
13 | if (buffer->size() > maxBufferBytes) { |
| 12 | ✗ | lines.overflow = true; | |
| 13 | ✗ | return lines; | |
| 14 | } | ||
| 15 | 13 | qsizetype newline = -1; | |
| 16 |
2/2✓ Branch 24 → 7 taken 16 times.
✓ Branch 24 → 25 taken 13 times.
|
29 | while ((newline = buffer->indexOf('\n')) >= 0) { |
| 17 |
2/2✓ Branch 7 → 8 taken 16 times.
✓ Branch 8 → 9 taken 16 times.
|
16 | const QByteArray line = buffer->left(newline).trimmed(); |
| 18 |
1/1✓ Branch 10 → 11 taken 16 times.
|
16 | buffer->remove(0, newline + 1); |
| 19 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 16 times.
|
16 | if (line.size() > maxFrameBytes) { |
| 20 | ✗ | ++lines.oversized; | |
| 21 | ✗ | continue; | |
| 22 | } | ||
| 23 |
1/2✓ Branch 15 → 16 taken 16 times.
✗ Branch 15 → 17 not taken.
|
16 | if (!line.isEmpty()) |
| 24 |
1/1✓ Branch 16 → 17 taken 16 times.
|
16 | lines.complete.append(line); |
| 25 | 16 | } | |
| 26 |
2/2✓ Branch 26 → 27 taken 1 time.
✓ Branch 26 → 28 taken 12 times.
|
13 | if (buffer->size() > maxFrameBytes) |
| 27 | 1 | lines.overflow = true; | |
| 28 | 13 | return lines; | |
| 29 | ✗ | } | |
| 30 | |||
| 31 | 16 | Event decode(const QByteArray &line) { | |
| 32 | 16 | Event event; | |
| 33 | 16 | QJsonParseError parseError; | |
| 34 |
1/1✓ Branch 2 → 3 taken 16 times.
|
16 | const QJsonDocument document = QJsonDocument::fromJson(line, &parseError); |
| 35 |
3/3✓ Branch 3 → 4 taken 16 times.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 9 taken 15 times.
|
16 | if (!document.isObject()) { |
| 36 |
1/1✓ Branch 5 → 6 taken 1 time.
|
1 | event.error = parseError.errorString(); |
| 37 | 1 | return event; | |
| 38 | } | ||
| 39 | 15 | event.valid = true; | |
| 40 |
1/1✓ Branch 9 → 10 taken 15 times.
|
15 | const QJsonObject object = document.object(); |
| 41 |
2/2✓ Branch 13 → 14 taken 15 times.
✓ Branch 14 → 15 taken 15 times.
|
15 | event.kind = object.value(QStringLiteral("event")).toString(); |
| 42 |
2/2✓ Branch 22 → 23 taken 10 times.
✓ Branch 22 → 24 taken 5 times.
|
15 | if (event.kind != QLatin1String("message")) |
| 43 | 10 | return event; | |
| 44 |
2/2✓ Branch 27 → 28 taken 5 times.
✓ Branch 28 → 29 taken 5 times.
|
5 | event.id = object.value(QStringLiteral("id")).toString(); |
| 45 |
3/3✓ Branch 37 → 38 taken 5 times.
✓ Branch 38 → 39 taken 5 times.
✓ Branch 39 → 40 taken 5 times.
|
5 | const QByteArray text = object.value(QStringLiteral("message")).toString().toUtf8(); |
| 46 |
4/4✓ Branch 48 → 49 taken 5 times.
✓ Branch 49 → 50 taken 5 times.
✓ Branch 55 → 56 taken 4 times.
✓ Branch 55 → 64 taken 1 time.
|
10 | if (object.value(QStringLiteral("encoding")).toString() == QLatin1String("base64")) { |
| 47 |
1/1✓ Branch 57 → 58 taken 4 times.
|
4 | const auto decoded = QByteArray::fromBase64Encoding(text, QByteArray::AbortOnBase64DecodingErrors); |
| 48 | 4 | event.payloadValid = static_cast<bool>(decoded); | |
| 49 |
1/2✓ Branch 60 → 61 taken 4 times.
✗ Branch 60 → 62 not taken.
|
4 | if (decoded) |
| 50 | 4 | event.payload = decoded.decoded; | |
| 51 | 4 | } else { | |
| 52 | 1 | event.payload = text; | |
| 53 | } | ||
| 54 | 5 | return event; | |
| 55 | 16 | } | |
| 56 | |||
| 57 | } // namespace NtfyStream | ||
| 58 |