-
Notifications
You must be signed in to change notification settings - Fork 0
/
tengine-access-log-if.patch
1167 lines (1130 loc) · 32.7 KB
/
tengine-access-log-if.patch
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
diff --git a/src/http/modules/ngx_http_log_module.c b/src/http/modules/ngx_http_log_module.c
index ef279f3..f204625 100644
--- a/src/http/modules/ngx_http_log_module.c
+++ b/src/http/modules/ngx_http_log_module.c
@@ -15,6 +15,9 @@
#define NGX_HTTP_LOG_ESCAPE_ASCII 3
+#define NGX_HTTP_LOG_CONF 0x80000001
+
+
typedef struct ngx_http_log_op_s ngx_http_log_op_t;
typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
@@ -42,6 +45,7 @@ typedef struct {
typedef struct {
ngx_array_t formats; /* array of ngx_http_log_fmt_t */
ngx_uint_t combined_used; /* unsigned combined_used:1 */
+ ngx_uint_t seq; /* conditional log sequence */
} ngx_http_log_main_conf_t;
@@ -52,27 +56,25 @@ typedef struct {
typedef struct {
-#if (NGX_SYSLOG)
- ngx_syslog_t *syslog;
-#endif
- ngx_open_file_t *file;
- ngx_http_log_script_t *script;
- time_t disk_full_time;
- time_t error_log_time;
- ngx_http_log_fmt_t *format;
+ ngx_array_t *codes;
+ ngx_flag_t log;
+ ngx_flag_t is_and;
+} ngx_http_log_condition_t;
+
+
+/*
+ * variables for sampled_log
+ * ratio = sample/scope, 10's multiple
+ * scatter = ceil(scope/sample)
+ * (inflexion-1)*scatter + (sample-inflxion)*(scatter-1) - scope < 0
+ * scope - (inflexion-1)*scatter - (sample-inflxion)*(scatter-1) <= scatter - 1
+ *
+ * for example:
+ * ratio = 0.3, then scope = 10, sample = 3, scatter = 4, inflexion = 2
+ * ratio = 0.35, then scope = 100, sample = 35, scatter = 3, inflexion = 31
+ */
- /*
- * variables for sampled_log
- * ratio = sample/scope, 10's multiple
- * scatter = ceil(scope/sample)
- * (inflexion-1)*scatter + (sample-inflxion)*(scatter-1) - scope < 0
- * scope - (inflexion-1)*scatter - (sample-inflxion)*(scatter-1)
- * <= scatter - 1
- *
- * for example:
- * ratio = 0.3, then scope = 10, sample = 3, scatter = 4, inflexion = 2
- * ratio = 0.35, then scope = 100, sample = 35, scatter = 3, inflexion = 31
- */
+typedef struct {
ngx_uint_t scope;
ngx_uint_t sample;
ngx_uint_t scatter;
@@ -80,11 +82,32 @@ typedef struct {
ngx_uint_t scope_count;
ngx_uint_t sample_count;
ngx_uint_t scatter_count;
+} ngx_http_log_sample_t;
+
+
+typedef struct {
+ ngx_array_t *conditions;
+ ngx_http_log_sample_t *sample;
+} ngx_http_log_env_t;
+
+
+typedef struct {
+#if (NGX_SYSLOG)
+ ngx_syslog_t *syslog;
+#endif
+ ngx_open_file_t *file;
+ ngx_http_log_script_t *script;
+ time_t disk_full_time;
+ time_t error_log_time;
+ ngx_http_log_fmt_t *format;
+ ngx_http_log_sample_t *sample;
+ ngx_int_t var_index; /* for conditional log */
} ngx_http_log_t;
typedef struct {
ngx_array_t *logs; /* array of ngx_http_log_t */
+ ngx_http_log_env_t *env;
ngx_open_file_cache_t *open_file_cache;
time_t open_file_cache_valid;
@@ -146,6 +169,29 @@ static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size,
ngx_uint_t flag);
+static char *ngx_http_log_condition_block(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf);
+static char *ngx_http_log_env_block(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf);
+static char *ngx_http_log_block_if(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf);
+static char *ngx_http_log_block_sample(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf);
+
+static char *ngx_http_log_condition_value(ngx_conf_t *cf,
+ ngx_http_log_condition_t *lc, ngx_str_t *value);
+static char *ngx_http_log_condition(ngx_conf_t *cf,
+ ngx_http_log_condition_t *lc);
+static char *ngx_http_log_sample_rate(ngx_conf_t *cf, ngx_str_t *value,
+ ngx_http_log_sample_t **sample);
+static ngx_http_variable_t *ngx_http_log_copy_var(ngx_conf_t *cf,
+ ngx_http_variable_t *var, ngx_uint_t seq);
+
+static ngx_int_t ngx_http_log_variable_value(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_log_do_if(ngx_http_request_t *r,
+ ngx_array_t *conditions);
+static ngx_int_t ngx_http_log_do_sample(ngx_http_log_sample_t *sample);
static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
@@ -155,8 +201,6 @@ static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
-static ngx_int_t ngx_http_log_set_ratio(ngx_conf_t *cf, ngx_str_t *value,
- ngx_http_log_t *log);
static char *ngx_http_log_compile_format(ngx_conf_t *cf,
ngx_array_t *flushes, ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s);
static char *ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -183,7 +227,7 @@ static ngx_command_t ngx_http_log_commands[] = {
{ ngx_string("access_log"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
- |NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1234,
+ |NGX_HTTP_LMT_CONF|NGX_CONF_1MORE,
ngx_http_log_set_log,
NGX_HTTP_LOC_CONF_OFFSET,
0,
@@ -210,6 +254,36 @@ static ngx_command_t ngx_http_log_commands[] = {
offsetof(ngx_http_log_loc_conf_t, log_empty_request),
NULL },
+ { ngx_string("log_condition"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK
+ |NGX_CONF_NOARGS,
+ ngx_http_log_condition_block,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ 0,
+ NULL },
+
+ { ngx_string("log_env"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK
+ |NGX_CONF_TAKE1,
+ ngx_http_log_env_block,
+ 0,
+ 0,
+ NULL },
+
+ { ngx_string("if"),
+ NGX_HTTP_LOG_CONF|NGX_CONF_1MORE,
+ ngx_http_log_block_if,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ 0,
+ NULL },
+
+ { ngx_string("sample"),
+ NGX_HTTP_LOG_CONF|NGX_CONF_1MORE,
+ ngx_http_log_block_sample,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ 0,
+ NULL },
+
ngx_null_command
};
@@ -286,13 +360,14 @@ static ngx_http_log_var_t ngx_http_log_vars[] = {
static ngx_int_t
ngx_http_log_handler(ngx_http_request_t *r)
{
- u_char *line, *p;
- size_t len;
- ngx_uint_t i, l, bypass, threshold;
- ngx_http_log_t *log;
- ngx_open_file_t *file;
- ngx_http_log_op_t *op;
- ngx_http_log_loc_conf_t *lcf;
+ u_char *line, *p;
+ size_t len;
+ ngx_uint_t i, l, bypass;
+ ngx_http_log_t *log;
+ ngx_open_file_t *file;
+ ngx_http_log_op_t *op;
+ ngx_http_log_loc_conf_t *lcf;
+ ngx_http_variable_value_t *vv;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http log handler");
@@ -303,6 +378,20 @@ ngx_http_log_handler(ngx_http_request_t *r)
return NGX_OK;
}
+ bypass = 0;
+
+ if (lcf->env && lcf->env->conditions
+ && ngx_http_log_do_if(r, lcf->env->conditions) == NGX_DECLINED)
+ {
+ bypass = 1;
+ }
+
+ if (bypass == 0 && lcf->env && lcf->env->sample
+ && ngx_http_log_do_sample(lcf->env->sample) == NGX_DECLINED)
+ {
+ bypass = 1;
+ }
+
if (r->headers_out.status == NGX_HTTP_BAD_REQUEST && !lcf->log_empty_request
&& (r->header_in && r->header_in->last == r->header_in->start))
{
@@ -311,38 +400,25 @@ ngx_http_log_handler(ngx_http_request_t *r)
log = lcf->logs->elts;
for (l = 0; l < lcf->logs->nelts; l++) {
+ if (log[l].var_index == NGX_ERROR && log[l].sample == NULL && bypass) {
+ continue;
+ }
- if (log[l].scope != 0) {
-
- bypass = 1;
-
- if (log[l].sample_count < log[l].sample) {
- if (log[l].scatter_count++ == 0) {
- bypass = 0;
- ++log[l].sample_count;
- }
-
- threshold = log[l].scatter;
- if (log[l].sample_count >= log[l].inflexion) {
- --threshold;
- }
-
- if (log[l].scatter_count == threshold) {
- log[l].scatter_count = 0;
- }
- }
-
- if (++log[l].scope_count == log[l].scope) {
- log[l].scope_count = 0;
- log[l].sample_count = 0;
- log[l].scatter_count = 0;
- }
-
- if (bypass == 1) {
+ if (log[l].var_index != NGX_ERROR) {
+ vv = ngx_http_get_indexed_variable(r, log[l].var_index);
+ if (vv != NULL && !vv->not_found
+ && (vv->len == 0 || (vv->len == 1 && vv->data[0] == '0')))
+ {
continue;
}
}
+ if (log[l].sample
+ && ngx_http_log_do_sample(log[l].sample) == NGX_DECLINED)
+ {
+ continue;
+ }
+
if (ngx_time() == log[l].disk_full_time) {
/*
@@ -1044,6 +1120,10 @@ ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
}
}
+ if (conf->env == NULL) {
+ conf->env = prev->env;
+ }
+
if (conf->logs || conf->off) {
return NGX_CONF_OK;
}
@@ -1073,7 +1153,8 @@ ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
log->script = NULL;
log->disk_full_time = 0;
log->error_log_time = 0;
- log->scope = 0;
+ log->sample = NULL;
+ log->var_index = NGX_ERROR;
#if NGX_SYSLOG
log->syslog = NULL;
#endif
@@ -1100,6 +1181,8 @@ ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_uint_t i, n;
ngx_http_log_t *log;
ngx_http_log_fmt_t *fmt;
+ ngx_http_variable_t *var;
+ ngx_http_log_sample_t *sample;
ngx_http_log_main_conf_t *lmcf;
ngx_http_script_compile_t sc;
ngx_uint_t skip_file = 0;
@@ -1132,6 +1215,7 @@ ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}
ngx_memzero(log, sizeof(ngx_http_log_t));
+ log->var_index = NGX_ERROR;
rc = ngx_log_target(cf->cycle, &value[1], (ngx_log_t *) log);
@@ -1217,11 +1301,20 @@ rest:
return NGX_CONF_OK;
}
+ sample = NULL;
+ var = NULL;
+
for (i = 3; i < cf->args->nelts; i++) {
if (ngx_strncmp(value[i].data, "ratio=", 6) == 0) {
- if (ngx_http_log_set_ratio(cf, &value[i], log) != NGX_OK) {
+ value[i].data += 6;
+ value[i].len -= 6;
+
+ if (ngx_http_log_sample_rate(cf, &value[i], &sample)
+ != NGX_CONF_OK)
+ {
return NGX_CONF_ERROR;
}
+
} else if (ngx_strncmp(value[i].data, "buffer=", 7) == 0) {
if (skip_file == 0) {
continue;
@@ -1259,6 +1352,15 @@ rest:
log->file->pos = log->file->buffer;
log->file->last = log->file->buffer + buf;
+ } else if (ngx_strncmp(value[i].data, "env=", 4) == 0) {
+ value[i].data += 5;
+ value[i].len -= 5;
+
+ var = ngx_http_add_variable(cf, &value[i], NGX_HTTP_VAR_CHANGEABLE);
+ if (var == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
} else {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid buffer value \"%V\"", &name);
@@ -1266,77 +1368,34 @@ rest:
}
}
- return NGX_CONF_OK;
-}
-
-
-static ngx_int_t
-ngx_http_log_set_ratio(ngx_conf_t *cf, ngx_str_t *value, ngx_http_log_t *log)
-{
- u_char *p, *last;
- uint64_t scope;
- ngx_uint_t rp;
-
- rp = 0;
- scope = 0;
-
- for (last = value->data + value->len - 1;
- last >= value->data + 6 && *last == '0';
- last--) /* void */ ;
-
- if (last == value->data + 6 && *last == '1') {
- return NGX_OK;
+ if (var == NULL) {
+ log->sample = sample;
+ return NGX_CONF_OK;
}
- for (p = value->data + 6; p <= last; p++) {
- if (*p == '.') {
- if (rp == 0) {
- rp = 1;
- scope = 1;
- continue;
- } else {
- goto invalid;
- }
- }
-
- if (*p < '0' && *p > '9') {
- goto invalid;
- }
-
- if (rp == 0 && *p != '0') {
- goto invalid;
- }
-
- log->sample *= 10;
- log->sample += *p - '0';
-
- scope *= 10;
+ if (var->get_handler == NULL && var->set_handler == NULL) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "variable for log env is not defined");
+ return NGX_CONF_ERROR;
+ }
- if (scope > NGX_MAX_UINT32_VALUE) {
- goto invalid;
+ if (var->get_handler != ngx_http_log_variable_value) {
+ log->sample = sample;
+ } else if (sample) {
+ var = ngx_http_log_copy_var(cf, var, ++lmcf->seq);
+ if (var == NULL) {
+ return NGX_CONF_ERROR;
}
- }
- if (log->sample == 0) {
- goto invalid;
+ ((ngx_http_log_env_t *) var->data)->sample = sample;
}
- log->scope = scope;
-
- log->scatter = log->scope / log->sample;
- if (log->scatter * log->sample != log->scope) {
- ++log->scatter;
+ log->var_index = ngx_http_get_variable_index(cf, &var->name);
+ if (log->var_index == NGX_ERROR) {
+ return NGX_CONF_ERROR;
}
- log->inflexion = log->scope - log->sample * log->scatter + log->sample + 1;
-
- return NGX_OK;
-
-invalid:
-
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "invalid parameter \"%V\"", value);
- return NGX_ERROR;
+ return NGX_CONF_OK;
}
@@ -1699,3 +1758,710 @@ ngx_http_log_init(ngx_conf_t *cf)
return NGX_OK;
}
+
+
+static char *
+ngx_http_log_condition_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+ char *rv;
+ ngx_conf_t pcf;
+ ngx_http_conf_ctx_t *ctx, *pctx;
+ ngx_http_log_condition_t *condition;
+
+ ngx_http_log_loc_conf_t *llcf = conf;
+
+ llcf->env = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_env_t));
+ if (llcf->env == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
+ if (ctx == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ pctx = cf->ctx;
+ ctx->main_conf = pctx->main_conf;
+ ctx->srv_conf = pctx->srv_conf;
+ ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
+ if (ctx->loc_conf == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ ctx->loc_conf[ngx_http_log_module.ctx_index] = llcf->env;
+
+ pcf = *cf;
+ cf->cmd_type = NGX_HTTP_LOG_CONF;
+ cf->ctx = ctx;
+ rv = ngx_conf_parse(cf, NULL);
+ *cf = pcf;
+
+ if (rv != NGX_CONF_OK) {
+ return rv;
+ }
+
+ if (llcf->env->conditions) {
+ condition = llcf->env->conditions->elts;
+ if (condition[llcf->env->conditions->nelts - 1].is_and) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "can not use \"and\" flag on the last condition");
+ return NGX_CONF_ERROR;
+ }
+ }
+
+ return NGX_CONF_OK;
+}
+
+static char *
+ngx_http_log_env_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+ char *rv;
+ ngx_str_t *value;
+ ngx_conf_t pcf;
+ ngx_http_log_env_t *env;
+ ngx_http_conf_ctx_t *ctx, *pctx;
+ ngx_http_variable_t *var;
+ ngx_http_log_condition_t *condition;
+
+ ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
+ if (ctx == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ pctx = cf->ctx;
+ ctx->main_conf = pctx->main_conf;
+ ctx->srv_conf = pctx->srv_conf;
+ ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
+ if (ctx->loc_conf == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ value = cf->args->elts;
+ value[1].len--;
+ value[1].data++;
+
+ var = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
+ if (var == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ if (var->data) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "duplicate access log var: \"%V\"",
+ &value[1]);
+ return NGX_CONF_ERROR;
+ }
+
+ env = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_env_t));
+ if (env == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ ctx->loc_conf[ngx_http_log_module.ctx_index] = env;
+
+ var->get_handler = ngx_http_log_variable_value;
+ var->data = (uintptr_t) env;
+
+ pcf = *cf;
+ cf->cmd_type = NGX_HTTP_LOG_CONF;
+ cf->ctx = ctx;
+ rv = ngx_conf_parse(cf, NULL);
+ *cf = pcf;
+
+ if (rv != NGX_CONF_OK) {
+ return rv;
+ }
+
+ if (env->conditions) {
+ condition = env->conditions->elts;
+ if (condition[env->conditions->nelts - 1].is_and) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "can not use \"and\" flag on the last condition");
+ return NGX_CONF_ERROR;
+ }
+ }
+
+ return NGX_CONF_OK;
+}
+
+
+static char *
+ngx_http_log_block_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+ uintptr_t *code;
+ ngx_str_t *value;
+ ngx_http_log_condition_t *lc;
+
+ ngx_http_log_env_t *env = conf;
+
+ if (env->conditions == NULL) {
+ env->conditions = ngx_array_create(cf->pool, 7,
+ sizeof(ngx_http_log_condition_t));
+ if (env->conditions == NULL) {
+ return NGX_CONF_ERROR;
+ }
+ }
+
+ lc = ngx_array_push(env->conditions);
+ if (lc == NULL) {
+ return NGX_CONF_ERROR;
+ }
+ ngx_memzero(lc, sizeof(ngx_http_log_condition_t));
+
+ value = cf->args->elts;
+ if (ngx_strcmp(value[cf->args->nelts - 1].data, "and") == 0) {
+ cf->args->nelts--;
+ lc->is_and = 1;
+ }
+
+ if (ngx_http_log_condition(cf, lc) != NGX_CONF_OK) {
+ return NGX_CONF_ERROR;
+ }
+
+ code = ngx_array_push_n(lc->codes, sizeof(uintptr_t));
+ if (code == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ *code = (uintptr_t) NULL;
+
+ return NGX_CONF_OK;
+}
+
+
+static char *
+ngx_http_log_block_sample(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+ ngx_str_t *value;
+ ngx_http_log_env_t *env = conf;
+
+ value = cf->args->elts;
+
+ return ngx_http_log_sample_rate(cf, &value[1], &env->sample);
+}
+
+
+static char *
+ngx_http_log_condition_value(ngx_conf_t *cf,
+ ngx_http_log_condition_t *lc, ngx_str_t *value)
+{
+ ngx_int_t n;
+ ngx_http_script_compile_t sc;
+ ngx_http_script_value_code_t *val;
+ ngx_http_script_complex_value_code_t *complex;
+
+ n = ngx_http_script_variables_count(value);
+
+ if (n == 0) {
+ val = ngx_http_script_start_code(cf->pool, &lc->codes,
+ sizeof(ngx_http_script_value_code_t));
+ if (val == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ n = ngx_atoi(value->data, value->len);
+
+ if (n == NGX_ERROR) {
+ n = 0;
+ }
+
+ val->code = ngx_http_script_value_code;
+ val->value = (uintptr_t) n;
+ val->text_len = (uintptr_t) value->len;
+ val->text_data = (uintptr_t) value->data;
+
+ return NGX_CONF_OK;
+ }
+
+ complex = ngx_http_script_start_code(cf->pool, &lc->codes,
+ sizeof(ngx_http_script_complex_value_code_t));
+ if (complex == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ complex->code = ngx_http_script_complex_value_code;
+ complex->lengths = NULL;
+
+ ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
+
+ sc.cf = cf;
+ sc.source = value;
+ sc.lengths = &complex->lengths;
+ sc.values = &lc->codes;
+ sc.variables = n;
+ sc.complete_lengths = 1;
+ sc.complete_values = 1;
+
+ if (ngx_http_script_compile(&sc) != NGX_OK) {
+ return NGX_CONF_ERROR;
+ }
+
+ return NGX_CONF_OK;
+}
+
+
+static char *
+ngx_http_log_condition(ngx_conf_t *cf, ngx_http_log_condition_t *lc)
+{
+ u_char *p;
+ size_t len;
+ ngx_str_t *value;
+ ngx_uint_t cur, last;
+ ngx_regex_compile_t rc;
+ ngx_http_script_code_pt *code;
+ ngx_http_script_file_code_t *fop;
+ ngx_http_script_regex_code_t *regex;
+ u_char errstr[NGX_MAX_CONF_ERRSTR];
+
+ value = cf->args->elts;
+ last = cf->args->nelts - 1;
+
+ if (value[1].len < 1 || value[1].data[0] != '(') {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid condition \"%V\"", &value[1]);
+ return NGX_CONF_ERROR;
+ }
+
+ if (value[1].len == 1) {
+ cur = 2;
+
+ } else {
+ cur = 1;
+ value[1].len--;
+ value[1].data++;
+ }
+
+ if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid condition \"%V\"", &value[last]);
+ return NGX_CONF_ERROR;
+ }
+
+ if (value[last].len == 1) {
+ last--;
+
+ } else {
+ value[last].len--;
+ value[last].data[value[last].len] = '\0';
+ }
+
+ len = value[cur].len;
+ p = value[cur].data;
+
+ if (len > 1 && p[0] == '$') {
+
+ if (cur != last && cur + 2 != last) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid condition \"%V\"", &value[cur]);
+ return NGX_CONF_ERROR;
+ }
+
+ if (ngx_http_log_condition_value(cf, lc, &value[cur])
+ != NGX_CONF_OK)
+ {
+ return NGX_CONF_ERROR;
+ }
+
+ if (cur == last) {
+ return NGX_CONF_OK;
+ }
+
+ cur++;
+
+ len = value[cur].len;
+ p = value[cur].data;
+
+ if (len == 1 && p[0] == '=') {
+
+ if (ngx_http_log_condition_value(cf, lc, &value[last])
+ != NGX_CONF_OK)
+ {
+ return NGX_CONF_ERROR;
+ }
+
+ code = ngx_http_script_start_code(cf->pool, &lc->codes,
+ sizeof(uintptr_t));
+ if (code == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ *code = ngx_http_script_equal_code;
+
+ return NGX_CONF_OK;
+ }
+
+ if (len == 2 && p[0] == '!' && p[1] == '=') {
+
+ if (ngx_http_log_condition_value(cf, lc, &value[last])
+ != NGX_CONF_OK)
+ {
+ return NGX_CONF_ERROR;
+ }
+
+ code = ngx_http_script_start_code(cf->pool, &lc->codes,
+ sizeof(uintptr_t));
+ if (code == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ *code = ngx_http_script_not_equal_code;
+ return NGX_CONF_OK;
+ }
+
+ if ((len == 1 && p[0] == '~')
+ || (len == 2 && p[0] == '~' && p[1] == '*')
+ || (len == 2 && p[0] == '!' && p[1] == '~')
+ || (len == 3 && p[0] == '!' && p[1] == '~' && p[2] == '*'))
+ {
+ regex = ngx_http_script_start_code(cf->pool, &lc->codes,
+ sizeof(ngx_http_script_regex_code_t));
+ if (regex == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
+
+ ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
+
+ rc.pattern = value[last];
+ rc.options = (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0;
+ rc.err.len = NGX_MAX_CONF_ERRSTR;
+ rc.err.data = errstr;
+
+ regex->regex = ngx_http_regex_compile(cf, &rc);
+ if (regex->regex == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ regex->code = ngx_http_script_regex_start_code;
+ regex->next = sizeof(ngx_http_script_regex_code_t);
+ regex->test = 1;
+ if (p[0] == '!') {
+ regex->negative_test = 1;
+ }
+ regex->name = value[last];
+
+ return NGX_CONF_OK;
+ }
+
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "unexpected \"%V\" in condition", &value[cur]);
+ return NGX_CONF_ERROR;
+
+ } else if ((len == 2 && p[0] == '-')
+ || (len == 3 && p[0] == '!' && p[1] == '-'))
+ {
+ if (cur + 1 != last) {
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid condition \"%V\"", &value[cur]);
+ return NGX_CONF_ERROR;
+ }
+
+ value[last].data[value[last].len] = '\0';
+ value[last].len++;
+
+ if (ngx_http_log_condition_value(cf, lc, &value[last])
+ != NGX_CONF_OK)
+ {
+ return NGX_CONF_ERROR;
+ }
+
+ fop = ngx_http_script_start_code(cf->pool, &lc->codes,
+ sizeof(ngx_http_script_file_code_t));
+ if (fop == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ fop->code = ngx_http_script_file_code;
+
+ if (p[1] == 'f') {
+ fop->op = ngx_http_script_file_plain;
+ return NGX_CONF_OK;
+ }
+
+ if (p[1] == 'd') {
+ fop->op = ngx_http_script_file_dir;
+ return NGX_CONF_OK;
+ }
+
+ if (p[1] == 'e') {
+ fop->op = ngx_http_script_file_exists;
+ return NGX_CONF_OK;
+ }
+
+ if (p[1] == 'x') {
+ fop->op = ngx_http_script_file_exec;
+ return NGX_CONF_OK;
+ }
+
+ if (p[0] == '!') {
+ if (p[2] == 'f') {
+ fop->op = ngx_http_script_file_not_plain;
+ return NGX_CONF_OK;
+ }
+
+ if (p[2] == 'd') {
+ fop->op = ngx_http_script_file_not_dir;
+ return NGX_CONF_OK;
+ }
+
+ if (p[2] == 'e') {
+ fop->op = ngx_http_script_file_not_exists;
+ return NGX_CONF_OK;
+ }
+
+ if (p[2] == 'x') {
+ fop->op = ngx_http_script_file_not_exec;
+ return NGX_CONF_OK;
+ }
+ }
+
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid condition \"%V\"", &value[cur]);
+ return NGX_CONF_ERROR;
+ }
+
+ ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+ "invalid condition \"%V\"", &value[cur]);
+
+ return NGX_CONF_ERROR;
+}
+
+
+static char *
+ngx_http_log_sample_rate(ngx_conf_t *cf, ngx_str_t *value,
+ ngx_http_log_sample_t **sample)
+{
+ u_char *p, *last;
+ uint64_t scope;
+ ngx_uint_t rp;
+
+ if (*sample) {
+ return "is duplicate";
+ }
+
+ rp = 0;
+ scope = 0;
+
+ for (last = value->data + value->len - 1;
+ last >= value->data && *last == '0';
+ last--) /* void */ ;
+
+ if (last == value->data && *last == '1') {
+ return NGX_CONF_OK;
+ }
+
+ *sample = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_sample_t));
+ if (*sample == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ for (p = value->data; p <= last; p++) {
+ if (*p == '.') {
+ if (rp == 0) {
+ rp = 1;
+ scope = 1;
+ continue;
+ } else {
+ goto invalid;
+ }
+ }
+
+ if (*p < '0' && *p > '9') {
+ goto invalid;
+ }
+
+ if (rp == 0 && *p != '0') {
+ goto invalid;
+ }
+
+ (*sample)->sample *= 10;
+ (*sample)->sample += *p - '0';
+
+ scope *= 10;
+
+ if (scope > NGX_MAX_UINT32_VALUE) {
+ goto invalid;
+ }
+ }
+
+ if ((*sample)->sample == 0) {
+ goto invalid;
+ }
+
+ (*sample)->scope = scope;
+
+ (*sample)->scatter = (*sample)->scope / (*sample)->sample;
+ if ((*sample)->scatter * (*sample)->sample != (*sample)->scope) {
+ (*sample)->scatter++;
+ }
+
+ (*sample)->inflexion = (*sample)->scope
+ - (*sample)->sample * (*sample)->scatter
+ + (*sample)->sample + 1;