Skip to content

Instantly share code, notes, and snippets.

@rrichardson
Created September 1, 2015 18:13
Show Gist options
  • Save rrichardson/577b0e0977d05ebe22f4 to your computer and use it in GitHub Desktop.
Save rrichardson/577b0e0977d05ebe22f4 to your computer and use it in GitHub Desktop.
int write_to_ngx(void* req_ctx, void* buffer, size_t size) {
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
void *p;
ngx_buf_t *b;
ngx_chain_t *cl;
ngx_int_t rc;
if (0 == size) {
WARN("0 size to write");
return 0;
}
r = (ngx_http_request_t*) req_ctx;
if (r == NULL) {
return -9;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return -8;
}
/*
ngx_http_cdata_check_context(ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
| NGX_HTTP_LUA_CONTEXT_ACCESS
| NGX_HTTP_LUA_CONTEXT_CONTENT); */
if (ctx->acquired_raw_req_socket) {
//"raw request socket acquired";
return -6;
}
if (r->header_only) {
//"header only";
return -5;
}
if (ctx->eof) {
return -4;
}
cl = ngx_http_lua_chain_get_free_buf(r->connection->log, r->pool,
&ctx->free_bufs, size);
if (cl == NULL) {
return -2;
}
b = cl->buf;
p = buffer;
b->last = ngx_copy(b->last, (u_char *) p, size);
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "lua write response");
rc = ngx_http_lua_send_chain_link(r, ctx, cl);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return -1;
}
dd("downstream write: %d, buf len: %d", (int) rc, (int) (b->last - b->pos));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment