~ubuntu-branches/ubuntu/raring/nginx/raring-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry, Kartik Mistry
  • Date: 2011-09-26 10:17:04 UTC
  • Revision ID: package-import@ubuntu.com-20110926101704-xij7mky6b7i8vx41
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
 
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
2
 
/* Copyright (C) agentzh */
3
 
 
 
1
#ifndef DDEBUG
4
2
#define DDEBUG 0
5
 
 
 
3
#endif
6
4
#include "ddebug.h"
7
5
 
8
6
#include "ngx_http_lua_headers.h"
 
7
#include "ngx_http_lua_headers_out.h"
 
8
#include "ngx_http_lua_headers_in.h"
9
9
#include "ngx_http_lua_util.h"
10
 
#include <ctype.h>
11
 
 
12
 
/* request time */
13
 
 
14
 
static ngx_int_t ngx_http_set_header(ngx_http_request_t *r,
15
 
    ngx_http_lua_header_val_t *hv, ngx_str_t *value);
16
 
 
17
 
static ngx_int_t ngx_http_set_header_helper(ngx_http_request_t *r,
18
 
    ngx_http_lua_header_val_t *hv, ngx_str_t *value,
19
 
    ngx_table_elt_t **output_header, ngx_flag_t no_create);
20
 
 
21
 
static ngx_int_t ngx_http_set_builtin_header(ngx_http_request_t *r,
22
 
    ngx_http_lua_header_val_t *hv, ngx_str_t *value);
23
 
 
24
 
static ngx_int_t ngx_http_set_content_length_header(ngx_http_request_t *r,
25
 
    ngx_http_lua_header_val_t *hv, ngx_str_t *value);
26
 
 
27
 
static ngx_int_t ngx_http_set_content_type_header(ngx_http_request_t *r,
28
 
        ngx_http_lua_header_val_t *hv, ngx_str_t *value);
29
 
 
30
 
static ngx_int_t ngx_http_clear_builtin_header(ngx_http_request_t *r,
31
 
    ngx_http_lua_header_val_t *hv, ngx_str_t *value);
32
 
 
33
 
static ngx_int_t ngx_http_clear_content_length_header(ngx_http_request_t *r,
34
 
        ngx_http_lua_header_val_t *hv, ngx_str_t *value);
35
 
 
36
 
 
37
 
static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
38
 
 
39
 
    { ngx_string("Server"),
40
 
                 offsetof(ngx_http_headers_out_t, server),
41
 
                 ngx_http_set_builtin_header },
42
 
 
43
 
    { ngx_string("Date"),
44
 
                 offsetof(ngx_http_headers_out_t, date),
45
 
                 ngx_http_set_builtin_header },
46
 
 
47
 
    { ngx_string("Content-Encoding"),
48
 
                 offsetof(ngx_http_headers_out_t, content_encoding),
49
 
                 ngx_http_set_builtin_header },
50
 
 
51
 
    { ngx_string("Location"),
52
 
                 offsetof(ngx_http_headers_out_t, location),
53
 
                 ngx_http_set_builtin_header },
54
 
 
55
 
    { ngx_string("Refresh"),
56
 
                 offsetof(ngx_http_headers_out_t, refresh),
57
 
                 ngx_http_set_builtin_header },
58
 
 
59
 
    { ngx_string("Last-Modified"),
60
 
                 offsetof(ngx_http_headers_out_t, last_modified),
61
 
                 ngx_http_set_builtin_header },
62
 
 
63
 
    { ngx_string("Content-Range"),
64
 
                 offsetof(ngx_http_headers_out_t, content_range),
65
 
                 ngx_http_set_builtin_header },
66
 
 
67
 
    { ngx_string("Accept-Ranges"),
68
 
                 offsetof(ngx_http_headers_out_t, accept_ranges),
69
 
                 ngx_http_set_builtin_header },
70
 
 
71
 
    { ngx_string("WWW-Authenticate"),
72
 
                 offsetof(ngx_http_headers_out_t, www_authenticate),
73
 
                 ngx_http_set_builtin_header },
