-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquidlogparser.h
More file actions
1105 lines (941 loc) · 34 KB
/
squidlogparser.h
File metadata and controls
1105 lines (941 loc) · 34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2022 *
* Volnei Cervi Puttini. All rights reserved. *
* vcputtini@gmail.com
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* 4. Neither the name of the Author nor the names of its contributors *
* may be used to endorse or promote products derived from this software*
* without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND *
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR *
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS *
* BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF *
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS*
* INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING *
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OFSUCH DAMAGE. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/*!
* \brief How this file is organized:
*
* Namespace: squidlogparser
*
* Utilities:
* class IPv4Addr: General handling of IPv4 addresses. It supports the basic
* operations needed for IPv4 addresses.
*
* template Visitor: Implements Visitor, a helper function for deducing the type
* of data stored in the variable std::variant.
* ----------------------------------------------------------------------------
* struct SquidLogData
* class DataKey
* class SquidLogParser
* class SLPQuery
* class SLPUrlParts
* class SLPRawToXML
*
* class SLPDatabase (This class is optional, see CMakeLists.txt for details.)
*
*/
#ifndef SQUIDLOGPARSER_H
#define SQUIDLOGPARSER_H
#include <algorithm>
#include <arpa/inet.h> // inet_pton()
#include <array>
#include <cctype>
#include <chrono>
#include <climits> // INT_MAX, LONG_MAX, UINT_MAX, ...
#include <cmath> // std::isless(), std::isgreater(), ...
#include <csignal>
#include <cstddef> // size_t
#include <cstdint>
#include <cstring>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <iomanip> // std::setw()
#include <iostream>
#include <iterator> // std::back_inserter() ...
#include <map>
#include <memory>
#include <numeric> // accumulate
#include <regex>
#include <set>
#include <string>
#include <string_view>
#include <thread>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <variant>
namespace fsys = std::filesystem; // Do Not Change!
/* ------------------------------------------------------------------------- */
#include <tinyxml2.h>
using namespace tinyxml2; // mandatory
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
// Boost:
// Reason: Better performance and analysis of RE.
#include <boost/regex.hpp>
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
// Activate the extension for class SLPDatabase
#if defined(DATABASE_EXTENSION)
#include <mariadb/conncpp.hpp>
#endif
/* ------------------------------------------------------------------------- */
#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || \
defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || \
defined(__WIN32__) || defined(__NT__)
#define Q_DECL_EXPORT __declspec(dllexport)
#define Q_DECL_IMPORT __declspec(dllimport)
#else
#define Q_DECL_EXPORT __attribute__((visibility("default")))
#define Q_DECL_IMPORT __attribute__((visibility("default")))
#endif
#if defined(SquidLogParser_LIBRARY)
#define SquidLogParser_EXPORT Q_DECL_EXPORT
#else
#define SquidLogParser_EXPORT Q_DECL_IMPORT
#endif
namespace squidlogparser {
/* Utilities---------------------------------------------------------------- */
/*!
* \brief General handling of IPv4 addresses. It supports the basic operations
* needed for IPv4 addresses.
*/
class SquidLogParser_EXPORT IPv4Addr
{
public:
explicit IPv4Addr();
explicit IPv4Addr(const std::string addr_);
explicit IPv4Addr(const char* addr_);
inline std::string getAddr() const;
inline uint32_t getInt() const;
static bool isValid(const std::string addr_);
static uint32_t iptol(const std::string addr_);
inline static std::string ltoip(uint32_t addr_);
IPv4Addr& operator=(const IPv4Addr& rhs_);
inline bool operator>(const IPv4Addr& rhs_) const;
inline bool operator<(const IPv4Addr& rhs_) const;
inline bool operator==(const IPv4Addr& rhs_) const;
inline bool operator!=(const IPv4Addr& rhs_) const;
private:
std::string str_;
uint32_t num_;
static void splitP(std::array<std::string, 4>& arr_, const std::string src_);
};
/* ------------------------------------------------------------------------- */
/*!
* \internal
* \brief Implements Visitor, a helper function for deducing the type of data
* stored in the variable std::variant.
* \note Based on: https://en.cppreference.com/w/cpp/utility/variant/visit
*/
template<class... Ts>
struct overloadedP : Ts...
{
using Ts::operator()...;
};
template<class... Ts>
overloadedP(Ts...) -> overloadedP<Ts...>;
struct Visitor
{
public:
enum class TypeVar
{
TInt = 0x00,
TLong,
TUint,
TString
};
using var_t = std::variant<int, long, uint32_t, std::string>;
TypeVar varType(var_t t_) const;
};
/* ------------------------------------------------------------------------- */
/*!
* \brief The SquidLogParserData struct
*
*
* \note Based on: http://www.squid-cache.org/Doc/config/logformat/
*
* The default formats available (which do not need re-defining) are:
*
* \verbatim
* logformat squid %ts.%03tu %6tr %>a %Ss/%03>Hs %<st %rm %ru %[un
* %Sh/%<a %mt
*
* logformat common %>a %[ui %[un [%tl] "%rm %ru HTTP/%rv"
* %>Hs %<st %Ss:%Sh
*
* logformat combined %>a %[ui %[un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st
* "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
*
* logformat referrer %ts.%03tu %>a %{Referer}>h %ru
*
* logformat useragent %>a [%tl] "%{User-Agent}>h"
* \endverbatim
*
*/
struct SquidLogParser_EXPORT SquidLogData
{
/*!
* \brief The LogFormat enum
*
* \warning Do not change the order of the objects defined below.
*/
enum class LogFormat
{
Squid = 0x00,
Common,
Combined,
Referrer,
UserAgent,
Unknown
};
// --------------------------------------------------------------------------
/*!
* \brief The LogFields enum
*
* \warning Do not change the order of the objects defined below.
*/
enum class Fields
{
Timestamp = 0x00,
CliSrcIpAddr, // >a
LocalTime, // tl - Local time
UserName, // un - User name (any available), esle return '-'
UserNameIdent, // ui - User name from ident
ResponseTime, // tr (squid)
ReqMethod, // rm - Request method (GET/POST etc)
ReqURL, // ru - Request URL received (or computed) and sanitized
ReqProtoVersion, // rv - Request protocol version
HttpStatus, // >Hs - HTTP status code sent to the client
ReqStatusHierStatus, // %Ss/%03>Hs e.g: TCP_MISS/200
TotalSizeReply, // <st
HierStatusIpAddress, // %Sh/%<a (squid) ex: ORIGINAL_DST/99.247.57.31
MimeContentType, // mt - MIME content type (squid)
OrigRcvReqHeader, // >h - Original received request header (combined)
Referrer, // %{referrer}
UserAgent, // %{User-Agent}
Unknown
};
/*!
* \internal
* \brief The DataSet_Squid struct
*
* \warning Do not change the order of the objects defined below.
*/
struct DataSet_Squid
{
uint32_t timeStamp = 0;
uint32_t cliSrcIpAddr = 0;
std::string localTime = {};
std::string userName = {};
std::string userNameIdent = {};
int responseTime = 0;
std::string reqMethod = {};
std::string reqURL = {};
std::string reqProtoVersion = {};
int httpStatus = 0;
std::string reqStatusHierStatus = {};
int totalSizeReply = 0;
std::string hierStatusIpAddress = {};
std::string mimeTypeContent = {};
std::string origRcvReqHeader = {};
std::string referrer = {};
std::string userAgent = {};
};
// --------------------------------------------------------------------------
/*!
* \brief Stores unique HTTP codes.
* The first value is the HTTP code, the second will be the count of
* occurrences.
*/
std::map<short, int> HttpCodesUniques_m;
/*!
* \brief FiletypeUniques_m
*/
std::map<std::string, int> FiletypeUniques_m;
/*!
* \note There're several HTTP codes that are considered unofficial. Here
* they will be treated as unknown. To see more details about these codes
* visit: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
*/
const std::map<const short, const std::string_view> HttpCodesText_m = {
// INFORMATIVE RESPONSES
{ 100, "Continue" },
{ 101, "Switching Protocol" },
{ 102, "Processing (WebDAV)" },
{ 103, "Early Hints" },
// SUCCESS_RESPONSES
{ 200, "OK" },
{ 201, "Created" },
{ 202, "Accepted" },
{ 203, "Non-Authoritative Information" },
{ 204, "No Content" },
{ 205, "Reset Content" },
{ 206, "Partial Content" },
{ 207, "Multi-Status (WebDAV)" },
{ 208, "Multi-Status (WebDAV)" },
{ 226, "IM Used (HTTP Delta encoding" },
// REDIRECT MESSAGES
{ 300, "Multiple Choice" },
{ 301, "Moved Permanently" },
{ 302, "Found" },
{ 303, "See Other" },
{ 304, "Not Modified" },
{ 307, "Temporary Redirect" },
{ 308, "Permanent Redirect" },
// CUSTM_ERROR_RESPONSES
{ 400, "Bad Request" },
{ 401, "Unauthorized" },
{ 402, "Payment Required" },
{ 403, "Forbidden" },
{ 405, "Method Not Allowed" },
{ 406, "Not Acceptable" },
{ 404, "Not Found" },
{ 407, "Proxy Authentication Required" },
{ 408, "Request Timeout" },
{ 410, "Gone" },
{ 409, "Conflict" },
{ 411, "Length Required" },
{ 412, "Precondition Failed" },
{ 413, "Payload Too Large" },
{ 414, "URI Too Long" },
{ 416, "Requested Range Not Satisfiable" },
{ 415, "Unsupported Media Type" },
{ 417, "Expectation Failed" },
{ 418, "I'm a teapot" },
{ 421, "Misdirected Request" },
{ 422, "Unprocessable Entity (WebDAV)" },
{ 423, "Locked (WebDAV)" },
{ 424, "Failed Dependency (WebDAV)" },
{ 425, "Too Early" },
{ 426, "Upgrade Required" },
{ 428, "Precondition Required" },
{ 429, "Too Many Requests" },
{ 431, "Request Header Fields Too Large" },
{ 451, "Unavailable For Legal Reasons" },
// SERVER_ERROR_RESPONSES
{ 500, "Internal Server Error" },
{ 501, "Not Implemented" },
{ 502, "Bad Gateway" },
{ 503, "Service Unavailable" },
{ 504, "Gateway Timeout" },
{ 505, "HTTP Version Not Supported" },
{ 506, "Variant Also Negotiates" },
{ 507, "Insufficient Storage" },
{ 508, "Not Extended" },
{ 511, "Network Authentication Required" },
{ 529, "Site is overloaded Unofficial" },
{ 530, "Site is frozen Unofficial" }
};
// --------------------------------------------------------------------------
enum class Compare
{
EQ = 0x00,
LT,
GT,
LE,
GE,
NE,
BTWAND,
BTWOR,
REGEX
};
// --------------------------------------------------------------------------
enum class FormatDB
{
UniqueField = 0x00,
SepFields
};
// --------------------------------------------------------------------------
enum class MethodType
{
MTGet = 0x00,
MTPut,
MTPost,
MTConnect,
MTHead,
MTDelete,
MTOptions,
MTPatch,
MTTrace,
MTOthers
};
/*!
* \brief The Method_t struct: Specifications: RFC 7231 Session 4: Request
* Methods and RFC 5789 Session 2: Path Method.
*/
struct Method_t
{
const std::string_view sv_;
} MethodText_t[10] = { { "GET" }, { "PUT" }, { "POST" },
{ "CONNECT" }, { "HEAD" }, { "DELETE" },
{ "OPTIONS" }, { "PATCH" }, { "TRACE" },
{ "OTHERS" } };
// --------------------------------------------------------------------------
enum class SLPError
{
SLP_SUCCESS = 0x00,
SLP_ERR_PARSER_FAILED,
SLP_ERR_INVALID_TIMESTAMP,
SLP_ERR_INCOMPLETE_NUM_ARGS,
SLP_ERR_INVALID_DATE,
SLP_ERR_INVALID_TIME,
SLP_ERR_INVALID_TS_OR_IP,
SLP_ERR_XML_FILE_NOT_SAVE,
SLP_ERR_XML_FILE_NAME_INCONSISTENT,
SLP_ERR_REGEX_COLLATE,
SLP_ERR_REGEX_CTYPE,
SLP_ERR_REGEX_ESCAPE,
SLP_ERR_REGEX_BACKREF,
SLP_ERR_REGEX_BRACK,
SLP_ERR_REGEX_PAREN,
SLP_ERR_REGEX_BRACE,
SLP_ERR_REGEX_BADBRACE,
SLP_ERR_REGEX_RANGE,
SLP_ERR_REGEX_SPACE,
SLP_ERR_REGEX_BADREPEAT,
SLP_ERR_REGEX_COMPLEXITY,
SLP_ERR_REGEX_STACK,
SLP_ERR_UNKNOWN = 0xff,
};
const std::unordered_map<SLPError, const std::string_view> mError = {
{ SLPError::SLP_SUCCESS, "Success!" },
{ SLPError::SLP_ERR_PARSER_FAILED,
"Parser Error: Probable reasons: badly formatted input." },
{ SLPError::SLP_ERR_INVALID_TIMESTAMP, "Invalid Timestamp." },
{ SLPError::SLP_ERR_INCOMPLETE_NUM_ARGS,
"Incomplete Number of Arguments." },
{ SLPError::SLP_ERR_INVALID_DATE, "Invalid Date: " },
{ SLPError::SLP_ERR_INVALID_TIME, "Invalid Time: " },
{ SLPError::SLP_ERR_INVALID_TS_OR_IP,
"Timestamp and/or IP address is wrong." },
{ SLPError::SLP_ERR_XML_FILE_NOT_SAVE, "File cannnot be save." },
{ SLPError::SLP_ERR_XML_FILE_NAME_INCONSISTENT,
"File name is inconsistent." },
/*! \note Base on: https://en.cppreference.com/w/cpp/regex/error_type
* \note Base on:
* https://www.boost.org/doc/libs/1_34_0/libs/regex/doc/error_type.html
*/
{ SLPError::SLP_ERR_REGEX_COLLATE,
"The expression contains an invalid collating element name." },
{ SLPError::SLP_ERR_REGEX_CTYPE,
"The expression contains an invalid character class name." },
{ SLPError::SLP_ERR_REGEX_ESCAPE,
"The expression contains an invalid escaped character or a trailing "
"escape." },
{ SLPError::SLP_ERR_REGEX_BACKREF,
"The expression contains an invalid back reference." },
{ SLPError::SLP_ERR_REGEX_BRACK,
"The expression contains mismatched square brackets ('[' and ']')." },
{ SLPError::SLP_ERR_REGEX_PAREN,
"The expression contains mismatched parentheses ('(' and ')')." },
{ SLPError::SLP_ERR_REGEX_BRACE, "Mismatched '{' and '}'." },
{ SLPError::SLP_ERR_REGEX_BADBRACE, "Invalid contents of a {...} block." },
{ SLPError::SLP_ERR_REGEX_RANGE,
"The expression contains an invalid character range (e.g. [b-a])." },
{ SLPError::SLP_ERR_REGEX_SPACE,
"There was not enough memory to convert the expression into a finite "
"state machine." },
{ SLPError::SLP_ERR_REGEX_BADREPEAT,
"One of *?+{ was not preceded by a valid regular expression." },
{ SLPError::SLP_ERR_REGEX_COMPLEXITY,
"The complexity of an attempted match exceeded a predefined level." },
{ SLPError::SLP_ERR_REGEX_STACK,
"There was not enough memory to perform a match." },
{ SLPError::SLP_ERR_UNKNOWN, "Unknown Error." }
};
};
/* ------------------------------------------------------------------------- */
/*!
* \brief Object to interact the composite key of the data map.
*/
class SquidLogParser_EXPORT DataKey
{
private:
using ipaddr_t = uint32_t;
using timestamp_t = uint32_t;
std::pair<timestamp_t, ipaddr_t> key_;
public:
explicit DataKey(timestamp_t date_, ipaddr_t ip_) { key_ = { date_, ip_ }; };
const timestamp_t& getTs() const { return key_.first; };
const ipaddr_t& getIp() const { return key_.second; };
/*!
* \internal
* \brief operator<()
* \param rhs_
* \return bool true|false
* \warning Indexing should be by timestamp only in order to keep the same
* data entry order. This is very important!
*/
bool operator<(const DataKey& rhs_) const
{
return key_.first < rhs_.key_.first;
};
};
/* -------------------------------------------------------------------------- */
/*!
* \brief Class used as data structure for the vector that will store the data
* about Http Request Codes. It'll allow simpler access to field values.
*/
class HRCData
{
public:
short getCode() const { return code_; };
std::string getDescription() const { return descr_; };
int getScore() const { return score_; };
private:
explicit HRCData(const short c_, const std::string d_, const int s_)
: code_(std::move(c_))
, descr_(std::move(d_))
, score_(std::move(s_))
{
}
private:
friend class SLPQuery;
short code_ = {};
std::string descr_ = {};
int score_ = {};
};
/*!
* \brief Helper class for the new functions SLPQuery::getFTDetails(),
* SLPQuery::countFiletypes() and SLPQuery::totalFiles()
*/
class FiletypesData
{
public:
std::string getDescription() const { return descr_; };
int getScore() const { return score_; };
private:
explicit FiletypesData(const std::string d_, const int s_)
: descr_(std::move(d_))
, score_(std::move(s_))
{
}
private:
friend class SLPQuery;
std::string descr_ = {};
int score_ = {};
};
/* -------------------------------------------------------------------------- */
/*!
* \brief The SquidLogParser class
*/
class SquidLogParser_EXPORT SquidLogParser
: public SquidLogData
, public Visitor
{
public:
explicit SquidLogParser(LogFormat log_fmt_ = LogFormat::Squid);
SquidLogParser& append(const std::string& raw_log_);
SLPError errorNum() const noexcept;
std::string getErrorText() const;
size_t size() const;
void clear();
int getPartInt(Fields f_) const;
uint32_t getPartUInt(Fields f_) const;
std::string getPartStr(Fields f_) const;
// Convenience functions
uint32_t addrToNumeric(const std::string addr_ = std::string()) const;
std::string numericToAddr(const uint32_t&& ip_ = 0) const;
uint32_t unixTimestamp(const std::string d_ = std::string()) const;
std::string unixToSquidDate(std::time_t uts_) const;
SquidLogParser::SLPError toXML(const std::string&& fn_ = std::string(),
const std::string&& d0_ = std::string(),
const std::string&& ip0_ = std::string(),
const std::string&& d1_ = std::string(),
const std::string&& ip1_ = std::string());
std::string ShowDecodedUrl(const std::string raw_) const;
static constexpr std::string_view invalidText = "@@@"; // Don't change!
// Test only. Will be removed soon.
void printuniq()
{
int i = 0;
for (const auto& a : FiletypeUniques_m /*HttpCodesUniques_m*/) {
std::cout << "( " << i << " ) -> " << a.first << " : " << a.second
<< "\n";
++i;
}
}
protected:
SLPError slpError_ = SLPError::SLP_SUCCESS;
std::multimap<DataKey, DataSet_Squid> mEntry;
template<typename TString = std::string, typename TSize = size_t>
TString toLower(TString s_, TSize sz_ = 0);
std::string strRight(const std::string src_, const char sep_) const;
std::string getFiletype(const std::string& url_) const;
bool isMonth(const std::string&& s_);
int monthToNumber(const std::string&& s_) const;
std::string numberToMonth(const int&& m_) const;
std::tm mkTime(const std::string d_) const;
void setError(SLPError e_);
std::string getErrorRE(boost::regex_error& e_) const;
constexpr int intFields(Fields f_, const DataSet_Squid& d_) const;
constexpr uint32_t uint32Fields(Fields f_, const DataSet_Squid& d_) const;
std::string strFields(Fields f_, const DataSet_Squid& d_) const;
template<typename TVarD, typename TMin, typename TMax, typename TCompare>
bool decision(TVarD&& data_, TMin&& min_, TMax&& max_, TCompare&& cmp_) const;
template<typename TVarS, typename TVarD, typename TCompare>
bool decision(TVarS&& lhs_, TVarD&& rhs_, TCompare&& cmp_) const;
LogFormat getFormat() { return logFmt_; }
private:
friend class SLPDatabase;
private:
LogFormat logFmt_;
std::string rawLog_;
std::string logFileName_;
DataSet_Squid ds_squid_;
static const constexpr char* nmonths_[] = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
/*
* Don't change the regular expressions below, it will make the whole
* program crash!
*/
static constexpr char cp_fmt_squid_date[] =
"^(\\d{2})/([A-Z]{1}[a-z]{2})/(\\d{4}):(\\d{2}):(\\d{2}):(\\d{2}).*$";
static constexpr char cp_id_fmt_squid_[] =
"^(\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (.*)$";
static constexpr char cp_id_fmt_common_[] =
"^(\\S+) (\\S+) (\\S+) \\[(\\S+ \\S+)\\] \\\"(\\S+) (\\S+) (\\S+)\\\" "
"(\\S+) (\\S+) (.*)$";
static constexpr char cp_id_fmt_combined_[] =
"^(\\S+) (\\S+) (\\S+) \\[(.*?)\\] \\\"(\\S+) (\\S+) (\\S+)\\\" (\\S+) "
"(\\S+) \\\"(\\S+)\\\" "
"\\\"(.*?)\\\" (.*)$";
static constexpr char cp_id_fmt_referrer_[] = "^(\\S+) (\\S+) (\\S+) (.*)$";
static constexpr char cp_id_fmt_useragent_[] =
"^(\\S+) \\[(\\S+ \\S+)\\] \\\"(.*?)\\\"";
boost::regex re_id_fmt_squid_;
boost::regex re_id_fmt_common_;
boost::regex re_id_fmt_combined_;
boost::regex re_id_fmt_referrer_;
boost::regex re_id_fmt_useragent_;
SLPError parserSquid();
SLPError parserCommon();
SLPError parserCombined();
SLPError parserReferrer();
SLPError parserUserAgent();
void removeExtraWhiteSpaces(const std::string& input_, std::string& output_);
static void signalHandler(const int signum_);
void printException(const std::exception& e_,
const char* fname_,
const int line_);
};
/* ------------------------------------------------------------------------- */
/*!
* \brief The SLPQuery class
*/
class SquidLogParser_EXPORT SLPQuery : public SquidLogParser
{
public:
explicit SLPQuery(SquidLogParser* obj_);
SLPQuery& select(const std::string&& d0_ = std::string(),
const std::string&& t0_ = std::string(),
const std::string&& d1_ = std::string(),
const std::string&& t1_ = std::string());
void field(Fields fld_, Compare cmp_, Visitor::var_t&& t_);
std::vector<int> getInt(const std::string&& ts_,
const std::string&& ip_,
Fields fld_) const;
std::vector<uint32_t> getUInt(const std::string&& ts_,
const std::string&& ip_,
Fields fld_) const;
std::vector<std::string> getStr(const std::string&& ts_,
const std::string&& ip_,
Fields fld_) const;
long sumTotalSizeReply() const;
long sumResponseTime() const;
struct accReqMethods_t
{
int Get;
int Put;
int Post;
int Connect;
int Head;
int Delete;
int Options;
int Patch;
int Trace;
int Others;
};
accReqMethods_t countByReqMethod() const;
void countByHttpCodes(const short&& code_ = 0);
std::pair<int, std::string> getHRCScore(const short&& code_ = 0);
int countByFiletype(const std::string&& extension_ = std::string());
inline std::string MethodText(MethodType mt_) const;
size_t size() const;
void clear();
// This type can be used by the user to simplify the return expression of the
// getHRCDetails() function
// HttpRequestCodes_V is a user-defined type that represents the std::vector
// that contains the fields: Code, Description and Score.
using HttpRequestCodes_V = std::vector<HRCData>;
HttpRequestCodes_V getHRCDetails();
// Analogous to the above function
using Filetypes_V = std::vector<FiletypesData>;
Filetypes_V getFTDetails();
size_t getIndexByFiletype(const std::string&& extension_) const;
size_t countFiletypes() const;
int totalFiles() const;
protected:
std::multimap<DataKey, DataSet_Squid> mSubset_;
private:
LogFormat logFmt_;
SLPError slpError_;
using var_t = Visitor::var_t;
struct Info_t
{
int flag_;
uint32_t begin_date_;
uint32_t end_date_;
uint32_t begin_ip_;
uint32_t end_ip_;
} info_t;
};
/* SLPUrlParts -------------------------------------------------------------- */
/*!
* \brief Implementation of the URL parser.
*
* It breaks the URL into its main parts as follows:
* SCHEME, DOMAIN, USERNAME, PASSWORD, PATH, QUERY and FRAGMENT.
*
* scheme://<userinfo@>DOMAIN:PORT/path?query#fragment
*
* It was inspired by the built-in PARSE_URL() function of Apache IMPALA.
*/
class SLPUrlParts
{
public:
explicit SLPUrlParts(const std::string rawUrl_);
std::string getScheme() const;
std::string getDomain() const;
std::string getUsername() const;
std::string getPassword() const;
std::string getPath() const;
std::string getQuery() const;
std::string getFragment() const;
private:
std::string raw_url_;
struct UrlAnatomy_t
{
std::string scheme_;
std::string domain_;
std::string username_;
std::string password_;
std::string path_;
std::string query_;
std::string fragment_;
} url_t;
void parseUrl();
bool hasEscape(const std::string text_);
bool getUserInfo(size_t& pos_);
};
/* ------------------------------------------------------------------------- */
/*!
* \internal
* \brief Helper object for XML file creation.
*/
class SquidLogParser_EXPORT SLPRawToXML : public SquidLogParser
{
public:
explicit SLPRawToXML(LogFormat fmt_, size_t count_);
SLPRawToXML& append(const DataSet_Squid& ds_);
SLPError save(const std::string fn_);
SLPError close();
private:
XMLDocument doc;
XMLElement* root;
XMLElement* root_node;
XMLDeclaration* decl;
SLPError slpError_;
LogFormat logFmt_;
DataSet_Squid ds_squid_ = {};
std::string fname_ = {};
size_t cnt_ = 0;
std::string localTime() const;
void writePart();
SLPError normFn(std::string& fn_) const;
};
/* SLPDatabase ------------------------------------------------------------- */
#if defined(DATABASE_EXTENSION)
/*!
* \brief The DBEData struct
*/
struct SquidLogParser_EXPORT DBEData
{
const std::array<std::pair<const std::string, const std::string>, 11>
scm_squid_a = { { { "ID", "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT" },
{ "ID_MONTH", "TINYINT UNSIGNED NOT NULL" },
{ "TIMESTAMP", "INTEGER UNSIGNED" },
{ "CLIENT_SRC_IP", "VARCHAR(40)" },
{ "REQSTATUS_HIERSTATUS", "VARCHAR(50)" },
{ "TOTAL_SIZE_REPLY", "INTEGER" },
{ "REQ_METHOD", "CHAR(10)" },
{ "URL", "BLOB" },
{ "USER_NAME", "VARCHAR(30)" },
{ "HIERSTATUS_IPADDRESS", "VARCHAR(30)" },
{ "MIMETYPE", "VARCHAR(100)" } } };
const std::array<std::pair<const std::string, const std::string>, 12>
scm_common_a = { { { "ID", "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT" },
{ "ID_MONTH", "TINYINT UNSIGNED NOT NULL" },
{ "CLIENT_SRC_IP", "VARCHAR(40)" },
{ "USER_NAME_FROM_IDENT", "VARCHAR(30)" },
{ "USER_NAME", "VARCHAR(30)" },
{ "LOCAL_TIME", "VARCHAR(50)" },
{ "REQ_METHOD", "CHAR(10)" },
{ "URL", "BLOB" },
{ "REQ_PROTO_VERSION", "VARCHAR(50)" },
{ "HTTP_STATUS", "INTEGER" },
{ "TOTAL_SIZE_REPLY", "INTEGER" },
{ "REQSTATUS_HIERSTATUS", "VARCHAR(50)" } } };
const std::array<std::pair<const std::string, const std::string>, 14>
scm_combined_a = { { { "ID", "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT" },
{ "ID_MONTH", "TINYINT UNSIGNED NOT NULL" },
{ "CLIENT_SRC_IP", "VARCHAR(40)" },
{ "USER_NAME_FROM_IDENT", "VARCHAR(30)" },
{ "USER_NAME", "VARCHAR(30)" },
{ "LOCAL_TIME", "VARCHAR(50)" },
{ "REQ_METHOD", "CHAR(10)" },
{ "URL", "BLOB" },
{ "REQ_PROTO_VERSION", "VARCHAR(50)" },
{ "HTTP_STATUS", "INTEGER" },
{ "TOTAL_SIZE_REPLY", "INTEGER" },
{ "REFERRER", "TEXT" },
{ "USER_AGENT", "TEXT" },
{ "HIERSTATUS_IPADDRESS", "VARCHAR(30)" } } };
const std::array<std::pair<const std::string, const std::string>, 6>
scm_ref_a = { { { "ID", "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT" },
{ "ID_MONTH", "TINYINT UNSIGNED NOT NULL" },
{ "TIMESTAMP", "INTEGER UNSIGNED" },
{ "CLIENT_SRC_IP", "VARCHAR(40)" },
{ "REFERRER", "TEXT" },
{ "URL", "BLOB" } } };
const std::array<std::pair<const std::string, const std::string>, 5>
scm_uagent_a = { { { "ID", "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT" },
{ "ID_MONTH", "TINYINT UNSIGNED NOT NULL" },
{ "CLIENT_SRC_IP", "VARCHAR(40)" },
{ "LOCAL_TIME", "VARCHAR(50)" },
{ "USER_AGENT", "TEXT" } }
};
enum class DBError
{
DBE_SUCCESS = 0x00,
DBE_ERR_LOGFORMAT,
DBE_ERR_INVALID_DBNAME,
DBE_ERR_INVALID_HNAME,
DBE_ERR_INVALID_UNAME,
DBE_ERR_INVALID_UPASS,
DBE_ERR_INVLAID_CONNECTION,
DBE_ERR_FILE_NOTOPEN,
DBE_ERR_FILE_NOTFOUND,
DBE_ERR_FILE_NOTREGULAR,
DBE_UNKNOWN
};
const std::unordered_map<DBError, const std::string_view> mDBError = {
{ DBError::DBE_SUCCESS, "Success!" },