Skip to content

Commit 9962a09

Browse files
committed
Merge head table with headers table in http-codec.
1 parent ea7de89 commit 9962a09

4 files changed

Lines changed: 32 additions & 41 deletions

File tree

app/modules/http-codec.lua

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ local function decoder(read, write, isClient)
3232
local head = chunk
3333
local item
3434
local offset = 1
35-
local headers = {}
3635
local contentLength
3736
local chunkedEncoding
3837
while true do
@@ -45,7 +44,7 @@ local function decoder(read, write, isClient)
4544

4645
-- If this is the first line, parse it special
4746
elseif not item then
48-
item = { headers = headers }
47+
item = {}
4948
if isClient then
5049
item.version, item.code, item.reason = string.match(head, "^HTTP/(%d%.%d) (%d+) ([^\r]+)\r\n", offset)
5150
item.version = tonumber(item.version)
@@ -68,7 +67,7 @@ local function decoder(read, write, isClient)
6867
elseif lowerKey == "connection" then
6968
item.keepAlive = string.lower(value) == "keep-alive"
7069
end
71-
headers[#headers + 1] = {key, value}
70+
item[#item + 1] = {key, value}
7271
offset = s + 2
7372

7473
-- When a double "\r\n\r\n" if found, we're done with the head.
@@ -177,15 +176,13 @@ local function encoder(read, write, isClient)
177176
local reason = item.reason or STATUS_CODES[item.code]
178177
head = { 'HTTP/' .. version .. ' ' .. item.code .. ' ' .. reason .. '\r\n' }
179178
end
180-
if item.headers then
181-
for i = 1, #item.headers do
182-
local key, value = unpack(item.headers[i])
183-
local lowerKey = string.lower(key)
184-
if lowerKey == "transfer-encoding" then
185-
chunkedEncoding = string.lower(value) == "chunked"
186-
end
187-
head[#head + 1] = key .. ': ' .. tostring(value) .. '\r\n'
179+
for i = 1, #item do
180+
local key, value = unpack(item[i])
181+
local lowerKey = string.lower(key)
182+
if lowerKey == "transfer-encoding" then
183+
chunkedEncoding = string.lower(value) == "chunked"
188184
end
185+
head[#head + 1] = key .. ': ' .. tostring(value) .. '\r\n'
189186
end
190187
head[#head + 1] = '\r\n'
191188
write(table.concat(head))

bench/http-cluster/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,18 @@ local function app(read, write)
116116
print(req.method, req.url)
117117

118118
local body = req.path .. "\n"
119-
local headers = {
119+
local head = {
120+
code = 200,
120121
{ "Server", "Luvit" },
121122
{ "Content-Type", "text/plain" },
122123
{ "Content-Length", #body },
123124
}
124125
if req.keepAlive then
125-
headers[#headers + 1] = { "Connection", "Keep-Alive" }
126+
head[#head + 1] = { "Connection", "Keep-Alive" }
126127
end
127128

128129
-- Write the response headers and body
129-
write {
130-
code = 200,
131-
headers = headers
132-
}
130+
write(head)
133131
write(body)
134132

135133
-- If the request didn't support keepalive, we should break the loop

bench/http-cluster/app.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@ return function (read, write)
2323
for req in read do
2424
-- print("Writing response headers")
2525
local body = req.path .. "\n"
26-
local headers = {
26+
local res = {
27+
code = 200,
2728
{ "Server", "Luvit" },
2829
{ "Content-Type", "text/plain" },
2930
{ "Content-Length", #body },
3031
}
3132
if req.keepAlive then
32-
headers[#headers + 1] = { "Connection", "Keep-Alive" }
33+
res[#res + 1] = { "Connection", "Keep-Alive" }
3334
end
3435

35-
write {
36-
code = 200,
37-
headers = headers
38-
}
36+
write(res)
3937
-- print("Writing body")
4038
write(body)
4139

tests/test-http-codec.lua

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ require('tap')(function (test)
4444
assert(req.method == "GET")
4545
assert(req.path == "/path")
4646
assert(req.version == 1.1)
47-
local headers = req.headers
48-
assert(#headers == 1)
49-
assert(headers[1][1] == "User-Agent")
50-
assert(headers[1][2] == "Luvit-Test")
47+
assert(#req == 1)
48+
assert(req[1][1] == "User-Agent")
49+
assert(req[1][2] == "Luvit-Test")
5150
end)
5251

5352
test("http client parser", function ()
@@ -61,10 +60,9 @@ require('tap')(function (test)
6160
assert(res.code == 200)
6261
assert(res.reason == "OK")
6362
assert(res.version == 1.0)
64-
local headers = res.headers
65-
assert(#headers == 1)
66-
assert(headers[1][1] == "User-Agent")
67-
assert(headers[1][2] == "Luvit-Test")
63+
assert(#res == 1)
64+
assert(res[1][1] == "User-Agent")
65+
assert(res[1][2] == "Luvit-Test")
6866
end)
6967

7068
test("http 1.0 Keep-Alive", function ()
@@ -158,9 +156,9 @@ require('tap')(function (test)
158156

159157
test("server encoder - Keepalive", function ()
160158
local output = testCodec(codec.server.encoder, {
161-
{ code = 200, headers = {
159+
{ code = 200,
162160
{"Content-Length", 12}
163-
}},
161+
},
164162
"Hello World\n",
165163
{ code = 304 },
166164
})
@@ -173,9 +171,9 @@ require('tap')(function (test)
173171

174172
test("server encoder - Chunked Encoding", function ()
175173
local output = testCodec(codec.server.encoder, {
176-
{ code = 200, headers = {
174+
{ code = 200,
177175
{"Transfer-Encoding", "chunked"}
178-
}},
176+
},
179177
"Hello World\n",
180178
"Another Chunk",
181179
false,
@@ -192,18 +190,18 @@ require('tap')(function (test)
192190

193191
test("client encoder", function ()
194192
local output = testCodec(codec.client.encoder, {
195-
{ method = "GET", path = "/my-resource", headers = {
193+
{ method = "GET", path = "/my-resource",
196194
{"Accept", "*/*"}
197-
}},
198-
{ method = "GET", path = "/favicon.ico", headers = {
195+
},
196+
{ method = "GET", path = "/favicon.ico",
199197
{"Accept", "*/*"}
200-
}},
201-
{ method = "GET", path = "/orgs/luvit", headers = {
198+
},
199+
{ method = "GET", path = "/orgs/luvit",
202200
{"User-Agent", "Luvit Unit Tests"},
203201
{"Host", "api.github.com"},
204202
{"Accept", "*/*"},
205203
{"Authorization", "token 6d2fc6ae08215d69d693f5ca76ea87c7780a4275"},
206-
}}
204+
}
207205
})
208206
p(output)
209207
end)

0 commit comments

Comments
 (0)