summaryrefslogtreecommitdiff
path: root/src/configfile-glue.c
blob: 0c2f7065efc59a1b02fcafd4a0baaa1ebbeb9a84 (plain)
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
#include "first.h"

#include "base.h"
#include "buffer.h"
#include "array.h"
#include "log.h"
#include "http_header.h"
#include "sock_addr.h"

#undef __declspec_dllimport__
#define __declspec_dllimport__  __declspec_dllexport__

#include "configfile.h"
#include "plugin.h"

#include <string.h>
#include <stdlib.h>     /* strtol */

__declspec_dllexport__
array plugin_stats; /* global */

/**
 * like all glue code this file contains functions which
 * are the external interface of lighttpd. The functions
 * are used by the server itself and the plugins.
 *
 * The main-goal is to have a small library in the end
 * which is linked against both and which will define
 * the interface itself in the end.
 *
 */

/* internal reference to srv->config_context array of (data_config *) */
static struct {
    const data_config * const *data; /* (srv->config_context->data) */
    uint32_t used;                   /* (srv->config_context->used) */
} config_reference;


void config_get_config_cond_info(config_cond_info * const cfginfo, uint32_t idx) {
    const data_config * const dc = (data_config *)config_reference.data[idx];
    cfginfo->comp = dc->comp;
    cfginfo->cond = dc->cond;
    cfginfo->string = &dc->string;
    cfginfo->comp_key = dc->comp_key;
}

int config_capture(server *srv, int idx) {
    data_config * const dc = (data_config *)config_reference.data[idx];
    return (dc->capture_idx)
      ? dc->capture_idx
      : (dc->capture_idx = ++srv->config_captures);
}

int config_feature_bool (const server *srv, const char *feature, int default_value) {
    return srv->srvconf.feature_flags
      ? config_plugin_value_tobool(
          array_get_element_klen(srv->srvconf.feature_flags,
                                 feature, strlen(feature)), default_value)
      : default_value;
}

int32_t config_feature_int (const server *srv, const char *feature, int32_t default_value) {
    return srv->srvconf.feature_flags
      ? config_plugin_value_to_int32(
          array_get_element_klen(srv->srvconf.feature_flags,
                                 feature, strlen(feature)), default_value)
      : default_value;
}

int config_plugin_value_tobool (const data_unset *du, int default_value)
{
    if (NULL == du) return default_value;
    if (du->type == TYPE_STRING) {
        const buffer *b = &((const data_string *)du)->value;
        if (buffer_eq_icase_slen(b, CONST_STR_LEN("enable"))
            || buffer_eq_icase_slen(b, CONST_STR_LEN("enabled"))
            || buffer_eq_icase_slen(b, CONST_STR_LEN("true"))
            || buffer_eq_icase_slen(b, CONST_STR_LEN("1")))
            return 1;
        else if (buffer_eq_icase_slen(b, CONST_STR_LEN("disable"))
                 || buffer_eq_icase_slen(b, CONST_STR_LEN("disabled"))
                 || buffer_eq_icase_slen(b, CONST_STR_LEN("false"))
                 || buffer_eq_icase_slen(b, CONST_STR_LEN("0")))
            return 0;
        else
            return default_value;
    }
    else if (du->type == TYPE_INTEGER)
        return (0 != ((const data_integer *)du)->value);
    else
        return default_value;
}

int32_t config_plugin_value_to_int32 (const data_unset *du, int32_t default_value)
{
    if (NULL == du) return default_value;
    if (du->type == TYPE_STRING) {
        const buffer * const b = &((const data_string *)du)->value;
        char *err;
        long v = strtol(b->ptr, &err, 10);
        return (*err=='\0' && err != b->ptr && INT32_MIN <= v && v <= INT32_MAX)
          ? (int32_t)v
          : default_value;
    }
    else if (du->type == TYPE_INTEGER)
        return ((const data_integer *)du)->value;
    else
        return default_value;
}

