Http2 library implemented in lua language.
High performance http2 protocol server and client library based on cfadmin.
using local httpc = require "lua-http2.httpc".
Use the new method creates an httpc request object.
Use the connect method of the httpc object to connect to the server; return true for success, return false and string for failure.
Use the request method of the httpc object to request Server.
-
url- http2path. (e.g/api); -
method- http2method. (e.gGET、POST); -
headers- http2headers. (e.g{["content-type"] = "application/json"}) -
body|args- http2argsorbody. (e.gtable = {{"a",1}, {"b",2}}、"a=1&b=2"、"{}") -
timeout- request timeout. (timeoutMUST> 0)
using local httpd = require "lua-http2.httpd".
Use the new method creates an httpd server object.
Use the route method to register an http2 route callback.
req - is the request context of the client.
resp - can include 'response content' and 'response header'`.
Specify the folder parameter as the static file lookup directory.
Start listening to the specified IP and Port.
Write the request log to the file specified in filepath.
Turn off print and write any request records.
All code after this method is not executed.
Next, we will introduce the examples and use methods of 'client' and 'server' respectively.
Server code example
require "utils"
local httpd = require "lua-http2.httpd"
local h2 = httpd:new()
-- 注册路由
h2:route("/", function (req, resp)
var_dump(req)
resp['body'] = "Loging."
end)
-- 静态文件路由
h2:static("static")
-- -- 关闭请求日志
-- h2:nolog()
h2:listen("localhost", 80)
h2:run()[candy@MacBookPro:~/Documents/cfadmin] $ ./cfadmin
[2021/04/19 20:24:19] [INFO] h2 listen: 0.0.0.0:80
[2021/04/19 20:24:19] [INFO] h2 Web Server Running...
{
["headers"] = {
["host"] = "127.0.0.1",
[":scheme"] = "http",
["origin"] = "127.0.0.1",
["accept"] = "*/*",
["user-agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
[":path"] = "/",
[":authority"] = "127.0.0.1",
[":method"] = "GET",
["accept-encoding"] = "gzip, deflate, identity",
},
}
[2021/04/19 20:24:28] - 127.0.0.1 - 127.0.0.1 - / - GET - 200 - req_time: 0.000029/SecClient code example
require "utils"
local httpc = require "lua-http2.httpc"
-- 创建对象
local hc = httpc:new { domain = "http://127.0.0.1/" }
-- 连接到服务器
if not hc:connect() then
return print("连接失败")
end
-- 发送请求
local opt, errinfo = hc:request("/", "GET")
if not opt then
return print(false, errinfo)
end
var_dump(opt)[candy@MacBookPro:~/Documents/cfadmin] $ ./cfadmin
{
["headers"] = {
["content-type"] = "text/html; charset=utf-8",
["server"] = "cfadmin/0.1",
["date"] = "Mon, 19 Apr 2021 20:24:28 GMT",
[":status"] = "200",
},
["body"] = "Loging.",
}