Skip to content

Commit 7aa12d0

Browse files
authored
Merge pull request #2 from awmackowiak/am-logging
Fix after review
2 parents 75cf7a4 + d386de9 commit 7aa12d0

7 files changed

+46
-103
lines changed

Diff for: src/ngx_http_modsecurity_body_filter.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,17 @@ ngx_http_modsecurity_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
156156

157157
if (is_request_processed) {
158158
ngx_pool_t *old_pool;
159-
struct timeval start_tv;
160-
ngx_gettimeofday(&start_tv);
159+
struct timespec start_tv;
160+
(void) clock_gettime(CLOCK_MONOTONIC, &start_tv);
161161

162162
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
163163
msc_process_response_body(ctx->modsec_transaction);
164164
ngx_http_modsecurity_pcre_malloc_done(old_pool);
165165

166166
/* XXX: I don't get how body from modsec being transferred to nginx's buffer. If so - after adjusting of nginx's
167167
XXX: body we can proceed to adjust body size (content-length). see xslt_body_filter() for example */
168-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
169168
ctx->resp_body_phase_time = ngx_http_modsecurity_compute_processing_time(start_tv);
169+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
170170
if (ret > 0) {
171171
return ret;
172172
}

Diff for: src/ngx_http_modsecurity_common.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,6 @@ ngx_int_t ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r);
169169
/* ngx_http_modsecurity_rewrite.c */
170170
ngx_int_t ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r);
171171

172-
ngx_msec_int_t ngx_http_modsecurity_compute_processing_time(struct timeval tv);
172+
ngx_msec_int_t ngx_http_modsecurity_compute_processing_time(struct timespec tv);
173173

174174
#endif /* _NGX_HTTP_MODSECURITY_COMMON_H_INCLUDED_ */

Diff for: src/ngx_http_modsecurity_header_filter.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
446446
return ngx_http_next_header_filter(r);
447447
}
448448

449-
struct timeval start_tv;
450-
ngx_gettimeofday(&start_tv);
449+
struct timespec start_tv;
450+
(void) clock_gettime(CLOCK_MONOTONIC, &start_tv);
451451