int config_plugin_values_init_block(server * const srv, const array * const ca, const config_plugin_keys_t * const cpk, const char * const mname, config_plugin_value_t *cpv) {
    /*(cpv must be list with sufficient elements to store all matches + 1)*/

    int rc = 1; /* default is success */

    for (int i = 0; cpk[i].ktype != T_CONFIG_UNSET; ++i) {
        const data_unset * const du =
          array_get_element_klen(ca, cpk[i].k, cpk[i].klen);
        if (NULL == du) continue; /* not found */

        cpv->k_id = i;
        cpv->vtype = cpk[i].ktype;

        switch (cpk[i].ktype) {
          case T_CONFIG_ARRAY:
          case T_CONFIG_ARRAY_KVANY:
          case T_CONFIG_ARRAY_KVARRAY:
          case T_CONFIG_ARRAY_KVSTRING:
          case T_CONFIG_ARRAY_VLIST:
            if (du->type == TYPE_ARRAY) {
                cpv->v.a = &((const data_array *)du)->value;
            }
            else {
                log_error(srv->errh, __FILE__, __LINE__,
                  "%s should have been a list like "
                  "%s = ( \"...\" )", cpk[i].k, cpk[i].k);
                rc = 0;
                continue;
            }
            switch (cpk[i].ktype) {
              case T_CONFIG_ARRAY_KVANY:
                if (!array_is_kvany(cpv->v.a)) {
                    log_error(srv->errh, __FILE__, __LINE__,
                      "%s should have been a list of key => values like "
                      "%s = ( \"...\" => \"...\", \"...\" => \"...\" )",
                    cpk[i].k, cpk[i].k);
                    rc = 0;
                    continue;
                }
                break;
              case T_CONFIG_ARRAY_KVARRAY:
                if (!array_is_kvarray(cpv->v.a)) {
                    log_error(srv->errh, __FILE__, __LINE__,
                      "%s should have been a list of key => list like "
                      "%s = ( \"...\" => ( \"...\" => \"...\" ) )",
                    cpk[i].k, cpk[i].k);
                    rc = 0;
                    continue;
                }
                break;
              case T_CONFIG_ARRAY_KVSTRING:
                if (!array_is_kvstring(cpv->v.a)) {
                    log_error(srv->errh, __FILE__, __LINE__,
                      "%s should have been a list of key => string values like "
                      "%s = ( \"...\" => \"...\", \"...\" => \"...\" )",
                    cpk[i].k, cpk[i].k);
                    rc = 0;
                    continue;
                }
                break;
              case T_CONFIG_ARRAY_VLIST:
                if (!array_is_vlist(cpv->v.a)) {
                    log_error(srv->errh, __FILE__, __LINE__,
                      "%s should have been a list of string values like "
                      "%s = ( \"...\", \"...\" )",
                    cpk[i].k, cpk[i].k);
                    rc = 0;
                    continue;
                }
                break;
              /*case T_CONFIG_ARRAY:*/
              default:
                break;
            }
            break;
          case T_CONFIG_STRING:
            if (du->type == TYPE_STRING) {
                cpv->v.b = &((const data_string *)du)->value;
            }
            else {
                log_error(srv->errh, __FILE__, __LINE__,
                  "%s should have been a string like ... = \"...\"", cpk[i].k);
                rc = 0;
                continue;
            }
            break;
          case T_CONFIG_SHORT:
            switch(du->type) {
              case TYPE_INTEGER:
                cpv->v.shrt =
                  (unsigned short)((const data_integer *)du)->value;
                break;
              case TYPE_STRING: {
                /* If the value came from an environment variable, then it is
                 * a data_string, although it may contain a number in ASCII
                 * decimal format.  We try to interpret the string as a decimal
                 * short before giving up, in order to support setting numeric
                 * values with environment variables (e.g. port number).
                 */
                const char * const v = ((const data_string *)du)->value.ptr;
                if (v && *v) {
                    char *e;
                    long l = strtol(v, &e, 10);
                    if (e != v && !*e && l >= 0 && l <= 65535) {
                        cpv->v.shrt = (unsigned short)l;
                        break;
                    }
                }
                log_error(srv->errh, __FILE__, __LINE__,
                  "got a string but expected a short: %s %s", cpk[i].k, v);
                rc = 0;
                continue;
              }
              default:
                log_error(srv->errh, __FILE__, __LINE__,
                  "unexpected type for key: %s %d expected a short integer, "
                  "range 0 ... 65535", cpk[i].k, du->type);
                rc = 0;
                continue;
            }
            break;
          case T_CONFIG_INT:
            switch(du->type) {
              case TYPE_INTEGER:
                cpv->v.u = ((const data_integer *)du)->value;
                break;
              case TYPE_STRING: {
                const char * const v = ((const data_string *)du)->value.ptr;
                if (v && *v) {
                    char *e;
                    long l = strtol(v, &e, 10);
                    if (e != v && !*e && l >= 0) {
                        cpv->v.u = (unsigned int)l;
                        break;
                    }
                }
                log_error(srv->errh, __FILE__, __LINE__,
                  "got a string but expected an integer: %s %s",cpk[i].k,v);
                rc = 0;
                continue;
              }
              default:
                log_error(srv->errh, __FILE__, __LINE__,
                  "unexpected type for key: %s %d expected an integer, "
                  "range 0 ... 4294967295", cpk[i].k, du->type);
                rc = 0;
                continue;
            }
            break;
          case T_CONFIG_BOOL:
            {
                int v = config_plugin_value_tobool(du, -1);
                if (-1 == v) {
                    log_error(srv->errh, __FILE__, __LINE__,
                      "ERROR: unexpected type for key: %s (string) "
                      "\"(enable|disable)\"", cpk[i].k);
                    rc = 0;
                    continue;
                }
                cpv->v.u = v;
            }
            break;
          case T_CONFIG_LOCAL:
          case T_CONFIG_UNSET:
            continue;
          case T_CONFIG_UNSUPPORTED:
            log_error(srv->errh, __FILE__, __LINE__,
              "ERROR: found unsupported key: %s (%s)", cpk[i].k, mname);
            srv->srvconf.config_unsupported = 1;
            continue;
          case T_CONFIG_DEPRECATED:
            log_error(srv->errh, __FILE__, __LINE__,
              "ERROR: found deprecated key: %s (%s)", cpk[i].k, mname);
            srv->srvconf.config_deprecated = 1;
            continue;
        }

        ++cpv;
    }

    cpv->k_id = -1; /* indicate list end */

    return rc;
}

