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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
| int ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx) { int type; int idx; int found; u_char *data; size_t size; unsigned last; unsigned flush = 0; ngx_buf_t *b; ngx_chain_t *cl; ngx_chain_t *in;
idx = luaL_checkint(L, 2);
dd("index: %d", idx);
if (idx != 1 && idx != 2) { return luaL_error(L, "bad index: %d", idx); }
if (idx == 2) { last = lua_toboolean(L, 3); lua_getglobal(L, ngx_http_lua_chain_key); in = lua_touserdata(L, -1); lua_pop(L, 1);
if (last) { ctx->seen_last_in_filter = 1;
for (cl = in; cl; cl = cl->next) { if (cl->next == NULL) { if (r == r->main) { cl->buf->last_buf = 1;
} else { cl->buf->last_in_chain = 1; }
break; } }
} else { found = 0;
for (cl = in; cl; cl = cl->next) { b = cl->buf;
if (b->last_buf) { b->last_buf = 0; found = 1; }
if (b->last_in_chain) { b->last_in_chain = 0; found = 1; }
if (found && b->last == b->pos && !ngx_buf_in_memory(b)) {
b->sync = 1; } }
ctx->seen_last_in_filter = 0; }
return 0; }
type = lua_type(L, 3);
switch (type) { case LUA_TSTRING: case LUA_TNUMBER: data = (u_char *) lua_tolstring(L, 3, &size); break;
case LUA_TNIL: lua_getglobal(L, ngx_http_lua_chain_key); in = lua_touserdata(L, -1); lua_pop(L, 1);
last = 0;
for (cl = in; cl; cl = cl->next) { b = cl->buf;
if (b->flush) { flush = 1; }
if (b->last_in_chain || b->last_buf) { last = 1; }
dd("mark the buf as consumed: %d", (int) ngx_buf_size(b)); b->pos = b->last; }
goto done;
case LUA_TTABLE: size = ngx_http_lua_calc_strlen_in_table(L, 3 , 3 , 1 ); data = NULL; break;
default: return luaL_error(L, "bad chunk data type: %s", lua_typename(L, type)); }
lua_getglobal(L, ngx_http_lua_chain_key); in = lua_touserdata(L, -1); lua_pop(L, 1);
last = 0;
for (cl = in; cl; cl = cl->next) { b = cl->buf;
if (b->flush) { flush = 1; }
if (b->last_buf || b->last_in_chain) { last = 1; }
dd("mark the buf as consumed: %d", (int) ngx_buf_size(cl->buf)); cl->buf->pos = cl->buf->last; }
if (size == 0) { goto done; }
cl = ngx_http_lua_chain_get_free_buf(r->connection->log, r->pool, &ctx->free_bufs, size); if (cl == NULL) { return luaL_error(L, "no memory"); }
if (type == LUA_TTABLE) { cl->buf->last = ngx_http_lua_copy_str_in_table(L, 3, cl->buf->last); } else { cl->buf->last = ngx_copy(cl->buf->pos, data, size); }
done:
if (last || flush) { if (cl == NULL) { cl = ngx_http_lua_chain_get_free_buf(r->connection->log, r->pool, &ctx->free_bufs, 0); if (cl == NULL) { return luaL_error(L, "no memory"); } }
if (last) { ctx->seen_last_in_filter = 1;
if (r == r->main) { cl->buf->last_buf = 1;
} else { cl->buf->last_in_chain = 1; } }
if (flush) { cl->buf->flush = 1; } }
lua_pushlightuserdata(L, cl); lua_setglobal(L, ngx_http_lua_chain_key); return 0; }
|