442 lines
8.4 KiB
Plaintext
442 lines
8.4 KiB
Plaintext
--:Minify:--
|
|
local kernel = ...
|
|
|
|
local component = kernel.oc and kernel.oc.component
|
|
local internetAddress
|
|
|
|
for address in component.list("internet") do
|
|
internetAddress = address
|
|
break
|
|
end
|
|
|
|
if not internetAddress then
|
|
if kernel.log then
|
|
kernel.log("NET: No Internet component available")
|
|
end
|
|
return
|
|
end
|
|
|
|
local internet = component.proxy(internetAddress)
|
|
local tcpEnabled = false
|
|
local httpEnabled = false
|
|
|
|
if internet.isTcpEnabled then
|
|
local ok, result = pcall(internet.isTcpEnabled)
|
|
tcpEnabled = ok and result == true
|
|
end
|
|
|
|
if internet.isHttpEnabled then
|
|
local ok, result = pcall(internet.isHttpEnabled)
|
|
httpEnabled = ok and result == true
|
|
end
|
|
|
|
local function closeHandle(handle)
|
|
if handle and handle.close then
|
|
pcall(handle.close)
|
|
end
|
|
end
|
|
|
|
local function parseRawRequest(raw, address)
|
|
local headerEnd = raw:find("\r\n\r\n", 1, true)
|
|
|
|
local headerPart
|
|
local body = ""
|
|
|
|
if headerEnd then
|
|
headerPart = raw:sub(1, headerEnd - 1)
|
|
body = raw:sub(headerEnd + 4)
|
|
else
|
|
headerPart = raw
|
|
end
|
|
|
|
local lines = {}
|
|
|
|
for line in headerPart:gmatch("[^\r\n]+") do
|
|
lines[#lines + 1] = line
|
|
end
|
|
|
|
if #lines == 0 then
|
|
return nil, "EINVAL"
|
|
end
|
|
|
|
local method, path, ver =
|
|
lines[1]:match("^(%S+)%s+(%S+)%s+(%S+)$")
|
|
|
|
if not method then
|
|
return nil, "EINVAL"
|
|
end
|
|
|
|
if path:sub(1, 1) ~= "/" then
|
|
return nil, "EINVAL"
|
|
end
|
|
|
|
local headers = {}
|
|
|
|
for i = 2, #lines do
|
|
local k, v = lines[i]:match("^([^:]+):%s*(.*)$")
|
|
|
|
if k then
|
|
headers[k] = v
|
|
end
|
|
end
|
|
|
|
local url = address .. path
|
|
|
|
return {
|
|
url = url,
|
|
method = method,
|
|
headers = headers,
|
|
body = body
|
|
}, url
|
|
end
|
|
|
|
local function buildHTTPResponse(req)
|
|
if req.method ~= "GET" and req.method ~= "POST" then
|
|
return nil, "EOPNOTSUPP"
|
|
end
|
|
|
|
local postData
|
|
|
|
if req.method == "POST" then
|
|
postData = req.body or ""
|
|
end
|
|
|
|
local ok, response = pcall(
|
|
internet.request,
|
|
req.url,
|
|
postData,
|
|
req.headers
|
|
)
|
|
|
|
if not ok or not response then
|
|
return nil, tostring(response or "ECONN")
|
|
end
|
|
|
|
local connected, connectResult = pcall(
|
|
response.finishConnect
|
|
)
|
|
|
|
if not connected or connectResult == false then
|
|
closeHandle(response)
|
|
return nil, tostring(connectResult or "ECONN")
|
|
end
|
|
|
|
local code = 200
|
|
local msg = "OK"
|
|
local headers = {}
|
|
|
|
if response.response then
|
|
local responseOK, c, m, h = pcall(response.response)
|
|
|
|
if responseOK then
|
|
code = c or code
|
|
msg = m or msg
|
|
headers = h or headers
|
|
end
|
|
end
|
|
|
|
local chunks = {}
|
|
|
|
while true do
|
|
local readOK, chunk = pcall(response.read, 8192)
|
|
|
|
if not readOK then
|
|
closeHandle(response)
|
|
return nil, tostring(chunk)
|
|
end
|
|
|
|
if not chunk or #chunk == 0 then
|
|
break
|
|
end
|
|
|
|
chunks[#chunks + 1] = chunk
|
|
end
|
|
|
|
closeHandle(response)
|
|
|
|
local out = {
|
|
"HTTP/1.1 " .. tostring(code) .. " " .. tostring(msg)
|
|
}
|
|
|
|
for k, v in pairs(headers) do
|
|
if type(v) == "table" then
|
|
for i = 1, #v do
|
|
out[#out + 1] = k .. ": " .. tostring(v[i])
|
|
end
|
|
else
|
|
out[#out + 1] = k .. ": " .. tostring(v)
|
|
end
|
|
end
|
|
|
|
out[#out + 1] = ""
|
|
out[#out + 1] = table.concat(chunks)
|
|
|
|
return table.concat(out, "\r\n")
|
|
end
|
|
|
|
local function closeFile(socket)
|
|
if socket.file then
|
|
pcall(socket.file.close)
|
|
socket.file = nil
|
|
end
|
|
end
|
|
|
|
local httpHandler = {}
|
|
|
|
function httpHandler.connect(fd, address)
|
|
local fdo = kernel.currentTask.fd[fd]
|
|
|
|
if not fdo then
|
|
return nil, "EBADF"
|
|
end
|
|
|
|
fdo.socket = fdo.socket or {}
|
|
|
|
local socket = fdo.socket
|
|
|
|
socket.active = false
|
|
socket.loaded = false
|
|
socket.file = nil
|
|
|
|
fdo.handle.write = function(raw)
|
|
local req, url = parseRawRequest(raw, address)
|
|
|
|
if not req then
|
|
return nil, url
|
|
end
|
|
|
|
closeFile(socket)
|
|
|
|
socket.active = true
|
|
socket.loaded = false
|
|
socket.url = url
|
|
|
|
local response, err = buildHTTPResponse(req)
|
|
|
|
if not response then
|
|
socket.active = false
|
|
socket.error = err
|
|
return nil, err
|
|
end
|
|
|
|
socket.content = response
|
|
socket.file = kernel.sfile(response)
|
|
socket.loaded = true
|
|
socket.error = nil
|
|
|
|
return true
|
|
end
|
|
|
|
fdo.handle.read = function(count)
|
|
if not socket.active then
|
|
return
|
|
end
|
|
|
|
if socket.error then
|
|
return nil, socket.error
|
|
end
|
|
|
|
if not socket.file then
|
|
return
|
|
end
|
|
|
|
return socket.file.read(count)
|
|
end
|
|
|
|
fdo.handle.seek = function(whence, offset)
|
|
if not socket.active or not socket.file then
|
|
return
|
|
end
|
|
|
|
return socket.file.seek(whence, offset)
|
|
end
|
|
|
|
fdo.handle.close = function()
|
|
closeFile(socket)
|
|
|
|
socket.active = false
|
|
socket.loaded = false
|
|
socket.content = nil
|
|
socket.url = nil
|
|
socket.error = nil
|
|
|
|
return true
|
|
end
|
|
|
|
return true
|
|
end
|
|
|
|
local tcpHandler = {}
|
|
|
|
local function parseTCPAddress(address)
|
|
local host, port
|
|
|
|
if address:sub(1, 1) == "[" then
|
|
host, port = address:match("^%[([^%]]+)%]:(%d+)$")
|
|
|
|
if not host then
|
|
host = address:match("^%[([^%]]+)%]$")
|
|
end
|
|
else
|
|
host, port = address:match("^([^:]+):(%d+)$")
|
|
|
|
if not host then
|
|
host = address
|
|
end
|
|
end
|
|
|
|
if not host or host == "" then
|
|
return nil, nil, "EINVAL"
|
|
end
|
|
|
|
if port then
|
|
port = tonumber(port)
|
|
|
|
if not port or port < 1 or port > 65535 then
|
|
return nil, nil, "EINVAL"
|
|
end
|
|
end
|
|
|
|
return host, port
|
|
end
|
|
|
|
function tcpHandler.connect(fd, address)
|
|
if not tcpEnabled then
|
|
return nil, "EOPNOTSUPP"
|
|
end
|
|
|
|
local fdo = kernel.currentTask.fd[fd]
|
|
|
|
if not fdo then
|
|
return nil, "EBADF"
|
|
end
|
|
|
|
local host, port, err = parseTCPAddress(address)
|
|
|
|
if not host then
|
|
return nil, err
|
|
end
|
|
|
|
fdo.socket = fdo.socket or {}
|
|
|
|
local socket = fdo.socket
|
|
|
|
if socket.tcp then
|
|
closeHandle(socket.tcp)
|
|
end
|
|
|
|
socket.tcp = nil
|
|
socket.active = false
|
|
socket.error = nil
|
|
socket.host = host
|
|
socket.port = port
|
|
|
|
local ok, tcp = pcall(
|
|
internet.connect,
|
|
host,
|
|
port
|
|
)
|
|
|
|
if not ok or not tcp then
|
|
return nil, tostring(tcp or "ECONN")
|
|
end
|
|
|
|
socket.tcp = tcp
|
|
socket.active = true
|
|
|
|
local connected, result = pcall(
|
|
tcp.finishConnect
|
|
)
|
|
|
|
if not connected or result == false then
|
|
closeHandle(tcp)
|
|
|
|
socket.tcp = nil
|
|
socket.active = false
|
|
|
|
return nil, tostring(result or "ECONN")
|
|
end
|
|
|
|
fdo.handle.write = function(data)
|
|
if not socket.active or not socket.tcp then
|
|
return nil, "ENOTCONN"
|
|
end
|
|
|
|
if type(data) ~= "string" then
|
|
return nil, "EINVAL"
|
|
end
|
|
|
|
local writeOK, written = pcall(
|
|
socket.tcp.write,
|
|
data
|
|
)
|
|
|
|
if not writeOK then
|
|
return nil, tostring(written)
|
|
end
|
|
|
|
return written
|
|
end
|
|
|
|
fdo.handle.read = function(count)
|
|
if not socket.active or not socket.tcp then
|
|
return nil, "ENOTCONN"
|
|
end
|
|
|
|
local readOK, data = pcall(
|
|
socket.tcp.read,
|
|
count
|
|
)
|
|
|
|
if not readOK then
|
|
return nil, tostring(data)
|
|
end
|
|
|
|
return data
|
|
end
|
|
|
|
fdo.handle.seek = function()
|
|
return nil, "ESPIPE"
|
|
end
|
|
|
|
fdo.handle.close = function()
|
|
closeHandle(socket.tcp)
|
|
|
|
socket.tcp = nil
|
|
socket.active = false
|
|
socket.error = nil
|
|
socket.host = nil
|
|
socket.port = nil
|
|
|
|
return true
|
|
end
|
|
|
|
return true
|
|
end
|
|
|
|
if httpEnabled then
|
|
kernel.socket.registerProtocal(
|
|
"http://",
|
|
httpHandler
|
|
)
|
|
|
|
kernel.socket.registerProtocal(
|
|
"https://",
|
|
httpHandler
|
|
)
|
|
|
|
if kernel.log then
|
|
kernel.log("NET: HTTP/HTTPS initialized")
|
|
end
|
|
end
|
|
|
|
if tcpEnabled then
|
|
kernel.socket.registerProtocal(
|
|
"tcp://",
|
|
tcpHandler
|
|
)
|
|
|
|
if kernel.log then
|
|
kernel.log("NET: TCP initialized")
|
|
end
|
|
end |