43
43
typedef struct {
44
44
ngx_flag_t enable ;
45
45
ngx_str_t method ;
46
+ ngx_flag_t purge_all ;
46
47
ngx_array_t * access ; /* array of ngx_in_cidr_t */
47
48
ngx_array_t * access6 ; /* array of ngx_in6_cidr_t */
48
49
} ngx_http_cache_purge_conf_t ;
@@ -90,6 +91,11 @@ char *ngx_http_uwsgi_cache_purge_conf(ngx_conf_t *cf,
90
91
ngx_int_t ngx_http_uwsgi_cache_purge_handler (ngx_http_request_t * r );
91
92
# endif /* NGX_HTTP_UWSGI */
92
93
94
+ static ngx_int_t
95
+ ngx_http_purge_file_cache_noop (ngx_tree_ctx_t * ctx , ngx_str_t * path );
96
+ static ngx_int_t
97
+ ngx_http_purge_file_cache_delete_file (ngx_tree_ctx_t * ctx , ngx_str_t * path );
98
+
93
99
ngx_int_t ngx_http_cache_purge_access_handler (ngx_http_request_t * r );
94
100
ngx_int_t ngx_http_cache_purge_access (ngx_array_t * a , ngx_array_t * a6 ,
95
101
struct sockaddr * s );
@@ -105,6 +111,9 @@ void ngx_http_cache_purge_handler(ngx_http_request_t *r);
105
111
106
112
ngx_int_t ngx_http_file_cache_purge (ngx_http_request_t * r );
107
113
114
+
115
+ void ngx_http_cache_purge_all (ngx_http_request_t * r , ngx_http_file_cache_t * cache );
116
+
108
117
char * ngx_http_cache_purge_conf (ngx_conf_t * cf ,
109
118
ngx_http_cache_purge_conf_t * cpcf );
110
119
@@ -378,11 +387,12 @@ ngx_http_fastcgi_cache_purge_conf(ngx_conf_t *cf, ngx_command_t *cmd,
378
387
ngx_int_t
379
388
ngx_http_fastcgi_cache_purge_handler (ngx_http_request_t * r )
380
389
{
381
- ngx_http_file_cache_t * cache ;
382
- ngx_http_fastcgi_loc_conf_t * flcf ;
390
+ ngx_http_file_cache_t * cache ;
391
+ ngx_http_fastcgi_loc_conf_t * flcf ;
392
+ ngx_http_cache_purge_loc_conf_t * cplcf ;
383
393
# if (nginx_version >= 1007009 )
384
- ngx_http_fastcgi_main_conf_t * fmcf ;
385
- ngx_int_t rc ;
394
+ ngx_http_fastcgi_main_conf_t * fmcf ;
395
+ ngx_int_t rc ;
386
396
# endif /* nginx_version >= 1007009 */
387
397
388
398
if (ngx_http_upstream_create (r ) != NGX_OK ) {
@@ -414,6 +424,12 @@ ngx_http_fastcgi_cache_purge_handler(ngx_http_request_t *r)
414
424
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
415
425
}
416
426
427
+ // Purge-all option
428
+ cplcf = ngx_http_get_module_loc_conf (r , ngx_http_cache_purge_module );
429
+ if (cplcf -> conf -> purge_all ) {
430
+ ngx_http_cache_purge_all (r , cache );
431
+ }
432
+
417
433
# if (nginx_version >= 8011 )
418
434
r -> main -> count ++ ;
419
435
# endif
@@ -649,11 +665,12 @@ ngx_http_proxy_cache_purge_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
649
665
ngx_int_t
650
666
ngx_http_proxy_cache_purge_handler (ngx_http_request_t * r )
651
667
{
652
- ngx_http_file_cache_t * cache ;
653
- ngx_http_proxy_loc_conf_t * plcf ;
668
+ ngx_http_file_cache_t * cache ;
669
+ ngx_http_proxy_loc_conf_t * plcf ;
670
+ ngx_http_cache_purge_loc_conf_t * cplcf ;
654
671
# if (nginx_version >= 1007009 )
655
- ngx_http_proxy_main_conf_t * pmcf ;
656
- ngx_int_t rc ;
672
+ ngx_http_proxy_main_conf_t * pmcf ;
673
+ ngx_int_t rc ;
657
674
# endif /* nginx_version >= 1007009 */
658
675
659
676
if (ngx_http_upstream_create (r ) != NGX_OK ) {
@@ -685,6 +702,12 @@ ngx_http_proxy_cache_purge_handler(ngx_http_request_t *r)
685
702
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
686
703
}
687
704
705
+ // Purge-all option
706
+ cplcf = ngx_http_get_module_loc_conf (r , ngx_http_cache_purge_module );
707
+ if (cplcf -> conf -> purge_all ) {
708
+ ngx_http_cache_purge_all (r , cache );
709
+ }
710
+
688
711
# if (nginx_version >= 8011 )
689
712
r -> main -> count ++ ;
690
713
# endif
@@ -862,11 +885,12 @@ ngx_http_scgi_cache_purge_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
862
885
ngx_int_t
863
886
ngx_http_scgi_cache_purge_handler (ngx_http_request_t * r )
864
887
{
865
- ngx_http_file_cache_t * cache ;
866
- ngx_http_scgi_loc_conf_t * slcf ;
888
+ ngx_http_file_cache_t * cache ;
889
+ ngx_http_scgi_loc_conf_t * slcf ;
890
+ ngx_http_cache_purge_loc_conf_t * cplcf ;
867
891
# if (nginx_version >= 1007009 )
868
- ngx_http_scgi_main_conf_t * smcf ;
869
- ngx_int_t rc ;
892
+ ngx_http_scgi_main_conf_t * smcf ;
893
+ ngx_int_t rc ;
870
894
# endif /* nginx_version >= 1007009 */
871
895
872
896
if (ngx_http_upstream_create (r ) != NGX_OK ) {
@@ -898,6 +922,12 @@ ngx_http_scgi_cache_purge_handler(ngx_http_request_t *r)
898
922
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
899
923
}
900
924
925
+ // Purge-all option
926
+ cplcf = ngx_http_get_module_loc_conf (r , ngx_http_cache_purge_module );
927
+ if (cplcf -> conf -> purge_all ) {
928
+ ngx_http_cache_purge_all (r , cache );
929
+ }
930
+
901
931
# if (nginx_version >= 8011 )
902
932
r -> main -> count ++ ;
903
933
# endif
@@ -1098,11 +1128,12 @@ ngx_http_uwsgi_cache_purge_conf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1098
1128
ngx_int_t
1099
1129
ngx_http_uwsgi_cache_purge_handler (ngx_http_request_t * r )
1100
1130
{
1101
- ngx_http_file_cache_t * cache ;
1102
- ngx_http_uwsgi_loc_conf_t * ulcf ;
1131
+ ngx_http_file_cache_t * cache ;
1132
+ ngx_http_uwsgi_loc_conf_t * ulcf ;
1133
+ ngx_http_cache_purge_loc_conf_t * cplcf ;
1103
1134
# if (nginx_version >= 1007009 )
1104
- ngx_http_uwsgi_main_conf_t * umcf ;
1105
- ngx_int_t rc ;
1135
+ ngx_http_uwsgi_main_conf_t * umcf ;
1136
+ ngx_int_t rc ;
1106
1137
# endif /* nginx_version >= 1007009 */
1107
1138
1108
1139
if (ngx_http_upstream_create (r ) != NGX_OK ) {
@@ -1134,6 +1165,12 @@ ngx_http_uwsgi_cache_purge_handler(ngx_http_request_t *r)
1134
1165
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
1135
1166
}
1136
1167
1168
+ // Purge-all option
1169
+ cplcf = ngx_http_get_module_loc_conf (r , ngx_http_cache_purge_module );
1170
+ if (cplcf -> conf -> purge_all ) {
1171
+ ngx_http_cache_purge_all (r , cache );
1172
+ }
1173
+
1137
1174
# if (nginx_version >= 8011 )
1138
1175
r -> main -> count ++ ;
1139
1176
# endif
@@ -1144,6 +1181,27 @@ ngx_http_uwsgi_cache_purge_handler(ngx_http_request_t *r)
1144
1181
}
1145
1182
# endif /* NGX_HTTP_UWSGI */
1146
1183
1184
+
1185
+ static ngx_int_t
1186
+ ngx_http_purge_file_cache_noop (ngx_tree_ctx_t * ctx , ngx_str_t * path )
1187
+ {
1188
+ return NGX_OK ;
1189
+ }
1190
+
1191
+ static ngx_int_t
1192
+ ngx_http_purge_file_cache_delete_file (ngx_tree_ctx_t * ctx , ngx_str_t * path )
1193
+ {
1194
+ ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , ctx -> log , 0 ,
1195
+ "http file cache delete: \"%s\"" , path -> data );
1196
+
1197
+ if (ngx_delete_file (path -> data ) == NGX_FILE_ERROR ) {
1198
+ ngx_log_error (NGX_LOG_CRIT , ctx -> log , ngx_errno ,
1199
+ ngx_delete_file_n " \"%s\" failed" , path -> data );
1200
+ }
1201
+
1202
+ return NGX_OK ;
1203
+ }
1204
+
1147
1205
ngx_int_t
1148
1206
ngx_http_cache_purge_access_handler (ngx_http_request_t * r )
1149
1207
{
@@ -1401,6 +1459,7 @@ ngx_http_cache_purge_init(ngx_http_request_t *r, ngx_http_file_cache_t *cache,
1401
1459
void
1402
1460
ngx_http_cache_purge_handler (ngx_http_request_t * r )
1403
1461
{
1462
+ ngx_http_cache_purge_loc_conf_t * cplcf ;
1404
1463
ngx_int_t rc ;
1405
1464
1406
1465
# if (NGX_HAVE_FILE_AIO )
@@ -1409,11 +1468,17 @@ ngx_http_cache_purge_handler(ngx_http_request_t *r)
1409
1468
}
1410
1469
# endif
1411
1470
1412
- rc = ngx_http_file_cache_purge (r );
1471
+ cplcf = ngx_http_get_module_loc_conf (r , ngx_http_cache_purge_module );
1472
+ if (cplcf -> conf -> purge_all ) {
1473
+ rc = NGX_OK ;
1474
+ }
1475
+ else {
1476
+ rc = ngx_http_file_cache_purge (r );
1413
1477
1414
- ngx_log_debug2 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
1478
+ ngx_log_debug2 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
1415
1479
"http file cache purge: %i, \"%s\"" ,
1416
1480
rc , r -> cache -> file .name .data );
1481
+ }
1417
1482
1418
1483
switch (rc ) {
1419
1484
case NGX_OK :
@@ -1499,6 +1564,27 @@ ngx_http_file_cache_purge(ngx_http_request_t *r)
1499
1564
return NGX_OK ;
1500
1565
}
1501
1566
1567
+
1568
+ void
1569
+ ngx_http_cache_purge_all (ngx_http_request_t * r , ngx_http_file_cache_t * cache ) {
1570
+ ngx_log_debug (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
1571
+ "purge_all http in %s" ,
1572
+ cache -> path -> name .data );
1573
+
1574
+ // Walk the tree and remove all the files
1575
+ ngx_tree_ctx_t tree ;
1576
+ tree .init_handler = NULL ;
1577
+ tree .file_handler = ngx_http_purge_file_cache_delete_file ;
1578
+ tree .pre_tree_handler = ngx_http_purge_file_cache_noop ;
1579
+ tree .post_tree_handler = ngx_http_purge_file_cache_noop ;
1580
+ tree .spec_handler = ngx_http_purge_file_cache_noop ;
1581
+ tree .data = cache ;
1582
+ tree .alloc = 0 ;
1583
+ tree .log = ngx_cycle -> log ;
1584
+
1585
+ ngx_walk_tree (& tree , & cache -> path -> name );
1586
+ }
1587
+
1502
1588
char *
1503
1589
ngx_http_cache_purge_conf (ngx_conf_t * cf , ngx_http_cache_purge_conf_t * cpcf )
1504
1590
{
@@ -1510,7 +1596,11 @@ ngx_http_cache_purge_conf(ngx_conf_t *cf, ngx_http_cache_purge_conf_t *cpcf)
1510
1596
ngx_str_t * value ;
1511
1597
ngx_int_t rc ;
1512
1598
ngx_uint_t i ;
1599
+ ngx_uint_t from_position ;
1513
1600
1601
+ from_position = 2 ;
1602
+
1603
+ // xxx_cache_purge on|off|<method> [purge_all] [from all|<ip> [.. <ip>]]
1514
1604
value = cf -> args -> elts ;
1515
1605
1516
1606
if (ngx_strcmp (value [1 ].data , "off" ) == 0 ) {
@@ -1529,20 +1619,27 @@ ngx_http_cache_purge_conf(ngx_conf_t *cf, ngx_http_cache_purge_conf_t *cpcf)
1529
1619
return NGX_CONF_OK ;
1530
1620
}
1531
1621
1622
+ // We will purge all the keys
1623
+ if (ngx_strcmp (value [from_position ].data , "purge_all" ) == 0 ) {
1624
+ cpcf -> purge_all = 1 ;
1625
+ from_position ++ ;
1626
+ }
1627
+
1628
+
1532
1629
/* sanity check */
1533
- if (ngx_strcmp (value [2 ].data , "from" ) != 0 ) {
1630
+ if (ngx_strcmp (value [from_position ].data , "from" ) != 0 ) {
1534
1631
ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
1535
1632
"invalid parameter \"%V\", expected"
1536
- " \"from\" keyword" , & value [2 ]);
1633
+ " \"from\" keyword" , & value [from_position ]);
1537
1634
return NGX_CONF_ERROR ;
1538
1635
}
1539
1636
1540
- if (ngx_strcmp (value [3 ].data , "all" ) == 0 ) {
1637
+ if (ngx_strcmp (value [from_position + 1 ].data , "all" ) == 0 ) {
1541
1638
cpcf -> enable = 1 ;
1542
1639
return NGX_CONF_OK ;
1543
1640
}
1544
1641
1545
- for (i = 3 ; i < cf -> args -> nelts ; i ++ ) {
1642
+ for (i = ( from_position + 1 ) ; i < cf -> args -> nelts ; i ++ ) {
1546
1643
rc = ngx_ptocidr (& value [i ], & cidr );
1547
1644
1548
1645
if (rc == NGX_ERROR ) {
@@ -1560,7 +1657,7 @@ ngx_http_cache_purge_conf(ngx_conf_t *cf, ngx_http_cache_purge_conf_t *cpcf)
1560
1657
switch (cidr .family ) {
1561
1658
case AF_INET :
1562
1659
if (cpcf -> access == NULL ) {
1563
- cpcf -> access = ngx_array_create (cf -> pool , cf -> args -> nelts - 3 ,
1660
+ cpcf -> access = ngx_array_create (cf -> pool , cf -> args -> nelts - ( from_position + 1 ) ,
1564
1661
sizeof (ngx_in_cidr_t ));
1565
1662
if (cpcf -> access == NULL ) {
1566
1663
return NGX_CONF_ERROR ;
@@ -1580,7 +1677,7 @@ ngx_http_cache_purge_conf(ngx_conf_t *cf, ngx_http_cache_purge_conf_t *cpcf)
1580
1677
# if (NGX_HAVE_INET6 )
1581
1678
case AF_INET6 :
1582
1679
if (cpcf -> access6 == NULL ) {
1583
- cpcf -> access6 = ngx_array_create (cf -> pool , cf -> args -> nelts - 3 ,
1680
+ cpcf -> access6 = ngx_array_create (cf -> pool , cf -> args -> nelts - ( from_position + 1 ) ,
1584
1681
sizeof (ngx_in6_cidr_t ));
1585
1682
if (cpcf -> access6 == NULL ) {
1586
1683
return NGX_CONF_ERROR ;
@@ -1613,6 +1710,7 @@ ngx_http_cache_purge_merge_conf(ngx_http_cache_purge_conf_t *conf,
1613
1710
if (prev -> enable == 1 ) {
1614
1711
conf -> enable = prev -> enable ;
1615
1712
conf -> method = prev -> method ;
1713
+ conf -> purge_all = prev -> purge_all ;
1616
1714
conf -> access = prev -> access ;
1617
1715
conf -> access6 = prev -> access6 ;
1618
1716
0 commit comments