Changed receive function. Now uniform with all other functions. Returns nil
on error, return partial result in the end. http.lua rewritten.
This commit is contained in:
parent
2a14ac4fe4
commit
4919a83d22
9 changed files with 316 additions and 651 deletions
|
@ -8,7 +8,7 @@ dofile("noglobals.lua")
|
|||
local host, proxy, request, response, index_file
|
||||
local ignore, expect, index, prefix, cgiprefix, index_crlf
|
||||
|
||||
socket.http.TIMEOUT = 5
|
||||
socket.http.TIMEOUT = 10
|
||||
|
||||
local t = socket.time()
|
||||
|
||||
|
@ -49,7 +49,9 @@ local check_result = function(response, expect, ignore)
|
|||
for i,v in response do
|
||||
if not ignore[i] then
|
||||
if v ~= expect[i] then
|
||||
print(string.sub(tostring(v), 1, 70))
|
||||
local f = io.open("err", "w")
|
||||
f:write(tostring(v), "\n\n versus\n\n", tostring(expect[i]))
|
||||
f:close()
|
||||
fail(i .. " differs!")
|
||||
end
|
||||
end
|
||||
|
@ -57,8 +59,10 @@ local check_result = function(response, expect, ignore)
|
|||
for i,v in expect do
|
||||
if not ignore[i] then
|
||||
if v ~= response[i] then
|
||||
local f = io.open("err", "w")
|
||||
f:write(tostring(response[i]), "\n\n versus\n\n", tostring(v))
|
||||
v = string.sub(type(v) == "string" and v or "", 1, 70)
|
||||
print(string.sub(tostring(v), 1, 70))
|
||||
f:close()
|
||||
fail(i .. " differs!")
|
||||
end
|
||||
end
|
||||
|
@ -67,12 +71,14 @@ local check_result = function(response, expect, ignore)
|
|||
end
|
||||
|
||||
local check_request = function(request, expect, ignore)
|
||||
local t
|
||||
if not request.sink then
|
||||
request.sink, t = ltn12.sink.table(t)
|
||||
end
|
||||
request.source = request.source or
|
||||
(request.body and ltn12.source.string(request.body))
|
||||
local response = socket.http.request(request)
|
||||
check_result(response, expect, ignore)
|
||||
end
|
||||
|
||||
local check_request_cb = function(request, expect, ignore)
|
||||
local response = socket.http.request_cb(request)
|
||||
if t and table.getn(t) > 0 then response.body = table.concat(t) end
|
||||
check_result(response, expect, ignore)
|
||||
end
|
||||
|
||||
|
@ -183,7 +189,7 @@ ignore = {
|
|||
status = 1,
|
||||
headers = 1
|
||||
}
|
||||
check_request_cb(request, expect, ignore)
|
||||
check_request(request, expect, ignore)
|
||||
back = readfile(index_file .. "-back")
|
||||
check(back == index)
|
||||
os.remove(index_file .. "-back")
|
||||
|
@ -225,19 +231,11 @@ ignore = {
|
|||
status = 1,
|
||||
headers = 1
|
||||
}
|
||||
check_request_cb(request, expect, ignore)
|
||||
check_request(request, expect, ignore)
|
||||
back = readfile(index_file .. "-back")
|
||||
check(back == index)
|
||||
os.remove(index_file .. "-back")
|
||||
|
||||
------------------------------------------------------------------------
|
||||
io.write("testing simple post function with table args: ")
|
||||
back = socket.http.post {
|
||||
url = "http://" .. host .. cgiprefix .. "/cat",
|
||||
body = index
|
||||
}
|
||||
check(back == index)
|
||||
|
||||
------------------------------------------------------------------------
|
||||
io.write("testing http redirection: ")
|
||||
request = {
|
||||
|
@ -438,15 +436,6 @@ io.write("testing simple get function: ")
|
|||
body = socket.http.get("http://" .. host .. prefix .. "/index.html")
|
||||
check(body == index)
|
||||
|
||||
------------------------------------------------------------------------
|
||||
io.write("testing simple get function with table args: ")
|
||||
body = socket.http.get {
|
||||
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
|
||||
user = "luasocket",
|
||||
password = "password"
|
||||
}
|
||||
check(body == index)
|
||||
|
||||
------------------------------------------------------------------------
|
||||
io.write("testing HEAD method: ")
|
||||
socket.http.TIMEOUT = 1
|
||||
|
|
|
@ -17,14 +17,12 @@ function warn(...)
|
|||
io.stderr:write("WARNING: ", s, "\n")
|
||||
end
|
||||
|
||||
pad = string.rep(" ", 8192)
|
||||
|
||||
function remote(...)
|
||||
local s = string.format(unpack(arg))
|
||||
s = string.gsub(s, "\n", ";")
|
||||
s = string.gsub(s, "%s+", " ")
|
||||
s = string.gsub(s, "^%s*", "")
|
||||
control:send(pad, s, "\n")
|
||||
control:send(s, "\n")
|
||||
control:receive()
|
||||
end
|
||||
|
||||
|
@ -122,7 +120,13 @@ remote (string.format("str = data:receive(%d)",
|
|||
sent, err = data:send(p1, p2, p3, p4)
|
||||
if err then fail(err) end
|
||||
remote "data:send(str); data:close()"
|
||||
bp1, bp2, bp3, bp4, err = data:receive("*l", "*l", string.len(p3), "*a")
|
||||
bp1, err = data:receive()
|
||||
if err then fail(err) end
|
||||
bp2, err = data:receive()
|
||||
if err then fail(err) end
|
||||
bp3, err = data:receive(string.len(p3))
|
||||
if err then fail(err) end
|
||||
bp4, err = data:receive("*a")
|
||||
if err then fail(err) end
|
||||
if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 and bp4 == p4 then
|
||||
pass("patterns match")
|
||||
|
@ -186,7 +190,7 @@ end
|
|||
------------------------------------------------------------------------
|
||||
function test_totaltimeoutreceive(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, total
|
||||
local str, err, partial
|
||||
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
|
@ -198,9 +202,9 @@ function test_totaltimeoutreceive(len, tm, sl)
|
|||
data:send(str)
|
||||
]], 2*tm, len, sl, sl))
|
||||
data:settimeout(tm, "total")
|
||||
str, err, elapsed = data:receive(2*len)
|
||||
str, err, partial, elapsed = data:receive(2*len)
|
||||
check_timeout(tm, sl, elapsed, err, "receive", "total",
|
||||
string.len(str) == 2*len)
|
||||
string.len(str or partial) == 2*len)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
|
@ -226,7 +230,7 @@ end
|
|||
------------------------------------------------------------------------
|
||||
function test_blockingtimeoutreceive(len, tm, sl)
|
||||
reconnect()
|
||||
local str, err, total
|
||||
local str, err, partial
|
||||
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
|
||||
remote (string.format ([[
|
||||
data:settimeout(%d)
|
||||
|
@ -238,9 +242,9 @@ function test_blockingtimeoutreceive(len, tm, sl)
|
|||
data:send(str)
|
||||
]], 2*tm, len, sl, sl))
|
||||
data:settimeout(tm)
|
||||
str, err, elapsed = data:receive(2*len)
|
||||
str, err, partial, elapsed = data:receive(2*len)
|
||||
check_timeout(tm, sl, elapsed, err, "receive", "blocking",
|
||||
string.len(str) == 2*len)
|
||||
string.len(str or partial) == 2*len)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------------
|
||||
|
@ -298,7 +302,7 @@ end
|
|||
|
||||
------------------------------------------------------------------------
|
||||
function test_closed()
|
||||
local back, err
|
||||
local back, partial, err
|
||||
local str = 'little string'
|
||||
reconnect()
|
||||
pass("trying read detection")
|
||||
|
@ -308,10 +312,10 @@ function test_closed()
|
|||
data = nil
|
||||
]], str))
|
||||
-- try to get a line
|
||||
back, err = data:receive()
|
||||
if not err then fail("shold have gotten 'closed'.")
|
||||
back, err, partial = data:receive()
|
||||
if not err then fail("should have gotten 'closed'.")
|
||||
elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.")
|
||||
elseif str ~= back then fail("didn't receive partial result.")
|
||||
elseif str ~= partial then fail("didn't receive partial result.")
|
||||
else pass("graceful 'closed' received") end
|
||||
reconnect()
|
||||
pass("trying write detection")
|
||||
|
@ -456,7 +460,6 @@ test_methods(socket.udp(), {
|
|||
"setpeername",
|
||||
"setsockname",
|
||||
"settimeout",
|
||||
"shutdown",
|
||||
})
|
||||
|
||||
test("select function")
|
||||
|
@ -481,6 +484,7 @@ accept_timeout()
|
|||
accept_errors()
|
||||
|
||||
|
||||
|
||||
test("mixed patterns")
|
||||
test_mixed(1)
|
||||
test_mixed(17)
|
||||
|
|
|
@ -6,7 +6,7 @@ mesgt = {
|
|||
body = {
|
||||
preamble = "Some attatched stuff",
|
||||
[1] = {
|
||||
body = "Testing stuffing.\r\n.\r\nGot you.\r\n.Hehehe.\r\n"
|
||||
body = mime.eol(0, "Testing stuffing.\n.\nGot you.\n.Hehehe.\n")
|
||||
},
|
||||
[2] = {
|
||||
headers = {
|
||||
|
@ -29,7 +29,7 @@ mesgt = {
|
|||
["content-transfer-encoding"] = "QUOTED-PRINTABLE"
|
||||
},
|
||||
body = ltn12.source.chain(
|
||||
ltn12.source.file(io.open("message.lua", "rb")),
|
||||
ltn12.source.file(io.open("testmesg.lua", "rb")),
|
||||
ltn12.filter.chain(
|
||||
mime.normalize(),
|
||||
mime.encode("quoted-printable"),
|
||||
|
@ -46,8 +46,8 @@ mesgt = {
|
|||
-- ltn12.pump(source, sink)
|
||||
|
||||
print(socket.smtp.send {
|
||||
rcpt = {"<db@werx4.com>", "<diego@cs.princeton.edu>"},
|
||||
rcpt = "<diego@cs.princeton.edu>",
|
||||
from = "<diego@cs.princeton.edu>",
|
||||
source = socket.smtp.message(mesgt),
|
||||
server = "smtp.princeton.edu"
|
||||
server = "mail.cs.princeton.edu"
|
||||
})
|
||||
|
|
|
@ -22,6 +22,8 @@ while 1 do
|
|||
print("server: closing connection...")
|
||||
break
|
||||
end
|
||||
print(command);
|
||||
|
||||
(loadstring(command))()
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue