openresty / lua-resty-websocket

WebSocket support for the ngx_lua module (and OpenResty)
505 stars 106 forks source link

feature: client:connect() returns HTTP response header #88

Closed flrgh closed 3 months ago

flrgh commented 3 months ago

This allows callers of client:connect() to retrieve/consume the response status and headers.

zhuizhuhaomeng commented 3 months ago

We need a more real example of how you use the HTTP header. The current seems useless.

flrgh commented 3 months ago

@zhuizhuhaomeng the benefit is being able to read the response headers (not just the status line) from the server, which are often relevant to the client in further handling the WebSocket connection:


local ok, err, res = client:connect(...)
assert(ok, err)

local header = assert(res:match("X%-My%-Custom%-Header: (.-)[\r\n]"))
print("value of `X-My-Custom-Header` is " .. header)

For instance, when using this library to implement a WebSocket proxy, it is necessary to consume the response headers so that they can be forwarded to downstream clients.

In 725dfd83a784aba14d25a515dfa12bdeee048913 I have updated the test to make this use case more apparent.