74
 
 
75
 
    { ngx_string("Expires"),
76
 
                 offsetof(ngx_http_headers_out_t, expires),
77
 
                 ngx_http_set_builtin_header },
78
 
 
79
 
    { ngx_string("E-Tag"),
80
 
                 offsetof(ngx_http_headers_out_t, etag),
81
 
                 ngx_http_set_builtin_header },
82
 
 
83
 
    { ngx_string("Content-Length"),
84
 
                 offsetof(ngx_http_headers_out_t, content_length),
85
 
                 ngx_http_set_content_length_header },
86
 
 
87
 
    { ngx_string("Content-Type"),
88
 
                 0,
89
 
                 ngx_http_set_content_type_header },
90
 
 
91
 
    { ngx_null_string, 0, ngx_http_set_header }
92
 
};
93
 
 
94
 
 
95
 
/* request time implementation */
96
 
 
97
 
static ngx_int_t
98
 
ngx_http_set_header(ngx_http_request_t *r, ngx_http_lua_header_val_t *hv,
99
 
        ngx_str_t *value)
100
 
{
101
 
    return ngx_http_set_header_helper(r, hv, value, NULL, 0);
102
 
}
103
 
 
104
 
 
105
 
static ngx_int_t
106
 
ngx_http_set_header_helper(ngx_http_request_t *r, ngx_http_lua_header_val_t *hv,
107
 
        ngx_str_t *value, ngx_table_elt_t **output_header,
108
 
        ngx_flag_t no_create)
109
 
