This repository was archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathngx_http_full_request_log_module.c
326 lines (259 loc) · 10.9 KB
/
ngx_http_full_request_log_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
/*
* nginx module to log the full request content
*
* [email protected] 2010-12-15
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
ngx_open_file_t *file;
time_t disk_full_time;
time_t error_log_time;
} ngx_http_full_request_log_t;
typedef struct {
ngx_flag_t enable;
} ngx_http_full_request_log_main_conf_t;
typedef struct {
ngx_http_full_request_log_t *log;
ngx_flag_t off;
} ngx_http_full_request_log_loc_conf_t;
static ngx_int_t ngx_http_full_request_log_handler(ngx_http_request_t *r);
static void ngx_http_full_request_log_body_handler(ngx_http_request_t *r);
static void ngx_http_full_request_log_write(ngx_http_request_t *r, ngx_http_full_request_log_t *log, u_char *buf, size_t len);
static void *ngx_http_full_request_log_create_main_conf(ngx_conf_t *cf);
static void *ngx_http_full_request_log_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_full_request_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static char *ngx_http_full_request_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_full_request_log_init(ngx_conf_t *cf);
static ngx_command_t ngx_http_full_request_log_commands[] = {
{ ngx_string("full_request_log_enable"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },
{ ngx_string("full_request_log"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_TAKE1,
ngx_http_full_request_log_set_log,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_full_request_log_module_ctx = {
NULL, /* preconfiguration */
ngx_http_full_request_log_init, /* postconfiguration */
ngx_http_full_request_log_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_full_request_log_create_loc_conf, /* create location configuration */
ngx_http_full_request_log_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_full_request_log_module = {
NGX_MODULE_V1,
&ngx_http_full_request_log_module_ctx, /* module context */
ngx_http_full_request_log_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
};
static ngx_int_t ngx_http_full_request_log_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_full_request_log_loc_conf_t *lcf;
ngx_http_full_request_log_t *log;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http full request log handler");
lcf = ngx_http_get_module_loc_conf(r, ngx_http_full_request_log_module);
if (lcf->off) {
return NGX_OK;
}
log = lcf->log;
if (ngx_time() == log->disk_full_time) {
/*
* on FreeBSD writing to a full filesystem with enabled softupdates
* may block process for much longer time than writing to non-full
* filesystem, so we skip writing to a log for one second
*/
return NGX_OK;
}
rc = ngx_http_read_client_request_body(r, ngx_http_full_request_log_body_handler);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return NGX_DONE;
}
static void ngx_http_full_request_log_body_handler(ngx_http_request_t *r)
{
ngx_http_full_request_log_loc_conf_t *lcf;
ngx_http_full_request_log_t *log;
size_t len;
ngx_buf_t *b, *buf, *next;
ngx_uint_t i;
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_chain_t *cl;
lcf = ngx_http_get_module_loc_conf(r, ngx_http_full_request_log_module);
log = lcf->log;
len = 50 + sizeof(CRLF) - 1 + ngx_cached_http_log_time.len + sizeof(CRLF) - 1 + sizeof(CRLF) - 1 + r->request_line.len + sizeof(CRLF) - 1 + sizeof(CRLF) - 1;
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len + sizeof(CRLF) - 1;
}
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return;
}
b->last = ngx_copy(b->last, "--------------------------------------------------", 50);
*b->last++ = CR; *b->last++ = LF;
b->last = ngx_copy(b->last, ngx_cached_http_log_time.data, ngx_cached_http_log_time.len);
*b->last++ = CR; *b->last++ = LF;
*b->last++ = CR; *b->last++ = LF;
b->last = ngx_copy(b->last, r->request_line.data, r->request_line.len);
*b->last++ = CR; *b->last++ = LF;
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
*b->last++ = ':'; *b->last++ = ' ';
b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);
*b->last++ = CR; *b->last++ = LF;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http full request log header: \"%V: %V\"", &header[i].key, &header[i].value);
}
*b->last++ = CR; *b->last++ = LF;
ngx_http_full_request_log_write(r, log, b->pos, len);
if (r->request_body == NULL || r->request_body->bufs == NULL) {
return;
}
cl = r->request_body->bufs;
buf = cl->buf;
len = buf->last - buf->pos;
ngx_http_full_request_log_write(r, log, buf->pos, len);
if (cl->next != NULL) {
next = cl->next->buf;
len = next->last - next->pos;
ngx_http_full_request_log_write(r, log, next->pos, len);
}
ngx_http_full_request_log_write(r, log, (unsigned char *)"\r\n", 2);
}
static void ngx_http_full_request_log_write(ngx_http_request_t *r, ngx_http_full_request_log_t *log, u_char *buf, size_t len)
{
u_char *name;
time_t now;
ssize_t n;
ngx_err_t err;
name = log->file->name.data;
n = ngx_write_fd(log->file->fd, buf, len);
if (n == (ssize_t)len) {
return;
}
now = ngx_time();
if (n == -1) {
err = ngx_errno;
if (err == NGX_ENOSPC) {
log->disk_full_time = now;
}
if (now - log->error_log_time > 59) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, err, ngx_write_fd_n " to \"%s\" failed", name);
log->error_log_time = now;
}
return;
}
if (now - log->error_log_time > 59) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz", name, n, len);
log->error_log_time = now;
}
}
static ngx_int_t ngx_http_full_request_log_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_full_request_log_main_conf_t *lmcf;
ngx_http_core_main_conf_t *cmcf;
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_full_request_log_module);
if (lmcf->enable) {
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_full_request_log_handler;
}
return NGX_OK;
}
static char *ngx_http_full_request_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
ngx_http_full_request_log_main_conf_t *lmcf;
ngx_http_full_request_log_loc_conf_t *llcf = conf;
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_full_request_log_module);
if (lmcf->enable) {
value = cf->args->elts;
if (ngx_strcmp(value[1].data, "off") == 0) {
llcf->off = 1;
return NGX_CONF_OK;
}
llcf->off = 0;
llcf->log = ngx_pcalloc(cf->pool, sizeof(ngx_http_full_request_log_t));
ngx_memzero(llcf->log, sizeof(ngx_http_full_request_log_t));
llcf->log->file = ngx_conf_open_file(cf->cycle, &value[1]);
if (llcf->log->file == NULL) {
return NGX_CONF_ERROR;
}
}
return NGX_CONF_OK;
}
static void *ngx_http_full_request_log_create_main_conf(ngx_conf_t *cf)
{
ngx_http_full_request_log_main_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_full_request_log_main_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->enable = NGX_CONF_UNSET;
return conf;
}
static void *ngx_http_full_request_log_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_full_request_log_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_full_request_log_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->off = NGX_CONF_UNSET;
return conf;
}
static char *ngx_http_full_request_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_full_request_log_loc_conf_t *prev = parent;
ngx_http_full_request_log_loc_conf_t *conf = child;
if (conf->log || (conf->off != NGX_CONF_UNSET)) {
return NGX_CONF_OK;
}
conf->log = prev->log;
conf->off = prev->off;
return NGX_CONF_OK;
}