2 nixio - Linux I/O library for lua
4 Copyright 2008 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
15 local table = require "table"
16 local nixio = require "nixio"
17 local setmetatable, assert = setmetatable, assert
21 local BUFFERSIZE = 8096
22 local socket = nixio.socket_meta
23 local tls_socket = nixio.tls_socket_meta
25 function socket.recvall(self, len)
26 local block, code, msg = self:recv(len)
29 return "", code, msg, len
30 elseif #block == 0 then
31 return "", nil, nil, len
34 local data, total = {block}, #block
37 block, code, msg = self:recv(len - total)
40 return data, code, msg, len - #data
41 elseif #block == 0 then
42 return data, nil, nil, len - #data
45 data[#data+1], total = block, total + #block
48 return (#data > 1 and table.concat(data) or data[1]), nil, nil, 0
50 tls_socket.recvall = socket.recvall
52 function socket.sendall(self, data)
53 local total, block = 0
54 local sent, code, msg = self:send(data)
57 return total, code, msg, data
61 block, total = data:sub(sent + 1), total + sent
62 sent, code, msg = self:send(block)
65 return total, code, msg, block
69 return total + sent, nil, nil, ""
71 tls_socket.sendall = socket.sendall
73 function socket.linesource(self, limit)
74 limit = limit or BUFFERSIZE
77 return function(flush)
81 line = buffer:sub(bpos + 1)
88 _, endp, line = buffer:find("(.-)\r?\n", bpos + 1)
92 elseif #buffer < limit + bpos then
93 local newblock, code = self:recv(limit + bpos - #buffer)
96 elseif #newblock == 0 then
99 buffer = buffer:sub(bpos + 1) .. newblock
107 tls_socket.linesource = socket.linesource
109 function socket.blocksource(self, bs, limit)
110 bs = bs or BUFFERSIZE
116 elseif limit < toread then
121 local block, code, msg = self:recv(toread)
125 elseif #block == 0 then
129 limit = limit - #block
136 tls_socket.blocksource = socket.blocksource