~ubuntu-branches/ubuntu/raring/nginx/raring-security

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry, Kartik Mistry, Cyril Lavier, Michael Lustfield
  • Date: 2012-12-18 10:29:18 UTC
  • mfrom: (4.2.56 sid)
  • Revision ID: package-import@ubuntu.com-20121218102918-dxtwj9vj89sbj8dz
Tags: 1.2.6-1
[ Kartik Mistry ]
* New upstream release.
* debian/nginx-common.nginx.init:
  + Used log_*_msg instead of echo for better init messages.
  + Added patch to check start-stop-daemon exit status, Thanks to
    Sergey B Kirpichev <skirpichev@gmail.com> (Closes: #695374).
* debian/po/ja.po:
  + Added new Japanese translation. Thanks to victory <victory.deb@gmail.com>
    (Closes: #692481).
* debian/po/pt_BR.po:
  + Added new Brazilian Portuguese translation. Thanks to
    Adriano Rafael Gomes <adrianorg@gmail.com> (Closes: #692481).

[ Cyril Lavier ]
* debian/rules
  + Added RealIP module in nginx-naxsi (Closes: #693302).
* debian/modules/nginx-cache-purge/
  + Updated nginx-cache-purge module with the 2.0 version.
* debian/modules/nginx-lua/
  + Updated nginx-lua module with the 0.7.8 version.
* debian/modules/nginx-echo/
  + Updated the nginx-echo module with the 0.41 version.
* debian/modules/headers-more-nginx-module/
  + Updated the Headers-more module with the 0.19 version.
* debian/modules/README.Modules-versions
  + Updated the current version of modules following the updates.

[ Michael Lustfield ]
* debian/conf/sites-available/default
  + Uncommented listen lines to make server block default.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
#include "ngx_http_lua_control.h"
7
7
#include "ngx_http_lua_util.h"
 
8
#include "ngx_http_lua_coroutine.h"
8
9
 
9
10
 
10
11
static int ngx_http_lua_ngx_exec(lua_State *L);
11
12
static int ngx_http_lua_ngx_redirect(lua_State *L);
12
13
static int ngx_http_lua_ngx_exit(lua_State *L);
 
14
static int ngx_http_lua_on_abort(lua_State *L);
13
15
 
14
16
 
15
17
void
16
18
ngx_http_lua_inject_control_api(ngx_log_t *log, lua_State *L)
17
19
{
18
 
    ngx_int_t         rc;
19
 
 
20
20
    /* ngx.redirect */
21
21
 
22
22
    lua_pushcfunction(L, ngx_http_lua_ngx_redirect);
23
 
    lua_setfield(L, -2, "_redirect");
24
 
 
25
 
#if 1
26
 
    {
27
 
        const char    buf[] = "ngx._redirect(...) ngx._check_aborted()";
28
 
 
29
 
        rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "ngx.redirect");
30
 
    }
31
 
 
32
 
    if (rc != NGX_OK) {
33
 
        ngx_log_error(NGX_LOG_CRIT, log, 0,
34
 
                      "failed to load Lua code for ngx.redirect(): %i",
35
 
                      rc);
36
 
 
37
 
    } else {
38
 
        lua_setfield(L, -2, "redirect");
39
 
    }
40
 
#endif
 
23
    lua_setfield(L, -2, "redirect");
41
24
 
42
25
    /* ngx.exec */
43
26
 
44
27
    lua_pushcfunction(L, ngx_http_lua_ngx_exec);
45
 
    lua_setfield(L, -2, "_exec");
46
 
 
47
 
#if 1
48
 
    {
49
 
        const char    buf[] = "ngx._exec(...) ngx._check_aborted()";
50
 
 
51
 
        rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "ngx.exec");
52
 
    }
53
 
 
54
 
    if (rc != NGX_OK) {
55
 
        ngx_log_error(NGX_LOG_CRIT, log, 0,
56
 
                      "failed to load Lua code for ngx.exec(): %i",
57
 
                      rc);
58
 
 
59
 
    } else {
60
 
        lua_setfield(L, -2, "exec");
61
 
    }
62
 
#endif
 
28
    lua_setfield(L, -2, "exec");
63
29
 
64
30
    lua_pushcfunction(L, ngx_http_lua_ngx_exit);
65
31
    lua_setfield(L, -2, "throw_error"); /* deprecated */
67
33
    /* ngx.exit */
68
34
 
69
35
    lua_pushcfunction(L, ngx_http_lua_ngx_exit);
70
 
    lua_setfield(L, -2, "_exit");
71
 
 
72
 
#if 1
73
 
    {
74
 
        const char    buf[] = "ngx._exit(...) ngx._check_aborted()";
75
 
 
76
 
        rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "ngx.exit");
77
 
    }
78
 
 
79
 
    if (rc != NGX_OK) {
80
 
        ngx_log_error(NGX_LOG_CRIT, log, 0,
81
 
                      "failed to load Lua code for ngx.exit(): %i",
82
 
                      rc);
83
 
 
84
 
    } else {
85
 
        lua_setfield(L, -2, "exit");
86
 
    }
87
 
#endif
 
36
    lua_setfield(L, -2, "exit");
 
37
 
 
38
    /* ngx.on_abort */
 
39
 
 
40
    lua_pushcfunction(L, ngx_http_lua_on_abort);
 
41
    lua_setfield(L, -2, "on_abort");
88
42
}
89
43
 
90
44
 
108
62
                n);
