Skip to content

feature: tcpsock sslhandshake alpn#513

Open
bzp2010 wants to merge 5 commits intoopenresty:masterfrom
bzp2010:bzp/feat-tcpsock-sslhandshake-alpn
Open

feature: tcpsock sslhandshake alpn#513
bzp2010 wants to merge 5 commits intoopenresty:masterfrom
bzp2010:bzp/feat-tcpsock-sslhandshake-alpn

Conversation

@bzp2010
Copy link
Copy Markdown

@bzp2010 bzp2010 commented Oct 23, 2025

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-resty-core project.

Comment on lines +248 to +255
local _bytes = {}
for _, proto_str in ipairs(alpn) do
_bytes[#_bytes + 1] = #proto_str
for _, proto_byte in ipairs(
{ str_byte(proto_str, 1, #proto_str) }) do
_bytes[#_bytes + 1] = proto_byte
end
end
Copy link
Copy Markdown
Contributor

@theweakgod theweakgod Mar 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The str_byte loop works, but we can simplify this quite a bit.

Since ALPN identifiers are a finite IANA-registered set, a pre-built wire-format lookup avoids runtime byte construction entirely for common protocols, with a fallback for anything else:

local ALPN_WIRE = {
    ["h2"]       = "\x02h2",
    ["http/1.1"] = "\x08http/1.1",
    ["http/1.0"] = "\x08http/1.0",
}

local bytes_array = {}
for _, p in ipairs(alpn) do
    bytes_array[#bytes_array+1] = ALPN_WIRE[p] or (string.char(#p) .. p)
end
local _bytes = table.concat(bytes_array)
alpn_str[0].data = ffi.new("unsigned char[?]", #_bytes, _bytes)
alpn_str[0].len  = #_bytes

table.concat produces the full wire string in one allocation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants