~spuul/nginx/trunk

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-02-15 03:05:42 UTC
  • mfrom: (4.3.10 sid)
  • Revision ID: package-import@ubuntu.com-20140215030542-71ubtowl24vf7nfn
Tags: 1.4.5-1ubuntu1
* Resynchronise with Debian (LP: #1280511).  Remaining changes:
  - debian/patches/ubuntu-branding.patch:
    + Add Ubuntu branding to server_tokens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
static int
37
37
ngx_http_lua_ngx_get(lua_State *L)
38
38
{
 
39
    int                          status;
39
40
    ngx_http_request_t          *r;
40
41
    u_char                      *p;
41
42
    size_t                       len;
42
 
    ngx_http_lua_ctx_t          *ctx;
43
 
 
44
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
45
 
    lua_rawget(L, LUA_GLOBALSINDEX);
46
 
    r = lua_touserdata(L, -1);
47
 
    lua_pop(L, 1);
48
 
 
 
43
 
 
44
    r = ngx_http_lua_get_req(L);
49
45
    if (r == NULL) {
50
46
        return luaL_error(L, "no request object found");
51
47
    }
58
54
        && ngx_strncmp(p, "status", sizeof("status") - 1) == 0)
59
55
    {
60
56
        ngx_http_lua_check_fake_request(L, r);
61
 
        lua_pushnumber(L, (lua_Number) r->headers_out.status);
 
57
 
 
58
        if (r->err_status) {
 
59
            status = r->err_status;
 
60
 
 
61
        } else if (r->headers_out.status) {
 
62
            status = r->headers_out.status;
 
63
 
 
64
        } else if (r->http_version == NGX_HTTP_VERSION_9) {
 
65
            status = 9;
 
66
 
 
67
        } else {
 
68
            status = 0;
 
69
        }
 
70
 
 
71
        lua_pushinteger(L, status);
62
72
        return 1;
63
73
    }
64
74
 
78
88
    if (len == sizeof("headers_sent") - 1
79
89
        && ngx_strncmp(p, "headers_sent", sizeof("headers_sent") - 1) == 0)
80
90
    {
81
 
        ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
82
 
        if (ctx == NULL) {
83
 
            return luaL_error(L, "no ctx");
84
 
        }
85
 
 
86
 
        ngx_http_lua_check_fake_request2(L, r, ctx);
87
 
 
88
 
        dd("headers sent: %d", ctx->headers_sent);
89
 
 
90
 
        lua_pushboolean(L, ctx->headers_sent ? 1 : 0);
 
91
        ngx_http_lua_check_fake_request(L, r);
 
92
 
 
93
        dd("headers sent: %d", r->header_sent);
 
94
 
 
95
        lua_pushboolean(L, r->header_sent ? 1 : 0);
91
96
        return 1;
92
97
    }
93
98
 
104
109
    ngx_http_request_t          *r;
105
110
    u_char                      *p;
106
111
    size_t                       len;
107
 
    ngx_http_lua_ctx_t          *ctx;
108
 
 
109
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
110
 
    lua_rawget(L, LUA_GLOBALSINDEX);
111
 
    r = lua_touserdata(L, -1);
112
 
    lua_pop(L, 1);
113
 
 
114
 
    if (r == NULL) {
115
 
        return luaL_error(L, "no request object found");
116
 
    }
117
112
 
118
113
    /* we skip the first argument that is the table */
119
114
    p = (u_char *) luaL_checklstring(L, 2, &len);
121
116
    if (len == sizeof("status") - 1
122
117
        && ngx_strncmp(p, "status", sizeof("status") - 1) == 0)
123
118
    {
124
 
        ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
 
119
        r = ngx_http_lua_get_req(L);
 
120
        if (r == NULL) {
 
121
            return luaL_error(L, "no request object found");
 
122
        }
125
123
 
126
 
        if (ctx->headers_sent) {
 
124
        if (r->header_sent) {
127
125
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
128
126
                          "attempt to set ngx.status after sending out "
129
127
                          "response headers");
130
128
            return 0;
131
129
        }
132
130
 
133
 
        ngx_http_lua_check_fake_request2(L, r, ctx);
 
131
        ngx_http_lua_check_fake_request(L, r);
134
132
 
135
133
        /* get the value */
136
134
        r->headers_out.status = (ngx_uint_t) luaL_checknumber(L, 3);
 
135
 
 
136
        if (r->headers_out.status == 101) {
 
137
            /*
 
138
             * XXX work-around a bug in the Nginx core that 101 does
 
139
             * not have a default status line
 
140
             */
 
141
 
 
142
            ngx_str_set(&r->headers_out.status_line, "101 Switching Protocols");
 
143
 
 
144
        } else {
 
145
            r->headers_out.status_line.len = 0;
 
146
        }
 
147
 
 
148
        return 0;
 
149
    }
 
150
 
 
151
    if (len == sizeof("ctx") - 1
 
152
        && ngx_strncmp(p, "ctx", sizeof("ctx") - 1) == 0)
 
153
    {
 
154
        r = ngx_http_lua_get_req(L);
 
155
        if (r == NULL) {
 
156
            return luaL_error(L, "no request object found");
 
157
        }
 
158
 
 
159
        return ngx_http_lua_ngx_set_ctx(L);
 
160
    }
 
161
 
 
162
    lua_rawset(L, -3);
 
163
    return 0;
 
164
}
 
