1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| static int ngx_http_lua_ngx_header_set(lua_State *L) { r = ngx_http_lua_get_req(L); if (r == NULL) { return luaL_error(L, "no request object found"); }
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module); if (ctx == NULL) { return luaL_error(L, "no ctx"); }
ngx_http_lua_check_fake_request(L, r); if (r->header_sent || ctx->header_sent) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "attempt to " "set ngx.header.HEADER after sending out " "response headers"); return 0; }
p = (u_char *) luaL_checklstring(L, 2, &len);
dd("key: %.*s, len %d", (int) len, p, (int) len);
key.data = ngx_palloc(r->pool, len + 1); if (key.data == NULL) { return luaL_error(L, "no memory"); }
ngx_memcpy(key.data, p, len); key.data[len] = '\0'; key.len = len;
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module); if (llcf->transform_underscores_in_resp_headers) { p = key.data; for (i = 0; i < len; i++) { if (p[i] == '_') { p[i] = '-'; } } }
if (!ctx->headers_set) { rc = ngx_http_lua_set_content_type(r); if (rc != NGX_OK) { return luaL_error(L, "failed to set default content type: %d", (int) rc); }
ctx->headers_set = 1; }
if (lua_type(L, 3) == LUA_TNIL) { ngx_str_null(&value); } else if (lua_type(L, 3) == LUA_TTABLE) { n = luaL_getn(L, 3); if (n == 0) { ngx_str_null(&value); } else { for (i = 1; i <= n; i++) { dd("header value table index %d", (int) i);
lua_rawgeti(L, 3, i); p = (u_char *) luaL_checklstring(L, -1, &len);
value.data = ngx_palloc(r->pool, len); if (value.data == NULL) { return luaL_error(L, "no memory"); }
ngx_memcpy(value.data, p, len); value.len = len;
rc = ngx_http_lua_set_output_header(r, key, value, i == 1 );
if (rc == NGX_ERROR) { return luaL_error(L, "failed to set header %s (error: %d)", key.data, (int) rc); } }
return 0; } } else { p = (u_char *) luaL_checklstring(L, 3, &len); value.data = ngx_palloc(r->pool, len); if (value.data == NULL) { return luaL_error(L, "no memory"); }
ngx_memcpy(value.data, p, len); value.len = len; }
dd("key: %.*s, value: %.*s", (int) key.len, key.data, (int) value.len, value.data);
rc = ngx_http_lua_set_output_header(r, key, value, 1 );
if (rc == NGX_ERROR) { return luaL_error(L, "failed to set header %s (error: %d)", key.data, (int) rc); }
return 0; }
static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = { { ngx_string("Server"), offsetof(ngx_http_headers_out_t, server), ngx_http_set_builtin_header },
{ ngx_null_string, 0, ngx_http_set_header } };
ngx_int_t ngx_http_lua_set_output_header(ngx_http_request_t *r, ngx_str_t key, ngx_str_t value, unsigned override) { ngx_http_lua_set_header_t *handlers = ngx_http_lua_set_handlers;
dd("set header value: %.*s", (int) value.len, value.data);
hv.hash = ngx_hash_key_lc(key.data, key.len); hv.key = key; hv.offset = 0; hv.no_override = !override; hv.handler = NULL;
for (i = 0; handlers[i].name.len; i++) { if (hv.key.len != handlers[i].name.len || ngx_strncasecmp(hv.key.data, handlers[i].name.data, handlers[i].name.len) != 0) { dd("hv key comparison: %s <> %s", handlers[i].name.data, hv.key.data); continue; }
dd("Matched handler: %s %s", handlers[i].name.data, hv.key.data);
hv.offset = handlers[i].offset; hv.handler = handlers[i].handler;
break; }
if (handlers[i].name.len == 0 && handlers[i].handler) { hv.offset = handlers[i].offset; hv.handler = handlers[i].handler; }
#if 1 if (hv.handler == NULL) { return NGX_ERROR; } #endif
return hv.handler(r, &hv, &value); }
|