int config_plugin_values_init(server * const srv, void *p_d, const config_plugin_keys_t * const cpk, const char * const mname) {
    plugin_data_base * const p = (plugin_data_base *)p_d;
    array * const touched = srv->srvconf.config_touched;
    unsigned char matches[4096];   /*directives matches (4k is way too many!)*/
    unsigned short contexts[4096]; /*conditions matches (4k is way too many!)*/
    uint32_t n = 0;
    int rc = 1; /* default is success */
    force_assert(sizeof(matches) >= srv->config_context->used);

    /* save config reference data for later internal use
     * (config_plugin_values_init() is called with same srv->config_context) */
    config_reference.data = (const data_config * const *)srv->config_context->data;
    config_reference.used = srv->config_context->used;

    /* traverse config contexts twice: once to count, once to store matches */

    for (uint32_t u = 0; u < srv->config_context->used; ++u) {
        const data_config * const dc =
          (const data_config *)srv->config_context->data[u];
        const array * const ca = dc->value;

        matches[n] = 0;
        for (int i = 0; cpk[i].ktype != T_CONFIG_UNSET; ++i) {
            const data_unset * const du =
              array_get_element_klen(ca, cpk[i].k, cpk[i].klen);
            if (NULL == du) continue; /* not found */

            ++matches[n];

            array_set_key_value(touched,cpk[i].k,cpk[i].klen,CONST_STR_LEN(""));

            if (cpk[i].scope == T_CONFIG_SCOPE_CONNECTION || 0 == u) continue;

            if (cpk[i].scope == T_CONFIG_SCOPE_SERVER)
                /* server scope options should be set only in server scope */
                log_error(srv->errh, __FILE__, __LINE__,
                  "DEPRECATED: do not set server options in conditionals, "
                  "variable: %s", cpk[i].k);
            if (cpk[i].scope == T_CONFIG_SCOPE_SOCKET
                && (dc->comp!=COMP_SERVER_SOCKET || dc->cond!=CONFIG_COND_EQ))
                /* socket options should be set in socket or global scope */
                log_error(srv->errh, __FILE__, __LINE__,
                  "WARNING: %s must be in global scope or $SERVER[\"socket\"] "
                  "with '==', or else is ignored", cpk[i].k);
        }
        if (matches[n]) contexts[n++] = (unsigned short)u;
    }

    uint32_t elts = 0;
    for (uint32_t u = 0; u < n; ++u) elts += matches[u];
    p->nconfig = n;
    /*(+1 to include global scope, whether or not any directives exist)*/
    /*(+n for extra element to end each list)*/
    p->cvlist = (config_plugin_value_t *)
      ck_calloc(1+n+n+elts, sizeof(config_plugin_value_t));

    elts = 1+n;
    /* shift past first element if no directives in global scope */
    const uint32_t shft = (0 != n && 0 != contexts[0]);
    if (shft) ++p->nconfig;
    for (uint32_t u = 0; u < n; ++u) {
        config_plugin_value_t * const cpv = p->cvlist+shft+u;
        cpv->k_id = (int)contexts[u];
        cpv->v.u2[0] = elts;
        cpv->v.u2[1] = matches[u];
        elts += matches[u]+1;     /* +1 to end list with cpv->k_id = -1 */
    }

    for (uint32_t u = 0; u < n; ++u) {
        const array *ca =
          ((data_config const *)srv->config_context->data[contexts[u]])->value;
        config_plugin_value_t *cpv = p->cvlist + p->cvlist[shft+u].v.u2[0];
        if (!config_plugin_values_init_block(srv, ca, cpk, mname, cpv))
            rc = 0;
    }

    return rc;
}

__attribute_cold__
__attribute_noinline__
static void config_cond_result_trace(request_st * const r, const data_config * const dc, const int cached) {
    cond_cache_t * const cache = &r->cond_cache[dc->context_ndx];
    const char *msg;
    switch (cache->result) {
      case COND_RESULT_UNSET: msg = "unset"; break;
      case COND_RESULT_SKIP:  msg = "skipped"; break;
      case COND_RESULT_FALSE: msg = "false"; break;
      case COND_RESULT_TRUE:  msg = "true"; break;
      default:                msg = "invalid cond_result_t"; break;
    }
    log_error(r->conf.errh, __FILE__, __LINE__, "%d (%s) result: %s (cond: %s)",
              dc->context_ndx, &"uncached"[cached ? 2 : 0], msg, dc->key.ptr);
}

static cond_result_t config_check_cond_nocache(request_st *r, const data_config *dc, int debug_cond, cond_cache_t *cache);

static cond_result_t config_check_cond_nocache_calc(request_st * const r, const data_config * const dc, const int debug_cond, cond_cache_t * const cache) {
    cache->result = config_check_cond_nocache(r, dc, debug_cond, cache);
    if (debug_cond) config_cond_result_trace(r, dc, 0);
    return cache->result;
}

static cond_result_t config_check_cond_cached(request_st * const r, const data_config * const dc, const int debug_cond) {
    cond_cache_t * const cache = &r->cond_cache[dc->context_ndx];
    if (COND_RESULT_UNSET != cache->result) {
        if (debug_cond) config_cond_result_trace(r, dc, 1);
        return cache->result;
    }
    return config_check_cond_nocache_calc(r, dc, debug_cond, cache);
}

static int config_pcre_match(request_st *r, const data_config *dc, const buffer *b);

static cond_result_t config_check_cond_nocache_eval(request_st * const r, const data_config * const dc, const int debug_cond, cond_cache_t * const cache);

