diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2006-03-13 07:16:39 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2006-03-13 07:16:39 +0000 |
commit | 6248b915cba040a3d04fedd100637e1465a43cc7 (patch) | |
tree | d87bde69c65934524b2102f47ac48cf28897bf80 | |
parent | 00e74c304c7aa917fffd1e5b7adedfa05727333c (diff) | |
download | luasocket-6248b915cba040a3d04fedd100637e1465a43cc7.tar.gz luasocket-6248b915cba040a3d04fedd100637e1465a43cc7.tar.bz2 luasocket-6248b915cba040a3d04fedd100637e1465a43cc7.zip |
Fixing bugs...
-rw-r--r-- | src/ftp.lua | 3 | ||||
-rw-r--r-- | src/http.lua | 69 | ||||
-rw-r--r-- | src/unix.c | 146 | ||||
-rw-r--r-- | src/unix.h | 6 |
4 files changed, 134 insertions, 90 deletions
diff --git a/src/ftp.lua b/src/ftp.lua index 5e1b685..d3a48d4 100644 --- a/src/ftp.lua +++ b/src/ftp.lua | |||
@@ -116,10 +116,11 @@ function metat.__index:send(sendt) | |||
116 | if not self.pasvt then self:portconnect() end | 116 | if not self.pasvt then self:portconnect() end |
117 | -- get the sink, source and step for the transfer | 117 | -- get the sink, source and step for the transfer |
118 | local step = sendt.step or ltn12.pump.step | 118 | local step = sendt.step or ltn12.pump.step |
119 | local readt = {self.tp.c} | ||
119 | local checkstep = function(src, snk) | 120 | local checkstep = function(src, snk) |
120 | -- check status in control connection while downloading | 121 | -- check status in control connection while downloading |
121 | local readyt = socket.select(readt, nil, 0) | 122 | local readyt = socket.select(readt, nil, 0) |
122 | if readyt[tp] then self.try(self.tp:check("2..")) end | 123 | if readyt[tp] then code = self.try(self.tp:check("2..")) end |
123 | return step(src, snk) | 124 | return step(src, snk) |
124 | end | 125 | end |
125 | local sink = socket.sink("close-when-done", self.data) | 126 | local sink = socket.sink("close-when-done", self.data) |
diff --git a/src/http.lua b/src/http.lua index 1834061..91600e7 100644 --- a/src/http.lua +++ b/src/http.lua | |||
@@ -15,6 +15,7 @@ local mime = require("mime") | |||
15 | local string = require("string") | 15 | local string = require("string") |
16 | local base = _G | 16 | local base = _G |
17 | local table = require("table") | 17 | local table = require("table") |
18 | local print = print | ||
18 | module("socket.http") | 19 | module("socket.http") |
19 | 20 | ||
20 | ----------------------------------------------------------------------------- | 21 | ----------------------------------------------------------------------------- |
@@ -194,16 +195,19 @@ local function adjustproxy(reqt) | |||
194 | end | 195 | end |
195 | end | 196 | end |
196 | 197 | ||
197 | local function adjustheaders(headers, host) | 198 | local function adjustheaders(reqt) |
198 | -- default headers | 199 | -- default headers |
199 | local lower = { | 200 | local lower = { |
200 | ["user-agent"] = USERAGENT, | 201 | ["user-agent"] = USERAGENT, |
201 | ["host"] = host, | 202 | ["host"] = reqt.host, |
202 | ["connection"] = "close, TE", | 203 | ["connection"] = "close, TE", |
203 | ["te"] = "trailers" | 204 | ["te"] = "trailers" |
204 | } | 205 | } |
206 | -- if we are sending a body, tell server to let us know | ||
207 | -- if it this is a waste of time | ||
208 | if reqt.source then lower["expect"] = "100-continue" end | ||
205 | -- override with user headers | 209 | -- override with user headers |
206 | for i,v in base.pairs(headers or lower) do | 210 | for i,v in base.pairs(reqt.headers or lower) do |
207 | lower[string.lower(i)] = v | 211 | lower[string.lower(i)] = v |
208 | end | 212 | end |
209 | return lower | 213 | return lower |
@@ -229,7 +233,7 @@ local function adjustrequest(reqt) | |||
229 | -- ajust host and port if there is a proxy | 233 | -- ajust host and port if there is a proxy |
230 | nreqt.host, nreqt.port = adjustproxy(nreqt) | 234 | nreqt.host, nreqt.port = adjustproxy(nreqt) |
231 | -- adjust headers in request | 235 | -- adjust headers in request |
232 | nreqt.headers = adjustheaders(nreqt.headers, nreqt.host) | 236 | nreqt.headers = adjustheaders(nreqt) |
233 | return nreqt | 237 | return nreqt |
234 | end | 238 | end |
235 | 239 | ||
@@ -257,6 +261,7 @@ local function shouldreceivebody(reqt, code) | |||
257 | return 1 | 261 | return 1 |
258 | end | 262 | end |
259 | 263 | ||
264 | |||
260 | -- forward declarations | 265 | -- forward declarations |
261 | local trequest, tauthorize, tredirect | 266 | local trequest, tauthorize, tredirect |
262 | 267 | ||
@@ -274,46 +279,74 @@ function tredirect(reqt, location) | |||
274 | source = reqt.source, | 279 | source = reqt.source, |
275 | sink = reqt.sink, | 280 | sink = reqt.sink, |
276 | headers = reqt.headers, | 281 | headers = reqt.headers, |
277 | proxy = reqt.proxy, | 282 | proxy = reqt.proxy, |
278 | nredirects = (reqt.nredirects or 0) + 1, | 283 | nredirects = (reqt.nredirects or 0) + 1, |
279 | connect = reqt.connect | 284 | connect = reqt.connect |
280 | } | 285 | } |
281 | -- pass location header back as a hint we redirected | 286 | -- pass location header back as a hint we redirected |
282 | headers.location = headers.location or location | 287 | headers.location = headers.location or location |
283 | return result, code, headers, status | 288 | return result, code, headers, status |
284 | end | 289 | end |
285 | 290 | ||
286 | function trequest(reqt) | 291 | function trequest(reqt) |
292 | -- we loop until we get what we want, or | ||
293 | -- until we are sure there is no way to get it | ||
287 | reqt = adjustrequest(reqt) | 294 | reqt = adjustrequest(reqt) |
288 | local h = open(reqt.host, reqt.port, reqt.create) | 295 | local h = open(reqt.host, reqt.port, reqt.create) |
296 | -- send request line and headers | ||
289 | h:sendrequestline(reqt.method, reqt.uri) | 297 | h:sendrequestline(reqt.method, reqt.uri) |
290 | h:sendheaders(reqt.headers) | 298 | h:sendheaders(reqt.headers) |
291 | if reqt.source then h:sendbody(reqt.headers, reqt.source, reqt.step) end | 299 | local code = 100 |
292 | local code, headers, status | 300 | local headers, status |
293 | code, status = h:receivestatusline() | 301 | -- if there is a body, check for server status |
294 | headers = h:receiveheaders() | 302 | if reqt.source then |
303 | local ready = socket.select({h.c}, nil, TIMEOUT) | ||
304 | if ready[h.c] then | ||
305 | -- here the server sent us something | ||
306 | code, status = h:receivestatusline() | ||
307 | headers = h:receiveheaders() | ||
308 | end | ||
309 | -- if server is happy, send body | ||
310 | if code == 100 then | ||
311 | h:sendbody(reqt.headers, reqt.source, reqt.step) | ||
312 | -- can't send body again! | ||
313 | reqt.source = nil | ||
314 | end | ||
315 | end | ||
316 | -- ignore all further 100-continue messages | ||
317 | while code == 100 do | ||
318 | code, status = h:receivestatusline() | ||
319 | headers = h:receiveheaders() | ||
320 | end | ||
321 | -- at this point we should have a honest reply from the server | ||
295 | if shouldredirect(reqt, code, headers) then | 322 | if shouldredirect(reqt, code, headers) then |
296 | h:close() | 323 | h:close() |
297 | return tredirect(reqt, headers.location) | 324 | return tredirect(reqt, headers.location) |
298 | elseif shouldauthorize(reqt, code) then | 325 | elseif shouldauthorize(reqt, code) then |
299 | h:close() | 326 | h:close() |
300 | return tauthorize(reqt) | 327 | return tauthorize(reqt) |
301 | elseif shouldreceivebody(reqt, code) then | 328 | else |
302 | h:receivebody(headers, reqt.sink, reqt.step) | 329 | -- here we are finally done |
330 | if shouldreceivebody(reqt, code) then | ||
331 | h:receivebody(headers, reqt.sink, reqt.step) | ||
332 | end | ||
333 | h:close() | ||
334 | return 1, code, headers, status | ||
303 | end | 335 | end |
304 | h:close() | ||
305 | return 1, code, headers, status | ||
306 | end | 336 | end |
307 | 337 | ||
308 | local function srequest(u, body) | 338 | local function srequest(u, b, h) |
309 | local t = {} | 339 | local t = {} |
310 | local reqt = { | 340 | local reqt = { |
311 | url = u, | 341 | url = u, |
312 | sink = ltn12.sink.table(t) | 342 | sink = ltn12.sink.table(t) |
313 | } | 343 | } |
314 | if body then | 344 | if b then |
315 | reqt.source = ltn12.source.string(body) | 345 | reqt.source = ltn12.source.string(b) |
316 | reqt.headers = { ["content-length"] = string.len(body) } | 346 | reqt.headers = h or {} |
347 | reqt.headers["content-length"] = string.len(b) | ||
348 | reqt.headers["content-type"] = reqt.headers["content-type"] or | ||
349 | "application/x-www-form-urlencoded" | ||
317 | reqt.method = "POST" | 350 | reqt.method = "POST" |
318 | end | 351 | end |
319 | local code, headers, status = socket.skip(1, trequest(reqt)) | 352 | local code, headers, status = socket.skip(1, trequest(reqt)) |
@@ -41,7 +41,7 @@ static const char *unix_trybind(p_unix un, const char *path); | |||
41 | /* unix object methods */ | 41 | /* unix object methods */ |
42 | static luaL_reg un[] = { | 42 | static luaL_reg un[] = { |
43 | {"__gc", meth_close}, | 43 | {"__gc", meth_close}, |
44 | {"__tostring", aux_tostring}, | 44 | {"__tostring", auxiliar_tostring}, |
45 | {"accept", meth_accept}, | 45 | {"accept", meth_accept}, |
46 | {"bind", meth_bind}, | 46 | {"bind", meth_bind}, |
47 | {"close", meth_close}, | 47 | {"close", meth_close}, |
@@ -70,20 +70,30 @@ static t_opt opt[] = { | |||
70 | {NULL, NULL} | 70 | {NULL, NULL} |
71 | }; | 71 | }; |
72 | 72 | ||
73 | /* our socket creation function */ | ||
74 | static luaL_reg func[] = { | ||
75 | {"unix", global_create}, | ||
76 | {NULL, NULL} | ||
77 | }; | ||
78 | |||
79 | |||
73 | /*-------------------------------------------------------------------------*\ | 80 | /*-------------------------------------------------------------------------*\ |
74 | * Initializes module | 81 | * Initializes module |
75 | \*-------------------------------------------------------------------------*/ | 82 | \*-------------------------------------------------------------------------*/ |
76 | int luaopen_socketunix(lua_State *L) { | 83 | int luaopen_socket_unix(lua_State *L) { |
77 | /* create classes */ | 84 | /* create classes */ |
78 | aux_newclass(L, "unix{master}", un); | 85 | auxiliar_newclass(L, "unix{master}", un); |
79 | aux_newclass(L, "unix{client}", un); | 86 | auxiliar_newclass(L, "unix{client}", un); |
80 | aux_newclass(L, "unix{server}", un); | 87 | auxiliar_newclass(L, "unix{server}", un); |
81 | /* create class groups */ | 88 | /* create class groups */ |
82 | aux_add2group(L, "unix{master}", "unix{any}"); | 89 | auxiliar_add2group(L, "unix{master}", "unix{any}"); |
83 | aux_add2group(L, "unix{client}", "unix{any}"); | 90 | auxiliar_add2group(L, "unix{client}", "unix{any}"); |
84 | aux_add2group(L, "unix{server}", "unix{any}"); | 91 | auxiliar_add2group(L, "unix{server}", "unix{any}"); |
85 | /* define library functions */ | 92 | /* make sure the function ends up in the package table */ |
86 | lua_pushcfunction(L, global_create); | 93 | luaL_openlib(L, "socket", func, 0); |
94 | /* return the function instead of the 'socket' table */ | ||
95 | lua_pushstring(L, "unix"); | ||
96 | lua_gettable(L, -2); | ||
87 | return 1; | 97 | return 1; |
88 | } | 98 | } |
89 | 99 | ||
@@ -94,30 +104,30 @@ int luaopen_socketunix(lua_State *L) { | |||
94 | * Just call buffered IO methods | 104 | * Just call buffered IO methods |
95 | \*-------------------------------------------------------------------------*/ | 105 | \*-------------------------------------------------------------------------*/ |
96 | static int meth_send(lua_State *L) { | 106 | static int meth_send(lua_State *L) { |
97 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 107 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
98 | return buf_meth_send(L, &un->buf); | 108 | return buffer_meth_send(L, &un->buf); |
99 | } | 109 | } |
100 | 110 | ||
101 | static int meth_receive(lua_State *L) { | 111 | static int meth_receive(lua_State *L) { |
102 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 112 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
103 | return buf_meth_receive(L, &un->buf); | 113 | return buffer_meth_receive(L, &un->buf); |
104 | } | 114 | } |
105 | 115 | ||
106 | static int meth_getstats(lua_State *L) { | 116 | static int meth_getstats(lua_State *L) { |
107 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 117 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
108 | return buf_meth_getstats(L, &un->buf); | 118 | return buffer_meth_getstats(L, &un->buf); |
109 | } | 119 | } |
110 | 120 | ||
111 | static int meth_setstats(lua_State *L) { | 121 | static int meth_setstats(lua_State *L) { |
112 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 122 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
113 | return buf_meth_setstats(L, &un->buf); | 123 | return buffer_meth_setstats(L, &un->buf); |
114 | } | 124 | } |
115 | 125 | ||
116 | /*-------------------------------------------------------------------------*\ | 126 | /*-------------------------------------------------------------------------*\ |
117 | * Just call option handler | 127 | * Just call option handler |
118 | \*-------------------------------------------------------------------------*/ | 128 | \*-------------------------------------------------------------------------*/ |
119 | static int meth_setoption(lua_State *L) { | 129 | static int meth_setoption(lua_State *L) { |
120 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 130 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
121 | return opt_meth_setoption(L, opt, &un->sock); | 131 | return opt_meth_setoption(L, opt, &un->sock); |
122 | } | 132 | } |
123 | 133 | ||
@@ -125,21 +135,21 @@ static int meth_setoption(lua_State *L) { | |||
125 | * Select support methods | 135 | * Select support methods |
126 | \*-------------------------------------------------------------------------*/ | 136 | \*-------------------------------------------------------------------------*/ |
127 | static int meth_getfd(lua_State *L) { | 137 | static int meth_getfd(lua_State *L) { |
128 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 138 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
129 | lua_pushnumber(L, (int) un->sock); | 139 | lua_pushnumber(L, (int) un->sock); |
130 | return 1; | 140 | return 1; |
131 | } | 141 | } |
132 | 142 | ||
133 | /* this is very dangerous, but can be handy for those that are brave enough */ | 143 | /* this is very dangerous, but can be handy for those that are brave enough */ |
134 | static int meth_setfd(lua_State *L) { | 144 | static int meth_setfd(lua_State *L) { |
135 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 145 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
136 | un->sock = (t_sock) luaL_checknumber(L, 2); | 146 | un->sock = (t_socket) luaL_checknumber(L, 2); |
137 | return 0; | 147 | return 0; |
138 | } | 148 | } |
139 | 149 | ||
140 | static int meth_dirty(lua_State *L) { | 150 | static int meth_dirty(lua_State *L) { |
141 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 151 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
142 | lua_pushboolean(L, !buf_isempty(&un->buf)); | 152 | lua_pushboolean(L, !buffer_isempty(&un->buf)); |
143 | return 1; | 153 | return 1; |
144 | } | 154 | } |
145 | 155 | ||
@@ -148,25 +158,25 @@ static int meth_dirty(lua_State *L) { | |||
148 | * server object | 158 | * server object |
149 | \*-------------------------------------------------------------------------*/ | 159 | \*-------------------------------------------------------------------------*/ |
150 | static int meth_accept(lua_State *L) { | 160 | static int meth_accept(lua_State *L) { |
151 | p_unix server = (p_unix) aux_checkclass(L, "unix{server}", 1); | 161 | p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1); |
152 | p_tm tm = tm_markstart(&server->tm); | 162 | p_timeout tm = timeout_markstart(&server->tm); |
153 | t_sock sock; | 163 | t_socket sock; |
154 | int err = sock_accept(&server->sock, &sock, NULL, NULL, tm); | 164 | int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); |
155 | /* if successful, push client socket */ | 165 | /* if successful, push client socket */ |
156 | if (err == IO_DONE) { | 166 | if (err == IO_DONE) { |
157 | p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix)); | 167 | p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix)); |
158 | aux_setclass(L, "unix{client}", -1); | 168 | auxiliar_setclass(L, "unix{client}", -1); |
159 | /* initialize structure fields */ | 169 | /* initialize structure fields */ |
160 | sock_setnonblocking(&sock); | 170 | socket_setnonblocking(&sock); |
161 | clnt->sock = sock; | 171 | clnt->sock = sock; |
162 | io_init(&clnt->io, (p_send)sock_send, (p_recv)sock_recv, | 172 | io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv, |
163 | (p_error) sock_ioerror, &clnt->sock); | 173 | (p_error) socket_ioerror, &clnt->sock); |
164 | tm_init(&clnt->tm, -1, -1); | 174 | timeout_init(&clnt->tm, -1, -1); |
165 | buf_init(&clnt->buf, &clnt->io, &clnt->tm); | 175 | buffer_init(&clnt->buf, &clnt->io, &clnt->tm); |
166 | return 1; | 176 | return 1; |
167 | } else { | 177 | } else { |
168 | lua_pushnil(L); | 178 | lua_pushnil(L); |
169 | lua_pushstring(L, sock_strerror(err)); | 179 | lua_pushstring(L, socket_strerror(err)); |
170 | return 2; | 180 | return 2; |
171 | } | 181 | } |
172 | } | 182 | } |
@@ -185,18 +195,18 @@ static const char *unix_trybind(p_unix un, const char *path) { | |||
185 | #ifdef UNIX_HAS_SUN_LEN | 195 | #ifdef UNIX_HAS_SUN_LEN |
186 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) | 196 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) |
187 | + len + 1; | 197 | + len + 1; |
188 | err = sock_bind(&un->sock, (SA *) &local, local.sun_len); | 198 | err = socket_bind(&un->sock, (SA *) &local, local.sun_len); |
189 | 199 | ||
190 | #else | 200 | #else |
191 | err = sock_bind(&un->sock, (SA *) &local, | 201 | err = socket_bind(&un->sock, (SA *) &local, |
192 | sizeof(local.sun_family) + len); | 202 | sizeof(local.sun_family) + len); |
193 | #endif | 203 | #endif |
194 | if (err != IO_DONE) sock_destroy(&un->sock); | 204 | if (err != IO_DONE) socket_destroy(&un->sock); |
195 | return sock_strerror(err); | 205 | return socket_strerror(err); |
196 | } | 206 | } |
197 | 207 | ||
198 | static int meth_bind(lua_State *L) { | 208 | static int meth_bind(lua_State *L) { |
199 | p_unix un = (p_unix) aux_checkclass(L, "unix{master}", 1); | 209 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); |
200 | const char *path = luaL_checkstring(L, 2); | 210 | const char *path = luaL_checkstring(L, 2); |
201 | const char *err = unix_trybind(un, path); | 211 | const char *err = unix_trybind(un, path); |
202 | if (err) { | 212 | if (err) { |
@@ -220,22 +230,22 @@ static const char *unix_tryconnect(p_unix un, const char *path) | |||
220 | memset(&remote, 0, sizeof(remote)); | 230 | memset(&remote, 0, sizeof(remote)); |
221 | strcpy(remote.sun_path, path); | 231 | strcpy(remote.sun_path, path); |
222 | remote.sun_family = AF_UNIX; | 232 | remote.sun_family = AF_UNIX; |
223 | tm_markstart(&un->tm); | 233 | timeout_markstart(&un->tm); |
224 | #ifdef UNIX_HAS_SUN_LEN | 234 | #ifdef UNIX_HAS_SUN_LEN |
225 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) | 235 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) |
226 | + len + 1; | 236 | + len + 1; |
227 | err = sock_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); | 237 | err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); |
228 | #else | 238 | #else |
229 | err = sock_connect(&un->sock, (SA *) &remote, | 239 | err = socket_connect(&un->sock, (SA *) &remote, |
230 | sizeof(remote.sun_family) + len, &un->tm); | 240 | sizeof(remote.sun_family) + len, &un->tm); |
231 | #endif | 241 | #endif |
232 | if (err != IO_DONE) sock_destroy(&un->sock); | 242 | if (err != IO_DONE) socket_destroy(&un->sock); |
233 | return sock_strerror(err); | 243 | return socket_strerror(err); |
234 | } | 244 | } |
235 | 245 | ||
236 | static int meth_connect(lua_State *L) | 246 | static int meth_connect(lua_State *L) |
237 | { | 247 | { |
238 | p_unix un = (p_unix) aux_checkclass(L, "unix{master}", 1); | 248 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); |
239 | const char *path = luaL_checkstring(L, 2); | 249 | const char *path = luaL_checkstring(L, 2); |
240 | const char *err = unix_tryconnect(un, path); | 250 | const char *err = unix_tryconnect(un, path); |
241 | if (err) { | 251 | if (err) { |
@@ -244,7 +254,7 @@ static int meth_connect(lua_State *L) | |||
244 | return 2; | 254 | return 2; |
245 | } | 255 | } |
246 | /* turn master object into a client object */ | 256 | /* turn master object into a client object */ |
247 | aux_setclass(L, "unix{client}", 1); | 257 | auxiliar_setclass(L, "unix{client}", 1); |
248 | lua_pushnumber(L, 1); | 258 | lua_pushnumber(L, 1); |
249 | return 1; | 259 | return 1; |
250 | } | 260 | } |
@@ -254,8 +264,8 @@ static int meth_connect(lua_State *L) | |||
254 | \*-------------------------------------------------------------------------*/ | 264 | \*-------------------------------------------------------------------------*/ |
255 | static int meth_close(lua_State *L) | 265 | static int meth_close(lua_State *L) |
256 | { | 266 | { |
257 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 267 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
258 | sock_destroy(&un->sock); | 268 | socket_destroy(&un->sock); |
259 | lua_pushnumber(L, 1); | 269 | lua_pushnumber(L, 1); |
260 | return 1; | 270 | return 1; |
261 | } | 271 | } |
@@ -265,16 +275,16 @@ static int meth_close(lua_State *L) | |||
265 | \*-------------------------------------------------------------------------*/ | 275 | \*-------------------------------------------------------------------------*/ |
266 | static int meth_listen(lua_State *L) | 276 | static int meth_listen(lua_State *L) |
267 | { | 277 | { |
268 | p_unix un = (p_unix) aux_checkclass(L, "unix{master}", 1); | 278 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); |
269 | int backlog = (int) luaL_optnumber(L, 2, 32); | 279 | int backlog = (int) luaL_optnumber(L, 2, 32); |
270 | int err = sock_listen(&un->sock, backlog); | 280 | int err = socket_listen(&un->sock, backlog); |
271 | if (err != IO_DONE) { | 281 | if (err != IO_DONE) { |
272 | lua_pushnil(L); | 282 | lua_pushnil(L); |
273 | lua_pushstring(L, sock_strerror(err)); | 283 | lua_pushstring(L, socket_strerror(err)); |
274 | return 2; | 284 | return 2; |
275 | } | 285 | } |
276 | /* turn master object into a server object */ | 286 | /* turn master object into a server object */ |
277 | aux_setclass(L, "unix{server}", 1); | 287 | auxiliar_setclass(L, "unix{server}", 1); |
278 | lua_pushnumber(L, 1); | 288 | lua_pushnumber(L, 1); |
279 | return 1; | 289 | return 1; |
280 | } | 290 | } |
@@ -284,20 +294,20 @@ static int meth_listen(lua_State *L) | |||
284 | \*-------------------------------------------------------------------------*/ | 294 | \*-------------------------------------------------------------------------*/ |
285 | static int meth_shutdown(lua_State *L) | 295 | static int meth_shutdown(lua_State *L) |
286 | { | 296 | { |
287 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 297 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
288 | const char *how = luaL_optstring(L, 2, "both"); | 298 | const char *how = luaL_optstring(L, 2, "both"); |
289 | switch (how[0]) { | 299 | switch (how[0]) { |
290 | case 'b': | 300 | case 'b': |
291 | if (strcmp(how, "both")) goto error; | 301 | if (strcmp(how, "both")) goto error; |
292 | sock_shutdown(&un->sock, 2); | 302 | socket_shutdown(&un->sock, 2); |
293 | break; | 303 | break; |
294 | case 's': | 304 | case 's': |
295 | if (strcmp(how, "send")) goto error; | 305 | if (strcmp(how, "send")) goto error; |
296 | sock_shutdown(&un->sock, 1); | 306 | socket_shutdown(&un->sock, 1); |
297 | break; | 307 | break; |
298 | case 'r': | 308 | case 'r': |
299 | if (strcmp(how, "receive")) goto error; | 309 | if (strcmp(how, "receive")) goto error; |
300 | sock_shutdown(&un->sock, 0); | 310 | socket_shutdown(&un->sock, 0); |
301 | break; | 311 | break; |
302 | } | 312 | } |
303 | lua_pushnumber(L, 1); | 313 | lua_pushnumber(L, 1); |
@@ -311,8 +321,8 @@ error: | |||
311 | * Just call tm methods | 321 | * Just call tm methods |
312 | \*-------------------------------------------------------------------------*/ | 322 | \*-------------------------------------------------------------------------*/ |
313 | static int meth_settimeout(lua_State *L) { | 323 | static int meth_settimeout(lua_State *L) { |
314 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 324 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
315 | return tm_meth_settimeout(L, &un->tm); | 325 | return timeout_meth_settimeout(L, &un->tm); |
316 | } | 326 | } |
317 | 327 | ||
318 | /*=========================================================================*\ | 328 | /*=========================================================================*\ |
@@ -322,25 +332,25 @@ static int meth_settimeout(lua_State *L) { | |||
322 | * Creates a master unix object | 332 | * Creates a master unix object |
323 | \*-------------------------------------------------------------------------*/ | 333 | \*-------------------------------------------------------------------------*/ |
324 | static int global_create(lua_State *L) { | 334 | static int global_create(lua_State *L) { |
325 | t_sock sock; | 335 | t_socket sock; |
326 | int err = sock_create(&sock, AF_UNIX, SOCK_STREAM, 0); | 336 | int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0); |
327 | /* try to allocate a system socket */ | 337 | /* try to allocate a system socket */ |
328 | if (err == IO_DONE) { | 338 | if (err == IO_DONE) { |
329 | /* allocate unix object */ | 339 | /* allocate unix object */ |
330 | p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); | 340 | p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); |
331 | /* set its type as master object */ | 341 | /* set its type as master object */ |
332 | aux_setclass(L, "unix{master}", -1); | 342 | auxiliar_setclass(L, "unix{master}", -1); |
333 | /* initialize remaining structure fields */ | 343 | /* initialize remaining structure fields */ |
334 | sock_setnonblocking(&sock); | 344 | socket_setnonblocking(&sock); |
335 | un->sock = sock; | 345 | un->sock = sock; |
336 | io_init(&un->io, (p_send) sock_send, (p_recv) sock_recv, | 346 | io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv, |
337 | (p_error) sock_ioerror, &un->sock); | 347 | (p_error) socket_ioerror, &un->sock); |
338 | tm_init(&un->tm, -1, -1); | 348 | timeout_init(&un->tm, -1, -1); |
339 | buf_init(&un->buf, &un->io, &un->tm); | 349 | buffer_init(&un->buf, &un->io, &un->tm); |
340 | return 1; | 350 | return 1; |
341 | } else { | 351 | } else { |
342 | lua_pushnil(L); | 352 | lua_pushnil(L); |
343 | lua_pushstring(L, sock_strerror(err)); | 353 | lua_pushstring(L, socket_strerror(err)); |
344 | return 2; | 354 | return 2; |
345 | } | 355 | } |
346 | } | 356 | } |
@@ -16,10 +16,10 @@ | |||
16 | #include "socket.h" | 16 | #include "socket.h" |
17 | 17 | ||
18 | typedef struct t_unix_ { | 18 | typedef struct t_unix_ { |
19 | t_sock sock; | 19 | t_socket sock; |
20 | t_io io; | 20 | t_io io; |
21 | t_buf buf; | 21 | t_buffer buf; |
22 | t_tm tm; | 22 | t_timeout tm; |
23 | } t_unix; | 23 | } t_unix; |
24 | typedef t_unix *p_unix; | 24 | typedef t_unix *p_unix; |
25 | 25 | ||