165
 
 
166
 
 
167
#ifndef NGX_HTTP_LUA_NO_FFI_API
 
168
int
 
169
ngx_http_lua_ffi_get_resp_status(ngx_http_request_t *r)
 
170
{
 
171
    if (r->connection->fd == -1) {
 
172
        return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
 
173
    }
 
174
 
 
175
    if (r->err_status) {
 
176
        return r->err_status;
 
177
 
 
178
    } else if (r->headers_out.status) {
 
179
        return r->headers_out.status;
 
180
 
 
181
    } else if (r->http_version == NGX_HTTP_VERSION_9) {
 
182
        return 9;
 
183
 
 
184
    } else {
 
185
        return 0;
 
186
    }
 
187
}
 
188
 
 
189
 
 
190
int
 
191
ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int status)
 
192
{
 
193
    if (r->connection->fd == -1) {
 
194
        return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
 
195
    }
 
196
 
 
197
    if (r->header_sent) {
 
198
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
 
199
                      "attempt to set ngx.status after sending out "
 
200
                      "response headers");
 
201
        return NGX_DECLINED;
 
202
    }
 
203
 
 
204
    r->headers_out.status = status;
 
205
 
 
206
    if (status == 101) {
 
207
        /*
 
208
         * XXX work-around a bug in the Nginx core older than 1.5.5
 
209
         * that 101 does not have a default status line
 
210
         */
 
211
 
 
212
        ngx_str_set(&r->headers_out.status_line, "101 Switching Protocols");
 
213
 
 
214
    } else {
137
215
        r->headers_out.status_line.len = 0;
138
 
        return 0;
139
 
    }
140
 
 
141
 
    if (len == sizeof("ctx") - 1
142
 
        && ngx_strncmp(p, "ctx", sizeof("ctx") - 1) == 0)
143
 
    {
144
 
        return ngx_http_lua_ngx_set_ctx(L);
145
 
    }
146
 
 
147
 
    return luaL_error(L, "attempt to write to ngx. with the key \"%s\"", p);
148
 
}
 
216
    }
 
217
 
 
218
    return NGX_OK;
 
219
}
 
220
 
 
221
 
 
222
int
 
223
ngx_http_lua_ffi_is_subrequest(ngx_http_request_t *r)
 
224
{
 
225
    if (r->connection->fd == -1) {
 
226
        return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
 
227
    }
 
228
 
 
229
    return r != r->main;
 
230
}
 
231
 
 
232
 
 
233
int
 
234
ngx_http_lua_ffi_headers_sent(ngx_http_request_t *r)
 
235
{
 
236
    ngx_http_lua_ctx_t          *ctx;
 
237
 
 
238
    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
 
239
    if (ctx == NULL) {
 
240
        return NGX_HTTP_LUA_FFI_NO_REQ_CTX;
 
241
    }
 
242
 
 
243
    if (r->connection->fd == -1) {
 
244
        return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
 
245
    }
 
246
 
 
247
    return r->header_sent ? 1 : 0;
 
248
}
 
249
#endif /* NGX_HTTP_LUA_NO_FFI_API */
 
250
 
149
251
 
150
252
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */