-
Notifications
You must be signed in to change notification settings - Fork 7
/
ngx_http_lua_module.c
264 lines (209 loc) · 7.17 KB
/
ngx_http_lua_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
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#define OUT_BUFSIZE 256
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
#include "compat-5.1.h"
#endif
#if LUA_VERSION_NUM < 501
#define luaL_register(a, b, c) luaL_openlib((a), (b), (c), 0)
#endif
#define safe_emalloc(nmemb, size, offset) malloc((nmemb) * (size) + (offset))
//Global Setting
lua_State *L; /* lua state handle */
static char *ngx_http_lua_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_foo_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_lua_process_init(ngx_cycle_t *cycle);
static void ngx_http_lua_process_exit(ngx_cycle_t *cycle);
static ngx_int_t make_http_header(ngx_http_request_t *r);
static ngx_int_t make_http_get_body(ngx_http_request_t *r, char *out_buf);
static char g_foo_settings[64] = {0};
/* Commands */
static ngx_command_t ngx_http_lua_commands[] = {
{ ngx_string("ngx_lua_module"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_lua_set,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("lua"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_foo_set,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_lua_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
/* hook */
ngx_module_t ngx_http_lua_module = {
NGX_MODULE_V1,
&ngx_http_lua_module_ctx, /* module context */
ngx_http_lua_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_lua_process_init, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_http_lua_process_exit, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
/* setting header for no-cache */
static ngx_int_t make_http_header(ngx_http_request_t *r){
ngx_uint_t i;
ngx_table_elt_t *cc, **ccp;
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
ccp = r->headers_out.cache_control.elts;
if (ccp == NULL) {
if (ngx_array_init(&r->headers_out.cache_control, r->pool,
1, sizeof(ngx_table_elt_t *))
!= NGX_OK)
{
return NGX_ERROR;
}
ccp = ngx_array_push(&r->headers_out.cache_control);
if (ccp == NULL) {
return NGX_ERROR;
}
cc = ngx_list_push(&r->headers_out.headers);
if (cc == NULL) {
return NGX_ERROR;
}
cc->hash = 1;
cc->key.len = sizeof("Cache-Control") - 1;
cc->key.data = (u_char *) "Cache-Control";
*ccp = cc;
} else {
for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
ccp[i]->hash = 0;
}
cc = ccp[0];
}
cc->value.len = sizeof("no-cache") - 1;
cc->value.data = (u_char *) "no-cache";
return NGX_OK;
}
static ngx_int_t make_http_get_body(ngx_http_request_t *r, char *out_buf){
char *qs_start = (char *)r->args_start;
char *qs_end = (char *)r->uri_end;
char uri[128] = {0};
char *id;
if (qs_start == NULL || qs_end == NULL){
return NGX_HTTP_BAD_REQUEST;
}
if ((memcmp(qs_start, "id=", 3) == 0)){
id = qs_start + 3;
*qs_end = '\0';
}else{
return NGX_HTTP_BAD_REQUEST;
}
snprintf(uri, r->uri.len + 1, "%s", r->uri.data);
sprintf(out_buf, "Author: http://timyang.net/ nconfig=%snid=%snuri=%snret=%lxn", g_foo_settings, id, uri, ngx_random());
if (luaL_dofile(L, "/usr/local/nginx/conf/test.lua")) {
;
//if (luaL_loadfile(L, "/usr/local/nginx/conf/test.lua") || lua_pcall(L, 0, 0, 0)) { /* load the compile template functions */
sprintf(out_buf, "cannot run lua: %s", lua_tostring(L, -1));
//return NGX_HTTP_BAD_REQUEST;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_lua_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
/* Http Output Buffer */
char out_buf[OUT_BUFSIZE] = {0};
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK && rc != NGX_AGAIN) {
return rc;
}
/* make http header */
rc = make_http_header(r);
if (rc != NGX_OK) {
return rc;
}
if (r->method == NGX_HTTP_HEAD) {
r->headers_out.status = NGX_HTTP_OK;
return ngx_http_send_header(r);
} else if (r->method == NGX_HTTP_GET) {
/* make http get body buffer */
rc = make_http_get_body(r, out_buf);
if (rc != NGX_OK) {
return rc;
}
} else {
return NGX_HTTP_NOT_ALLOWED;
}
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.buf = b;
out.next = NULL;
b->pos = (u_char *)out_buf;
b->last = (u_char *)out_buf + strlen(out_buf);
b->memory = 1;
b->last_buf = 1;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = strlen(out_buf);
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out);
}
static char *
ngx_http_lua_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
/* register hanlder */
clcf->handler = ngx_http_lua_handler;
return NGX_CONF_OK;
}
static char *
ngx_http_foo_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value = cf->args->elts;
memcpy(g_foo_settings, value[1].data, value[1].len);
g_foo_settings[value[1].len] = '\0';
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_lua_process_init(ngx_cycle_t *cycle)
{
L = luaL_newstate();
if (L == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "Failed to initialize Lua VM");
//ngx_log_error(NGX_ERROR, cycle->log, 0, "Failed to initialize Lua VM");
return NGX_ERROR;
}
luaL_openlibs(L);
ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "Lua VM initialized!");
return NGX_OK;
}
static void
ngx_http_lua_process_exit(ngx_cycle_t *cycle)
{
lua_close(L);
return;
}