NIXIO: TLS-Support, bugfixes
[project/luci.git] / libs / nixio / lua / nixio / util.lua
1 --[[
2 nixio - Linux I/O library for lua
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local table = require "table"
16 local nixio = require "nixio"
17 local setmetatable, assert = setmetatable, assert
18
19 module "nixio.util"
20
21 local BUFFERSIZE = 8096
22 local socket = nixio.socket_meta
23 local tls_socket = nixio.tls_socket_meta
24
25 function socket.recvall(self, len)
26         local block, code, msg = self:recv(len)
27
28         if not block then
29                 return "", code, msg, len
30         elseif #block == 0 then
31                 return "", nil, nil, len
32         end
33
34         local data, total = {block}, #block
35
36         while len > total do
37                 block, code, msg = self:recv(len - total)
38
39                 if not block then
40                         return data, code, msg, len - #data
41                 elseif #block == 0 then
42                         return data, nil, nil, len - #data
43                 end
44
45                 data[#data+1], total = block, total + #block
46         end
47
48         return (#data > 1 and table.concat(data) or data[1]), nil, nil, 0
49 end
50 tls_socket.recvall = socket.recvall
51
52 function socket.sendall(self, data)
53         local total, block = 0
54         local sent, code, msg = self:send(data)
55
56         if not sent then
57                 return total, code, msg, data
58         end
59
60         while sent < #data do
61                 block, total = data:sub(sent + 1), total + sent
62                 sent, code, msg = self:send(block)
63                 
64                 if not sent then
65                         return total, code, msg, block
66                 end
67         end
68         
69         return total + sent, nil, nil, ""
70 end
71 tls_socket.sendall = socket.sendall
72
73 function socket.linesource(self, limit)
74         limit = limit or BUFFERSIZE
75         local buffer = ""
76         local bpos = 0
77         return function(flush)
78                 local line, endp, _
79                 
80                 if flush then
81                         line = buffer:sub(bpos + 1)
82                         buffer = ""
83                         bpos = 0
84                         return line
85                 end
86
87                 while not line do
88                         _, endp, line = buffer:find("(.-)\r?\n", bpos + 1)
89                         if line then
90                                 bpos = endp
91                                 return line
92                         elseif #buffer < limit + bpos then
93                                 local newblock, code = self:recv(limit + bpos - #buffer)
94                                 if not newblock then
95                                         return nil, code
96                                 elseif #newblock == 0 then
97                                         return nil
98                                 end
99                                 buffer = buffer:sub(bpos + 1) .. newblock
100                                 bpos = 0
101                         else
102                                 return nil, 0
103                         end
104                 end
105         end
106 end
107 tls_socket.linesource = socket.linesource
108
109 function socket.blocksource(self, bs, limit)
110         bs = bs or BUFFERSIZE
111         return function()
112                 local toread = bs
113                 if limit then
114                         if limit < 1 then
115                                 return nil
116                         elseif limit < toread then
117                                 toread = limit
118                         end
119                 end
120
121                 local block, code, msg = self:recv(toread)
122
123                 if not block then
124                         return nil, code
125                 elseif #block == 0 then
126                         return nil
127                 else
128                         if limit then
129                                 limit = limit - #block
130                         end
131
132                         return block
133                 end
134         end
135 end
136 tls_socket.blocksource = socket.blocksource