{
ngx_int_t rc;
u_char *err_msg;
size_t len;
dd("initialize nginx context in Lua VM, code chunk at stack top sp = 1");
ngx_http_lua_body_filter_by_lua_env(L, r, in);
lua_pushcfunction(L, ngx_http_lua_traceback);
lua_insert(L, 1); /* put it under chunk and args */
dd("protected call user code");
rc = lua_pcall(L, 0, 1, 1);
lua_remove(L, 1); /* remove traceback function */
if (rc != 0) {
/* error occured */
err_msg = (u_char *) lua_tolstring(L, -1, &len);
if (err_msg == NULL) {
err_msg = (u_char *) "unknown reason";
len = sizeof("unknown reason") - 1;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"failed to run body_filter_by_lua*: %*s", len, err_msg);
lua_settop(L, 0); /* clear remaining elems on stack */
return NGX_ERROR;
}
/* rc == 0 */
rc = (ngx_int_t) lua_tointeger(L, -1);
dd("got return value: %d", (int) rc);
lua_settop(L, 0);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return NGX_ERROR;
}
return NGX_OK;