File indexing completed on 2024-05-19 05:49:14

0001 /*
0002     SPDX-FileCopyrightText: 2007 Nicolas Ternisien <nicolas.ternisien@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "parsingHelper.h"
0008 
0009 #include <KFormat>
0010 #include <KLocalizedString>
0011 #include <QRegExp>
0012 
0013 #include "ksystemlog_debug.h"
0014 
0015 ParsingHelper *ParsingHelper::self = nullptr;
0016 
0017 ParsingHelper *ParsingHelper::instance()
0018 {
0019     if (!ParsingHelper::self) {
0020         ParsingHelper::self = new ParsingHelper();
0021     }
0022 
0023     return ParsingHelper::self;
0024 }
0025 
0026 ParsingHelper::ParsingHelper()
0027 {
0028     // Initialize Existing months
0029     mMapMonths[QStringLiteral("Jan")] = 1;
0030     mMapMonths[QStringLiteral("Feb")] = 2;
0031     mMapMonths[QStringLiteral("Mar")] = 3;
0032     mMapMonths[QStringLiteral("Apr")] = 4;
0033     mMapMonths[QStringLiteral("May")] = 5;
0034     mMapMonths[QStringLiteral("Jun")] = 6;
0035     mMapMonths[QStringLiteral("Jul")] = 7;
0036     mMapMonths[QStringLiteral("Aug")] = 8;
0037     mMapMonths[QStringLiteral("Sep")] = 9;
0038     mMapMonths[QStringLiteral("Oct")] = 10;
0039     mMapMonths[QStringLiteral("Nov")] = 11;
0040     mMapMonths[QStringLiteral("Dec")] = 12;
0041 
0042     // Initialize HTTP Responses
0043     // 1xx Responses
0044     mMapHTTPResponse[QStringLiteral("100")] = QStringLiteral("Continue");
0045     mMapHTTPResponse[QStringLiteral("101")] = QStringLiteral("Switching Protocols");
0046 
0047     // 2xx Responses
0048     mMapHTTPResponse[QStringLiteral("200")] = QStringLiteral("OK");
0049     mMapHTTPResponse[QStringLiteral("201")] = QStringLiteral("Created");
0050     mMapHTTPResponse[QStringLiteral("202")] = QStringLiteral("Accepted");
0051     mMapHTTPResponse[QStringLiteral("203")] = QStringLiteral("Non-Authoritative Information");
0052     mMapHTTPResponse[QStringLiteral("204")] = QStringLiteral("No Content");
0053     mMapHTTPResponse[QStringLiteral("205")] = QStringLiteral("Reset Content");
0054     mMapHTTPResponse[QStringLiteral("206")] = QStringLiteral("Partial Content");
0055 
0056     // 3xx Responses
0057     mMapHTTPResponse[QStringLiteral("300")] = QStringLiteral("OK");
0058     mMapHTTPResponse[QStringLiteral("301")] = QStringLiteral("Moved Permanently");
0059     mMapHTTPResponse[QStringLiteral("302")] = QStringLiteral("Found");
0060     mMapHTTPResponse[QStringLiteral("303")] = QStringLiteral("See Other");
0061     mMapHTTPResponse[QStringLiteral("304")] = QStringLiteral("Not Modified");
0062     mMapHTTPResponse[QStringLiteral("305")] = QStringLiteral("Use Proxy");
0063     mMapHTTPResponse[QStringLiteral("306")] = QStringLiteral("(Unused)");
0064     mMapHTTPResponse[QStringLiteral("307")] = QStringLiteral("Temporary Redirect");
0065 
0066     // 4xx Responses
0067     mMapHTTPResponse[QStringLiteral("400")] = QStringLiteral("Bad Request");
0068     mMapHTTPResponse[QStringLiteral("401")] = QStringLiteral("Unauthorized");
0069     mMapHTTPResponse[QStringLiteral("402")] = QStringLiteral("Payment Required");
0070     mMapHTTPResponse[QStringLiteral("403")] = QStringLiteral("Forbidden");
0071     mMapHTTPResponse[QStringLiteral("404")] = QStringLiteral("Not Found");
0072     mMapHTTPResponse[QStringLiteral("405")] = QStringLiteral("Method Not Allowed");
0073     mMapHTTPResponse[QStringLiteral("406")] = QStringLiteral("Not Acceptable");
0074     mMapHTTPResponse[QStringLiteral("407")] = QStringLiteral("Proxy Authentication Required");
0075     mMapHTTPResponse[QStringLiteral("408")] = QStringLiteral("Request Timeout");
0076     mMapHTTPResponse[QStringLiteral("409")] = QStringLiteral("Conflict");
0077     mMapHTTPResponse[QStringLiteral("410")] = QStringLiteral("Gone");
0078     mMapHTTPResponse[QStringLiteral("411")] = QStringLiteral("Length Required");
0079     mMapHTTPResponse[QStringLiteral("412")] = QStringLiteral("Precondition Failed");
0080     mMapHTTPResponse[QStringLiteral("413")] = QStringLiteral("Request Entity Too Large");
0081     mMapHTTPResponse[QStringLiteral("414")] = QStringLiteral("Request-URI Too Long");
0082     mMapHTTPResponse[QStringLiteral("415")] = QStringLiteral("Unsupported Media Type");
0083     mMapHTTPResponse[QStringLiteral("416")] = QStringLiteral("Requested Range Not Satisfiable");
0084     mMapHTTPResponse[QStringLiteral("417")] = QStringLiteral("Expectation Failed");
0085 
0086     // 5xx Responses
0087     mMapHTTPResponse[QStringLiteral("500")] = QStringLiteral("Internal Server Error");
0088     mMapHTTPResponse[QStringLiteral("501")] = QStringLiteral("Not Implemented");
0089     mMapHTTPResponse[QStringLiteral("502")] = QStringLiteral("Bad Gateway");
0090     mMapHTTPResponse[QStringLiteral("503")] = QStringLiteral("Service Unavailable");
0091     mMapHTTPResponse[QStringLiteral("504")] = QStringLiteral("Gateway Timeout");
0092     mMapHTTPResponse[QStringLiteral("505")] = QStringLiteral("HTTP Version Not Supported");
0093 }
0094 
0095 ParsingHelper::~ParsingHelper()
0096 {
0097 }
0098 
0099 QDateTime ParsingHelper::parseHttpDateTime(const QString &logLine)
0100 {
0101     // Format example : 22/May/2005:01:50:34 +0200
0102 
0103     const QString day = logLine.mid(0, 2);
0104     const QString month = logLine.mid(3, 3);
0105     const QString year = logLine.mid(7, 4);
0106 
0107     const QString hour = logLine.mid(12, 2);
0108     const QString min = logLine.mid(15, 2);
0109     const QString sec = logLine.mid(18, 2);
0110 
0111     // QString zone=logLine.mid(22,5);
0112 
0113     return QDateTime(QDate(year.toInt(), parseSyslogMonth(month), day.toInt()), QTime(hour.toInt(), min.toInt(), sec.toInt()));
0114 }
0115 
0116 /**
0117  * Example : "Oct  1 09:11:45 2005"
0118  */
0119 QDateTime ParsingHelper::parseSyslogDateTime(const QString &dateTime)
0120 {
0121     // TODO Create this regexp in constructor
0122     const static QRegExp regex(QLatin1String(R"((\S*)[ ]+(\d*) (\d*):(\d*):(\d*) (\d*))"));
0123 
0124     const int firstPosition = regex.indexIn(dateTime);
0125     if (firstPosition == -1) {
0126         qCDebug(KSYSTEMLOG) << "Unable to parse date " << dateTime;
0127         return QDateTime::currentDateTime();
0128     }
0129 
0130     return QDateTime(QDate(regex.cap(6).toInt(), parseSyslogMonth(regex.cap(1)), regex.cap(2).toInt()),
0131                      QTime(regex.cap(3).toInt(), regex.cap(4).toInt(), regex.cap(5).toInt(), 0));
0132 }
0133 
0134 QString ParsingHelper::syslogDateTimeRegexp() const
0135 {
0136     return QStringLiteral("(\\S*[ ]+\\d* \\d*:\\d*:\\d* \\d*)");
0137 }
0138 
0139 int ParsingHelper::parseSyslogMonth(const QString &string)
0140 {
0141     return mMapMonths.value(string, 1);
0142 }
0143 
0144 QString ParsingHelper::parseSize(const QString &stringSize)
0145 {
0146     const qint64 size = stringSize.toLongLong();
0147 
0148     return KFormat().formatByteSize(size);
0149 }
0150 
0151 QString ParsingHelper::parseHttpResponse(const QString &response)
0152 {
0153     // Search the response string in the map
0154     QMap<QString, QString>::Iterator const it = mMapHTTPResponse.find(response);
0155     if (it != mMapHTTPResponse.end()) {
0156         return i18nc("HttpResponseNumber HttpResponseDescription", "%1 %2", response, *it);
0157     } else {
0158         return response;
0159     }
0160 }
0161 
0162 QString ParsingHelper::parseAgent(const QString &agent)
0163 {
0164     return agent;
0165 }