~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_ndk.c

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry, Kartik Mistry
  • Date: 2011-09-26 10:17:04 UTC
  • mfrom: (4.2.38 sid)
  • Revision ID: package-import@ubuntu.com-20110926101704-x8pxngiujrmkxnn3
Tags: 1.1.4-2
[Kartik Mistry]
* debian/modules:
  + Updated nginx-upload-progress module, Thanks to upstream for fixing issue
    that FTBFS nginx on kFreeBSD-* archs.
  + Updated nginx-lua module to latest upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef DDEBUG
 
2
#define DDEBUG 0
 
3
#endif
 
4
#include "ddebug.h"
 
5
 
 
6
 
 
7
#include "ngx_http_lua_ndk.h"
 
8
 
 
9
 
 
10
#if defined(NDK) && NDK
 
11
 
 
12
 
 
13
static ndk_set_var_value_pt ngx_http_lookup_ndk_set_var_directive(u_char *name,
 
14
        size_t name_len);
 
15
static int ngx_http_lua_ndk_set_var_get(lua_State *L);
 
16
static int ngx_http_lua_ndk_set_var_set(lua_State *L);
 
17
static int ngx_http_lua_run_set_var_directive(lua_State *L);
 
18
 
 
19
 
 
20
int
 
21
ngx_http_lua_ndk_set_var_get(lua_State *L)
 
22
{
 
23
    ndk_set_var_value_pt                 func;
 
24
    size_t                               len;
 
25
    u_char                              *p;
 
26
 
 
27
    p = (u_char *) luaL_checklstring(L, 2, &len);
 
28
 
 
29
    dd("ndk.set_var metatable __index: %s", p);
 
30
 
 
31
    func = ngx_http_lookup_ndk_set_var_directive(p, len);
 
32
 
 
33
    if (func == NULL) {
 
34
        return luaL_error(L, "ndk.set_var: directive \"%s\" not found "
 
35
                "or does not use ndk_set_var_value",
 
36
                p);
 
37
 
 
38
    }
 
39
 
 
40
    lua_pushvalue(L, -1); /* table key key */
 
41
    lua_pushvalue(L, -1); /* table key key key */
 
42
 
 
43
    lua_pushlightuserdata(L, func); /* table key key key func */
 
44
 
 
45
    lua_pushcclosure(L, ngx_http_lua_run_set_var_directive, 2);
 
46
        /* table key key closure */
 
47
 
 
48
    lua_rawset(L, 1); /* table key */
 
49
 
 
50
    lua_rawget(L, 1); /* table closure */
 
51
 
 
52
    return 1;
 
53
}
 
54
 
 
55
 
 
56
int
 
57
ngx_http_lua_ndk_set_var_set(lua_State *L)
 
58
{
 
59
    return luaL_error(L, "Not allowed");
 
60
}
 
61
 
 
62
 
 
63
int
 
64
ngx_http_lua_run_set_var_directive(lua_State *L)
 
65
{
 
66
    ngx_int_t                            rc;
 
67
    ndk_set_var_value_pt                 func;
 
68
    ngx_str_t                            res;
 
69
    ngx_http_variable_value_t            arg;
 
70
    u_char                              *p;
 
71
    size_t                               len;
 
72
    ngx_http_request_t                  *r;
 
73
 
 
74
    if (lua_gettop(L) != 1) {
 
75
        return luaL_error(L, "expecting one argument");
 
76
    }
 
77
 
 
78
    arg.data = (u_char *) luaL_checklstring(L, 1, &len);
 
79
    arg.len = len;
 
80
 
 
81
    lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
 
82
    r = lua_touserdata(L, -1);
 
83
    lua_pop(L, 1);
 
84
 
 
85
    if (r == NULL) {
 
86
        return luaL_error(L, "no request object found");
 
87
    }
 
88
 
 
89
    p = (u_char *) luaL_checklstring(L, lua_upvalueindex(1), &len);
 
90
 
 
91
    dd("calling set_var func for %s", p);
 
92
 
 
93
    func = (ndk_set_var_value_pt) lua_touserdata(L, lua_upvalueindex(2));
 
94
 
 
95
    rc = func(r, &res, &arg);
 
96
 
 
97
    if (rc != NGX_OK) {
 
98
        return luaL_error(L, "calling directive %s failed with code %d",
 
99
                p, (int) rc);
 
100
    }
 
101
 
 
102
    lua_pushlstring(L, (char *) res.data, res.len);
 
103
 
 
104
    return 1;
 
105
}
 
106
 
 
107
 
 
108
static ndk_set_var_value_pt
 
109
ngx_http_lookup_ndk_set_var_directive(u_char *name,
 
110
        size_t name_len)
 
111
{
 
112
    ndk_set_var_t           *filter;
 
113
    ngx_uint_t               i;
 
114
    ngx_module_t            *module;
 
115
    ngx_command_t           *cmd;
 
116
 
 
117
    for (i = 0; ngx_modules[i]; i++) {
 
118
        module = ngx_modules[i];
 
119
        if (module->type != NGX_HTTP_MODULE) {
 
120
            continue;
 
121
        }
 
122
 
 
123
        cmd = ngx_modules[i]->commands;
 
124
        if (cmd == NULL) {
 
125
            continue;
 
126
        }
 
127
 
 
128
        for ( /* void */ ; cmd->name.len; cmd++) {
 
129
            if (cmd->set != ndk_set_var_value) {
 
130
                continue;
 
131
            }
 
132
 
 
133
            filter = cmd->post;
 
134
            if (filter == NULL) {
 
135
                continue;
 
136
            }
 
137
 
 
138
            if (cmd->name.len != name_len
 
139
                    || ngx_strncmp(cmd->name.data, name, name_len) != 0)
 
140
            {
 
141
                continue;
 
142
            }
 
143
 
 
144
            return (ndk_set_var_value_pt)(filter->func);
 
145
        }
 
146
    }
 
147
 
 
148
    return NULL;
 
149
}
 
150
 
 
151
 
 
152
void
 
153
ngx_http_lua_inject_ndk_api(lua_State *L)
 
154
{
 
155
    lua_createtable(L, 0, 1);    /* ndk.* */
 
156
 
 
157
    lua_newtable(L);    /* .set_var */
 
158
 
 
159
    lua_createtable(L, 0, 2); /* metatable for .set_var */
 
160
    lua_pushcfunction(L, ngx_http_lua_ndk_set_var_get);
 
161
    lua_setfield(L, -2, "__index");
 
162
    lua_pushcfunction(L, ngx_http_lua_ndk_set_var_set);
 
163
    lua_setfield(L, -2, "__newindex");
 
164
    lua_setmetatable(L, -2);
 
165
 
 
166
    lua_setfield(L, -2, "set_var");
 
167
 
 
168
    lua_getglobal(L, "package"); /* ndk package */
 
169
    lua_getfield(L, -1, "loaded"); /* ndk package loaded */
 
170
    lua_pushvalue(L, -3); /* ndk package loaded ndk */
 
171
    lua_setfield(L, -2, "ndk"); /* ndk package loaded */
 
172
    lua_pop(L, 2);
 
173
 
 
174
    lua_setglobal(L, "ndk");
 
175
}
 
176
 
 
177
 
 
178
#endif /* defined(NDK) && NDK */
 
179