Skip to content

Commit 5151207

Browse files
committed
Align fields boundary and content
1 parent 87f92be commit 5151207

1 file changed

Lines changed: 27 additions & 37 deletions

File tree

src/msclogparser.c

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This file is part of the libmsclogparser project.
33
4-
Copyright (c) 2023 Digitalwave
4+
Copyright (c) 2023-2026 Digitalwave
55
66
Authors: Ervin Hegedüs <airween@digitalwave.hu>
77
@@ -570,16 +570,11 @@ static void parse_tail(loglinetype linetype, char *line, size_t *startpos, logda
570570
set_error(l, "[hostname] field is truncated!", *startpos, l->log_entry_raw_length);
571571
return;
572572
}
573-
// 'hostname' is relevant only if type of log is Apache
574-
// Nginx [hostname ""] contains the IP address, therefore we
575-
// have to process that later - see end of this function
576-
if (linetype == LOG_TYPE_APACHE) {
577-
tbuff[t] = '\0';
578-
l->log_hostname = mscl_stradd(l, tbuff, t);
579-
*startpos = k;
580-
if (tailstart != NULL) {
581-
*tailstart = matches[0];
582-
}
573+
tbuff[t] = '\0';
574+
l->log_hostname = mscl_stradd(l, tbuff, t);
575+
*startpos = k;
576+
if (tailstart != NULL) {
577+
*tailstart = matches[0];
583578
}
584579
}
585580
else {
@@ -639,7 +634,8 @@ static void parse_tail(loglinetype linetype, char *line, size_t *startpos, logda
639634

640635
// Nginx [hostname ""] filed contains the server IP address, we have to process
641636
// the last 'host: "..."' field
642-
if (linetype == LOG_TYPE_NGINX) {
637+
// enter this block only if hostname is empty, eg. log is chunked
638+
if (linetype == LOG_TYPE_NGINX && strlen(l->log_hostname) == 0) {
643639
memset(&matches, '\0', sizeof(size_t)*2);
644640
mcnt = find_pattern(line, hostlast, l->log_entry_raw_length, "host: \"", 7, &matches, MATCH_FIRST);
645641
if (mcnt > 0) {
@@ -1042,15 +1038,15 @@ int parse (char * line, size_t len, loglinetype t, logdata * l) {
10421038
l->log_unique_id = mscl_stradd(l, "", 0);
10431039

10441040
if (t == LOG_TYPE_APACHE) {
1045-
// [Thu Sep 22 14:51:12.636955 2022] [:error] [pid 19765:tid 139903325140736] [client 165.232.134.42:52179] [client 165.232.134.42]
1046-
// [Thu Sep 22 14:51:12.636955 2022] [:info] [pid 1:tid 1] [client 1.2.3.4:1] [client 1.2.3.4]
1041+
// [Thu Sep 22 14:51:12.636955 2022] [security2:error] [pid 19765:tid 139903325140736] [client 165.232.134.42:52179]
1042+
// [Thu Sep 22 14:51:12.636955 2022] [security2:info] [pid 1:tid 1] [client 1.2.3.4:1]
10471043
// | | | | | | | | | | | | |
10481044
offset = 90;
10491045
}
10501046
else if (t == LOG_TYPE_NGINX) {
10511047
// 2022/12/20 17:04:13 [info] 59513#59513: *1
10521048
// 2022/12/20 17:04:13 [info] 1#1: *1
1053-
// | | | |
1049+
// | | | |
10541050
offset = 30;
10551051
}
10561052

@@ -1082,38 +1078,32 @@ int parse (char * line, size_t len, loglinetype t, logdata * l) {
10821078
if (t == LOG_TYPE_APACHE) {
10831079

10841080
// catch the [client ...] fields
1085-
// first possible occurrance position of first [client ...] is 55
1081+
// first possible occurrance position of first [client ...] is 65
10861082
// only in case of Apache
10871083

10881084
// catch the [client ...] fields
10891085
memset(matches, '\0', sizeof(size_t)*2);
1090-
// first possible occurrance position of first [client ...] is 55
1086+
// first possible occurrance position of first [client ...] is 65
10911087
// only in case of Apache
1092-
mcnt = find_pattern(line, 55, len, "[client ", 8, &matches, MATCH_FIRST);
1088+
mcnt = find_pattern(line, 65, len, "[client ", 8, &matches, MATCH_FIRST);
10931089
if (mcnt != 1) {
1094-
set_error(l, "Can't find [client] field!", 0, len);
1095-
return 1;
1096-
}
1097-
else {
1098-
size_t pos = matches[0] + matches[1];
1099-
memset(matches, '\0', sizeof(size_t)*2);
1100-
// find the second [client] position, this shows the end of real [client]
1101-
mcnt = find_pattern(line, pos, len, "[client ", 8, &matches, MATCH_FIRST);
1090+
mcnt = find_pattern(line, 65, len, "[remote ", 8, &matches, MATCH_FIRST);
11021091
if (mcnt != 1) {
1103-
set_error(l, "Can't find second [client] field!", 0, len);
1092+
set_error(l, "Can't find [client] or [remote] field!", 0, len);
11041093
return 1;
11051094
}
1106-
else {
1107-
int ci = 0;
1108-
int k = pos;
1109-
char client[50];
1110-
while(k < matches[0]-1 && line[k] != ']') {
1111-
client[ci++] = line[k++];
1112-
}
1113-
client[ci] = '\0';
1114-
l->log_client = mscl_stradd(l, client, ci);
1115-
}
11161095
}
1096+
size_t pos = matches[0] + matches[1];
1097+
memset(matches, '\0', sizeof(size_t)*2);
1098+
1099+
int ci = 0;
1100+
int k = pos;
1101+
char client[50];
1102+
while(k < matches[0]-1 && line[k] != ']') {
1103+
client[ci++] = line[k++];
1104+
}
1105+
client[ci] = '\0';
1106+
l->log_client = mscl_stradd(l, client, ci);
11171107

11181108
//ap_log_msg_type log_msg_type;
11191109
memset(matches, '\0', sizeof(size_t)*2);

0 commit comments

Comments
 (0)