static int
ngx_http_lua_ngx_send_empty_headers(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
lua_getglobal(L, GLOBALS_SYMBOL_REQUEST);
r = lua_touserdata(L, -1);
lua_pop(L, 1);
if (r) {
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx && ctx->headers_sent == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua send empty headers");
ctx->headers_sent = 1;
}
return 0;
}
dd("(lua-ngx-send-empty-headers) can't find nginx request object!");
return 0;
}
void
ngx_http_lua_inject_output_api(lua_State *L)
{
lua_pushcfunction(L, ngx_http_lua_ngx_send_empty_headers);
lua_setfield(L, -2, "send_empty_headers");
...
}
我尝试了一下,像上面这样,增加了一个API.
location = /hello {
content_by_lua '
ngx.send_empty_headers()
ngx.say("Hello World")
';
}
然后这样测试了一下,是可以跳过发送http头的,不知道这种解决思路是否合理?
在 2012年5月8日星期二UTC+8下午11时00分18秒,agentzh写道:
2012/5/8 Li Zhenchun <liz...@gmail.com>:
> 这个需求可以做下面的更改:
> 客户端连接后,先发一个http请求头,然后下面的通讯完全是自定义,包括服务器返回的第一个包也不是http响应头。
> 也就是半模仿websocket协议,这样的需求在现在的框架下能支持吗?
>目前,ngx.req.socket() 返回的 cosocket 对象是只读的。需要让其支持可写,理论上就可以支持 WebSocket
的。即向下游的输出不通过 ngx.print 和 ngx.say,而只接向下游 cosocket 对象写数据。
Regards,
-agentzh