~ubuntu-branches/ubuntu/quantal/nginx/quantal-updates

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/src/ngx_http_lua_directive.c

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2011-04-16 13:47:58 UTC
  • mfrom: (4.2.31 sid)
  • Revision ID: james.westby@ubuntu.com-20110416134758-yqca2qp5crh2hw2f
Tags: 1.0.0-2
* debian/rules:
  + Removed --with-file-aio support. Fixed FTBFS on kFreeBSD-* arch
    (Closes: #621882)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
 
2
 
2
3
#define DDEBUG 0
3
4
 
 
5
#include "ngx_http_lua_common.h"
4
6
#include "ngx_http_lua_directive.h"
5
7
#include "ngx_http_lua_util.h"
6
8
#include "ngx_http_lua_cache.h"
7
9
#include "ngx_http_lua_conf.h"
8
10
#include "ngx_http_lua_setby.h"
9
11
#include "ngx_http_lua_contentby.h"
 
12
#include "ngx_http_lua_accessby.h"
 
13
#include "ngx_http_lua_rewriteby.h"
 
14
 
 
15
 
 
16
unsigned  ngx_http_lua_requires_rewrite = 0;
 
17
unsigned  ngx_http_lua_requires_access  = 0;
 
18
 
 
19
 
 
20
char *
 
21
ngx_http_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
22
{
 
23
    ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
 
24
            "lua_code_cache is off; this will hurt performance");
 
25
 
 
26
    return ngx_conf_set_flag_slot(cf, cmd, conf);
 
27
}
10
28
 
11
29
 
12
30
char *
54
72
char *
55
73
ngx_http_lua_set_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
56
74
{
57
 
    ngx_str_t           *args;
 
75
    ngx_str_t           *value;
58
76
    ngx_str_t            target;
59
77
    ndk_set_var_t        filter;
 
78
    u_char              *p;
 
79
 
 
80
    ngx_http_lua_set_var_data_t     *filter_data;
60
81
 
61
82
    /*
62
 
     * args[0] = "set_by_lua"
63
 
     * args[1] = target variable name
64
 
     * args[2] = lua script to be executed
65
 
     * args[3..] = real params
 
83
     * value[0] = "set_by_lua"
 
84
     * value[1] = target variable name
 
85
     * value[2] = lua script to be executed
 
86
     * value[3..] = real params
66
87
     * */
67
 
    args = cf->args->elts;
68
 
    target = args[1];
69
 
 
70
 
    /*  prevent variable appearing in Lua inline script/file path */
71
 
 
72
 
#if 0
73
 
    if (ngx_http_lua_has_inline_var(&args[2])) {
74
 
        ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
75
 
                "Lua inline script or file path should not has inline variable: %V",
76
 
                &args[2]);
77
 
        return NGX_CONF_ERROR;
78
 
    }
79
 
#endif
 
88
    value = cf->args->elts;
 
89
    target = value[1];
80
90
 
81
91
    filter.type = NDK_SET_VAR_MULTI_VALUE_DATA;
82
92
    filter.func = cmd->post;
83
 
    filter.size = cf->args->nelts - 2;    /*  get number of real params + 1 (lua script) */
84
 
    filter.data = (void*)filter.size;
85
 
 
86
 
    return ndk_set_var_multi_value_core(cf, &target, &args[2], &filter);
 
93
    filter.size = cf->args->nelts - 2;    /*  get number of real params
 
94
                                              + 1 (lua script) */
 
95
 
 
96
    filter_data = ngx_palloc(cf->pool, sizeof(ngx_http_lua_set_var_data_t));
 
97
    if (filter_data == NULL) {
 
98
        return NGX_CONF_ERROR;
 
99
    }
 
100
 
 
101
    filter_data->size = filter.size;
 
102
 
 
103
    p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
 
104
    if (p == NULL) {
 
105
        return NGX_CONF_ERROR;
 
106
    }
 
107
 
 
108
    filter_data->key = p;
 
109
 
 
110
    p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
 