{
110
 
    ngx_table_elt_t             *h;
111
 
    ngx_list_part_t             *part;
112
 
    ngx_uint_t                  i;
113
 
    ngx_flag_t                  matched = 0;
114
 
 
115
 
    if (hv->no_override) {
116
 
        goto new_header;
117
 
    }
118
 
 
119
 
    part = &r->headers_out.headers.part;
120
 
    h = part->elts;
 
10
 
 
11
 
 
12
static int ngx_http_lua_ngx_req_header_set_helper(lua_State *L);
 
13
static int ngx_http_lua_ngx_header_get(lua_State *L);
 
14
static int ngx_http_lua_ngx_header_set(lua_State *L);
 
15
static int ngx_http_lua_ngx_req_get_headers(lua_State *L);
 
16
static int ngx_http_lua_ngx_req_header_clear(lua_State *L);
 
17
static int ngx_http_lua_ngx_req_header_set(lua_State *L);
 
18
 
 
19
 
 
20
static int
 
21
ngx_http_lua_ngx_req_get_headers(lua_State *L) {
 
22
    ngx_list_part_t              *part;
 
23
    ngx_table_elt_t              *header;
 
24
    ngx_http_request_t           *r;
 
25
    ngx_uint_t                    i;
 
26
 
 
27
    if (lua_gettop(L) != 0) {
 
28
        return luaL_error(L, "expecting 0 arguments but seen %d",
 
29
                lua_gettop(L));
 
30
    }
 
31
 
 
32
    lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
 
33
    r = lua_touserdata(L, -1);
 
34
    lua_pop(L, 1);
 
35
 
 
36
    if (r == NULL) {
 
37
        return luaL_error(L, "no request object found");
 
38
    }
 
39
 
 
40
    lua_createtable(L, 0, 4);
 
41
 
 
42
    part = &r->headers_in.headers.part;
 
43
    header = part->elts;
121
44
 
122
45
    for (i = 0; /* void */; i++) {
 
46
 
123
47
        if (i >= part->nelts) {
124
48
            if (part->next == NULL) {
125
49
                break;
126
50
            }
 
51
 
127
52
            part = part->next;
128
 
            h = part->elts;
 
53
            header = part->elts;
129
54
            i = 0;
130
55
        }
131
56
 
132
 
        if (
133
 
            (h[i].key.len == hv->key.len
134
 
                && ngx_strncasecmp(h[i].key.data,
135
 
                    hv->key.data,
136
 
                                   h[i].key.len) == 0)
137
 
            ||
138
 
            (h[i].key.len >= hv->key.len-1
139
 
                && ngx_strncasecmp(h[i].key.data,
140
 
                    hv->key.data,
141
 
                                   hv->key.len-1) == 0)
142
 
            )
143
 
        {
144
 
            if (value->len == 0) {
145
 
                dd("clearing normal header for %.*s", (int) hv->key.len,
146
 
                        hv->key.data);
147
 
 
148
 
                h[i].hash = 0;
149
 
            }
150
 
 
151
 
            h[i].value = *value;
152
 
 
153
 
            if (output_header) {
154
 
                *output_header = &h[i];
155
 
            }
156
 
 
157
 
            /* return NGX_OK; */
158
 
            matched = 1;
159
 
        }
160
 
    }
161
 
 
162
 
    if (matched){
163
 
      return NGX_OK;
164
 
    }
165
 
 
166
 
    if (no_create && value->len == 0) {
167
 
        return NGX_OK;
168
 
    }
169
 
 
170
 
new_header:
171
 
    h = ngx_list_push(&r->headers_out.headers);
172
 
 
173
 
    if (h == NULL) {
174
 
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
175
 
    }
176
 
 
177
 
    if (value->len == 0) {
178
 
        h->hash = 0;
179
 
    } else {
180
 
        h->hash = hv->hash;
181
 
    }
182
 
 
183
 
    h->key = hv->key;
184
 
    h->value = *value;
185
 
 
186
 
    h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
187
 
    if (h->lowcase_key == NULL) {
188
 
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
189
 
    }
190
 
 
191
 
    ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
192
 
 
193
 
    if (output_header) {
194
 
        *output_header = h;
195
 
    }
196
 
 
197
 
    return NGX_OK;
198
 
}
199
 
 
200
 
 
201
 
static ngx_int_t
202
 
ngx_http_set_builtin_header(ngx_http_request_t *r,
203
 
        ngx_http_lua_header_val_t *hv, ngx_str_t *value)
204
 
{
205
 
    ngx_table_elt_t  *h, **old;
206
 
 
207
 
    if (hv->offset) {
208
 
        old = (ngx_table_elt_t **) ((char *) &r->headers_out + hv->offset);
209
 
 
210
 
    } else {
211
 
        old = NULL;
212
 
    }
213
 
 
214
 
    if (old == NULL || *old == NULL) {
215
 
        return ngx_http_set_header_helper(r, hv, value, old, 0);
216
 
    }
217
 
 
218
 
    h = *old;
219
 
 
220
 
    if (value->len == 0) {
221
 
        dd("clearing the builtin header");
222
 
 
223
 
        h->hash = 0;
224
 
        h->value = *value;
225
 
 
226
 
        return NGX_OK;
227
 
    }
228
 
 
229
 
    h->hash = hv->hash;
230
 
    h->key = hv->key;
231
 
    h->value = *value;
232
 
 
233
 
    return NGX_OK;
234
 
}
235
 
 
236
 
 
237
 
static ngx_int_t
238
 
ngx_http_set_content_type_header(ngx_http_request_t *r,
239
 
        ngx_http_lua_header_val_t *hv, ngx_str_t *value)
240
 
{
241
 
    r->headers_out.content_type_len = value->len;
242
 
    r->headers_out.content_type = *value;
243
 
    r->headers_out.content_type_hash = hv->hash;
244
 
    r->headers_out.content_type_lowcase = NULL;
245
 
 
246
 
    value->len = 0;
247
 
 
248
 
    return ngx_http_set_header_helper(r, hv, value, NULL, 1);
249
 
}
250
 
 
251
 
 
252
 
static ngx_int_t
253
 
ngx_http_set_content_length_header(ngx_http_request_t *r,
254
 
        ngx_http_lua_header_val_t *hv, ngx_str_t *value)
255
 
{
256
 
    off_t           len;
257
 
 
258
 
    if (value->len == 0) {
259
 
        return ngx_http_clear_content_length_header(r, hv, value);
260
 
    }
261
 
 
262
 
    len = ngx_atosz(value->data, value->len);
263
 
    if (len == NGX_ERROR) {
264
 
        return NGX_ERROR;
265
 
    }
266
 
 
267
 
    r->headers_out.content_length_n = len;
268
 
 
269
 
    return ngx_http_set_builtin_header(r, hv, value);
270
 
}
271
 
 
272
 
 
273
 
static ngx_int_t
274
 
ngx_http_clear_content_length_header(ngx_http_request_t *r,
275
 
        ngx_http_lua_header_val_t *hv, ngx_str_t *value)
276
 
{
277
 
    r->headers_out.content_length_n = -1;
278
 
 
279
 
    return ngx_http_clear_builtin_header(r, hv, value);
280
 
}
281
 
 
282
 
 
283
 
static ngx_int_t
284
 
ngx_http_clear_builtin_header(ngx_http_request_t *r,
285
 
        ngx_http_lua_header_val_t *hv, ngx_str_t *value)
286
 
{
287
 
    value->len = 0;
288
 
 
289
 
    return ngx_http_set_builtin_header(r, hv, value);
290
 
}
291
 
 
292
 
 
293
 
ngx_int_t
294
 
ngx_http_lua_set_header(ngx_http_request_t *r, ngx_str_t key, ngx_str_t value,
295
 
        ngx_flag_t override)
296
 
{
297
 
    ngx_http_lua_header_val_t         hv;
298
 
    ngx_http_lua_set_header_t        *handlers = ngx_http_lua_set_handlers;
299
 
 
300
 
    ngx_uint_t                        i;
301
 
 
302
 
    dd("set header value: %.*s", (int) value.len, value.data);
303
 
 
304
 
    hv.hash = 1;
305
 
    hv.key = key;
306
 
 
307
 
    hv.offset = 0;
308
 
    hv.no_override = ! override;
309
 
 
310
 
    for (i = 0; handlers[i].name.len; i++) {
311
 
        if (hv.key.len != handlers[i].name.len
312
 
                || ngx_strncasecmp(hv.key.data, handlers[i].name.data,
313
 
                    handlers[i].name.len) != 0)
314
 
        {
315
 
            dd("hv key comparison: %s <> %s", handlers[i].name.data,
316
 
                    hv.key.data);
317
 
 
318
 
            continue;
319
 
        }
320
 
 
321
 
        dd("Matched handler: %s %s", handlers[i].name.data, hv.key.data);
322
 
 
323
 
        hv.offset = handlers[i].offset;
324
 
        hv.handler = handlers[i].handler;
325
 
 
326
 
        break;
327
 
    }
328
 
 
329
 
    if (handlers[i].name.len == 0 && handlers[i].handler) {
330
 
        hv.offset = handlers[i].offset;
331
 
        hv.handler = handlers[i].handler;
332
 
    }
333
 
 
334
 
    return hv.handler(r, &hv, &value);
 
57
        lua_pushlstring(L, (char *) header[i].key.data, header[i].key.len);
 
58
            /* stack: table key */
 
59
 
 
60
        lua_pushlstring(L, (char *) header[i].value.data,
 
61
                header[i].value.len); /* stack: table key value */
 
62
 
 
63
        ngx_http_lua_set_multi_value_table(L, -3);
 
64
 
 
65
        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
 
66
                       "lua request header: \"%V: %V\"",
 
67
                       &header[i].key, &header[i].value);
 
68
    }
 
