33
33
34
34
#include "ngx_http_json_log_variables.h"
35
35
#include "ngx_json_log_text.h"
36
+ #include "ngx_json_log_str.h"
36
37
37
38
#include <jansson.h>
38
39
39
40
#include <sys/types.h>
40
41
#include <sys/stat.h>
41
42
#include <unistd.h>
42
43
#include <fcntl.h>
44
+ #include <stdio.h>
43
45
44
46
typedef ngx_queue_t * (* get_body_queue_pt )(ngx_http_request_t * r );
45
47
46
48
static ngx_int_t
47
49
ngx_http_json_log_get_variable_req_headers (ngx_http_request_t * r ,
48
50
ngx_http_variable_value_t * v , uintptr_t data );
49
51
50
-
51
-
52
52
/* variables list */
53
53
static ngx_http_variable_t ngx_http_json_log_variables_list [] = {
54
54
{ ngx_string ("http_json_log_req_headers" ),
@@ -70,11 +70,19 @@ static ngx_http_variable_t ngx_http_json_log_variables_list[] = {
70
70
ngx_http_json_log_set_variable_req_body ,
71
71
NULL ,
72
72
0 , 0 , 0
73
+ },
74
+ { ngx_string ("http_json_log_req_body_hexdump" ),
75
+ ngx_http_json_log_set_variable_req_body_hexdump ,
76
+ NULL ,
77
+ 0 , 0 , 0
78
+ },
79
+ { ngx_string ("http_json_err_log_req_hexdump" ),
80
+ ngx_http_json_log_set_variable_req_body_hexdump ,
81
+ NULL ,
82
+ 0 , 0 , 0
73
83
}
74
84
};
75
85
76
-
77
-
78
86
ngx_http_variable_t *
79
87
ngx_http_json_log_variables (size_t * len ) {
80
88
@@ -115,6 +123,10 @@ ngx_http_json_log_local_variable_needs_body_filter(ngx_str_t *name) {
115
123
sizeof ("http_json_log_req_body" )) == 0 ) {
116
124
return 1 ;
117
125
}
126
+ if (ngx_strncmp ("http_json_log_req_body_hexdump" , name -> data ,
127
+ sizeof ("http_json_log_req_body_hexdump" )) == 0 ) {
128
+ return 1 ;
129
+ }
118
130
return NGX_ERROR ;
119
131
}
120
132
@@ -135,9 +147,10 @@ ngx_http_json_log_local_variable_needs_header_filter(ngx_str_t *name) {
135
147
void
136
148
ngx_http_json_log_register_variables (ngx_conf_t * cf ) {
137
149
138
- ngx_http_variable_t * v ;
139
- size_t l = 0 , local_vars_len ;
140
- ngx_http_variable_t * local_vars = NULL ;
150
+ ngx_http_variable_t * v ;
151
+ ngx_http_variable_t * local_vars = NULL ;
152
+ size_t l = 0 ;
153
+ size_t local_vars_len ;
141
154
142
155
local_vars = ngx_http_json_log_variables (& local_vars_len );
143
156
/* Register variables */
@@ -155,11 +168,11 @@ static ngx_int_t
155
168
ngx_http_json_log_get_variable_req_headers (ngx_http_request_t * r ,
156
169
ngx_http_variable_value_t * v , uintptr_t data ) {
157
170
158
- ngx_uint_t i ;
159
- ngx_list_part_t * part = & r -> headers_in .headers .part ;
160
- ngx_table_elt_t * header = part -> elts ;
161
- char * key = NULL ;
162
- json_t * object = json_object ();
171
+ ngx_uint_t i ;
172
+ ngx_list_part_t * part = & r -> headers_in .headers .part ;
173
+ ngx_table_elt_t * header = part -> elts ;
174
+ char * key = NULL ;
175
+ json_t * object = json_object ();
163
176
164
177
for (i = 0 ; i < part -> nelts ; ++ i ) {
165
178
if (!header [i ].key .data || !header [i ].key .len ) {
183
196
ngx_http_json_log_set_variable_resp_headers (ngx_http_request_t * r ,
184
197
ngx_http_variable_value_t * v , uintptr_t data ) {
185
198
186
- size_t i ;
199
+ size_t i ;
187
200
ngx_table_elt_t * header ;
188
201
ngx_array_t * headers = (ngx_array_t * ) data ;
189
202
json_t * object ;
@@ -227,20 +240,30 @@ ngx_http_json_log_set_variable_resp_headers(ngx_http_request_t *r,
227
240
set_current_mem_pool (NULL );
228
241
}
229
242
230
-
231
243
void
232
244
ngx_http_json_log_set_variable_req_body (ngx_http_request_t * r ,
233
245
ngx_http_variable_value_t * v , uintptr_t data ) {
234
246
235
247
ngx_str_t base64 ;
236
- ngx_str_t * payload ;
237
248
json_t * object = NULL ;
238
- payload = (ngx_str_t * ) data ;
249
+ ngx_str_t * payload = (ngx_str_t * ) data ;
239
250
240
251
if (!payload || !payload -> data ) {
241
252
return ;
242
253
}
243
254
255
+ /* Empty payload */
256
+ if (!payload -> len ) {
257
+ set_current_mem_pool (r -> pool );
258
+ object = json_stringn ((const char * ) "" , 0 );
259
+ if (object ) {
260
+ v -> valid = 1 ;
261
+ v -> data = (void * ) object ;
262
+ }
263
+ set_current_mem_pool (NULL );
264
+ return ;
265
+ }
266
+
244
267
base64 .len = ngx_base64_encoded_length (payload -> len );
245
268
base64 .data = ngx_pcalloc (r -> pool , base64 .len );
246
269
if (!base64 .data ) {
@@ -256,5 +279,58 @@ ngx_http_json_log_set_variable_req_body(ngx_http_request_t *r,
256
279
v -> data = (void * ) object ;
257
280
}
258
281
set_current_mem_pool (NULL );
282
+ }
283
+
284
+ void
285
+ ngx_http_json_log_set_variable_req_body_hexdump (ngx_http_request_t * r ,
286
+ ngx_http_variable_value_t * v , uintptr_t data ) {
287
+
288
+ ngx_str_t hexdump ;
289
+ json_t * object = NULL ;
290
+ json_t * value = NULL ;
291
+ ngx_str_t * payload = (ngx_str_t * ) data ;
292
+ size_t start , i ;
259
293
294
+ if (!payload || !payload -> data ) {
295
+ return ;
296
+ }
297
+
298
+ /* Empty payload */
299
+ if (!payload -> len ) {
300
+ set_current_mem_pool (r -> pool );
301
+ object = json_stringn ((const char * ) "" , 0 );
302
+ if (object ) {
303
+ v -> valid = 1 ;
304
+ v -> data = (void * ) object ;
305
+ }
306
+ set_current_mem_pool (NULL );
307
+ return ;
308
+ }
309
+
310
+ hexdump .len = ngx_json_log_hexdump_length (payload -> len , 16 );
311
+ hexdump .data = ngx_pcalloc (r -> pool , hexdump .len );
312
+ if (!hexdump .data ) {
313
+ return ;
314
+ }
315
+
316
+ ngx_json_log_hexdump (payload , & hexdump );
317
+
318
+ set_current_mem_pool (r -> pool );
319
+
320
+ object = json_array ();
321
+ start = 0 ;
322
+ for (i = 0 ; i < hexdump .len ; ++ i ) {
323
+ if (hexdump .data [i ] == '\n' ) {
324
+ value = json_stringn ((const char * ) hexdump .data + start , (i - start ));
325
+ json_array_append (object , value );
326
+ ++ i ;
327
+ start = i ;
328
+ }
329
+ }
330
+
331
+ if (object ) {
332
+ v -> valid = 1 ;
333
+ v -> data = (void * ) object ;
334
+ }
335
+ set_current_mem_pool (NULL );
260
336
}
0 commit comments