mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
httpd.write_response capture socket error
This commit is contained in:
@@ -8,6 +8,14 @@ local mode = ...
|
|||||||
|
|
||||||
if mode == "agent" then
|
if mode == "agent" then
|
||||||
|
|
||||||
|
local function response(id, ...)
|
||||||
|
local ok, err = httpd.write_response(sockethelper.writefunc(id), ...)
|
||||||
|
if not ok then
|
||||||
|
-- if err == sockethelper.socket_error , that means socket closed.
|
||||||
|
skynet.error(string.format("fd = %d, %s", id, err))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
skynet.dispatch("lua", function (_,_,id)
|
skynet.dispatch("lua", function (_,_,id)
|
||||||
socket.start(id)
|
socket.start(id)
|
||||||
@@ -15,7 +23,7 @@ skynet.start(function()
|
|||||||
local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192)
|
local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192)
|
||||||
if code then
|
if code then
|
||||||
if code ~= 200 then
|
if code ~= 200 then
|
||||||
httpd.write_response(sockethelper.writefunc(id), code)
|
response(id, code)
|
||||||
else
|
else
|
||||||
local tmp = {}
|
local tmp = {}
|
||||||
if header.host then
|
if header.host then
|
||||||
@@ -29,8 +37,7 @@ skynet.start(function()
|
|||||||
table.insert(tmp, string.format("query: %s= %s", k,v))
|
table.insert(tmp, string.format("query: %s= %s", k,v))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
response(id, code, table.concat(tmp,"\n"))
|
||||||
httpd.write_response(sockethelper.writefunc(id), code , table.concat(tmp,"\n"))
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if url == sockethelper.socket_error then
|
if url == sockethelper.socket_error then
|
||||||
|
|||||||
@@ -227,8 +227,8 @@ local function readall(readbytes, bodylimit)
|
|||||||
return 200, url, method, header, body
|
return 200, url, method, header, body
|
||||||
end
|
end
|
||||||
|
|
||||||
function httpd.read_request(readbytes, bodylimit)
|
function httpd.read_request(...)
|
||||||
local ok, code, url, method, header, body = pcall(readall, readbytes, bodylimit)
|
local ok, code, url, method, header, body = pcall(readall, ...)
|
||||||
if ok then
|
if ok then
|
||||||
return code, url, method, header, body
|
return code, url, method, header, body
|
||||||
else
|
else
|
||||||
@@ -236,7 +236,7 @@ function httpd.read_request(readbytes, bodylimit)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function httpd.write_response(writefunc, statuscode, bodyfunc, header)
|
local function writeall(writefunc, statuscode, bodyfunc, header)
|
||||||
local statusline = string.format("HTTP/1.1 %03d %s\r\n", statuscode, http_status_msg[statuscode] or "")
|
local statusline = string.format("HTTP/1.1 %03d %s\r\n", statuscode, http_status_msg[statuscode] or "")
|
||||||
writefunc(statusline)
|
writefunc(statusline)
|
||||||
if header then
|
if header then
|
||||||
@@ -267,4 +267,8 @@ function httpd.write_response(writefunc, statuscode, bodyfunc, header)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function httpd.write_response(...)
|
||||||
|
return pcall(writeall, ...)
|
||||||
|
end
|
||||||
|
|
||||||
return httpd
|
return httpd
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ local readbytes = socket.read
|
|||||||
local writebytes = socket.write
|
local writebytes = socket.write
|
||||||
|
|
||||||
local sockethelper = {}
|
local sockethelper = {}
|
||||||
local socket_error = {}
|
local socket_error = setmetatable({} , { __tostring = function() return "[Socket Error]" end })
|
||||||
|
|
||||||
sockethelper.socket_error = socket_error
|
sockethelper.socket_error = socket_error
|
||||||
|
|
||||||
@@ -28,4 +28,4 @@ function sockethelper.writefunc(fd)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return sockethelper
|
return sockethelper
|
||||||
|
|||||||
Reference in New Issue
Block a user