static cond_result_t config_check_cond_nocache(request_st * const r, const data_config * const dc, const int debug_cond, cond_cache_t * const cache) {
	/* check parent first */
	if (dc->parent && dc->parent->context_ndx) {
		/**
		 * a nested conditional 
		 *
		 * if the parent is not decided yet or false, we can't be true either 
		 */
		if (debug_cond) {
			log_error(r->conf.errh, __FILE__, __LINE__, "go parent %s", dc->parent->key.ptr);
		}

		switch (config_check_cond_cached(r, dc->parent, debug_cond)) {
		case COND_RESULT_UNSET:
			/* decide later */
			return COND_RESULT_UNSET;
		case COND_RESULT_SKIP:
		case COND_RESULT_FALSE:
			/* failed precondition */
			return COND_RESULT_SKIP;
		case COND_RESULT_TRUE:
			/* proceed */
			break;
		}
	}

	if (dc->prev) {
		/**
		 * a else branch; can only be executed if the previous branch
		 * was evaluated as "false" (not unset/skipped/true)
		 */
		if (debug_cond) {
			log_error(r->conf.errh, __FILE__, __LINE__, "go prev %s", dc->prev->key.ptr);
		}

		/* make sure prev is checked first */
		switch (config_check_cond_cached(r, dc->prev, debug_cond)) {
		case COND_RESULT_UNSET:
			/* decide later */
			return COND_RESULT_UNSET;
		case COND_RESULT_SKIP:
		case COND_RESULT_TRUE:
			/* failed precondition */
			return COND_RESULT_SKIP;
		case COND_RESULT_FALSE:
			/* proceed */
			break;
		}
	}

	if (!(r->conditional_is_valid & (1 << dc->comp))) {
		if (debug_cond) {
			log_error(r->conf.errh, __FILE__, __LINE__,
			  "%d %s not available yet", dc->comp, dc->key.ptr);
		}

		return COND_RESULT_UNSET;
	}

	/* if we had a real result before and weren't cleared just return it */
	switch (cache->local_result) {
	case COND_RESULT_TRUE:
	case COND_RESULT_FALSE:
		return cache->local_result;
	default:
		break;
	}

	if (CONFIG_COND_ELSE == dc->cond)
		return (cache->local_result = COND_RESULT_TRUE);
		/* remember result of local condition for a partial reset */

	return config_check_cond_nocache_eval(r, dc, debug_cond, cache);
}

