Implement internet.request POST

This commit is contained in:
Łukasz Magiera 2016-02-11 21:47:23 +01:00
parent 684604176d
commit 8820138c3c

View File

@ -61,11 +61,25 @@ function internet.start()
end end
function component.request(url, post) function component.request(url, post)
local host = url:match("http://([^/]+)") checkArg(1, url, "string")
if not host then io.stderr:write("LERR" .. url .. "\n") end checkArg(2, post, "string", "nil")
local host, uri = url:match("http://([^/]+)([^#]+)")
if not host then native.log("internet.request host match error: " .. url .. "\n") end
local socket = component.connect(host, 80) local socket = component.connect(host, 80)
if socket.finishConnect() then if socket.finishConnect() then
socket.write("GET " .. url .. " HTTP/1.1\r\nHost: " .. host .. "\r\nConnection: close\r\n\r\n") local request
if not post then
request = "GET " .. uri .. " HTTP/1.1\r\nHost: " .. host .. "\r\nConnection: close\r\n\r\n"
else
request = "POST " .. uri .. " HTTP/1.1\r\nHost: " .. host .. "\r\nConnection: close\r\n"
.. "Content-Type: application/x-www-form-urlencoded\r\nUser-Agent: LuPI/1.0\r\n"
.. "Content-Length: " .. math.floor(#post) .. "\r\n\r\n"
.. post .. "\r\n\r\n"
end
socket.write(request)
if native.debug then
native.log("internet.request:\n-- request begin --\n" .. request .. "\n-- request end --")
end
end end
local stream = {} local stream = {}
@ -102,6 +116,9 @@ function internet.start()
local finishConnect = function() --Read header local finishConnect = function() --Read header
header = {} header = {}
header.status = connection:read("*l"):match("HTTP/.%.. (%d+) (.+)\r") header.status = connection:read("*l"):match("HTTP/.%.. (%d+) (.+)\r")
if native.debug then
native.log("internet.request:\n-- response begin --\n" .. header.status .. "\n")
end
while true do while true do
local line = connection:read("*l") local line = connection:read("*l")
if not line or line == "" or line == "\r" then if not line or line == "" or line == "\r" then
@ -109,6 +126,12 @@ function internet.start()
end end
local k, v = line:match("([^:]+): (.+)\r") local k, v = line:match("([^:]+): (.+)\r")
header[k:lower()] = v header[k:lower()] = v
if native.debug then
native.log(line)
end
end
if native.debug then
native.log("-- response end --")
end end
header["content-length"] = tonumber(header["content-length"]) header["content-length"] = tonumber(header["content-length"])
end end
@ -119,7 +142,7 @@ function internet.start()
if not header then if not header then
finishConnect() finishConnect()
end end
if header["content-length"] < 1 then if not header["content-length"] or header["content-length"] < 1 then
return nil return nil
end end
checkArg(1, n, "number", "nil") checkArg(1, n, "number", "nil")