-
Notifications
You must be signed in to change notification settings - Fork 3
/
ngx_http_req_stat_var.c
456 lines (385 loc) · 10 KB
/
ngx_http_req_stat_var.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*************************************************
* Author: jie123108@163.com
* Copyright: jie123108
*************************************************/
#include "ngx_http_req_stat_module.h"
u_char *
ngx_http_req_stat_msec(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
ngx_time_t *tp;
tp = ngx_timeofday();
return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
}
int ngx_http_req_stat_uri_parse(ngx_http_request_t *r,size_t* begin, size_t* end)
{
size_t len = 0;
size_t url_pos = 0;
int parse_step = 0;
int for_break = 0;
for(len=0;len<r->request_line.len;len++){
switch(parse_step){
case 0://解析method
if(r->request_line.data[len] == ' '){
parse_step++;
}
break;
case 1://解析url
if(r->request_line.data[len] != ' ')
{
if(url_pos == 0){
url_pos = len;
parse_step++;
}
}
break;
case 2:
if(r->request_line.data[len] == '?'
|| r->request_line.data[len] == ' '
|| r->request_line.data[len] == '\t'){
parse_step++;
for_break = 1;
break;
}
break;
}
if(for_break) break;
}
if(url_pos == 0){
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"invalid uri \"%V\" not found url!", &r->request_line);
return -1;
}
*begin = url_pos;
*end = len;
return 0;
}
size_t ngx_http_req_stat_uri_full_getlen(ngx_http_request_t *r, uintptr_t data)
{
if(r->request_line.len == 0){
return 1;
}
size_t begin=0;
size_t end = 0;
int ret = ngx_http_req_stat_uri_parse(r, &begin, &end);
if(ret == -1){
return 1;
}
return end-begin;
}
u_char *
ngx_http_req_stat_uri_full(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
if(r->request_line.len == 0){
return ngx_sprintf(buf, "-");
}
size_t begin=0;
size_t end = 0;
int ret = ngx_http_req_stat_uri_parse(r, &begin, &end);
if(ret == -1){
return ngx_sprintf(buf, "-");
}
ngx_str_t tmp = {end-begin, r->request_line.data+begin};
return ngx_sprintf(buf, "%V", &tmp);
}
#if 0
uint8_t valid_value[256] =
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
typedef enum {
NORESP,
NOJSON,
OK_NOTFOUND,
OK_VALUE_INVALID,
OK_TRUE,
OK_FALSE,
OK_CNT
}JSON_STATUS;
const char* get_str_status(JSON_STATUS status){
switch(status){
case NORESP: return "noresp";
case NOJSON: return "nojson";
case OK_NOTFOUND: return "ok_notfound";
case OK_VALUE_INVALID: return "ok_invalid";
case OK_TRUE: return "ok_true";
case OK_FALSE: return "ok_false";
case OK_CNT:
default:
return "ok_err";
}
}
int get_str_status_len(JSON_STATUS status){
switch(status){
case NORESP: return sizeof("noresp")-1;
case NOJSON: return sizeof("nojson")-1;
case OK_NOTFOUND: return sizeof("ok_notfound")-1;
case OK_VALUE_INVALID: return sizeof("ok_invalid")-1;
case OK_TRUE: return sizeof("ok_true")-1;
case OK_FALSE: return sizeof("ok_false")-1;
case OK_CNT:
default:
return sizeof("ok_err")-1;
}
}
JSON_STATUS ngx_http_req_stat_find_ok_status(const u_char* json, size_t len)
{
printf("resp: %.*s\n", (int)len, json);
#define IS_YH(c) (c=='"'||c=='\'')
unsigned i;
int step = 0;
int stop = 0;
int begin = 0;
JSON_STATUS status = OK_NOTFOUND;
for(i=0;i<len; i++){
switch(step){
case 0: //find "ok"
if(i<len-4 && IS_YH(json[i]) && json[++i] == 'o'
&& json[++i]=='k' && IS_YH(json[++i]))
{
step++;
}
break;
case 1:
if(valid_value[(uint8_t)json[i]]){//find f or t.
begin = i;
step++;
}
break;
case 2:
if(!valid_value[(uint8_t)json[i]]){
step++;
stop = 1;
//printf("ok_val: %.*s\n", i-begin, json+begin);
if(i - begin == 4 && strncasecmp((const char*)json+begin, "true", 4)==0){
status = OK_TRUE;
}else if(i-begin == 5 && strncasecmp((const char*)json+begin, "false", 5)==0){
status = OK_FALSE;
}else{
status = OK_VALUE_INVALID;
}
}
break;
}
if(stop) break;
}
return status;
}
ngx_str_t ngx_http_req_stat_find_reason(const u_char* json, size_t len)
{
#define IS_YH(c) (c=='"'||c=='\'')
unsigned i;
int step = 0;
int stop = 0;
int begin = 0;
ngx_str_t reason = ngx_string("-");
for(i=0;i<len; i++){
switch(step){
case 0: //find "reason"
if(i<len-8 && IS_YH(json[i])
&&json[++i] == 'r' && json[++i]=='e' && json[++i]=='a'
&&json[++i] == 's' && json[++i]=='o' && json[++i]=='n'
&&IS_YH(json[++i]))
{
step++;
}
break;
case 1://查找到reason 值的左边引号
if(i<len-1 && IS_YH(json[i]) ){//find "
begin = i+1;
step++;
}
break;
case 2://查找到reason值的右边引号
if(IS_YH(json[i])){
step++;
stop = 1;
if(i-begin > 0){
reason.len = i-begin;
reason.data = (u_char*)json+begin;
}
}
break;
}
if(stop) break;
}
return reason;
}
inline JSON_STATUS ngx_http_req_stat_get_json_status(ngx_http_request_t *r)
{
if(r->out != NULL && r->out->buf != NULL){
ngx_buf_t* buf = r->out->buf;
if(ngx_buf_size(buf)> 5 && ngx_buf_in_memory_only(buf)){
return ngx_http_req_stat_find_ok_status(buf->pos, buf->end-buf->pos);
}else{
//printf("#######ngx_buf_size(buf)> 5 && ngx_buf_in_memory_only(buf)###\n");
return NOJSON;
}
}else{
//printf("r->out != NULL && r->out->buf != NULL\n");
return NORESP;
}
}
size_t ngx_http_req_stat_json_status_getlen(ngx_http_request_t *r, uintptr_t data)
{
if(r->request_line.len == 0){
return 1;
}
JSON_STATUS status = ngx_http_req_stat_get_json_status(r);
return get_str_status_len(status);
}
u_char *
ngx_http_req_stat_json_status(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
if(r->request_line.len == 0){
return ngx_sprintf(buf, "-");
}
JSON_STATUS status = ngx_http_req_stat_get_json_status(r);
return ngx_sprintf(buf, "%s", get_str_status(status));
}
inline ngx_str_t ngx_http_req_stat_get_json_reason(ngx_http_request_t *r)
{
static ngx_str_t empty_reason = ngx_string("-");
if(r->out != NULL && r->out->buf != NULL && ngx_buf_size(r->out->buf)> 8){
ngx_buf_t* buf = r->out->buf;
if(ngx_buf_in_memory_only(buf)){
return ngx_http_req_stat_find_reason(buf->pos, buf->end-buf->pos);
}else{
return empty_reason;
}
}else{
return empty_reason;
}
}
size_t ngx_http_req_stat_json_reason_getlen(ngx_http_request_t *r, uintptr_t data)
{
if(r->request_line.len == 0){
return 1;
}
ngx_str_t reason = ngx_http_req_stat_get_json_reason(r);
return reason.len;
}
u_char *
ngx_http_req_stat_json_reason(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
if(r->request_line.len == 0){
return ngx_sprintf(buf, "-");
}
ngx_str_t reason = ngx_http_req_stat_get_json_reason(r);
return ngx_sprintf(buf, "%V", &reason);
}
#endif
u_char *
ngx_http_req_stat_request_time(ngx_http_request_t *r, u_char *buf,
ngx_http_req_stat_op_t *op)
{
ngx_time_t *tp;
ngx_msec_int_t ms;
tp = ngx_timeofday();
ms = (ngx_msec_int_t)
((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
ms = ngx_max(ms, 0);
return ngx_sprintf(buf, "%T.%04M", ms / 1000, ms % 1000);
}
u_char *
ngx_http_req_stat_status(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
ngx_uint_t status;
if (r->err_status) {
status = r->err_status;
} else if (r->headers_out.status) {
status = r->headers_out.status;
} else if (r->http_version == NGX_HTTP_VERSION_9) {
*buf++ = '0';
*buf++ = '0';
*buf++ = '9';
return buf;
} else {
status = 0;
}
return ngx_sprintf(buf, "%ui", status);
}
u_char* ngx_http_req_stat_date(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
u_char* p = ngx_cpymem(buf, ngx_cached_http_log_iso8601.data, 10);
//buf[4] = '-';
//buf[7] = '-';
return p;
}
u_char* ngx_http_req_stat_time(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
u_char* p = ngx_cpymem(buf, ngx_cached_err_log_time.data+11, 8);
return p;
}
u_char* ngx_http_req_stat_year(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
u_char* p = ngx_cpymem(buf, ngx_cached_err_log_time.data, 4);
return p;
}
u_char* ngx_http_req_stat_month(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
u_char* p = ngx_cpymem(buf, ngx_cached_err_log_time.data+5, 2);
return p;
}
u_char* ngx_http_req_stat_day(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
u_char* p = ngx_cpymem(buf, ngx_cached_err_log_time.data+8, 2);
return p;
}
u_char* ngx_http_req_stat_hour(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
u_char* p = ngx_cpymem(buf, ngx_cached_err_log_time.data+11, 2);
return p;
}
u_char* ngx_http_req_stat_minute(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
u_char* p = ngx_cpymem(buf, ngx_cached_err_log_time.data+14, 2);
return p;
}
u_char* ngx_http_req_stat_second(ngx_http_request_t *r, u_char *buf, ngx_http_req_stat_op_t *op)
{
u_char* p = ngx_cpymem(buf, ngx_cached_err_log_time.data+17, 2);
return p;
}
u_char *
ngx_http_req_stat_bytes_sent(ngx_http_request_t *r, u_char *buf,
ngx_http_req_stat_op_t *op)
{
return ngx_sprintf(buf, "%O", r->connection->sent);
}
/*
* although there is a real $body_bytes_sent variable,
* this log operation code function is more optimized for logging
*/
u_char *
ngx_http_req_stat_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
ngx_http_req_stat_op_t *op)
{
off_t length;
length = r->connection->sent - r->header_size;
if (length > 0) {
return ngx_sprintf(buf, "%O", length);
}
*buf = '0';
return buf + 1;
}
u_char *
ngx_http_req_stat_request_length(ngx_http_request_t *r, u_char *buf,
ngx_http_req_stat_op_t *op)
{
return ngx_sprintf(buf, "%O", r->request_length);
}