# lhttp
一个提供给lua使用的http协议解析库。
# 依赖
- [lua](https://github.com/xiyoo0812/lua.git)5.3以上
- [luakit](https://github.com/xiyoo0812/luakit.git)一个luabind库
- 项目路径如下<br>
|--proj <br>
 |--lua <br>
 |--lhttp <br>
 |--luakit
# 注意事项
- 本库只做http协议解析
- 数据收发由调用方实现
- mimalloc: 参考[quanta](https://github.com/xiyoo0812/quanta.git)使用,不用则在工程文件中注释
# 用法
```lua
--本示例使用了quanta引擎
--https://github.com/xiyoo0812/quanta.git
--http_server.lua
local lhttp = require("lhttp")
local ljson = require("lcjson")
local Socket = import("driver/socket.lua")
local type = type
local tostring = tostring
local getmetatable = getmetatable
local log_err = logger.err
local log_info = logger.info
local log_debug = logger.debug
local json_encode = ljson.encode
local tunpack = table.unpack
local signalquit = signal.quit
local saddr = qstring.addr
local thread_mgr = quanta.get("thread_mgr")
local HttpServer = class()
local prop = property(HttpServer)
prop:reader("listener", nil) --网络连接对象
prop:reader("ip", nil) --http server地址
prop:reader("port", 8080) --http server端口
prop:reader("clients", {}) --clients
prop:reader("requests", {}) --requests
prop:reader("get_handlers", {}) --get_handlers
prop:reader("put_handlers", {}) --put_handlers
prop:reader("del_handlers", {}) --del_handlers
prop:reader("post_handlers", {}) --post_handlers
function HttpServer:__init(http_addr)
self:setup(http_addr)
end
function HttpServer:setup(http_addr)
self.ip, self.port = saddr(http_addr)
local socket = Socket(self)
if not socket:listen(self.ip, self.port) then
log_info("[HttpServer][setup] now listen %s failed", http_addr)
signalquit(1)
return
end
log_info("[HttpServer][setup] listen(%s:%s) success!", self.ip, self.port)
self.listener = socket
end
function HttpServer:on_socket_error(socket, token, err)
if socket == self.listener then
log_info("[HttpServer][on_socket_error] listener(%s:%s) close!", self.ip, self.port)
self.listener = nil
return
end
log_debug("[HttpServer][on_socket_error] client(token:%s) close!", token)
self.clients[token] = nil
self.requests[token] = nil
end
function HttpServer:on_socket_accept(socket, token)
log_debug("[HttpServer][on_socket_accept] client(token:%s) connected!", token)
self.clients[token] = socket
end
function HttpServer:on_socket_recv(socket, token)
local request = self.requests[token]
if not request then
request = lhttp.create_request()
log_debug("[HttpServer][on_socket_accept] create_request(token:%s)!", token)
self.requests[token] = request
end
local buf = socket:get_recvbuf()
if #buf == 0 or not request.parse(buf) then
log_err("[HttpServer][on_socket_recv] http request append failed, close client(token:%s)!", token)
self:response(socket, 400, "this http request parse error!")
return
end
socket:pop(#buf)
thread_mgr:fork(function()
local method = request.method
local headers = request.get_headers()
if method == "GET" then
local querys = request.get_params()
return self:on_http_request(self.get_handlers, socket, request.url, querys, headers)
end
if method == "POST" then
return self:on_http_request(self.post_handlers, socket, request.url, request.body, headers)
end
if method == "PUT" then
return self:on_http_request(self.put_handlers, socket, request.url, request.body, headers)
end
if method == "DELETE" then
local querys = request.get_params()
return self:on_http_request(self.del_handlers, socket, request.url, querys, headers)
end
end)
end
--注册get回调
function HttpServer:register_get(url, handler, target)
log_debug("[HttpServer][register_get] url: %s", url)
self.get_handlers[url] = { handler, target }
end
--注册post回调
function HttpServer:register_post(url, handler, target)
log_debug("[HttpServer][register_post] url: %s", url)
self.post_handlers[url] = { handler, target }
end
--注册put回调
function HttpServer:register_put(url, handler, target)
log_debug("[HttpServer][register_put] url: %s", url)
self.put_handlers[url] = { handler, target }
end
--注册del回调
function HttpServer:register_del(url, handler, target)
log_debug("[HttpServer][register_del] url: %s", url)
self.del_handlers[url] = { handler, target }
end
--生成response
function HttpServer:build_response(status, content, headers)
local response = lhttp.create_response()
response.status = status
response.content = content
for name, value in pairs(headers or {}) do
response.set_header(name, value)
end
return response
end
--http post 回调
function HttpServer:on_http_request(handlers, socket, url, ...)
log_info("[HttpServer][on_http_request] request %s process!", url)
local handler_info = handlers[url] or handlers["*"]
if handler_info then
local handler, target = tunpack(handler_info)
if not target then
if type(handler) == "function" then
local ok, response = pcall(handler, url, ...)
if not ok then
response = { code = 1, msg = response }
end
self:response(socket, 200, response)
return
end
else
if type(handler) == "string" then
handler = target[handler]
end
if type(handler) == "function" then
local ok, response = pcall(handler, target, url, ...)
if not ok then
response = { code = 1, msg = response }
end
self:response(socket, 200, response)
return
end
end
end
log_err("[HttpServer][on_http_request] request %s hasn't process!", url)
self:response(socket, 404, "this http request hasn't process!")
end
function HttpServer:response(socket, status, response)
local token = socket:get_token()
if not token then
return
end
self.requests[token] = nil
if getmetatable(response) then
socket:send(response.serialize())
socket:close()
return
end
local new_resp = lhttp.create_response()
if type(response) == "table" then
new_resp.set_header("Content-Type", "application/json")
new_resp.content = json_encode(response)
else
new_resp.set_header("Content-Type", "text/plain")
new_resp.content = (type(response) == "string") and response or tostring(response)
end
new_resp.status = status
socket:send(new_resp.serialize())
socket:close()
end
return HttpServer
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
概述 一个基于lua的分布式游戏服务器引擎框架! 优势 轻量级 简单、易上手 稳定性强 扩展性强 热更新 编译 msvc : 打开项目目录下的sln工程文件,编译即可。 linux:在项目根目录,执行make all。 编译lua需要readline,请提前安装。 http模块依赖curl,请提前安装。 工程 本项目使用lmake管理 根目录配置lmake --lmake --工程名 SOLUTION = "quanta" --lmake目录 LMAKE_DIR = "extend/lmake" --mimalloc MIMALLOC = false 子项目配置*.lmake 执行以下指令自动生成项目文件(makefile/vcxproj) # lmake_dir: lmake项目路径 # solution_dir: 工程根目录 ./lua lmake_dir/lmake.lua solution_dir 执行测试代码 测试代码位于server/test,入口文件为server/test.lua cd bin ./quanta ./test.conf 基础服务 router: qu
资源推荐
资源详情
资源评论
收起资源包目录
基于lua的分布式游戏服务器引擎框架 (602个子文件)
libcurl.a 53KB
start.bat 383B
build.bat 280B
accord.bat 222B
all_export.bat 192B
excellua.bat 191B
encrypt.bat 190B
test.bat 75B
stop.bat 27B
miniz.c 312KB
segment.c 68KB
pb.c 66KB
lvm.c 57KB
os.c 56KB
lgc.c 55KB
lstrlib.c 55KB
lparser.c 55KB
lz4.c 54KB
ltests.c 51KB
lcode.c 51KB
lua_cjson.c 42KB
alloc.c 37KB
lapi.c 35KB
page.c 35KB
ldo.c 34KB
lauxlib.c 32KB
ltable.c 31KB
ldebug.c 27KB
init.c 26KB
arena.c 24KB
options.c 23KB
loadlib.c 23KB
stats.c 22KB
sha2.c 22KB
liolib.c 22KB
region.c 21KB
heap.c 20KB
lua.c 20KB
lobject.c 19KB
lmathlib.c 18KB
bitmap.c 17KB
llex.c 17KB
segment-cache.c 16KB
alloc-override.c 15KB
lbaselib.c 15KB
des56.c 15KB
luac.c 15KB
alloc-aligned.c 14KB
random.c 13KB
alloc-override-osx.c 13KB
ldblib.c 13KB
ltablib.c 13KB
lcrypt.c 13KB
loslib.c 12KB
page-queue.c 11KB
lstate.c 11KB
sha1.c 10KB
lfunc.c 8KB
ltm.c 8KB
lutf8lib.c 8KB
md5.c 8KB
lundump.c 8KB
lstring.c 7KB
xxtea.c 7KB
alloc-posix.c 6KB
lmem.c 6KB
strbuf.c 6KB
fpconv.c 6KB
lcorolib.c 5KB
ldump.c 5KB
lopcodes.c 4KB
base64.c 4KB
lctype.c 2KB
onelua.c 2KB
linit.c 2KB
lzio.c 1KB
static.c 1KB
share.conf 4KB
test.conf 2KB
monitor.conf 1KB
cache.conf 1KB
mongo.conf 1KB
mysql.conf 945B
redis.conf 938B
gateway.conf 902B
luapath.conf 855B
login.conf 813B
cachesvr.conf 759B
router.conf 752B
robot.conf 721B
accord.conf 713B
admin.conf 710B
online.conf 573B
DetourNavMeshQuery.cpp 102KB
tinyxml2.cpp 61KB
DetourNavMesh.cpp 44KB
DetourNavMeshBuilder.cpp 24KB
socket_stream.cpp 16KB
logger.cpp 14KB
socket_mgr.cpp 11KB
共 602 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
你的月亮和太阳
- 粉丝: 211
- 资源: 91
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功