111
    p = ngx_http_lua_digest_hex(p, value[2].data, value[2].len);
 
112
    *p = '\0';
 
113
 
 
114
    filter.data = filter_data;
 
115
 
 
116
    return ndk_set_var_multi_value_core(cf, &target, &value[2], &filter);
87
117
}
88
118
 
89
119
 
94
124
    lua_State                   *L;
95
125
    ngx_int_t                    rc;
96
126
    ngx_http_lua_main_conf_t    *lmcf;
 
127
    ngx_http_lua_loc_conf_t     *llcf;
97
128
    char                        *err = NULL;
98
129
 
 
130
    ngx_http_lua_set_var_data_t     *filter_data = data;
 
131
 
99
132
    lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
 
133
 
100
134
    L = lmcf->lua;
101
135
 
 
136
    llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
 
137
 
102
138
    /*  load Lua inline script (w/ cache)        sp = 1 */
103
 
    rc = ngx_http_lua_cache_loadbuffer(L, v[0].data, v[0].len, "set_by_lua_inline", &err);
 
139
    rc = ngx_http_lua_cache_loadbuffer(L, v[0].data, v[0].len,
 
140
            filter_data->key, "set_by_lua_inline", &err,
 
141
            llcf->enable_code_cache);
104
142
 
105
143
    if (rc != NGX_OK) {
106
144
        if (err == NULL) {
113
151
        return NGX_ERROR;
114
152
    }
115
153
 
116
 
    rc = ngx_http_lua_set_by_chunk(L, r, val, v, (size_t) data);
 
154
    rc = ngx_http_lua_set_by_chunk(L, r, val, v, filter_data->size);
117
155
    if (rc != NGX_OK) {
118
156
        return NGX_ERROR;
119
157
    }
130
168
    ngx_int_t                    rc;
131
169
    u_char                      *script_path;
132
170
    ngx_http_lua_main_conf_t    *lmcf;
 
171
    ngx_http_lua_loc_conf_t     *llcf;
133
172
    char                        *err;
134
173
 
 
174
    ngx_http_lua_set_var_data_t     *filter_data = data;
 
175
 
135
176
    script_path = ngx_http_lua_rebase_path(r->pool, v[0].data, v[0].len);
136
177
 
137
178
    if (script_path == NULL) {
138
 
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
139
 
                "Failed to allocate memory to store absolute path: raw path='%v'",
140
 
                &v[0]);
141
 
 
142
179
        return NGX_ERROR;
143
180
    }
144
181
 
145
182
    lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
 
183
 
146
184
    L = lmcf->lua;
147
185
 
 
186
    llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
 
187
 
148
188
    /*  load Lua script file (w/ cache)        sp = 1 */
149
 
    rc = ngx_http_lua_cache_loadfile(L, (char *)script_path, &err);
 
189
    rc = ngx_http_lua_cache_loadfile(L, script_path, filter_data->key,
 
190
            &err, llcf->enable_code_cache);
150
191
 
