Porting to LUA 5.0 final
This commit is contained in:
parent
c1ef3e7103
commit
0f6c8d50a9
32 changed files with 1539 additions and 1128 deletions
35
src/http.lua
35
src/http.lua
|
@ -7,7 +7,8 @@
|
|||
-----------------------------------------------------------------------------
|
||||
|
||||
local Public, Private = {}, {}
|
||||
socket.http = Public
|
||||
local socket = _G[LUASOCKET_LIBNAME] -- get LuaSocket namespace
|
||||
socket.http = Public -- create http sub namespace
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
-- Program constants
|
||||
|
@ -24,19 +25,15 @@ Public.BLOCKSIZE = 8192
|
|||
-----------------------------------------------------------------------------
|
||||
-- Tries to get a pattern from the server and closes socket on error
|
||||
-- sock: socket connected to the server
|
||||
-- ...: pattern to receive
|
||||
-- pattern: pattern to receive
|
||||
-- Returns
|
||||
-- ...: received pattern
|
||||
-- err: error message if any
|
||||
-- received pattern on success
|
||||
-- nil followed by error message on error
|
||||
-----------------------------------------------------------------------------
|
||||
function Private.try_receive(...)
|
||||
local sock = arg[1]
|
||||
local data, err = sock.receive(unpack(arg))
|
||||
if err then
|
||||
sock:close()
|
||||
return nil, err
|
||||
end
|
||||
return data
|
||||
function Private.try_receive(sock, pattern)
|
||||
local data, err = sock:receive(pattern)
|
||||
if not data then sock:close() end
|
||||
return data, err
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
|
@ -47,8 +44,8 @@ end
|
|||
-- err: error message if any, nil if successfull
|
||||
-----------------------------------------------------------------------------
|
||||
function Private.try_send(sock, data)
|
||||
local err = sock:send(data)
|
||||
if err then sock:close() end
|
||||
local sent, err = sock:send(data)
|
||||
if not sent then sock:close() end
|
||||
return err
|
||||
end
|
||||
|
||||
|
@ -285,21 +282,21 @@ end
|
|||
-- nil if successfull, or an error message in case of error
|
||||
-----------------------------------------------------------------------------
|
||||
function Private.send_indirect(data, send_cb, chunk, size)
|
||||
local sent, err
|
||||
sent = 0
|
||||
local total, sent, err
|
||||
total = 0
|
||||
while 1 do
|
||||
if type(chunk) ~= "string" or type(size) ~= "number" then
|
||||
data:close()
|
||||
if not chunk and type(size) == "string" then return size
|
||||
else return "invalid callback return" end
|
||||
end
|
||||
err = data:send(chunk)
|
||||
sent, err = data:send(chunk)
|
||||
if err then
|
||||
data:close()
|
||||
return err
|
||||
end
|
||||
sent = sent + string.len(chunk)
|
||||
if sent >= size then break end
|
||||
total = total + sent
|
||||
if total >= size then break end
|
||||
chunk, size = send_cb()
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue