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
|
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef NGX_HTTP_ECHO_MODULE_H
#define NGX_HTTP_ECHO_MODULE_H
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
extern ngx_module_t ngx_http_echo_module;
/* config directive's opcode */
typedef enum {
echo_opcode_echo_sync,
echo_opcode_echo,
echo_opcode_echo_request_body,
echo_opcode_echo_sleep,
echo_opcode_echo_flush,
echo_opcode_echo_blocking_sleep,
echo_opcode_echo_reset_timer,
echo_opcode_echo_before_body,
echo_opcode_echo_after_body,
echo_opcode_echo_location_async,
echo_opcode_echo_location,
echo_opcode_echo_subrequest_async,
echo_opcode_echo_subrequest,
echo_opcode_echo_duplicate,
echo_opcode_echo_read_request_body,
echo_opcode_echo_foreach_split,
echo_opcode_echo_end,
echo_opcode_echo_abort_parent,
echo_opcode_echo_exec
} ngx_http_echo_opcode_t;
/* all the various config directives (or commands) are
* divided into two categories: "handler commands",
* and "filter commands". For instance, the "echo"
* directive is a handler command while
* "echo_before_body" is a filter one. */
typedef enum {
echo_handler_cmd,
echo_filter_cmd
} ngx_http_echo_cmd_category_t;
/* compiled form of a config directive argument's value */
typedef struct {
/* holds the raw string of the argument value */
ngx_str_t raw_value;
/* fields "lengths" and "values" are set by
* the function ngx_http_script_compile,
* iff the argument value indeed contains
* nginx variables like "$foo" */
ngx_array_t *lengths;
ngx_array_t *values;
} ngx_http_echo_arg_template_t;
/* represent a config directive (or command) like "echo". */
typedef struct {
ngx_http_echo_opcode_t opcode;
/* each argument is of type echo_arg_template_t: */
ngx_array_t *args;
} ngx_http_echo_cmd_t;
/* location config struct */
typedef struct {
/* elements of the following arrays are of type
* ngx_http_echo_cmd_t */
ngx_array_t *handler_cmds;
ngx_array_t *before_body_cmds;
ngx_array_t *after_body_cmds;
unsigned seen_leading_output;
ngx_int_t status;
} ngx_http_echo_loc_conf_t;
typedef struct {
ngx_int_t requires_filter;
#if nginx_version >= 1011011
ngx_buf_t **busy_buf_ptrs;
ngx_int_t busy_buf_ptr_count;
#endif
} ngx_http_echo_main_conf_t;
typedef struct {
ngx_array_t *choices; /* items after splitting */
ngx_uint_t next_choice; /* current item index */
ngx_uint_t cmd_index; /* cmd index for the echo_foreach direcitve */
} ngx_http_echo_foreach_ctx_t;
/* context struct in the request handling cycle, holding
* the current states of the command evaluator */
typedef struct {
/* index of the next handler command in
* ngx_http_echo_loc_conf_t's "handler_cmds" array. */
ngx_uint_t next_handler_cmd;
/* index of the next before-body filter command in
* ngx_http_echo_loc_conf_t's "before_body_cmds" array. */
ngx_uint_t next_before_body_cmd;
/* index of the next after-body filter command in
* ngx_http_echo_loc_conf_t's "after_body_cmds" array. */
ngx_uint_t next_after_body_cmd;
ngx_http_echo_foreach_ctx_t *foreach;
ngx_time_t timer_begin;
ngx_event_t sleep;
ngx_uint_t counter;
unsigned before_body_sent:1;
unsigned skip_filter:1;
unsigned wait_read_request_body:1;
unsigned waiting:1;
unsigned done:1;
unsigned run_post_subrequest:1;
unsigned header_sent:1; /* r->header_sent is not sufficient
* because special header filters like
* ngx_http_image_filter_module's may
* intercept the whole header filter chain
* leaving r->header_sent unset. So we
* should always test both flags. */
} ngx_http_echo_ctx_t;
#endif /* NGX_HTTP_ECHO_MODULE_H */
|