151
192
    if (rc != NGX_OK) {
152
193
        if (err == NULL) {
159
200
        return NGX_ERROR;
160
201
    }
161
202
 
162
 
    rc = ngx_http_lua_set_by_chunk(L, r, val, v, (size_t) data);
 
203
    rc = ngx_http_lua_set_by_chunk(L, r, val, v, filter_data->size);
163
204
    if (rc != NGX_OK) {
164
205
        return NGX_ERROR;
165
206
    }
169
210
 
170
211
 
171
212
char *
 
213
ngx_http_lua_rewrite_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
214
{
 
215
    ngx_str_t                   *value;
 
216
    ngx_http_lua_loc_conf_t     *llcf = conf;
 
217
    u_char                      *p;
 
218
 
 
219
    ngx_http_compile_complex_value_t         ccv;
 
220
 
 
221
    dd("enter");
 
222
 
 
223
#if defined(nginx_version) && nginx_version >= 8042 && nginx_version <= 8053
 
224
    return "does not work with " NGINX_VER;
 
225
#endif
 
226
 
 
227
    /*  must specifiy a content handler */
 
228
    if (cmd->post == NULL) {
 
229
        return NGX_CONF_ERROR;
 
230
    }
 
231
 
 
232
    if (llcf->rewrite_handler) {
 
233
        return "is duplicate";
 
234
    }
 
235
 
 
236
    value = cf->args->elts;
 
237
 
 
238
    if (value[1].len == 0) {
 
239
        /*  Oops...Invalid location conf */
 
240
        ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
 
241
                "Invalid location config: no runnable Lua code");
 
242
 
 
243
        return NGX_CONF_ERROR;
 
244
    }
 
245
 
 
246
    if (cmd->post == ngx_http_lua_rewrite_handler_inline) {
 
247
        /* Don't eval nginx variables for inline lua code */
 
248
        llcf->rewrite_src.value = value[1];
 
249
 
 
250
        p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
 
251
        if (p == NULL) {
 
252
            return NGX_CONF_ERROR;
 
253
        }
 
254
 
 
255
        llcf->rewrite_src_key = p;
 
256
 
 
257
        p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
 
258
        p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
 
259
        *p = '\0';
 
260
 
 
261
    } else {
 
262
        ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
 
263
        ccv.cf = cf;
 
264
        ccv.value = &value[1];
 
265
        ccv.complex_value = &llcf->rewrite_src;
 
266
 
 
267
        if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
 
268
            return NGX_CONF_ERROR;
 
269
        }
 
270
 
 
271
        if (llcf->rewrite_src.lengths == NULL) {
 
272
            /* no variable found */
 
273
            p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
 
274
            if (p == NULL) {
 
275
                return NGX_CONF_ERROR;
 
276
            }
 
277
 
 
278
            llcf->rewrite_src_key = p;
 
279
 
 
280
            p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
 
281
            p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
 
282
            *p = '\0';
 
283
        }
 
284
    }
 
285
 
 
286
    llcf->rewrite_handler = cmd->post;
 
287
 
 
288
    if (! ngx_http_lua_requires_rewrite) {
 
289
        ngx_http_lua_requires_rewrite = 1;
 
290
    }
 
291
 
 
292
    return NGX_CONF_OK;
 
293
}
 
294
 
 
295
 
 
296
char *
 
297
ngx_http_lua_access_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
298
{
 
299
    ngx_str_t                   *value;
 
300
    ngx_http_lua_loc_conf_t     *llcf = conf;
 
301
    u_char                      *p;
 
302
 
 
303
    ngx_http_compile_complex_value_t         ccv;
 
304
 
 
305
    dd("enter");
 
306
 
 
307
    /*  must specifiy a content handler */
 
308
    if (cmd->post == NULL) {
 
309
        return NGX_CONF_ERROR;
 
310
    }
 
311
 
 
312
    if (llcf->access_handler) {
 
313
        return "is duplicate";
 
314
    }
 
315
 
 
316
    value = cf->args->elts;
 
317
 
 
318
    if (value[1].len == 0) {
 
319
        /*  Oops...Invalid location conf */
 
320
        ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
 
321
                "Invalid location config: no runnable Lua code");
 
322
 
 
323
        return NGX_CONF_ERROR;
 
324
    }
 
325
 
 
326
    if (cmd->post == ngx_http_lua_access_handler_inline) {
 
327
        /* Don't eval nginx variables for inline lua code */
 
328
        llcf->access_src.value = value[1];
 
329
 
 
330
        p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
 
331
        if (p == NULL) {
 
332
            return NGX_CONF_ERROR;
 
333
        }
 
334
 
 
335
        llcf->access_src_key = p;
 
336
 
 
337
        p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
 
338
        p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
 
339
        *p = '\0';
 
340
 
 
341
    } else {
 
342
        ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
 
343
        ccv.cf = cf;
 
344
        ccv.value = &value[1];
 
345
        ccv.complex_value = &llcf->access_src;
 
346
 
 
347
        if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
 
348
            return NGX_CONF_ERROR;
 
349
        }
 