69
 
 
70
    return 1;
 
71
}
 
72
 
 
73
 
 
74
static int
 
75
ngx_http_lua_ngx_header_get(lua_State *L)
 
76
{
 
77
    ngx_http_request_t          *r;
 
78
    u_char                      *p;
 
79
    ngx_str_t                    key;
 
80
    ngx_uint_t                   i;
 
81
    size_t                       len;
 
82
 
 
83
    lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
 
84
    r = lua_touserdata(L, -1);
 
85
    lua_pop(L, 1);
 
86
 
 
87
    if (r == NULL) {
 
88
        return luaL_error(L, "no request object found");
 
89
    }
 
90
 
 
91
    /* we skip the first argument that is the table */
 
92
    p = (u_char *) luaL_checklstring(L, 2, &len);
 
93
 
 
94
    dd("key: %.*s, len %d", (int) len, p, (int) len);
 
95
 
 
96
    /* replace "_" with "-" */
 
97
    for (i = 0; i < len; i++) {
 
98
        if (p[i] == '_') {
 
99
            p[i] = '-';
 
100
        }
 
101
    }
 
102
 
 
103
    key.data = ngx_palloc(r->pool, len + 1);
 
104
    if (key.data == NULL) {
 
105
        return luaL_error(L, "out of memory");
 
106
    }
 
107
 
 
108
    ngx_memcpy(key.data, p, len);
 
109
 
 
110
    key.data[len] = '\0';
 
111
 
 
112
    key.len = len;
 
113
 
 
114
    return ngx_http_lua_get_output_header(L, r, &key);
 
115
}
 
