diff --git a/extend/lcodec/src/http.h b/extend/lcodec/src/http.h index 8d4c79e8..2ccfc99d 100644 --- a/extend/lcodec/src/http.h +++ b/extend/lcodec/src/http.h @@ -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 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 diff --git a/script/network/http_server.lua b/script/network/http_server.lua index 916812c8..5d609211 100644 --- a/script/network/http_server.lua +++ b/script/network/http_server.lua @@ -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 @@ -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 回调 @@ -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 diff --git a/server/center/gm_mgr.lua b/server/center/gm_mgr.lua index 9e1a600d..b8c205b3 100644 --- a/server/center/gm_mgr.lua +++ b/server/center/gm_mgr.lua @@ -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 diff --git a/server/robot/accord_mgr.lua b/server/robot/accord_mgr.lua index 76f7786c..db7cae78 100644 --- a/server/robot/accord_mgr.lua +++ b/server/robot/accord_mgr.lua @@ -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 diff --git a/server/test/http_test.lua b/server/test/http_test.lua index 7eda90df..886c82e4 100644 --- a/server/test/http_test.lua +++ b/server/test/http_test.lua @@ -13,7 +13,7 @@ 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 @@ -21,7 +21,7 @@ if quanta.index == 1 then 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