350
 
 
351
        if (llcf->access_src.lengths == NULL) {
 
352
            /* no variable found */
 
353
            p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
 
354
            if (p == NULL) {
 
355
                return NGX_CONF_ERROR;
 
356
            }
 
357
 
 
358
            llcf->access_src_key = p;
 
359
 
 
360
            p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
 
361
            p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
 
362
            *p = '\0';
 
363
        }
 
364
    }
 
365
 
 
366
    llcf->access_handler = cmd->post;
 
367
 
 
368
    if (! ngx_http_lua_requires_access) {
 
369
        ngx_http_lua_requires_access = 1;
 
370
    }
 
371
 
 
372
    return NGX_CONF_OK;
 
373
}
 
374
 
 
375
 
 
376
char *
172
377
ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
173
378
{
174
 
    ngx_str_t                   *args;
 
379
    ngx_str_t                   *value;
175
380
    ngx_http_core_loc_conf_t    *clcf;
176
381
    ngx_http_lua_loc_conf_t     *llcf = conf;
 
382
    u_char                      *p;
 
383
 
 
384
    ngx_http_compile_complex_value_t         ccv;
177
385
 
178
386
    dd("enter");
179
387
 
182
390
        return NGX_CONF_ERROR;
183
391
    }
184
392
 
185
 
    /*  update lua script data */
186
 
    /*
187
 
     * args[0] = "content_by_lua"
188
 
     * args[1] = lua script to be executed
189
 
     * */
190
 
    args = cf->args->elts;
191
 
 
192
 
    /*  prevent variable appearing in Lua inline script/file path */
193
 
 
194
 
#if 0
195
 
    if (ngx_http_lua_has_inline_var(&args[1])) {
196
 
        ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
197
 
                "Lua inline script or file path should not has inline variable: %V",
198
 
                &args[1]);
199
 
 
200
 
        return NGX_CONF_ERROR;
 
393
    if (llcf->content_handler) {
 
394
        return "is duplicate";
201
395
    }
202
 
#endif
203
 
 
204
 
    if (args[1].len == 0) {
 
396
 
 
397
    value = cf->args->elts;
 
398
 
 
399
    if (value[1].len == 0) {
205
400
        /*  Oops...Invalid location conf */
206
401
        ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
207
402
                "Invalid location config: no runnable Lua code");
208
403
        return NGX_CONF_ERROR;
209
404
    }
210
405
 
211
 
    llcf->src = args[1];
 
406
    if (cmd->post == ngx_http_lua_content_handler_inline) {
 
407
        /* Don't eval nginx variables for inline lua code */
 
408
        llcf->content_src.value = value[1];
 
409
 
 
410
        p = ngx_palloc(cf->pool, NGX_HTTP_LUA_INLINE_KEY_LEN + 1);
 
411
        if (p == NULL) {
 
412
            return NGX_CONF_ERROR;
 
413
        }
 
414
 
 
415
        llcf->content_src_key = p;
 
416
 
 
417
        p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
 
418
        p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
 
419
        *p = '\0';
 
420
 
 
421
    } else {
 
422
        ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
 
423
        ccv.cf = cf;
 
424
        ccv.value = &value[1];
 
425
        ccv.complex_value = &llcf->content_src;
 
426
 
 
427
        if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
 
428
            return NGX_CONF_ERROR;
 
429
        }
 
430
 
 
431
        if (llcf->content_src.lengths == NULL) {
 
432
            /* no variable found */
 
433
            p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
 
434
            if (p == NULL) {
 
435
                return NGX_CONF_ERROR;
 
436
            }
 
437
 
 
438
            llcf->content_src_key = p;
 
439
 
 
440
            p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
 
441
            p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
 
442
            *p = '\0';
 
443
        }
 
444
    }
 
445
 
 
446
    llcf->content_handler = cmd->post;
212
447
 
213
448
    /*  register location content handler */
214
449
    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
215
450
    if (clcf == NULL) {
216
451
        return NGX_CONF_ERROR;
217
452
    }
218
 
    clcf->handler = cmd->post;
 
