Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ SSL handshake if the `wss://` scheme is used.
[ngx.ssl.parse_pem_priv_key](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ssl.md#parse_pem_priv_key)
function provided by lua-resty-core.

* `host`

Specifies the value of the `Host` header sent in the handshake request. If not provided, the `Host` header will be derived from the hostname/address and port in the connection URI.

* `server_name`

Specifies the server name (SNI) to use when performing the TLS handshake with the server. If not provided, the `host` value or the `<host/addr>:<port>` from the connection URI will be used.

The SSL connection mode (`wss://`) requires at least `ngx_lua` 0.9.11 or OpenResty 1.7.4.1.

[Back to TOC](#table-of-contents)
Expand Down
29 changes: 22 additions & 7 deletions lib/resty/websocket/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function _M.connect(self, uri, opts)
end

local scheme = m[1]
local host = m[2]
local addr = m[2]
local port = m[3]
local path = m[4]

Expand All @@ -117,6 +117,7 @@ function _M.connect(self, uri, opts)
local ssl_verify, server_name, headers, proto_header, origin_header
local sock_opts = {}
local client_cert, client_priv_key
local header_host

if opts then
local protos = opts.protocols
Expand Down Expand Up @@ -155,9 +156,11 @@ function _M.connect(self, uri, opts)
"client_priv_key must be provided with client_cert")
end

if opts.ssl_verify or opts.server_name then
ssl_verify = opts.ssl_verify
server_name = opts.server_name or host
ssl_verify = opts.ssl_verify

server_name = opts.server_name
if server_name ~= nil and type(server_name) ~= "string" then
return nil, "SSL server_name must be a string"
end

if opts.headers then
Expand All @@ -166,13 +169,18 @@ function _M.connect(self, uri, opts)
return nil, "custom headers must be a table"
end
end

header_host = opts.host
if header_host ~= nil and type(header_host) ~= "string" then
return nil, "custom host header must be a string"
end
end

local ok, err
if is_unix then
ok, err = sock:connect(host, sock_opts)
ok, err = sock:connect(addr, sock_opts)
else
ok, err = sock:connect(host, port, sock_opts)
ok, err = sock:connect(addr, port, sock_opts)
end
if not ok then
return nil, "failed to connect: " .. err
Expand All @@ -196,6 +204,9 @@ function _M.connect(self, uri, opts)
return nil, "failed to set TLS client certificate: " .. err
end
end

server_name = server_name or header_host or addr

ok, err = sock:sslhandshake(false, server_name, ssl_verify)
if not ok then
return nil, "ssl handshake failed: " .. err
Expand All @@ -218,8 +229,12 @@ function _M.connect(self, uri, opts)
rand(256) - 1)

local key = encode_base64(bytes)

local host_header = header_host
or (is_unix and "unix_sock" or addr .. ":" .. port)

local req = "GET " .. path .. " HTTP/1.1\r\nUpgrade: websocket\r\nHost: "
.. (is_unix and "unix_sock" or host .. ":" .. port)
.. host_header
.. "\r\nSec-WebSocket-Key: " .. key
.. (proto_header or "")
.. "\r\nSec-WebSocket-Version: 13"
Expand Down
Loading