Skip to content

Commit

Permalink
fix http2 window handle
Browse files Browse the repository at this point in the history
  • Loading branch information
findstr committed Sep 16, 2024
1 parent 06ed2b7 commit 134a285
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
9 changes: 5 additions & 4 deletions lualib/core/http/h2stream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ local function frame_winupdate(ch, id, flag, dat)
if n > 0 then
local dat = remove(ch, 1)
if dat then
n = n - #dat
local len = remove(ch, 1)
n = n - len
ch.socket:write(dat)
end
end
Expand Down Expand Up @@ -440,7 +441,6 @@ local function handshake_as_client(ch, socket)
end
frame_settings(ch, id, f, dat)
write(socket, build_setting(0x01))
write(socket, build_winupdate(0, 0, 1*1024*1024))
while true do
local t,f,dat,id = read_frame(socket)
if not t then
Expand Down Expand Up @@ -479,7 +479,6 @@ local function handshake_as_server(socket, ch)
end
frame_settings(ch, id, f, dat)
write(socket, build_setting(0x01))
write(socket, build_winupdate(0, 0, 1*1024*1024))
while true do
local t,f,dat,id = read_frame(socket)
if not t then
Expand Down Expand Up @@ -726,13 +725,15 @@ local function write_func(close)
end
end
dat = dat or ""
local body_len = #dat
dat = build_body(s.id, ch.frame_max_size, dat, close)
local win = ch.window_size
if win <= 0 then
ch[#ch + 1] = dat
ch[#ch + 1] = body_len
return true, "ok"
else
ch.window_size = win - #dat
ch.window_size = win - body_len
return ch.socket:write(dat)
end
end
Expand Down
50 changes: 41 additions & 9 deletions test/testhttp2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,38 @@ local testaux = require "test.testaux"

local f<const> = io.open("./a.txt", "w")

local data = crypto.randomkey(65*1024)

local alpn_protos = {"http/1.1", "h2"}
local function POST(url, header, body)
if body then
header = header or {}
header["content-length"] = #body * 2
end
local stream<close>, err = http.request("POST", url, header, false, alpn_protos)
if not stream then
return nil, err
end
local version = stream.version
if version == "HTTP/2" then
stream:write(body)
stream:close(body)
else
stream:write(body)
end
local status, header = stream:readheader()
if not status then
return nil, header
end
testaux.asserteq(stream.channel.window_size, 65535, "http2.client window_size")
local body = stream:readall()
return {
status = status,
header = header,
body = body,
}
end

if not crypto.digestsign then
print("not enable openssl")
return
Expand All @@ -29,7 +61,7 @@ http.listen {
testaux.asserteq(stream.path, "/test", "http2.server path")
testaux.asserteq(header['hello'], "world", "http2.server header")
local body = stream:readall()
testaux.asserteq(body, "http2", "http2 body")
testaux.asserteq(body, data .. data, "http2 body")
stream:respond(200, {["foo"] = header['foo']})
stream:close("http2")
end
Expand Down Expand Up @@ -62,14 +94,14 @@ print("test http2 server")
local wg = waitgroup:create()
for i = 1, 2000 do
wg:fork(function()
local key = crypto.randomkey(1028)
local ack, err = http.POST("https://localhost:8082/test", {
['hello'] = 'world',
['foo'] = key,
}, "http2")
testaux.asserteq(ack.status, 200, "http2.client status")
testaux.asserteq(ack.header['foo'], key, "http2.client header")
testaux.asserteq(ack.body, 'http2', "http2.client body")
local key = crypto.randomkey(1028)
local ack, err = POST("https://localhost:8082/test", {
['hello'] = 'world',
['foo'] = key,
}, data)
testaux.asserteq(ack.status, 200, "http2.client status")
testaux.asserteq(ack.header['foo'], key, "http2.client header")
testaux.asserteq(ack.body, 'http2', "http2.client body")
end)
end
wg:wait()
Expand Down

0 comments on commit 134a285

Please sign in to comment.