453
 
 
454
    clcf->handler = ngx_http_lua_content_handler;
219
455
 
220
456
    return NGX_CONF_OK;
221
457
}
222
458
 
223
 
 
224
 
ngx_int_t
225
 
ngx_http_lua_content_handler_inline(ngx_http_request_t *r)
226
 
{
227
 
    lua_State                   *L;
228
 
    ngx_int_t                    rc;
229
 
    ngx_http_lua_main_conf_t    *lmcf;
230
 
    ngx_http_lua_loc_conf_t     *llcf;
231
 
    char                        *err;
232
 
 
233
 
    llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
234
 
    lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
235
 
    L = lmcf->lua;
236
 
 
237
 
    /*  load Lua inline script (w/ cache) sp = 1 */
238
 
    rc = ngx_http_lua_cache_loadbuffer(L, llcf->src.data,
239
 
            llcf->src.len, "content_by_lua", &err);
240
 
 
241
 
    if (rc != NGX_OK) {
242
 
        if (err == NULL) {
243
 
            err = "unknown error";
244
 
        }
245
 
 
246
 
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
247
 
                "Failed to load Lua inlined code: %s", err);
248
 
 
249
 
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
250
 
    }
251
 
 
252
 
    rc = ngx_http_lua_content_by_chunk(L, r);
253
 
 
254
 
    if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
255
 
        return rc;
256
 
    }
257
 
 
258
 
    if (rc == NGX_DONE) {
259
 
        return NGX_DONE;
260
 
    }
261
 
 
262
 
    if (rc == NGX_AGAIN) {
263
 
#if defined(nginx_version) && nginx_version >= 8011
264
 
        r->main->count++;
265
 
#endif
266
 
        return NGX_DONE;
267
 
    }
268
 
 
269
 
    return NGX_OK;
270
 
}
271
 
 
272
 
 
273
 
ngx_int_t
274
 
ngx_http_lua_content_handler_file(ngx_http_request_t *r)
275
 
{
276
 
    lua_State                       *L;
277
 
    ngx_int_t                        rc;
278
 
    u_char                          *script_path;
279
 
    ngx_http_lua_main_conf_t        *lmcf;
280
 
    ngx_http_lua_loc_conf_t         *llcf;
281
 
    char                            *err;
282
 
 
283
 
    llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
284
 
 
285
 
    script_path = ngx_http_lua_rebase_path(r->pool, llcf->src.data,
286
 
            llcf->src.len);
287
 
 
288
 
    if (script_path == NULL) {
289
 
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
290
 
                "Failed to allocate memory to store absolute path: raw path='%v'",
291
 
                &(llcf->src));
292
 
 
293
 
        return NGX_ERROR;
294
 
    }
295
 
 
296
 
    lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
297
 
    L = lmcf->lua;
298
 
 
299
 
    /*  load Lua script file (w/ cache)        sp = 1 */
300
 
    rc = ngx_http_lua_cache_loadfile(L, (char *) script_path, &err);
301
 
 
302
 
    if (rc != NGX_OK) {
303
 
        if (err == NULL) {
304
 
            err = "unknown error";
305
 
        }
306
 
 
307
 
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
308
 
                "Failed to load Lua inlined code: %s", err);
309
 
 
310
 
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
311
 
    }
312
 
 
313
 
    /*  make sure we have a valid code chunk */
314
 
    assert(lua_isfunction(L, -1));
315
 
 
316
 
    rc = ngx_http_lua_content_by_chunk(L, r);
317
 
 
318
 
    if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
319
 
        return rc;
320
 
    }
321
 
 
322
 
    if (rc == NGX_DONE) {
323
 
        return NGX_DONE;
324
 
    }
325
 
 
326
 
    if (rc == NGX_AGAIN) {
327
 
#if defined(nginx_version) && nginx_version >= 8011
328
 
        r->main->count++;
329
 
#endif
330
 
        return NGX_DONE;
331
 
    }
332
 
 
333
 
    return NGX_OK;
334
 
}
335