116
 
 
117
 
 
118
static int
 
119
ngx_http_lua_ngx_header_set(lua_State *L)
 
120
{
 
121
    ngx_http_request_t          *r;
 
122
    u_char                      *p;
 
123
    ngx_str_t                    key;
 
124
    ngx_str_t                    value;
 
125
    ngx_uint_t                   i;
 
126
    size_t                       len;
 
127
    ngx_http_lua_ctx_t          *ctx;
 
128
    ngx_int_t                    rc;
 
129
    ngx_uint_t                   n;
 
130
 
 
131
    lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
 
132
    r = lua_touserdata(L, -1);
 
133
    lua_pop(L, 1);
 
134
 
 
135
    if (r == NULL) {
 
136
        return luaL_error(L, "no request object found");
 
137
    }
 
138
 
 
139
    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
 
140
 
 
141
    if (ctx->headers_sent) {
 
142
        return luaL_error(L, "attempt to set ngx.header.HEADER after "
 
143
                "sending out response headers");
 
144
    }
 
145
 
 
146
    /* we skip the first argument that is the table */
 
147
    p = (u_char *) luaL_checklstring(L, 2, &len);
 
148
 
 
149
    dd("key: %.*s, len %d", (int) len, p, (int) len);
 
150
 
 
151
    /* replace "_" with "-" */
 
152
    for (i = 0; i < len; i++) {
 
153
        if (p[i] == '_') {
 
154
            p[i] = '-';
 
155
        }
 
156
    }
 
157
 
 
158
    key.data = ngx_palloc(r->pool, len + 1);
 
159
    if (key.data == NULL) {
 
160
        return luaL_error(L, "out of memory");
 
161
    }
 
162
 
 
163
    ngx_memcpy(key.data, p, len);
 
164
 
 
165
    key.data[len] = '\0';
 
166
 
 
167
    key.len = len;
 
168
 
 
169
    if (! ctx->headers_set) {
 
170
        rc = ngx_http_set_content_type(r);
 
171
        if (rc != NGX_OK) {
 
172
            return luaL_error(L,
 
173
                    "failed to set default content type: %d",
 
174
                    (int) rc);
 
175
        }
 
176
 
 
177
        ctx->headers_set = 1;
 
178
    }
 
179
 
 
180
    if (lua_type(L, 3) == LUA_TNIL) {
 
181
        value.data = NULL;
 
182
        value.len = 0;
 
183
 
 
184
    } else if (lua_type(L, 3) == LUA_TTABLE) {
 
185
        n = luaL_getn(L, 3);
 
186
        if (n == 0) {
 
187
            value.data = NULL;
 
188
            value.len = 0;
 
189
 
 
190
        } else {
 
191
            for (i = 1; i <= n; i++) {
 
192
                dd("header value table index %d", (int) i);
 
193
 
 
194
                lua_rawgeti(L, 3, i);
 
195
                p = (u_char *) luaL_checklstring(L, -1, &len);
 
196
 
 
197
                value.data = ngx_palloc(r->pool, len);
 
198
                if (value.data == NULL) {
 
199
                    return luaL_error(L, "out of memory");
 
200
                }
 
201
 
 
202
                ngx_memcpy(value.data, p, len);
 
203
                value.len = len;
 
204
 
 
205
                rc = ngx_http_lua_set_output_header(r, key, value,
 
206
                        i == 1 /* override */);
 
207
 
 
208
                if (rc != NGX_OK) {
 
209
                    return luaL_error(L,
 
210
                            "failed to set header %s (error: %d)",
 
211
                            key.data, (int) rc);
 
212
                }
 
213
            }
 
214
 
 
215
            return 0;
 
216
        }
 
217
 
 
218
    } else {
 
219
        p = (u_char *) luaL_checklstring(L, 3, &len);
 
220
        value.data = ngx_palloc(r->pool, len);
 
221
        if (value.data == NULL) {
 
222
            return luaL_error(L, "out of memory");
 
223
        }
 
224
 
 
225
        ngx_memcpy(value.data, p, len);
 
226
        value.len = len;
 
227
    }
 
228
 
 
229
    dd("key: %.*s, value: %.*s",
 
230
            (int) key.len, key.data, (int) value.len, value.data);
 
231
 
 
232
    rc = ngx_http_lua_set_output_header(r, key, value, 1 /* override */);
 
233
 
 
234
    if (rc != NGX_OK) {
 
235
        return luaL_error(L, "failed to set header %s (error: %d)",
 
236
                key.data, (int) rc);
 
237
    }
 
238
 
 
239
    return 0;
 
240
}
 
