mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
add ws/wss server and client support
This commit is contained in:
@@ -8,81 +8,6 @@ local table = table
|
||||
|
||||
local httpc = {}
|
||||
|
||||
local function request(interface, method, host, url, recvheader, header, content)
|
||||
local read = interface.read
|
||||
local write = interface.write
|
||||
local header_content = ""
|
||||
if header then
|
||||
if not header.host then
|
||||
header.host = host
|
||||
end
|
||||
for k,v in pairs(header) do
|
||||
header_content = string.format("%s%s:%s\r\n", header_content, k, v)
|
||||
end
|
||||
else
|
||||
header_content = string.format("host:%s\r\n",host)
|
||||
end
|
||||
|
||||
if content then
|
||||
local data = string.format("%s %s HTTP/1.1\r\n%scontent-length:%d\r\n\r\n", method, url, header_content, #content)
|
||||
write(data)
|
||||
write(content)
|
||||
else
|
||||
local request_header = string.format("%s %s HTTP/1.1\r\n%scontent-length:0\r\n\r\n", method, url, header_content)
|
||||
write(request_header)
|
||||
end
|
||||
|
||||
local tmpline = {}
|
||||
local body = internal.recvheader(read, tmpline, "")
|
||||
if not body then
|
||||
error(socket.socket_error)
|
||||
end
|
||||
|
||||
local statusline = tmpline[1]
|
||||
local code, info = statusline:match "HTTP/[%d%.]+%s+([%d]+)%s+(.*)$"
|
||||
code = assert(tonumber(code))
|
||||
|
||||
local header = internal.parseheader(tmpline,2,recvheader or {})
|
||||
if not header then
|
||||
error("Invalid HTTP response header")
|
||||
end
|
||||
|
||||
local length = header["content-length"]
|
||||
if length then
|
||||
length = tonumber(length)
|
||||
end
|
||||
local mode = header["transfer-encoding"]
|
||||
if mode then
|
||||
if mode ~= "identity" and mode ~= "chunked" then
|
||||
error ("Unsupport transfer-encoding")
|
||||
end
|
||||
end
|
||||
|
||||
if mode == "chunked" then
|
||||
body, header = internal.recvchunkedbody(read, nil, header, body)
|
||||
if not body then
|
||||
error("Invalid response body")
|
||||
end
|
||||
else
|
||||
-- identity mode
|
||||
if length then
|
||||
if #body >= length then
|
||||
body = body:sub(1,length)
|
||||
else
|
||||
local padding = read(length - #body)
|
||||
body = body .. padding
|
||||
end
|
||||
elseif code == 204 or code == 304 or code < 200 then
|
||||
body = ""
|
||||
-- See https://stackoverflow.com/questions/15991173/is-the-content-length-header-required-for-a-http-1-0-response
|
||||
else
|
||||
-- no content-length, read all
|
||||
body = body .. interface.readall()
|
||||
end
|
||||
end
|
||||
|
||||
return code, body
|
||||
end
|
||||
|
||||
local async_dns
|
||||
|
||||
@@ -172,7 +97,7 @@ function httpc.request(method, host, url, recvheader, header, content)
|
||||
if interface.init then
|
||||
interface.init()
|
||||
end
|
||||
local ok , statuscode, body = pcall(request, interface, method, host, url, recvheader, header, content)
|
||||
local ok , statuscode, body = pcall(internal.request, interface, method, host, url, recvheader, header, content)
|
||||
finish = true
|
||||
socket.close(fd)
|
||||
if interface.close then
|
||||
|
||||
Reference in New Issue
Block a user