More nixio fixes, initial httpclient
[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
24 function socket.recvall(self, len)
25         local block, code, msg = self:recv(len)
26
27         if not block then
28                 return "", code, msg, len
29         elseif #block == 0 then
30                 return "", nil, nil, len
31         end
32
33         local data, total = {block}, #block
34
35         while len > total do
36                 block, code, msg = self:recv(len - total)
37
38                 if not block then
39                         return data, code, msg, len - #data
40                 elseif #block == 0 then
41                         return data, nil, nil, len - #data
42                 end
43
44                 data[#data+1], total = block, total + #block
45         end
46
47         return (#data > 1 and table.concat(data) or data[1]), nil, nil, 0
48 end
49
50 function socket.sendall(self, data)
51         local total, block = 0
52         local sent, code, msg = self:send(data)
53
54         if not sent then
55                 return total, code, msg, data
56         end
57
58         while sent < #data do
59                 block, total = data:sub(sent + 1), total + sent
60                 sent, code, msg = self:send(block)
61                 
62                 if not sent then
63                         return total, code, msg, block
64                 end
65         end
66         
67         return total + sent, nil, nil, ""
68 end
69
70 function socket.linesource(self, limit)
71         limit = limit or BUFFERSIZE
72         local buffer = ""
73         local bpos = 0
74         return function(flush)
75                 local line, endp, _
76                 
77                 if flush then
78                         line = buffer:sub(bpos + 1)
79                         buffer = ""
80                         bpos = 0
81                         return line
82                 end
83
84                 while not line do
85                         _, endp, line = buffer:find("(.-)\r?\n", bpos + 1)
86                         if line then
87                                 bpos = endp
88                                 return line
89                         elseif #buffer < limit + bpos then
90                                 local newblock, code = self:recv(limit + bpos - #buffer)
91                                 if not newblock then
92                                         return nil, code
93                                 elseif #newblock == 0 then
94                                         return nil
95                                 end
96                                 buffer = buffer:sub(bpos + 1) .. newblock
97                                 bpos = 0
98                         else
99                                 return nil, 0
100                         end
101                 end
102         end
103 end
104
105 function socket.blocksource(self, bs, limit)
106         bs = bs or BUFFERSIZE
107         return function()
108                 local toread = bs
109                 if limit then
110                         if limit < 1 then
111                                 return nil
112                         elseif limit < toread then
113                                 toread = limit
114                         end
115                 end
116
117                 local block, code, msg = self:recv(toread)
118
119                 if not block then
120                         return nil, code
121                 elseif #block == 0 then
122                         return nil
123                 else
124                         if limit then
125                                 limit = limit - #block
126                         end
127
128                         return block
129                 end
130         end
131 end