241
 
 
242
 
 
243
static int
 
244
ngx_http_lua_ngx_req_header_clear(lua_State *L)
 
245
{
 
246
    if (lua_gettop(L) != 1) {
 
247
        return luaL_error(L, "expecting one arguments, but seen %d",
 
248
                lua_gettop(L));
 
249
    }
 
250
 
 
251
    lua_pushnil(L);
 
252
 
 
253
    return ngx_http_lua_ngx_req_header_set_helper(L);
 
254
}
 
255
 
 
256
 
 
257
static int
 
258
ngx_http_lua_ngx_req_header_set(lua_State *L)
 
259
{
 
260
    if (lua_gettop(L) != 2) {
 
261
        return luaL_error(L, "expecting two arguments, but seen %d",
 
262
                lua_gettop(L));
 
263
    }
 
264
 
 
265
    return ngx_http_lua_ngx_req_header_set_helper(L);
 
266
}
 
267
 
 
268
 
 
269
static int
 
270
ngx_http_lua_ngx_req_header_set_helper(lua_State *L)
 
271
{
 
272
    ngx_http_request_t          *r;
 
273
    u_char                      *p;
 
274
    ngx_str_t                    key;
 
275
    ngx_str_t                    value;
 
276
    ngx_uint_t                   i;
 
277
    size_t                       len;
 
278
    ngx_int_t                    rc;
 
279
    ngx_uint_t                   n;
 
280
 
 
281
    lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
 
282
    r = lua_touserdata(L, -1);
 
283
    lua_pop(L, 1);
 
284
 
 
285
    if (r == NULL) {
 
286
        return luaL_error(L, "no request object found");
 
287
    }
 
288
 
 
289
    p = (u_char *) luaL_checklstring(L, 1, &len);
 
290
 
 
291
    dd("key: %.*s, len %d", (int) len, p, (int) len);
 
292
 
 
293
    /* replace "_" with "-" */
 
294
    for (i = 0; i < len; i++) {
 
295
        if (p[i] == '_') {
 
296
            p[i] = '-';
 
297
        }
 
298
    }
 
299
 
 
300
    key.data = ngx_palloc(r->pool, len + 1);
 
301
    if (key.data == NULL) {
 
302
        return luaL_error(L, "out of memory");
 
303
    }
 
304
 
 
305
    ngx_memcpy(key.data, p, len);
 
306
 
 
307
    key.data[len] = '\0';
 
308
 
 
309
    key.len = len;
 
310
 
 
311
    if (lua_type(L, 2) == LUA_TNIL) {
 
312
        value.data = NULL;
 
313
        value.len = 0;
 
314
 
 
315
    } else if (lua_type(L, 2) == LUA_TTABLE) {
 
316
        n = luaL_getn(L, 2);
 
317
        if (n == 0) {
 
318
            value.data = NULL;
 
319
            value.len = 0;
 
320
 
 
321
        } else {
 
322
            for (i = 1; i <= n; i++) {
 
323
                dd("header value table index %d", (int) i);
 
324
 
 
325
                lua_rawgeti(L, 2, i);
 
326
                p = (u_char *) luaL_checklstring(L, -1, &len);
 
327
 
 
328
                value.data = ngx_palloc(r->pool, len);
 
329
                if (value.data == NULL) {
 
330
                    return luaL_error(L, "out of memory");
 
331
                }
 
332
 
 
333
                ngx_memcpy(value.data, p, len);
 
334
                value.len = len;
 
335
 
 
336
                rc = ngx_http_lua_set_input_header(r, key, value,
 
337
                        i == 1 /* override */);
 
338
 
 
339
                if (rc != NGX_OK) {
 
340
                    return luaL_error(L,
 
341
                            "failed to set header %s (error: %d)",
 
342
                            key.data, (int) rc);
 
343
                }
 
344
            }
 
345
 
 
346
            return 0;
 
347
        }
 
348
 
 
349
    } else {
 
350
        p = (u_char *) luaL_checklstring(L, 2, &len);
 
351
        value.data = ngx_palloc(r->pool, len);
 
352
        if (value.data == NULL) {
 
353
            return luaL_error(L, "out of memory");
 
354
        }
 
355
 
 
356
        ngx_memcpy(value.data, p, len);
 
357
        value.len = len;
 
358
    }
 
359
 
 
360
    dd("key: %.*s, value: %.*s",
 
361
            (int) key.len, key.data, (int) value.len, value.data);
 
362
 
 
363
    rc = ngx_http_lua_set_input_header(r, key, value, 1 /* override */);
 
364
 
 
365
    if (rc != NGX_OK) {
 
366
        return luaL_error(L, "failed to set header %s (error: %d)",
 
367
                key.data, (int) rc);
 
368
    }
 
369
 
 
370
    return 0;
 
371
}
 