static cond_result_t config_check_cond_nocache_eval(request_st * const r, const data_config * const dc, const int debug_cond, cond_cache_t * const cache) {
	/* pass the rules */

	static const struct const_char_buffer {
	  const char *ptr;
	  uint32_t used;
	  uint32_t size;
	} empty_string = { "", 1, 0 };

	const buffer *l;
	switch (dc->comp) {
	case COMP_HTTP_HOST:
		l = &r->uri.authority;
		break;
	case COMP_HTTP_REMOTE_IP:
		l = r->dst_addr_buf;
		break;
	case COMP_HTTP_SCHEME:
		l = &r->uri.scheme;
		break;
	case COMP_HTTP_URL:
		l = &r->uri.path;
		break;
	case COMP_HTTP_QUERY_STRING:
		l = &r->uri.query;
		break;
	case COMP_SERVER_SOCKET:
		l = r->con->srv_socket->srv_token;
		break;
	case COMP_HTTP_REQUEST_HEADER:
		l = http_header_request_get(r, dc->ext, BUF_PTR_LEN(&dc->comp_tag));
		break;
	case COMP_HTTP_REQUEST_METHOD:
		l = http_method_buf(r->http_method);
		break;
	default:
		return (cache->local_result = COND_RESULT_FALSE);
	}

	if (__builtin_expect( (buffer_is_empty(l)), 0))
		l = (buffer *)&empty_string;

	if (debug_cond)
		log_error(r->conf.errh, __FILE__, __LINE__,
			"%s compare to %s", dc->comp_key, l->ptr);

	int match;
	switch(dc->cond) {
	case CONFIG_COND_NE:
	case CONFIG_COND_EQ:
		match = (dc->cond == CONFIG_COND_EQ);
		if (dc->comp == COMP_HTTP_HOST && dc->string.ptr[0] != '/') {
			uint_fast32_t llen = buffer_clen(l);
			uint_fast32_t dlen = buffer_clen(&dc->string);
			/* check names match, whether or not :port suffix present */
			/*(not strictly checking for port match for alt-svc flexibility,
			 * though if strings are same length, port is checked for match)*/
			/*(r->uri.authority not strictly checked here for excess ':')*/
			/*(r->uri.authority lowercased during request parsing)*/
			if (llen && llen != dlen) {
				match ^= ((llen > dlen)
				             ? l->ptr[dlen] == ':' && llen - dlen <= 6
				             : dc->string.ptr[(dlen = llen)] == ':')
				         && 0 == memcmp(l->ptr, dc->string.ptr, dlen);
				break;
			}
		}
		else if (dc->comp == COMP_HTTP_REMOTE_IP && dc->string.ptr[0] != '/') {
			/* CIDR mask comparisons only supported for COND_EQ, COND_NE */
			/* compare using structure data after end of string
			 * (generated at startup when parsing config) */
			const sock_addr * const addr = (sock_addr *)
			  (((uintptr_t)dc->string.ptr + dc->string.used + 1 + 7) & ~7);
			int bits = ((unsigned char *)dc->string.ptr)[dc->string.used];
			match ^= (bits)
			  ? sock_addr_is_addr_eq_bits(addr, r->dst_addr, bits)
			  : sock_addr_is_addr_eq(addr, r->dst_addr);
			break;
		}
		match ^= (buffer_is_equal(l, &dc->string));
		break;
	case CONFIG_COND_NOMATCH:
	case CONFIG_COND_MATCH:
		match = (dc->cond == CONFIG_COND_MATCH);
		match ^= (config_pcre_match(r, dc, l) > 0);
		break;
	case CONFIG_COND_PREFIX:
	case CONFIG_COND_SUFFIX:
		{
			uint_fast32_t llen = buffer_clen(l);
			uint_fast32_t dlen = buffer_clen(&dc->string);
			uint_fast32_t off  = (dc->cond == CONFIG_COND_PREFIX)
			                   ? 0
			                   : llen - dlen; /*(underflow caught below)*/
			match = !(dlen <= llen
			          && 0 == memcmp(l->ptr+off, dc->string.ptr, dlen));
		}
		break;
	default:
		match = 1; /* return (cache->local_result = COND_RESULT_FALSE); below */
		break;
	}
	/* remember result of local condition for a partial reset */
	cache->local_result = match ? COND_RESULT_FALSE : COND_RESULT_TRUE;
	return cache->local_result;
}

__attribute_noinline__
static cond_result_t config_check_cond_calc(request_st * const r, const int context_ndx, cond_cache_t * const cache) {
    const data_config * const dc = config_reference.data[context_ndx];
    const int debug_cond = r->conf.log_condition_handling;
    if (debug_cond) {
        log_error(r->conf.errh, __FILE__, __LINE__,
                  "=== start of condition block ===");
    }
    return config_check_cond_nocache_calc(r, dc, debug_cond, cache);
}

/* future: might make static inline in header for plugins */
int config_check_cond(request_st * const r, const int context_ndx) {
    cond_cache_t * const cache = &r->cond_cache[context_ndx];
    return COND_RESULT_TRUE
        == (COND_RESULT_UNSET != cache->result
              ? (cond_result_t)cache->result
              : config_check_cond_calc(r, context_ndx, cache));
}

/* if we reset the cache result for a node, we also need to clear all
 * child nodes and else-branches*/
static void config_cond_clear_node(cond_cache_t * const cond_cache, const data_config * const dc) {
	/* if a node is "unset" all children are unset too */
	if (cond_cache[dc->context_ndx].result != COND_RESULT_UNSET) {
		cond_cache[dc->context_ndx].result = COND_RESULT_UNSET;

		for (uint32_t i = 0; i < dc->children.used; ++i) {
			const data_config *dc_child = dc->children.data[i];
			if (NULL == dc_child->prev) {
				/* only call for first node in if-else chain */
				config_cond_clear_node(cond_cache, dc_child);
			}
		}
		if (NULL != dc->next) config_cond_clear_node(cond_cache, dc->next);
	}
}

/**
 * reset the config-cache for a named item
 */
void config_cond_cache_reset_item(request_st * const r, comp_key_t item) {
	cond_cache_t * const cond_cache = r->cond_cache;
	const data_config * const * const data = config_reference.data;
	const uint32_t used = config_reference.used;
	for (uint32_t i = 0; i < used; ++i) {
		const data_config * const dc = data[i];

		if (item == dc->comp) {
			/* clear local_result */
			cond_cache[i].local_result = COND_RESULT_UNSET;
			/* clear result in subtree (including the node itself) */
			config_cond_clear_node(cond_cache, dc);
		}
	}
}

/**
 * reset the config cache to its initial state at connection start
 */
void config_cond_cache_reset(request_st * const r) {
	/* resetting all entries; no need to follow children as in config_cond_cache_reset_item */
	/* static_assert(0 == COND_RESULT_UNSET); */
	const uint32_t used = config_reference.used;
	if (used > 1)
		memset(r->cond_cache, 0, used*sizeof(cond_cache_t));
}

#ifdef HAVE_PCRE2_H
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#elif defined(HAVE_PCRE_H)
#include <pcre.h>
#endif

static int config_pcre_match(request_st * const r, const data_config * const dc, const buffer * const b) {

  #ifdef HAVE_PCRE2_H

    if (__builtin_expect( (0 == dc->capture_idx), 1))
        return pcre2_match(dc->code, (PCRE2_SPTR)BUF_PTR_LEN(b),
                           0, 0, dc->match_data, NULL);

    const int capture_offset = dc->capture_idx - 1;
    cond_match_t * const cond_match =
      r->cond_match[capture_offset] = r->cond_match_data + capture_offset;
    pcre2_match_data *match_data = cond_match->match_data;
    if (__builtin_expect( (NULL == match_data), 0)) {
        /*(allocate on demand)*/
      #if 0 /*(if we did not want to share dc->match_data across requests)*/
        /* index 0 is reused for all matches for which captures not used by
         * other directives within the condition, so allocate for up to 9
         * captures, plus 1 for %0 for full match.  Number of captures is
         * checked at startup to be <= 9 in data_config_pcre_compile()
         * (future: could save a few bytes if max captures were calculated
         *  at startup in config_finalize()) */
        match_data = cond_match->match_data = (0 == dc->capture_idx)
          ? pcre2_match_data_create(10, NULL)
          : pcre2_match_data_create_from_pattern(dc->code, NULL);
      #else
        match_data = cond_match->match_data =
          pcre2_match_data_create_from_pattern(dc->code, NULL);
      #endif
        force_assert(match_data);
        cond_match->matches = pcre2_get_ovector_pointer(match_data);
    }
    cond_match->comp_value = b; /*holds pointer to b (!) for pattern subst*/
    cond_match->captures =
      pcre2_match(dc->code, (PCRE2_SPTR)BUF_PTR_LEN(b), 0, 0, match_data, NULL);
    return cond_match->captures;

  #elif defined(HAVE_PCRE_H)

    if (__builtin_expect( (0 == dc->capture_idx), 1)) {
        int matches[3 * 10];
        return pcre_exec(dc->regex, dc->regex_study, BUF_PTR_LEN(b), 0, 0,
                         matches, sizeof(matches)/sizeof(*matches));
    }

    const int capture_offset = dc->capture_idx - 1;
    cond_match_t * const cond_match =
      r->cond_match[capture_offset] = r->cond_match_data + capture_offset;
    if (__builtin_expect( (NULL == cond_match->matches), 0)) {
        /*(allocate on demand)*/
        cond_match->matches = ck_malloc(dc->ovec_nelts * sizeof(int));
    }
    cond_match->comp_value = b; /*holds pointer to b (!) for pattern subst*/
    cond_match->captures =
      pcre_exec(dc->regex, dc->regex_study, BUF_PTR_LEN(b), 0, 0,
                cond_match->matches, dc->ovec_nelts);
    return cond_match->captures;

  #else

    UNUSED(r);
    UNUSED(dc);
    UNUSED(b);
    return 0;

  #endif
}