Skip to content

Commit

Permalink
优化HTTP代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Oct 11, 2023
1 parent b3dc798 commit 6d98de2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
7 changes: 6 additions & 1 deletion extend/lcodec/src/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,17 @@ namespace lcodec {

void parse_http_packet(lua_State* L, string_view& buf) {
size_t pos = buf.find(CRLF2);
if (pos == string_view::npos) throw length_error("http text not full");
if (pos == string_view::npos) {
throw length_error("http text not full");
}
string_view header = buf.substr(0, pos);
buf.remove_prefix(pos + LCRLF2);
auto begining = read_line(header);
vector<string_view> parts;
split(begining, " ", parts);
if (parts.size() < 2) {
throw invalid_argument("invalid http healder");
}
//method
lua_pushlstring(L, parts[0].data(), parts[0].size());
//url + params
Expand Down
38 changes: 15 additions & 23 deletions script/network/http_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ prop:reader("hcodec", nil) --codec
prop:reader("jcodec", nil) --codec
prop:reader("listener", nil) --网络连接对象
prop:reader("clients", {}) --clients
prop:reader("get_handlers", {}) --get_handlers
prop:reader("put_handlers", {}) --put_handlers
prop:reader("del_handlers", {}) --del_handlers
prop:reader("post_handlers", {}) --post_handlers
prop:reader("handlers", {}) --handlers

function HttpServer:__init(http_addr)
self.jcodec = jsoncodec()
self.hcodec = httpcodec(self.jcodec)
self.handlers = { GET = {}, POST = {}, PUT = {}, DELETE = {} }
self:setup(http_addr)
end

Expand Down Expand Up @@ -67,42 +65,36 @@ end

function HttpServer:on_socket_recv(socket, method, url, params, headers, body)
log_debug("[HttpServer][on_socket_recv] recv: {}, {}, {}, {}, {}!", method, url, params, headers, body)
if method == "GET" then
return self:on_http_request(self.get_handlers, socket, url, params, headers)
end
if method == "POST" then
return self:on_http_request(self.post_handlers, socket, url, body, params, headers)
end
if method == "PUT" then
return self:on_http_request(self.put_handlers, socket, url, body, params, headers)
end
if method == "DELETE" then
return self:on_http_request(self.del_handlers, socket, url, params, headers)
local handlers = self.handlers[method]
if not handlers then
self:response(socket, 404, "this http method hasn't suppert!")
return
end
self:on_http_request(handlers, socket, url, body, params, headers)
end

--注册get回调
function HttpServer:register_get(url, handler, target)
log_debug("[HttpServer][register_get] url: {}", url)
self.get_handlers[url] = { handler, target }
self.handlers.GET[url] = { handler, target }
end

--注册post回调
function HttpServer:register_post(url, handler, target)
log_debug("[HttpServer][register_post] url: {}", url)
self.post_handlers[url] = { handler, target }
self.handlers.POST[url] = { handler, target }
end

--注册put回调
function HttpServer:register_put(url, handler, target)
log_debug("[HttpServer][register_put] url: {}", url)
self.put_handlers[url] = { handler, target }
self.handlers.PUT[url] = { handler, target }
end

--注册del回调
function HttpServer:register_del(url, handler, target)
log_debug("[HttpServer][register_del] url: {}", url)
self.del_handlers[url] = { handler, target }
self.handlers.DELETE[url] = { handler, target }
end

--http post 回调
Expand Down Expand Up @@ -156,10 +148,10 @@ end

--取消url
function HttpServer:unregister(url)
self.get_handlers[url] = nil
self.put_handlers[url] = nil
self.del_handlers[url] = nil
self.post_handlers[url] = nil
self.handlers.GET[url] = nil
self.handlers.PUT[url] = nil
self.handlers.POST[url] = nil
self.handlers.DELETE[url] = nil
end

return HttpServer
6 changes: 3 additions & 3 deletions server/center/gm_mgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ end
--http 回调
----------------------------------------------------------------------
--gm_page
function GM_Mgr:on_gm_page(url, params)
function GM_Mgr:on_gm_page(url, body, params)
return self.gm_page, {["Access-Control-Allow-Origin"] = "*", ["X-Frame-Options"]= "ALLOW_FROM"}
end

--gm列表
function GM_Mgr:on_gmlist(url, params)
function GM_Mgr:on_gmlist(url, body, params)
return { text = "GM指令", nodes = cmdline:get_displays() }
end

--monitor拉取
function GM_Mgr:on_monitors(url, params)
function GM_Mgr:on_monitors(url, body, params)
log_debug("[GM_Mgr][on_monitors] body: {}", params)
local nodes = {}
for _, addr in pairs(self.monitors) do
Expand Down
2 changes: 1 addition & 1 deletion server/robot/accord_mgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function AccordMgr:get_src_name(src_path)
end

-- 拉取日志
function AccordMgr:on_message(url, params)
function AccordMgr:on_message(url, body, params)
-- log_debug("[AccordMgr][on_message] open_id: {}", params.open_id)
return robot_mgr:get_accord_message(params.open_id)
end
Expand Down
4 changes: 2 additions & 2 deletions server/test/http_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ if quanta.index == 1 then
log_debug("on_post: {}, {}, {}", path, body, params)
return data
end
local on_get = function(path, params)
local on_get = function(path, body, params)
log_debug("on_get: {}, {}", path, params)
return data
end
local on_put = function(path, body, params)
log_debug("on_put: {}, {}, {}", path, body, params)
return data
end
local on_del = function(path, params)
local on_del = function(path, body, params)
log_debug("on_del: {}, {}", path, params)
return data
end
Expand Down

0 comments on commit 6d98de2

Please sign in to comment.