debug console support simple http

This commit is contained in:
Cloud Wu
2016-06-12 14:51:18 +08:00
parent f84fe84b96
commit 9546537256
2 changed files with 47 additions and 1 deletions

View File

@@ -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

View File

@@ -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