372
 
 
373
 
 
374
void
 
375
ngx_http_lua_inject_resp_header_api(lua_State *L)
 
376
{
 
377
    lua_newtable(L);    /* .header */
 
378
 
 
379
    lua_createtable(L, 0, 2); /* metatable for .header */
 
380
    lua_pushcfunction(L, ngx_http_lua_ngx_header_get);
 
381
    lua_setfield(L, -2, "__index");
 
382
    lua_pushcfunction(L, ngx_http_lua_ngx_header_set);
 
383
    lua_setfield(L, -2, "__newindex");
 
384
    lua_setmetatable(L, -2);
 
385
 
 
386
    lua_setfield(L, -2, "header");
 
387
}
 
388
 
 
389
 
 
390
void
 
391
ngx_http_lua_inject_req_header_api(lua_State *L)
 
392
{
 
393
    lua_pushcfunction(L, ngx_http_lua_ngx_req_header_clear);
 
394
    lua_setfield(L, -2, "clear_header");
 
395
 
 
396
    lua_pushcfunction(L, ngx_http_lua_ngx_req_header_set);
 
397
    lua_setfield(L, -2, "set_header");
 
398
 
 
399
    lua_pushcfunction(L, ngx_http_lua_ngx_req_get_headers);
 
400
    lua_setfield(L, -2, "get_headers");
335
401
}
336
402