452452
/*
453453
* Lets ask nginx to keep the response body in memory
@@ -527,12 +527,10 @@ ngx_http_modsecurity_header_filter(ngx_http_request_t *r)
527527
#endif
528528

529529
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
530-
msc_process_response_headers(ctx->modsec_transaction, status, http_response_ver);
530+
msc_process_response_headers(ctx->modsec_transaction, status, http_response_ver);
531531
ngx_http_modsecurity_pcre_malloc_done(old_pool);
532-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
533-
534532
ctx->resp_headers_phase_time = ngx_http_modsecurity_compute_processing_time(start_tv);
535-
533+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);
536534
if (r->error_page) {
537535
return ngx_http_next_header_filter(r);
538536
}

Diff for: src/ngx_http_modsecurity_log.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ ngx_http_modsecurity_log_handler(ngx_http_request_t *r)
7171
dd("already logged earlier");
7272
return NGX_OK;
7373
}
74-
struct timeval start_tv;
75-
ngx_gettimeofday(&start_tv);
74+
struct timespec start_tv;
75+
(void) clock_gettime(CLOCK_MONOTONIC, &start_tv);
7676

7777
dd("calling msc_process_logging for %p", ctx);
7878
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);

Diff for: src/ngx_http_modsecurity_module.c

+30-80
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,7 @@ static char *ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void
3333
static void ngx_http_modsecurity_cleanup_instance(void *data);
3434
static void ngx_http_modsecurity_cleanup_rules(void *data);
3535

36-
static ngx_int_t ngx_http_modsecurity_req_headers_phase_time(ngx_http_request_t *r,
37-
ngx_http_variable_value_t *v, uintptr_t data);
38-
static ngx_int_t ngx_http_modsecurity_req_body_phase_time(ngx_http_request_t *r,
39-
ngx_http_variable_value_t *v, uintptr_t data);
40-
static ngx_int_t ngx_http_modsecurity_resp_headers_phase_time(ngx_http_request_t *r,
41-
ngx_http_variable_value_t *v, uintptr_t data);
42-
static ngx_int_t ngx_http_modsecurity_resp_body_phase_time(ngx_http_request_t *r,
43-
ngx_http_variable_value_t *v, uintptr_t data);
44-
static ngx_int_t ngx_http_modsecurity_logging_phase_time(ngx_http_request_t *r,
36+
static ngx_int_t ngx_http_modsecurity_phase_time(ngx_http_request_t *r,
4537
ngx_http_variable_value_t *v, uintptr_t data);
4638
static ngx_int_t ngx_http_modsecurity_time_variable(ngx_http_request_t *r,
4739
ngx_http_variable_value_t *v, uintptr_t data, ngx_msec_int_t usec);
@@ -541,26 +533,26 @@ ngx_module_t ngx_http_modsecurity_module = {
541533

542534
static ngx_http_variable_t ngx_http_modsecurity_vars[] = {
543535
{ ngx_string("modsecurity_req_headers_phase_time"), NULL,
544-
ngx_http_modsecurity_req_headers_phase_time, 0,
545-
NGX_HTTP_VAR_NOCACHEABLE, 0 },
536+
ngx_http_modsecurity_phase_time, 0,
537+
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
546538

547539
{ ngx_string("modsecurity_req_body_phase_time"), NULL,
548-
ngx_http_modsecurity_req_body_phase_time, 0,
549-
NGX_HTTP_VAR_NOCACHEABLE, 0 },
540+
ngx_http_modsecurity_phase_time, 1,
541+
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
550542

551543
{ ngx_string("modsecurity_resp_headers_phase_time"), NULL,
552-
ngx_http_modsecurity_resp_headers_phase_time, 0,
553-
NGX_HTTP_VAR_NOCACHEABLE, 0 },
544+
ngx_http_modsecurity_phase_time, 2,
545+
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
554546

555547
{ ngx_string("modsecurity_resp_body_phase_time"), NULL,
556-
ngx_http_modsecurity_resp_body_phase_time, 0,
557-
NGX_HTTP_VAR_NOCACHEABLE, 0 },
548+
ngx_http_modsecurity_phase_time, 3,
549+
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
558550

559551
{ ngx_string("modsecurity_logging_phase_time"), NULL,
560-
ngx_http_modsecurity_logging_phase_time, 0,
561-
NGX_HTTP_VAR_NOCACHEABLE, 0 },
552+
ngx_http_modsecurity_phase_time, 4,
553+
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
562554

563-
ngx_http_null_variable
555+
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
564556
};
565557

566558

@@ -850,21 +842,7 @@ ngx_http_modsecurity_cleanup_rules(void *data)
850842

851843

852844
static ngx_int_t
853-
ngx_http_modsecurity_req_headers_phase_time(ngx_http_request_t *r,
854-
ngx_http_variable_value_t *v, uintptr_t data)
855-
{
856-
ngx_http_modsecurity_ctx_t *ctx;
857-
858-
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
859-
if (ctx == NULL) {
860-
return NGX_ERROR;
861-
}
862-
return ngx_http_modsecurity_time_variable(r, v, data, ctx->req_headers_phase_time);
863-
}
864-
865-
866-
static ngx_int_t
867-
ngx_http_modsecurity_req_body_phase_time(ngx_http_request_t *r,
845+
ngx_http_modsecurity_phase_time(ngx_http_request_t *r,
868846
ngx_http_variable_value_t *v, uintptr_t data)
869847
{
870848
ngx_http_modsecurity_ctx_t *ctx;
@@ -873,49 +851,21 @@ ngx_http_modsecurity_req_body_phase_time(ngx_http_request_t *r,
873851
if (ctx == NULL) {
874852
return NGX_ERROR;
875853
}
876-
return ngx_http_modsecurity_time_variable(r, v, data, ctx->req_body_phase_time);
877-
}
878854

879-
880-
static ngx_int_t
881-
ngx_http_modsecurity_resp_headers_phase_time(ngx_http_request_t *r,
882-
ngx_http_variable_value_t *v, uintptr_t data)
883-
{
884-
ngx_http_modsecurity_ctx_t *ctx;
885-
886-
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
887-
if (ctx == NULL) {
888-
return NGX_ERROR;
889-
}
890-
return ngx_http_modsecurity_time_variable(r, v, data, ctx->resp_headers_phase_time);
891-
}
892-
893-
894-
static ngx_int_t
895-
ngx_http_modsecurity_resp_body_phase_time(ngx_http_request_t *r,
896-
ngx_http_variable_value_t *v, uintptr_t data)
897-
{
898-
ngx_http_modsecurity_ctx_t *ctx;
899-
900-
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
901-
if (ctx == NULL) {
902-
return NGX_ERROR;
903-
}
904-
return ngx_http_modsecurity_time_variable(r, v, data, ctx->resp_body_phase_time);
905-
}
906-
907-
908-
static ngx_int_t
909-
ngx_http_modsecurity_logging_phase_time(ngx_http_request_t *r,
910-
ngx_http_variable_value_t *v, uintptr_t data)
911-
{
912-
ngx_http_modsecurity_ctx_t *ctx;
913-
914-
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
915-
if (ctx == NULL) {
916-
return NGX_ERROR;
855+
switch(data) {
856+
case 0:
857+
return ngx_http_modsecurity_time_variable(r, v, data, ctx->req_headers_phase_time);
858+
case 1:
859+
return ngx_http_modsecurity_time_variable(r, v, data, ctx->req_body_phase_time);
860+
case 2:
861+
return ngx_http_modsecurity_time_variable(r, v, data, ctx->resp_headers_phase_time);
862+
case 3:
863+
return ngx_http_modsecurity_time_variable(r, v, data, ctx->resp_body_phase_time);
864+
case 4:
865+
return ngx_http_modsecurity_time_variable(r, v, data, ctx->logging_phase_time);
866+
default:
867+
return -1;
917868
}
918-
return ngx_http_modsecurity_time_variable(r, v, data, ctx->logging_phase_time);
919869
}
920870

921871

@@ -946,10 +896,10 @@ ngx_http_modsecurity_time_variable(ngx_http_request_t *r,
946896

947897

948898
ngx_msec_int_t
949-
ngx_http_modsecurity_compute_processing_time(struct timeval tv) {
950-
struct timeval current_tv;
951-
ngx_gettimeofday(&current_tv);
952-
return (ngx_msec_int_t) ((current_tv.tv_sec - tv.tv_sec) * 1000000 + (current_tv.tv_usec - tv.tv_usec));
899+
ngx_http_modsecurity_compute_processing_time(struct timespec tv) {
900+
struct timespec current_tv;
901+
(void) clock_gettime(CLOCK_MONOTONIC, &current_tv);
902+
return (ngx_msec_int_t) ((current_tv.tv_sec - tv.tv_sec) * 1000000 + (current_tv.tv_nsec - tv.tv_nsec) / 1000);
953903
};
954904

955905
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

Diff for: src/ngx_http_modsecurity_pre_access.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
140140
int ret = 0;
141141
int already_inspected = 0;
142142

143-
struct timeval start_tv;
144-
ngx_gettimeofday(&start_tv);
143+
struct timespec start_tv;
144+
(void) clock_gettime(CLOCK_MONOTONIC, &start_tv);
145145

146146
dd("request body is ready to be processed");
147147

@@ -212,11 +212,8 @@ ngx_http_modsecurity_pre_access_handler(ngx_http_request_t *r)
212212
/* XXX: once more -- is body can be modified ? content-length need to be adjusted ? */
213213

214214
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
215-
216215
msc_process_request_body(ctx->modsec_transaction);
217-
218216
ctx->req_body_phase_time = ngx_http_modsecurity_compute_processing_time(start_tv);
219-
220217
ngx_http_modsecurity_pcre_malloc_done(old_pool);
221218

222219
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 0);

Diff for: src/ngx_http_modsecurity_rewrite.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
5151
if (ctx == NULL)
5252
{
5353
int ret = 0;
54-
struct timeval start_tv;
55-
56-
ngx_gettimeofday(&start_tv);
54+
struct timespec start_tv;
55+
(void) clock_gettime(CLOCK_MONOTONIC, &start_tv);
5756

5857
ngx_connection_t *connection = r->connection;
5958
/**
@@ -208,10 +207,8 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
208207
msc_process_request_headers(ctx->modsec_transaction);
209208
ngx_http_modsecurity_pcre_malloc_done(old_pool);
210209
dd("Processing intervention with the request headers information filled in");
211-
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 1);
212-
213210
ctx->req_headers_phase_time = ngx_http_modsecurity_compute_processing_time(start_tv);
214-
211+
ret = ngx_http_modsecurity_process_intervention(ctx->modsec_transaction, r, 1);
215212
if (r->error_page) {
216213
return NGX_DECLINED;
217214
}
@@ -221,5 +218,6 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
221218
}
222219
}
223220

221+
224222
return NGX_DECLINED;
225223
}

0 commit comments

Comments
 (0)