109
63
    }
110
64
 
111
 
    lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
 
65
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
 
66
    lua_rawget(L, LUA_GLOBALSINDEX);
112
67
    r = lua_touserdata(L, -1);
113
68
    lua_pop(L, 1);
114
69
 
137
92
    uri.len = len;
138
93
 
139
94
    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
 
95
    if (ctx == NULL) {
 
96
        return luaL_error(L, "no ctx found");
 
97
    }
 
98
 
 
99
    ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
 
100
                               | NGX_HTTP_LUA_CONTEXT_ACCESS
 
101
                               | NGX_HTTP_LUA_CONTEXT_CONTENT);
 
102
 
 
103
    ngx_http_lua_check_if_abortable(L, ctx);
140
104
 
141
105
    if (ngx_http_parse_unsafe_uri(r, &uri, &args, &flags)
142
106
        != NGX_OK)
171
135
 
172
136
            break;
173
137
 
 
138
        case LUA_TNIL:
 
139
            user_args.data = NULL;
 
140
            user_args.len = 0;
 
141
            break;
 
142
 
174
143
        default:
175
144
            msg = lua_pushfstring(L, "string, number, or table expected, "
176
145
                    "but got %s", luaL_typename(L, 2));
248
217
        rc = NGX_HTTP_MOVED_TEMPORARILY;
249
218
    }
250
219
 
251
 
    lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
 
220
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
 
221
    lua_rawget(L, LUA_GLOBALSINDEX);
252
222
    r = lua_touserdata(L, -1);
253
223
    lua_pop(L, 1);
254
224
 
261
231
        return luaL_error(L, "no request ctx found");
262
232
    }
263
233
 
 
234
    ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
 
235
                               | NGX_HTTP_LUA_CONTEXT_ACCESS
 
236
                               | NGX_HTTP_LUA_CONTEXT_CONTENT);
 
237
 
 
238
    ngx_http_lua_check_if_abortable(L, ctx);
 
239
 
264
240
    if (ctx->headers_sent) {
265
241
        return luaL_error(L, "attempt to call ngx.redirect after sending out "
266
242
                "the headers");
317
293
        return luaL_error(L, "expecting one argument");
318
294
    }
319
295
 
320
 
    lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
 
296
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
 
297
    lua_rawget(L, LUA_GLOBALSINDEX);
321
298
    r = lua_touserdata(L, -1);
322
299
    lua_pop(L, 1);
323
300
 
330
307
        return luaL_error(L, "no request ctx found");
331
308
    }
332
309
 
 
310
    ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
 
311
                               | NGX_HTTP_LUA_CONTEXT_ACCESS
 
312
                               | NGX_HTTP_LUA_CONTEXT_CONTENT);
 
