1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "jsonrpcparser.h" |
6 | #include "basicjsonstructures.h" |
7 | |
8 | #include <QRegularExpression> |
9 | #include <QDebug> |
10 | #include <QJsonDocument> |
11 | #include <QJsonObject> |
12 | #include <QCoreApplication> |
13 | |
14 | #include <iostream> |
15 | |
16 | namespace newlsp { |
17 | |
18 | void JsonRpcParser::doParseReadLine(const QByteArray &line) |
19 | { |
20 | static QVector<bool> formatFlag = {false, false, false, false}; |
21 | static int contentLengthVal{0}; |
22 | static QString contentTypeVal{"" }; |
23 | static QString charsetVal{"" }; |
24 | |
25 | if (qAppName() == "unioncode" ) { |
26 | qInfo() << line; |
27 | } |
28 | |
29 | auto cleanCache = [=]() { |
30 | contentLengthVal = 0; |
31 | contentTypeVal = "" ; |
32 | charsetVal = "" ; |
33 | formatFlag = {false, false, false, false}; |
34 | }; |
35 | |
36 | QRegularExpression regExpContentLength("^Content-Length:\\s?(?<" + newlsp::RK_CONTENT_LENGTH + ">[0-9]+)" ); |
37 | auto match = regExpContentLength.match(line); |
38 | if (!formatFlag[0] && match.hasMatch()) { |
39 | formatFlag[0] = true; |
40 | contentLengthVal = match.captured(newlsp::RK_CONTENT_LENGTH).toInt(); |
41 | return; |
42 | } |
43 | |
44 | QRegularExpression regExpContentType("^Content-Type:\\s?(?<" + newlsp::RK_CONTENT_TYPE + ">\\S+);" + |
45 | "\\s?charset=(?<" + newlsp::RK_CHARSET + ">\\S+)" ); |
46 | match = regExpContentType.match(line); |
47 | if (!formatFlag[1] && formatFlag[0] && match.hasMatch()) { |
48 | formatFlag[1] = true; |
49 | contentTypeVal = match.captured(newlsp::RK_CONTENT_TYPE); |
50 | charsetVal = match.captured(newlsp::RK_CHARSET); |
51 | return; |
52 | } |
53 | |
54 | QRegularExpression regExpSplitter("" ); |
55 | match = regExpSplitter.match(line); |
56 | if (!formatFlag[2] && formatFlag[0] && match.hasMatch()) { |
57 | formatFlag[2] = true; |
58 | return; |
59 | } |
60 | |
61 | if ((formatFlag[0] && formatFlag[2]) |
62 | || (formatFlag[0] && formatFlag[1] && formatFlag[2])) { |
63 | if (contentLengthVal > line.size()) { |
64 | std::cout << "json size error" << " " |
65 | << "ContentLength: " << " " |
66 | << contentLengthVal << " " |
67 | << "Json size:" << " " |
68 | << line.size() << " " |
69 | << QString(line).toStdString() << " " |
70 | << std::endl; |
71 | } else { |
72 | QByteArray other = line.mid(contentLengthVal, line.size()); |
73 | QByteArray body = line.mid(0, contentLengthVal); |
74 | QJsonParseError err; |
75 | QJsonObject jsonObj = QJsonDocument::fromJson(body, &err).object(); |
76 | if (err.error != QJsonParseError::NoError) { |
77 | std::cerr << err.errorString().toStdString() << std::endl; |
78 | } else { |
79 | Q_EMIT readedJsonObject(jsonObj); |
80 | cleanCache(); |
81 | if (!other.isEmpty()) { |
82 | // if (qAppName() == "unioncode") { |
83 | // qInfo() << "do next" << other; |
84 | // } |
85 | return doParseReadLine(other); |
86 | } |
87 | } |
88 | } |
89 | } |
90 | cleanCache(); |
91 | } |
92 | |
93 | } //newlsp |
94 | |