diff --git a/lualib/http/sockethelper.lua b/lualib/http/sockethelper.lua index cac3130d..735da45d 100644 --- a/lualib/http/sockethelper.lua +++ b/lualib/http/sockethelper.lua @@ -8,7 +8,43 @@ local socket_error = setmetatable({} , { __tostring = function() return "[Socket sockethelper.socket_error = socket_error -function sockethelper.readfunc(fd) +local function preread(fd, str) + return function (sz) + if str then + if sz == #str or sz == nil then + local ret = str + str = nil + return ret + else + if sz < #str then + local ret = str:sub(1,sz) + str = str:sub(sz + 1) + return ret + else + sz = sz - #str + local ret = readbytes(fd, sz) + if ret then + return str .. ret + else + error(socket_error) + end + end + end + else + local ret = readbytes(fd, sz) + if ret then + return ret + else + error(socket_error) + end + end + end +end + +function sockethelper.readfunc(fd, pre) + if pre then + return preread(fd, pre) + end return function (sz) local ret = readbytes(fd, sz) if ret then diff --git a/service/debug_console.lua b/service/debug_console.lua index 9e121944..4d0edee2 100644 --- a/service/debug_console.lua +++ b/service/debug_console.lua @@ -4,6 +4,8 @@ local core = require "skynet.core" local socket = require "socket" local snax = require "snax" local memory = require "memory" +local httpd = require "http.httpd" +local sockethelper = require "http.sockethelper" local port = tonumber(...) local COMMAND = {} @@ -84,6 +86,14 @@ local function console_main_loop(stdin, print) if not cmdline then break end + if cmdline:sub(1,4) == "GET " then + -- http + local code, url = httpd.read_request(sockethelper.readfunc(stdin, cmdline.. "\n"), 8192) + local cmdline = url:sub(2):gsub("/"," ") + docmd(cmdline, print, stdin) + socket.close(stdin) + return + end if cmdline ~= "" then docmd(cmdline, print, stdin) end