@@ -36,7 +36,10 @@ static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf);
36
36
static char * ngx_http_modsecurity_merge_conf (ngx_conf_t * cf , void * parent , void * child );
37
37
static void ngx_http_modsecurity_cleanup_instance (void * data );
38
38
static void ngx_http_modsecurity_cleanup_rules (void * data );
39
+ static ngx_int_t ngx_http_modsecurity_add_variables (ngx_conf_t * cf );
40
+ static ngx_int_t ngx_http_modsecurity_status_variable (ngx_http_request_t * r , ngx_http_variable_value_t * v , uintptr_t data );
39
41
42
+ static ngx_str_t ngx_http_modsecurity_status = ngx_string ("modsecurity_status" );
40
43
41
44
/*
42
45
* PCRE malloc/free workaround, based on
@@ -223,6 +226,7 @@ ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_re
223
226
224
227
if (intervention .status != 200 )
225
228
{
229
+ ctx -> status = intervention .status ;
226
230
/**
227
231
* FIXME: this will bring proper response code to audit log in case
228
232
* when e.g. error_page redirect was triggered, but there still won't be another
@@ -284,6 +288,8 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
284
288
return NULL ;
285
289
}
286
290
291
+ ctx -> status = 0 ;
292
+
287
293
mmcf = ngx_http_get_module_main_conf (r , ngx_http_modsecurity_module );
288
294
mcf = ngx_http_get_module_loc_conf (r , ngx_http_modsecurity_module );
289
295
@@ -514,7 +520,7 @@ static ngx_command_t ngx_http_modsecurity_commands[] = {
514
520
515
521
516
522
static ngx_http_module_t ngx_http_modsecurity_ctx = {
517
- NULL , /* preconfiguration */
523
+ ngx_http_modsecurity_add_variables , /* preconfiguration */
518
524
ngx_http_modsecurity_init , /* postconfiguration */
519
525
520
526
ngx_http_modsecurity_create_main_conf , /* create main configuration */
@@ -817,4 +823,43 @@ ngx_http_modsecurity_cleanup_rules(void *data)
817
823
}
818
824
819
825
826
+ static ngx_int_t
827
+ ngx_http_modsecurity_add_variables (ngx_conf_t * cf )
828
+ {
829
+ ngx_http_variable_t * v ;
830
+
831
+ v = ngx_http_add_variable (cf , & ngx_http_modsecurity_status ,
832
+ NGX_HTTP_VAR_NOCACHEABLE );
833
+ if (v == NULL ) {
834
+ return NGX_ERROR ;
835
+ }
836
+
837
+ v -> get_handler = ngx_http_modsecurity_status_variable ;
838
+
839
+ return NGX_OK ;
840
+ }
841
+
842
+
843
+ static ngx_int_t
844
+ ngx_http_modsecurity_status_variable (ngx_http_request_t * r ,
845
+ ngx_http_variable_value_t * v , uintptr_t data )
846
+ {
847
+ ngx_http_modsecurity_ctx_t * ctx ;
848
+
849
+ ctx = ngx_http_get_module_ctx (r , ngx_http_modsecurity_module );
850
+ if (ctx == NULL || ctx -> status == 0 ) {
851
+ v -> not_found = 1 ;
852
+ return NGX_OK ;
853
+ }
854
+
855
+ v -> len = ngx_sprintf (v -> data , "%03ui" , ctx -> status ) - v -> data ;
856
+ v -> valid = 1 ;
857
+ v -> no_cacheable = 0 ;
858
+ v -> not_found = 0 ;
859
+
860
+ return NGX_OK ;
861
+ }
862
+
863
+
864
+
820
865
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
0 commit comments