forked from nginx-modules/ngx_cache_purge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_cache_purge_module.c
2152 lines (1679 loc) · 63.7 KB
/
ngx_cache_purge_module.c
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) 2009-2014, FRiCKLE <[email protected]>
* Copyright (c) 2009-2014, Piotr Sikora <[email protected]>
* All rights reserved.
*
* This project was fully funded by yo.se.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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 OF SUCH DAMAGE.
*/
#include <ngx_config.h>
#include <nginx.h>
#include <ngx_core.h>
#include <ngx_http.h>
#ifndef nginx_version
#error This module cannot be build against an unknown nginx version.
#endif
#define NGX_REPONSE_TYPE_HTML 1
#define NGX_REPONSE_TYPE_XML 2
#define NGX_REPONSE_TYPE_JSON 3
#define NGX_REPONSE_TYPE_TEXT 4
static const char ngx_http_cache_purge_content_type_json[] = "application/json";
static const char ngx_http_cache_purge_content_type_html[] = "text/html";
static const char ngx_http_cache_purge_content_type_xml[] = "text/xml";
static const char ngx_http_cache_purge_content_type_text[] = "text/plain";
static size_t ngx_http_cache_purge_content_type_json_size = sizeof(ngx_http_cache_purge_content_type_json);
static size_t ngx_http_cache_purge_content_type_html_size = sizeof(ngx_http_cache_purge_content_type_html);
static size_t ngx_http_cache_purge_content_type_xml_size = sizeof(ngx_http_cache_purge_content_type_xml);
static size_t ngx_http_cache_purge_content_type_text_size = sizeof(ngx_http_cache_purge_content_type_text);
static const char ngx_http_cache_purge_body_templ_json[] = "{\"Key\": \"%s\"}";
static const char ngx_http_cache_purge_body_templ_html[] = "<html><head><title>Successful purge</title></head><body bgcolor=\"white\"><center><h1>Successful purge</h1><p>Key : %s</p></center></body></html>";
static const char ngx_http_cache_purge_body_templ_xml[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><status><Key><![CDATA[%s]]></Key></status>";
static const char ngx_http_cache_purge_body_templ_text[] = "Key:%s\n";
static size_t ngx_http_cache_purge_body_templ_json_size = sizeof(ngx_http_cache_purge_body_templ_json);
static size_t ngx_http_cache_purge_body_templ_html_size = sizeof(ngx_http_cache_purge_body_templ_html);
static size_t ngx_http_cache_purge_body_templ_xml_size = sizeof(ngx_http_cache_purge_body_templ_xml);
static size_t ngx_http_cache_purge_body_templ_text_size = sizeof(ngx_http_cache_purge_body_templ_text);
#if (NGX_HTTP_CACHE)
typedef struct {
ngx_flag_t enable;
ngx_str_t method;
ngx_flag_t purge_all;
ngx_array_t *access; /* array of ngx_in_cidr_t */
ngx_array_t *access6; /* array of ngx_in6_cidr_t */
} ngx_http_cache_purge_conf_t;
typedef struct {
# if (NGX_HTTP_FASTCGI)
ngx_http_cache_purge_conf_t fastcgi;
# endif /* NGX_HTTP_FASTCGI */
# if (NGX_HTTP_PROXY)
ngx_http_cache_purge_conf_t proxy;
# endif /* NGX_HTTP_PROXY */
# if (NGX_HTTP_SCGI)
ngx_http_cache_purge_conf_t scgi;
# endif /* NGX_HTTP_SCGI */
# if (NGX_HTTP_UWSGI)
ngx_http_cache_purge_conf_t uwsgi;
# endif /* NGX_HTTP_UWSGI */
ngx_http_cache_purge_conf_t *conf;
ngx_http_handler_pt handler;
ngx_http_handler_pt original_handler;
ngx_uint_t resptype; /* response content-type */
} ngx_http_cache_purge_loc_conf_t;
# if (NGX_HTTP_FASTCGI)
char *ngx_http_fastcgi_cache_purge_conf(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
ngx_int_t ngx_http_fastcgi_cache_purge_handler(ngx_http_request_t *r);
# endif /* NGX_HTTP_FASTCGI */
# if (NGX_HTTP_PROXY)
char *ngx_http_proxy_cache_purge_conf(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
ngx_int_t ngx_http_proxy_cache_purge_handler(ngx_http_request_t *r);
# endif /* NGX_HTTP_PROXY */
# if (NGX_HTTP_SCGI)
char *ngx_http_scgi_cache_purge_conf(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
ngx_int_t ngx_http_scgi_cache_purge_handler(ngx_http_request_t *r);
# endif /* NGX_HTTP_SCGI */
# if (NGX_HTTP_UWSGI)
char *ngx_http_uwsgi_cache_purge_conf(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
ngx_int_t ngx_http_uwsgi_cache_purge_handler(ngx_http_request_t *r);
# endif /* NGX_HTTP_UWSGI */
char *ngx_http_cache_purge_response_type_conf(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static ngx_int_t
ngx_http_purge_file_cache_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path);
static ngx_int_t
ngx_http_purge_file_cache_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path);
ngx_int_t ngx_http_cache_purge_access_handler(ngx_http_request_t *r);
ngx_int_t ngx_http_cache_purge_access(ngx_array_t *a, ngx_array_t *a6,
struct sockaddr *s);
ngx_int_t ngx_http_cache_purge_send_response(ngx_http_request_t *r);
# if (nginx_version >= 1007009)
ngx_int_t ngx_http_cache_purge_cache_get(ngx_http_request_t *r,
ngx_http_upstream_t *u, ngx_http_file_cache_t **cache);
# endif /* nginx_version >= 1007009 */
ngx_int_t ngx_http_cache_purge_init(ngx_http_request_t *r,
ngx_http_file_cache_t *cache, ngx_http_complex_value_t *cache_key);
void ngx_http_cache_purge_handler(ngx_http_request_t *r);
ngx_int_t ngx_http_file_cache_purge(ngx_http_request_t *r);
void ngx_http_cache_purge_all(ngx_http_request_t *r, ngx_http_file_cache_t *cache);
void ngx_http_cache_purge_partial(ngx_http_request_t *r, ngx_http_file_cache_t *cache);
ngx_int_t ngx_http_cache_purge_is_partial(ngx_http_request_t *r);
char *ngx_http_cache_purge_conf(ngx_conf_t *cf,
ngx_http_cache_purge_conf_t *cpcf);
void *ngx_http_cache_purge_create_loc_conf(ngx_conf_t *cf);
char *ngx_http_cache_purge_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_command_t ngx_http_cache_purge_module_commands[] = {
# if (NGX_HTTP_FASTCGI)
{
ngx_string("fastcgi_cache_purge"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_http_fastcgi_cache_purge_conf,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
# endif /* NGX_HTTP_FASTCGI */
# if (NGX_HTTP_PROXY)
{
ngx_string("proxy_cache_purge"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_http_proxy_cache_purge_conf,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
# endif /* NGX_HTTP_PROXY */
# if (NGX_HTTP_SCGI)
{
ngx_string("scgi_cache_purge"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_http_scgi_cache_purge_conf,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
# endif /* NGX_HTTP_SCGI */
# if (NGX_HTTP_UWSGI)
{
ngx_string("uwsgi_cache_purge"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_http_uwsgi_cache_purge_conf,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL
},
# endif /* NGX_HTTP_UWSGI */
{ ngx_string("cache_purge_response_type"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_cache_purge_response_type_conf,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_cache_purge_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_cache_purge_create_loc_conf, /* create location configuration */
ngx_http_cache_purge_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_cache_purge_module = {
NGX_MODULE_V1,
&ngx_http_cache_purge_module_ctx, /* module context */
ngx_http_cache_purge_module_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
# if (NGX_HTTP_FASTCGI)
extern ngx_module_t ngx_http_fastcgi_module;
# if (nginx_version >= 1007009)
typedef struct {
ngx_array_t caches; /* ngx_http_file_cache_t * */
} ngx_http_fastcgi_main_conf_t;
# endif /* nginx_version >= 1007009 */
# if (nginx_version >= 1007008)
typedef struct {
ngx_array_t *flushes;
ngx_array_t *lengths;
ngx_array_t *values;
ngx_uint_t number;
ngx_hash_t hash;
} ngx_http_fastcgi_params_t;
# endif /* nginx_version >= 1007008 */
typedef struct {
ngx_http_upstream_conf_t upstream;
ngx_str_t index;
# if (nginx_version >= 1007008)
ngx_http_fastcgi_params_t params;
ngx_http_fastcgi_params_t params_cache;
# else
ngx_array_t *flushes;
ngx_array_t *params_len;
ngx_array_t *params;
# endif /* nginx_version >= 1007008 */
ngx_array_t *params_source;
ngx_array_t *catch_stderr;
ngx_array_t *fastcgi_lengths;
ngx_array_t *fastcgi_values;
# if (nginx_version >= 8040) && (nginx_version < 1007008)
ngx_hash_t headers_hash;
ngx_uint_t header_params;
# endif /* nginx_version >= 8040 && nginx_version < 1007008 */
# if (nginx_version >= 1001004)
ngx_flag_t keep_conn;
# endif /* nginx_version >= 1001004 */
ngx_http_complex_value_t cache_key;
# if (NGX_PCRE)
ngx_regex_t *split_regex;
ngx_str_t split_name;
# endif /* NGX_PCRE */
} ngx_http_fastcgi_loc_conf_t;
char *
ngx_http_fastcgi_cache_purge_conf(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf) {
ngx_http_compile_complex_value_t ccv;
ngx_http_cache_purge_loc_conf_t *cplcf;
ngx_http_core_loc_conf_t *clcf;
ngx_http_fastcgi_loc_conf_t *flcf;
ngx_str_t *value;
# if (nginx_version >= 1007009)
ngx_http_complex_value_t cv;
# endif /* nginx_version >= 1007009 */
cplcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_cache_purge_module);
/* check for duplicates / collisions */
if (cplcf->fastcgi.enable != NGX_CONF_UNSET) {
return "is duplicate";
}
if (cf->args->nelts != 3) {
return ngx_http_cache_purge_conf(cf, &cplcf->fastcgi);
}
if (cf->cmd_type & (NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF)) {
return "(separate location syntax) is not allowed here";
}
flcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_fastcgi_module);
# if (nginx_version >= 1007009)
if (flcf->upstream.cache > 0)
# else
if (flcf->upstream.cache != NGX_CONF_UNSET_PTR
&& flcf->upstream.cache != NULL)
# endif /* nginx_version >= 1007009 */
{
return "is incompatible with \"fastcgi_cache\"";
}
if (flcf->upstream.upstream || flcf->fastcgi_lengths) {
return "is incompatible with \"fastcgi_pass\"";
}
if (flcf->upstream.store > 0
# if (nginx_version < 1007009)
|| flcf->upstream.store_lengths
# endif /* nginx_version >= 1007009 */
) {
return "is incompatible with \"fastcgi_store\"";
}
value = cf->args->elts;
/* set fastcgi_cache part */
# if (nginx_version >= 1007009)
flcf->upstream.cache = 1;
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = &cv;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
if (cv.lengths != NULL) {
flcf->upstream.cache_value = ngx_palloc(cf->pool,
sizeof(ngx_http_complex_value_t));
if (flcf->upstream.cache_value == NULL) {
return NGX_CONF_ERROR;
}
*flcf->upstream.cache_value = cv;
} else {
flcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
&ngx_http_fastcgi_module);
if (flcf->upstream.cache_zone == NULL) {
return NGX_CONF_ERROR;
}
}
# else
flcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
&ngx_http_fastcgi_module);
if (flcf->upstream.cache == NULL) {
return NGX_CONF_ERROR;
}
# endif /* nginx_version >= 1007009 */
/* set fastcgi_cache_key part */
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[2];
ccv.complex_value = &flcf->cache_key;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
/* set handler */
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
cplcf->fastcgi.enable = 0;
cplcf->conf = &cplcf->fastcgi;
clcf->handler = ngx_http_fastcgi_cache_purge_handler;
return NGX_CONF_OK;
}
ngx_int_t
ngx_http_fastcgi_cache_purge_handler(ngx_http_request_t *r) {
ngx_http_file_cache_t *cache;
ngx_http_fastcgi_loc_conf_t *flcf;
ngx_http_cache_purge_loc_conf_t *cplcf;
# if (nginx_version >= 1007009)
ngx_http_fastcgi_main_conf_t *fmcf;
ngx_int_t rc;
# endif /* nginx_version >= 1007009 */
if (ngx_http_upstream_create(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
r->upstream->conf = &flcf->upstream;
# if (nginx_version >= 1007009)
fmcf = ngx_http_get_module_main_conf(r, ngx_http_fastcgi_module);
r->upstream->caches = &fmcf->caches;
rc = ngx_http_cache_purge_cache_get(r, r->upstream, &cache);
if (rc != NGX_OK) {
return rc;
}
# else
cache = flcf->upstream.cache->data;
# endif /* nginx_version >= 1007009 */
if (ngx_http_cache_purge_init(r, cache, &flcf->cache_key) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* Purge-all option */
cplcf = ngx_http_get_module_loc_conf(r, ngx_http_cache_purge_module);
if (cplcf->conf->purge_all) {
ngx_http_cache_purge_all(r, cache);
} else {
if (ngx_http_cache_purge_is_partial(r)) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache purge with partial enabled");
ngx_http_cache_purge_partial(r, cache);
}
}
# if (nginx_version >= 8011)
r->main->count++;
# endif
ngx_http_cache_purge_handler(r);
return NGX_DONE;
}
# endif /* NGX_HTTP_FASTCGI */
# if (NGX_HTTP_PROXY)
extern ngx_module_t ngx_http_proxy_module;
typedef struct {
ngx_str_t key_start;
ngx_str_t schema;
ngx_str_t host_header;
ngx_str_t port;
ngx_str_t uri;
} ngx_http_proxy_vars_t;
# if (nginx_version >= 1007009)
typedef struct {
ngx_array_t caches; /* ngx_http_file_cache_t * */
} ngx_http_proxy_main_conf_t;
# endif /* nginx_version >= 1007009 */
# if (nginx_version >= 1007008)
typedef struct {
ngx_array_t *flushes;
ngx_array_t *lengths;
ngx_array_t *values;
ngx_hash_t hash;
} ngx_http_proxy_headers_t;
# endif /* nginx_version >= 1007008 */
typedef struct {
ngx_http_upstream_conf_t upstream;
# if (nginx_version >= 1007008)
ngx_array_t *body_flushes;
ngx_array_t *body_lengths;
ngx_array_t *body_values;
ngx_str_t body_source;
ngx_http_proxy_headers_t headers;
ngx_http_proxy_headers_t headers_cache;
# else
ngx_array_t *flushes;
ngx_array_t *body_set_len;
ngx_array_t *body_set;
ngx_array_t *headers_set_len;
ngx_array_t *headers_set;
ngx_hash_t headers_set_hash;
# endif /* nginx_version >= 1007008 */
ngx_array_t *headers_source;
# if (nginx_version < 8040)
ngx_array_t *headers_names;
# endif /* nginx_version < 8040 */
ngx_array_t *proxy_lengths;
ngx_array_t *proxy_values;
ngx_array_t *redirects;
# if (nginx_version >= 1001015)
ngx_array_t *cookie_domains;
ngx_array_t *cookie_paths;
# endif /* nginx_version >= 1001015 */
# if (nginx_version < 1007008)
ngx_str_t body_source;
# endif /* nginx_version < 1007008 */
# if (nginx_version >= 1011006)
ngx_http_complex_value_t *method;
# else
ngx_str_t method;
# endif /* nginx_version >= 1011006 */
ngx_str_t location;
ngx_str_t url;
ngx_http_complex_value_t cache_key;
ngx_http_proxy_vars_t vars;
ngx_flag_t redirect;
# if (nginx_version >= 1001004)
ngx_uint_t http_version;
# endif /* nginx_version >= 1001004 */
ngx_uint_t headers_hash_max_size;
ngx_uint_t headers_hash_bucket_size;
# if (NGX_HTTP_SSL)
# if (nginx_version >= 1005006)
ngx_uint_t ssl;
ngx_uint_t ssl_protocols;
ngx_str_t ssl_ciphers;
# endif /* nginx_version >= 1005006 */
# if (nginx_version >= 1007000)
ngx_uint_t ssl_verify_depth;
ngx_str_t ssl_trusted_certificate;
ngx_str_t ssl_crl;
# endif /* nginx_version >= 1007000 */
# if (nginx_version >= 1007008)
ngx_str_t ssl_certificate;
ngx_str_t ssl_certificate_key;
ngx_array_t *ssl_passwords;
# endif /* nginx_version >= 1007008 */
# endif
} ngx_http_proxy_loc_conf_t;
char *
ngx_http_proxy_cache_purge_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_compile_complex_value_t ccv;
ngx_http_cache_purge_loc_conf_t *cplcf;
ngx_http_core_loc_conf_t *clcf;
ngx_http_proxy_loc_conf_t *plcf;
ngx_str_t *value;
# if (nginx_version >= 1007009)
ngx_http_complex_value_t cv;
# endif /* nginx_version >= 1007009 */
cplcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_cache_purge_module);
/* check for duplicates / collisions */
if (cplcf->proxy.enable != NGX_CONF_UNSET) {
return "is duplicate";
}
if (cf->args->nelts != 3) {
return ngx_http_cache_purge_conf(cf, &cplcf->proxy);
}
if (cf->cmd_type & (NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF)) {
return "(separate location syntax) is not allowed here";
}
plcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_proxy_module);
# if (nginx_version >= 1007009)
if (plcf->upstream.cache > 0)
# else
if (plcf->upstream.cache != NGX_CONF_UNSET_PTR
&& plcf->upstream.cache != NULL)
# endif /* nginx_version >= 1007009 */
{
return "is incompatible with \"proxy_cache\"";
}
if (plcf->upstream.upstream || plcf->proxy_lengths) {
return "is incompatible with \"proxy_pass\"";
}
if (plcf->upstream.store > 0
# if (nginx_version < 1007009)
|| plcf->upstream.store_lengths
# endif /* nginx_version >= 1007009 */
) {
return "is incompatible with \"proxy_store\"";
}
value = cf->args->elts;
/* set proxy_cache part */
# if (nginx_version >= 1007009)
plcf->upstream.cache = 1;
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = &cv;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
if (cv.lengths != NULL) {
plcf->upstream.cache_value = ngx_palloc(cf->pool,
sizeof(ngx_http_complex_value_t));
if (plcf->upstream.cache_value == NULL) {
return NGX_CONF_ERROR;
}
*plcf->upstream.cache_value = cv;
} else {
plcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
&ngx_http_proxy_module);
if (plcf->upstream.cache_zone == NULL) {
return NGX_CONF_ERROR;
}
}
# else
plcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
&ngx_http_proxy_module);
if (plcf->upstream.cache == NULL) {
return NGX_CONF_ERROR;
}
# endif /* nginx_version >= 1007009 */
/* set proxy_cache_key part */
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[2];
ccv.complex_value = &plcf->cache_key;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
/* set handler */
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
cplcf->proxy.enable = 0;
cplcf->conf = &cplcf->proxy;
clcf->handler = ngx_http_proxy_cache_purge_handler;
return NGX_CONF_OK;
}
ngx_int_t
ngx_http_proxy_cache_purge_handler(ngx_http_request_t *r) {
ngx_http_file_cache_t *cache;
ngx_http_proxy_loc_conf_t *plcf;
ngx_http_cache_purge_loc_conf_t *cplcf;
# if (nginx_version >= 1007009)
ngx_http_proxy_main_conf_t *pmcf;
ngx_int_t rc;
# endif /* nginx_version >= 1007009 */
if (ngx_http_upstream_create(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
r->upstream->conf = &plcf->upstream;
# if (nginx_version >= 1007009)
pmcf = ngx_http_get_module_main_conf(r, ngx_http_proxy_module);
r->upstream->caches = &pmcf->caches;
rc = ngx_http_cache_purge_cache_get(r, r->upstream, &cache);
if (rc != NGX_OK) {
return rc;
}
# else
cache = plcf->upstream.cache->data;
# endif /* nginx_version >= 1007009 */
if (ngx_http_cache_purge_init(r, cache, &plcf->cache_key) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* Purge-all option */
cplcf = ngx_http_get_module_loc_conf(r, ngx_http_cache_purge_module);
if (cplcf->conf->purge_all) {
ngx_http_cache_purge_all(r, cache);
} else {
if (ngx_http_cache_purge_is_partial(r)) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache purge with partial enabled");
ngx_http_cache_purge_partial(r, cache);
}
}
# if (nginx_version >= 8011)
r->main->count++;
# endif
ngx_http_cache_purge_handler(r);
return NGX_DONE;
}
# endif /* NGX_HTTP_PROXY */
# if (NGX_HTTP_SCGI)
extern ngx_module_t ngx_http_scgi_module;
# if (nginx_version >= 1007009)
typedef struct {
ngx_array_t caches; /* ngx_http_file_cache_t * */
} ngx_http_scgi_main_conf_t;
# endif /* nginx_version >= 1007009 */
# if (nginx_version >= 1007008)
typedef struct {
ngx_array_t *flushes;
ngx_array_t *lengths;
ngx_array_t *values;
ngx_uint_t number;
ngx_hash_t hash;
} ngx_http_scgi_params_t;
# endif /* nginx_version >= 1007008 */
typedef struct {
ngx_http_upstream_conf_t upstream;
# if (nginx_version >= 1007008)
ngx_http_scgi_params_t params;
ngx_http_scgi_params_t params_cache;
ngx_array_t *params_source;
# else
ngx_array_t *flushes;
ngx_array_t *params_len;
ngx_array_t *params;
ngx_array_t *params_source;
ngx_hash_t headers_hash;
ngx_uint_t header_params;
# endif /* nginx_version >= 1007008 */
ngx_array_t *scgi_lengths;
ngx_array_t *scgi_values;
ngx_http_complex_value_t cache_key;
} ngx_http_scgi_loc_conf_t;
char *
ngx_http_scgi_cache_purge_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_compile_complex_value_t ccv;
ngx_http_cache_purge_loc_conf_t *cplcf;
ngx_http_core_loc_conf_t *clcf;
ngx_http_scgi_loc_conf_t *slcf;
ngx_str_t *value;
# if (nginx_version >= 1007009)
ngx_http_complex_value_t cv;
# endif /* nginx_version >= 1007009 */
cplcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_cache_purge_module);
/* check for duplicates / collisions */
if (cplcf->scgi.enable != NGX_CONF_UNSET) {
return "is duplicate";
}
if (cf->args->nelts != 3) {
return ngx_http_cache_purge_conf(cf, &cplcf->scgi);
}
if (cf->cmd_type & (NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF)) {
return "(separate location syntax) is not allowed here";
}
slcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_scgi_module);
# if (nginx_version >= 1007009)
if (slcf->upstream.cache > 0)
# else
if (slcf->upstream.cache != NGX_CONF_UNSET_PTR
&& slcf->upstream.cache != NULL)
# endif /* nginx_version >= 1007009 */
{
return "is incompatible with \"scgi_cache\"";
}
if (slcf->upstream.upstream || slcf->scgi_lengths) {
return "is incompatible with \"scgi_pass\"";
}
if (slcf->upstream.store > 0
# if (nginx_version < 1007009)
|| slcf->upstream.store_lengths
# endif /* nginx_version >= 1007009 */
) {
return "is incompatible with \"scgi_store\"";
}
value = cf->args->elts;
/* set scgi_cache part */
# if (nginx_version >= 1007009)
slcf->upstream.cache = 1;
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = &cv;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
if (cv.lengths != NULL) {
slcf->upstream.cache_value = ngx_palloc(cf->pool,
sizeof(ngx_http_complex_value_t));
if (slcf->upstream.cache_value == NULL) {
return NGX_CONF_ERROR;
}
*slcf->upstream.cache_value = cv;
} else {
slcf->upstream.cache_zone = ngx_shared_memory_add(cf, &value[1], 0,
&ngx_http_scgi_module);
if (slcf->upstream.cache_zone == NULL) {
return NGX_CONF_ERROR;
}
}
# else
slcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
&ngx_http_scgi_module);
if (slcf->upstream.cache == NULL) {
return NGX_CONF_ERROR;
}
# endif /* nginx_version >= 1007009 */
/* set scgi_cache_key part */
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[2];
ccv.complex_value = &slcf->cache_key;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
/* set handler */
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
cplcf->scgi.enable = 0;
cplcf->conf = &cplcf->scgi;
clcf->handler = ngx_http_scgi_cache_purge_handler;
return NGX_CONF_OK;
}
ngx_int_t
ngx_http_scgi_cache_purge_handler(ngx_http_request_t *r) {
ngx_http_file_cache_t *cache;
ngx_http_scgi_loc_conf_t *slcf;
ngx_http_cache_purge_loc_conf_t *cplcf;
# if (nginx_version >= 1007009)
ngx_http_scgi_main_conf_t *smcf;
ngx_int_t rc;
# endif /* nginx_version >= 1007009 */
if (ngx_http_upstream_create(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
slcf = ngx_http_get_module_loc_conf(r, ngx_http_scgi_module);
r->upstream->conf = &slcf->upstream;
# if (nginx_version >= 1007009)
smcf = ngx_http_get_module_main_conf(r, ngx_http_scgi_module);
r->upstream->caches = &smcf->caches;
rc = ngx_http_cache_purge_cache_get(r, r->upstream, &cache);
if (rc != NGX_OK) {
return rc;
}
# else
cache = slcf->upstream.cache->data;
# endif /* nginx_version >= 1007009 */
if (ngx_http_cache_purge_init(r, cache, &slcf->cache_key) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* Purge-all option */
cplcf = ngx_http_get_module_loc_conf(r, ngx_http_cache_purge_module);
if (cplcf->conf->purge_all) {
ngx_http_cache_purge_all(r, cache);
} else {
if (ngx_http_cache_purge_is_partial(r)) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache purge with partial enabled");
ngx_http_cache_purge_partial(r, cache);
}
}
# if (nginx_version >= 8011)
r->main->count++;
# endif
ngx_http_cache_purge_handler(r);
return NGX_DONE;
}
# endif /* NGX_HTTP_SCGI */
# if (NGX_HTTP_UWSGI)
extern ngx_module_t ngx_http_uwsgi_module;
# if (nginx_version >= 1007009)
typedef struct {
ngx_array_t caches; /* ngx_http_file_cache_t * */
} ngx_http_uwsgi_main_conf_t;