Browse code

core: parser/parse_hname2 - option to skip printing logs on parse failure

Daniel-Constantin Mierla authored on 02/06/2021 16:00:35
Showing 2 changed files
... ...
@@ -219,21 +219,25 @@ int ksr_hname_init_config(void)
219 219
  * - fills hdr structure (must not be null)
220 220
  * - set hdr->type=HDR_ERROR_T in case of parsing error
221 221
  * - if emode==1, then parsing does not expect : after header name
222
+ * - if logmode==1, then print error log messages on parsing failure
222 223
  * - returns pointer after : if emode==0 or after header name if emode==1
224
+ *   in case of parsing error, returns begin and sets hdr->type to HDR_ERROR_T
223 225
  */
224 226
 char *parse_sip_header_name(char* const begin, const char* const end,
225
-		hdr_field_t* const hdr, int emode)
227
+		hdr_field_t* const hdr, int emode, int logmode)
226 228
 {
227 229
 	char *p;
228 230
 	int i;
229 231
 
230
-	if (end <= begin) {
232
+	if (begin == NULL || end == NULL || end <= begin) {
231 233
 		hdr->type = HDR_ERROR_T;
232 234
 		return begin;
233 235
 	}
234 236
 	if(_ksr_hname_chars_idx[(unsigned char)(*begin)] == 0) {
235
-		LM_ERR("invalid start of header name for [%.*s]\n",
236
-				(int)(end-begin), begin);
237
+		if(likely(logmode)) {
238
+			LM_ERR("invalid start of header name for [%.*s]\n",
239
+					(int)(end-begin), begin);
240
+		}
237 241
 		hdr->type = HDR_ERROR_T;
238 242
 		return begin;
239 243
 	}
... ...
@@ -262,8 +266,10 @@ char *parse_sip_header_name(char* const begin, const char* const end,
262 266
 		}
263 267
 		if(*p != ' ' && *p != '\t') {
264 268
 			/* no white space - bad header name format */
265
-			LM_ERR("invalid header name for [%.*s]\n",
266
-					(int)(end-begin), begin);
269
+			if(likely(logmode)) {
270
+				LM_ERR("invalid header name for [%.*s]\n",
271
+						(int)(end-begin), begin);
272
+			}
267 273
 			hdr->type = HDR_ERROR_T;
268 274
 			return begin;
269 275
 		}
... ...
@@ -271,8 +277,10 @@ char *parse_sip_header_name(char* const begin, const char* const end,
271 277
 
272 278
 	if(p == end) {
273 279
 		/* no : found - emode==0 */
274
-		LM_ERR("invalid end of header name for [%.*s]\n",
275
-				(int)(end-begin), begin);
280
+		if(likely(logmode)) {
281
+			LM_ERR("invalid end of header name for [%.*s]\n",
282
+					(int)(end-begin), begin);
283
+		}
276 284
 		hdr->type = HDR_ERROR_T;
277 285
 		return begin;
278 286
 	}
... ...
@@ -298,7 +306,7 @@ done:
298 306
 
299 307
 char* parse_hname2(char* const begin, const char* const end, struct hdr_field* const hdr)
300 308
 {
301
-	return parse_sip_header_name(begin, end, hdr, 0);
309
+	return parse_sip_header_name(begin, end, hdr, 0, 1);
302 310
 }
303 311
 
304 312
 /**
... ...
@@ -307,11 +315,11 @@ char* parse_hname2(char* const begin, const char* const end, struct hdr_field* c
307 315
  */
308 316
 char* parse_hname2_short(char* const begin, const char* const end, struct hdr_field* const hdr)
309 317
 {
310
-	return parse_sip_header_name(begin, end, hdr, 0);
318
+	return parse_sip_header_name(begin, end, hdr, 0, 1);
311 319
 }
312 320
 
313 321
 char* parse_hname2_str (str* const hbuf, hdr_field_t* const hdr)
314 322
 {
315
-	return parse_sip_header_name(hbuf->s, hbuf->s + hbuf->len, hdr, 1);
323
+	return parse_sip_header_name(hbuf->s, hbuf->s + hbuf->len, hdr, 1, 1);
316 324
 }
317 325
 
... ...
@@ -35,6 +35,15 @@
35 35
 /** Fast 32-bit header field name parser.
36 36
  * @file
37 37
  */
38
+
39
+char *parse_sip_header_name(char* const begin, const char* const end,
40
+		hdr_field_t* const hdr, int emode, int logmode);
41
+
42
+/* macro to find header name without printing errors in failure case */
43
+#define find_hname2_str(hbuf, hdr) \
44
+	parse_sip_header_name((hbuf)->s, (hbuf)->s + (hbuf)->len, hdr, 1, 0)
45
+
46
+
38 47
 char* parse_hname2(char* const begin, const char* const end, struct hdr_field* const hdr);
39 48
 char* parse_hname2_short(char* const begin, const char* const end, struct hdr_field* const hdr);
40 49
 char* parse_hname2_str (str* const hbuf, hdr_field_t* const hdr);