313
 
333
314
    rc = (ngx_int_t) luaL_checkinteger(L, 1);
334
315
 
335
 
    if (rc >= NGX_HTTP_SPECIAL_RESPONSE && ctx->headers_sent) {
336
 
        return luaL_error(L, "attempt to call ngx.exit after sending "
337
 
                "out the headers");
 
316
    if (ctx->no_abort
 
317
        && rc != NGX_ERROR
 
318
        && rc != NGX_HTTP_CLOSE
 
319
        && rc != NGX_HTTP_REQUEST_TIME_OUT
 
320
        && rc != NGX_HTTP_CLIENT_CLOSED_REQUEST)
 
321
    {
 
322
        return luaL_error(L, "attempt to abort with pending subrequests");
 
323
    }
 
324
 
 
325
    if (ctx->headers_sent
 
326
        && rc >= NGX_HTTP_SPECIAL_RESPONSE
 
327
        && rc != NGX_HTTP_REQUEST_TIME_OUT
 
328
        && rc != NGX_HTTP_CLIENT_CLOSED_REQUEST
 
329
        && rc != NGX_HTTP_CLOSE)
 
330
    {
 
331
        if (rc != (ngx_int_t) r->headers_out.status) {
 
332
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "attempt to "
 
333
                          "set status %i via ngx.exit after sending out the "
 
334
                          "response status %ui", rc, r->headers_out.status);
 
335
        }
 
336
 
 
337
        rc = NGX_HTTP_OK;
338
338
    }
339
339
 
340
340
    ctx->exit_code = rc;
347
347
    return lua_yield(L, 0);
348
348
}
349
349
 
 
350
 
 
351
static int
 
352
ngx_http_lua_on_abort(lua_State *L)
 
353
{
 
354
    ngx_http_request_t           *r;
 
355
    ngx_http_lua_ctx_t           *ctx;
 
356
    ngx_http_lua_co_ctx_t        *coctx = NULL;
 
357
    ngx_http_lua_loc_conf_t      *llcf;
 
358
 
 
359
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
 
360
    lua_rawget(L, LUA_GLOBALSINDEX);
 
361
    r = lua_touserdata(L, -1);
 
362
    lua_pop(L, 1);
 
363
 
 
364
    if (r == NULL) {
 
365
        return luaL_error(L, "no request found");
 
366
    }
 
367
 
 
368
    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
 
369
    if (ctx == NULL) {
 
370
        return luaL_error(L, "no request ctx found");
 
371
    }
 
372
 
 
373
    if (ctx->on_abort_co_ctx) {
 
374
        lua_pushnil(L);
 
375
        lua_pushliteral(L, "duplicate call");
 
376
        return 2;
 
377
    }
 
378
 
 
379
    llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
 
380
    if (!llcf->check_client_abort) {
 
381
        lua_pushnil(L);
 
382
        lua_pushliteral(L, "lua_check_client_abort is off");
 
383
        return 2;
 
384
    }
 
385
 
 
386
    ngx_http_lua_coroutine_create_helper(L, r, ctx, &coctx);
 
387
 
 
388
    lua_pushlightuserdata(L, &ngx_http_lua_coroutines_key);
 
389
    lua_rawget(L, LUA_REGISTRYINDEX);
 
390
    lua_pushvalue(L, -2);
 
391
 
 
392
    dd("on_wait thread 1: %p", lua_tothread(L, -1));
 
393
 
 
394
    coctx->co_ref = luaL_ref(L, -2);
 
395
    lua_pop(L, 1);
 
396
 
 
397
    coctx->is_uthread = 1;
 
398
    ctx->on_abort_co_ctx = coctx;
 
399
 
 
400
    dd("on_wait thread 2: %p", coctx->co);
 
401
 
 
402
    coctx->co_status = NGX_HTTP_LUA_CO_SUSPENDED;
 
403
    coctx->parent_co_ctx = ctx->cur_co_ctx;
 
404
 
 
405
    lua_pushinteger(L, 1);
 
406
    return 1;
 
407
}
 
408