aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2002-12-02 23:34:41 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2002-12-02 23:34:41 +0000
commitd7e80592a69c076991ed4f4cc15d5390e14d1f0b (patch)
tree5fd9c99742b4a03e6a66f940a1f86f98d4db50c8
parentb796207ce06a66b04cce6686b3fa664c06703995 (diff)
downloadluasocket-d7e80592a69c076991ed4f4cc15d5390e14d1f0b.tar.gz
luasocket-d7e80592a69c076991ed4f4cc15d5390e14d1f0b.tar.bz2
luasocket-d7e80592a69c076991ed4f4cc15d5390e14d1f0b.zip
Already compiling and running for Lua 5.0 (alpha)
-rw-r--r--src/http.lua20
-rw-r--r--src/inet.c8
-rw-r--r--src/luasocket.c10
-rw-r--r--src/luasocket.h7
-rw-r--r--src/mbox.lua113
-rw-r--r--src/select.c15
-rw-r--r--src/smtp.lua2
-rw-r--r--src/timeout.c4
-rw-r--r--src/udp.c12
-rw-r--r--src/unix.c8
-rw-r--r--src/unix.h6
-rw-r--r--src/url.lua62
12 files changed, 165 insertions, 102 deletions
diff --git a/src/http.lua b/src/http.lua
index 9832a6b..dce3ac8 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -32,7 +32,7 @@ Public.BLOCKSIZE = 8192
32----------------------------------------------------------------------------- 32-----------------------------------------------------------------------------
33function Private.try_receive(...) 33function Private.try_receive(...)
34 local sock = arg[1] 34 local sock = arg[1]
35 local data, err = call(sock.receive, arg) 35 local data, err = sock.receive(unpack(arg))
36 if err then 36 if err then
37 sock:close() 37 sock:close()
38 return nil, err 38 return nil, err
@@ -62,7 +62,7 @@ end
62----------------------------------------------------------------------------- 62-----------------------------------------------------------------------------
63function Private.get_statuscode(line) 63function Private.get_statuscode(line)
64 local code, _ 64 local code, _
65 _, _, code = strfind(line, "HTTP/%d*%.%d* (%d%d%d)") 65 _, _, code = string.find(line, "HTTP/%d*%.%d* (%d%d%d)")
66 return tonumber(code) 66 return tonumber(code)
67end 67end
68 68
@@ -102,17 +102,17 @@ function Private.receive_headers(sock, headers)
102 -- headers go until a blank line is found 102 -- headers go until a blank line is found
103 while line ~= "" do 103 while line ~= "" do
104 -- get field-name and value 104 -- get field-name and value
105 _,_, name, value = strfind(line, "^(.-):%s*(.*)") 105 _,_, name, value = string.find(line, "^(.-):%s*(.*)")
106 if not name or not value then 106 if not name or not value then
107 sock:close() 107 sock:close()
108 return nil, "malformed reponse headers" 108 return nil, "malformed reponse headers"
109 end 109 end
110 name = strlower(name) 110 name = string.lower(name)
111 -- get next line (value might be folded) 111 -- get next line (value might be folded)
112 line, err = Private.try_receive(sock) 112 line, err = Private.try_receive(sock)
113 if err then return nil, err end 113 if err then return nil, err end
114 -- unfold any folded values 114 -- unfold any folded values
115 while not err and strfind(line, "^%s") do 115 while not err and string.find(line, "^%s") do
116 value = value .. line 116 value = value .. line
117 line, err = Private.try_receive(sock) 117 line, err = Private.try_receive(sock)
118 if err then return nil, err end 118 if err then return nil, err end
@@ -142,7 +142,7 @@ function Private.receivebody_bychunks(sock, headers, receive_cb)
142 local go, uerr = receive_cb(nil, err) 142 local go, uerr = receive_cb(nil, err)
143 return uerr or err 143 return uerr or err
144 end 144 end
145 size = tonumber(gsub(line, ";.*", ""), 16) 145 size = tonumber(string.gsub(line, ";.*", ""), 16)
146 if not size then 146 if not size then
147 err = "invalid chunk size" 147 err = "invalid chunk size"
148 sock:close() 148 sock:close()
@@ -299,7 +299,7 @@ function Private.send_indirect(data, send_cb, chunk, size)
299 data:close() 299 data:close()
300 return err 300 return err
301 end 301 end
302 sent = sent + strlen(chunk) 302 sent = sent + string.len(chunk)
303 if sent >= size then break end 303 if sent >= size then break end
304 chunk, size = send_cb() 304 chunk, size = send_cb()
305 end 305 end
@@ -391,7 +391,7 @@ function Private.fill_headers(headers, parsed)
391 lower["user-agent"] = Public.USERAGENT 391 lower["user-agent"] = Public.USERAGENT
392 -- override with user values 392 -- override with user values
393 for i,v in headers do 393 for i,v in headers do
394 lower[strlower(i)] = v 394 lower[string.lower(i)] = v
395 end 395 end
396 lower["host"] = parsed.host 396 lower["host"] = parsed.host
397 -- this cannot be overriden 397 -- this cannot be overriden
@@ -554,7 +554,7 @@ function Public.request_cb(request, response)
554 request.headers = Private.fill_headers(request.headers, parsed) 554 request.headers = Private.fill_headers(request.headers, parsed)
555 -- try to connect to server 555 -- try to connect to server
556 local sock 556 local sock
557 sock, response.error = connect(parsed.host, parsed.port) 557 sock, response.error = socket.connect(parsed.host, parsed.port)
558 if not sock then return response end 558 if not sock then return response end
559 -- set connection timeout so that we do not hang forever 559 -- set connection timeout so that we do not hang forever
560 sock:timeout(Public.TIMEOUT) 560 sock:timeout(Public.TIMEOUT)
@@ -619,7 +619,7 @@ function Public.request(request)
619 local response = {} 619 local response = {}
620 if request.body then 620 if request.body then
621 request.body_cb = function() 621 request.body_cb = function()
622 return request.body, strlen(request.body) 622 return request.body, string.len(request.body)
623 end 623 end
624 end 624 end
625 local cat = Concat.create() 625 local cat = Concat.create()
diff --git a/src/inet.c b/src/inet.c
index 5003ed9..3e89e88 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -38,9 +38,9 @@ static int inet_aton(cchar *cp, struct in_addr *inp);
38void inet_open(lua_State *L) 38void inet_open(lua_State *L)
39{ 39{
40 lua_pushcfunction(L, inet_lua_toip); 40 lua_pushcfunction(L, inet_lua_toip);
41 lua_setglobal(L, "toip"); 41 priv_newglobal(L, "toip");
42 lua_pushcfunction(L, inet_lua_tohostname); 42 lua_pushcfunction(L, inet_lua_tohostname);
43 lua_setglobal(L, "tohostname"); 43 priv_newglobal(L, "tohostname");
44 priv_newglobalmethod(L, "getsockname"); 44 priv_newglobalmethod(L, "getsockname");
45 priv_newglobalmethod(L, "getpeername"); 45 priv_newglobalmethod(L, "getpeername");
46} 46}
@@ -145,7 +145,7 @@ static int inet_lua_getpeername(lua_State *L)
145{ 145{
146 p_sock sock = (p_sock) lua_touserdata(L, 1); 146 p_sock sock = (p_sock) lua_touserdata(L, 1);
147 struct sockaddr_in peer; 147 struct sockaddr_in peer;
148 size_t peer_len = sizeof(peer); 148 int peer_len = sizeof(peer);
149 if (getpeername(sock->fd, (SA *) &peer, &peer_len) < 0) { 149 if (getpeername(sock->fd, (SA *) &peer, &peer_len) < 0) {
150 lua_pushnil(L); 150 lua_pushnil(L);
151 return 1; 151 return 1;
@@ -167,7 +167,7 @@ static int inet_lua_getsockname(lua_State *L)
167{ 167{
168 p_sock sock = (p_sock) lua_touserdata(L, 1); 168 p_sock sock = (p_sock) lua_touserdata(L, 1);
169 struct sockaddr_in local; 169 struct sockaddr_in local;
170 size_t local_len = sizeof(local); 170 int local_len = sizeof(local);
171 if (getsockname(sock->fd, (SA *) &local, &local_len) < 0) { 171 if (getsockname(sock->fd, (SA *) &local, &local_len) < 0) {
172 lua_pushnil(L); 172 lua_pushnil(L);
173 return 1; 173 return 1;
diff --git a/src/luasocket.c b/src/luasocket.c
index d329d4e..26bc014 100644
--- a/src/luasocket.c
+++ b/src/luasocket.c
@@ -56,5 +56,13 @@ LUASOCKET_API int lua_socketlibopen(lua_State *L)
56 buf_open(L); 56 buf_open(L);
57 tcps_open(L); 57 tcps_open(L);
58 udp_open(L); 58 udp_open(L);
59 return 1; 59#if LUASOCKET_DEBUG
60 lua_dofile(L, "concat.lua");
61 lua_dofile(L, "code.lua");
62 lua_dofile(L, "url.lua");
63 lua_dofile(L, "http.lua");
64 lua_dofile(L, "smtp.lua");
65 lua_dofile(L, "ftp.lua");
66#endif
67 return 0;
60} 68}
diff --git a/src/luasocket.h b/src/luasocket.h
index 95720e2..fd22606 100644
--- a/src/luasocket.h
+++ b/src/luasocket.h
@@ -11,7 +11,12 @@
11/*-------------------------------------------------------------------------*\ 11/*-------------------------------------------------------------------------*\
12* Current luasocket version 12* Current luasocket version
13\*-------------------------------------------------------------------------*/ 13\*-------------------------------------------------------------------------*/
14#define LUASOCKET_VERSION "LuaSocket 1.5" 14#define LUASOCKET_VERSION "LuaSocket 1.5 (alpha)"
15
16/*-------------------------------------------------------------------------*\
17* Library's namespace
18\*-------------------------------------------------------------------------*/
19#define LUASOCKET_LIBNAME "socket"
15 20
16/*-------------------------------------------------------------------------*\ 21/*-------------------------------------------------------------------------*\
17* This macro prefixes all exported API functions 22* This macro prefixes all exported API functions
diff --git a/src/mbox.lua b/src/mbox.lua
index 9cce9ff..2969111 100644
--- a/src/mbox.lua
+++ b/src/mbox.lua
@@ -1,45 +1,88 @@
1local Public = {} 1local Public = {}
2 2
3parse = Public 3mbox = Public
4 4
5function Public.headers(headers_s) 5function Public.split_message(message_s)
6 local message = {}
7 message_s = gsub(message_s, "\r\n", "\n")
8 gsub(message_s, "^(.-\n)\n", function (h) %message.headers = h end)
9 gsub(message_s, "^.-\n\n(.*)", function (b) %message.body = b end)
10 if not message.body then
11 gsub(message_s, "^\n(.*)", function (b) %message.body = b end)
12 end
13 if not message.headers and not message.body then
14 message.headers = message_s
15 end
16 return message.headers or "", message.body or ""
17end
18
19function Public.split_headers(headers_s)
20 local headers = {}
21 headers_s = gsub(headers_s, "\r\n", "\n")
22 headers_s = gsub(headers_s, "\n[ ]+", " ")
23 gsub("\n" .. headers_s, "\n([^\n]+)", function (h) tinsert(%headers, h) end)
24 return headers
25end
26
27function Public.parse_header(header_s)
28 header_s = gsub(header_s, "\n[ ]+", " ")
29 header_s = gsub(header_s, "\n+", "")
30 local _, __, name, value = strfind(header_s, "([^%s:]-):%s*(.*)")
31 return name, value
32end
33
34function Public.parse_headers(headers_s)
35 local headers_t = %Public.split_headers(headers_s)
6 local headers = {} 36 local headers = {}
7 headers_s = "\n" .. headers_s .. "$$$:\n" 37 for i = 1, getn(headers_t) do
8 local i, j = 1, 1 38 local name, value = %Public.parse_header(headers_t[i])
9 local name, value, _ 39 if name then
10 while 1 do 40 name = strlower(name)
11 j = strfind(headers_s, "\n%S-:", i+1) 41 if headers[name] then
12 if not j then break end 42 headers[name] = headers[name] .. ", " .. value
13 _,_, name, value = strfind(strsub(headers_s, i+1, j-1), "(%S-):%s?(.*)") 43 else headers[name] = value end
14 value = gsub(value or "", "\r\n", "\n") 44 end
15 value = gsub(value, "\n%s*", " ")
16 name = strlower(name)
17 if headers[name] then headers[name] = headers[name] .. ", " .. value
18 else headers[name] = value end
19 i, j = j, i
20 end 45 end
21 headers["$$$"] = nil
22 return headers 46 return headers
23end 47end
24 48
25function Public.message(message_s) 49function Public.parse_from(from)
26 message_s = gsub(message_s, "^.-\n", "") 50 local _, __, name, address = strfind(from, "^%s*(.-)%s*%<(.-)%>")
27 local _, headers_s, body 51 if not address then
28 _, _, headers_s, body = strfind(message_s, "^(.-\n)\n(.*)") 52 _, __, address = strfind(from, "%s*(.+)%s*")
29 headers_s = headers_s or ""
30 body = body or ""
31 return { headers = %Public.headers(headers_s), body = body }
32end
33
34function Public.mbox(mbox_s)
35 local mbox = {}
36 mbox_s = "\n" .. mbox_s .. "\nFrom "
37 local i, j = 1, 1
38 while 1 do
39 j = strfind(mbox_s, "\nFrom ", i + 1)
40 if not j then break end
41 tinsert(mbox, %Public.message(strsub(mbox_s, i + 1, j - 1)))
42 i, j = j, i
43 end 53 end
44 return mbox 54 name = name or ""
55 address = address or ""
56 if name == "" then name = address end
57 name = gsub(name, '"', "")
58 return name, address
59end
60
61function Public.split_mbox(mbox_s)
62 mbox = {}
63 mbox_s = gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n"
64 local nj, i, j = 1, 1, 1
65 while 1 do
66 i, nj = strfind(mbox_s, "\n\nFrom .-\n", j)
67 if not i then break end
68 local message = strsub(mbox_s, j, i-1)
69 tinsert(mbox, message)
70 j = nj+1
71 end
72 return mbox
73end
74
75function Public.parse_mbox(mbox_s)
76 local mbox = %Public.split_mbox(mbox_s)
77 for i = 1, getn(mbox) do
78 mbox[i] = %Public.parse_message(mbox[i])
79 end
80 return mbox
81end
82
83function Public.parse_message(message_s)
84 local message = {}
85 message.headers, message.body = %Public.split_message(message_s)
86 message.headers = %Public.parse_headers(message.headers)
87 return message
45end 88end
diff --git a/src/select.c b/src/select.c
index 9a24dbb..1aaa7fe 100644
--- a/src/select.c
+++ b/src/select.c
@@ -31,9 +31,12 @@ void select_open(lua_State *L)
31{ 31{
32 /* push select auxiliar lua function and register 32 /* push select auxiliar lua function and register
33 * select_lua_select with it as an upvalue */ 33 * select_lua_select with it as an upvalue */
34#include "lsselect.loh" 34#ifdef LUASOCKET_DEBUG
35#endif
36 luaL_loadfile(L, "lsselect.lua");
37 lua_call(L, 0, 1);
35 lua_pushcclosure(L, select_lua_select, 1); 38 lua_pushcclosure(L, select_lua_select, 1);
36 lua_setglobal(L, "select"); 39 priv_newglobal(L, "select");
37 /* create luasocket(select) table */ 40 /* create luasocket(select) table */
38 lua_pushstring(L, "luasocket(select)"); 41 lua_pushstring(L, "luasocket(select)");
39 lua_newtable(L); 42 lua_newtable(L);
@@ -61,8 +64,8 @@ static int select_lua_select(lua_State *L)
61 /* make sure we have enough arguments (nil is the default) */ 64 /* make sure we have enough arguments (nil is the default) */
62 lua_settop(L, 4); 65 lua_settop(L, 4);
63 /* pass FD_SET and manipulation functions */ 66 /* pass FD_SET and manipulation functions */
64 lua_newuserdatabox(L, &read); 67 lua_boxpointer(L, &read);
65 lua_newuserdatabox(L, &write); 68 lua_boxpointer(L, &write);
66 lua_pushcfunction(L, local_FD_SET); 69 lua_pushcfunction(L, local_FD_SET);
67 lua_pushcfunction(L, local_FD_ISSET); 70 lua_pushcfunction(L, local_FD_ISSET);
68 /* pass getfd function with selectable table as upvalue */ 71 /* pass getfd function with selectable table as upvalue */
@@ -121,7 +124,7 @@ static int local_select(lua_State *L)
121static int local_FD_SET(lua_State *L) 124static int local_FD_SET(lua_State *L)
122{ 125{
123 COMPAT_FD fd = (COMPAT_FD) lua_tonumber(L, 1); 126 COMPAT_FD fd = (COMPAT_FD) lua_tonumber(L, 1);
124 fd_set *set = (fd_set *) lua_touserdata(L, 2); 127 fd_set *set = (fd_set *) lua_topointer(L, 2);
125 if (fd >= 0) FD_SET(fd, set); 128 if (fd >= 0) FD_SET(fd, set);
126 return 0; 129 return 0;
127} 130}
@@ -129,7 +132,7 @@ static int local_FD_SET(lua_State *L)
129static int local_FD_ISSET(lua_State *L) 132static int local_FD_ISSET(lua_State *L)
130{ 133{
131 COMPAT_FD fd = (COMPAT_FD) lua_tonumber(L, 1); 134 COMPAT_FD fd = (COMPAT_FD) lua_tonumber(L, 1);
132 fd_set *set = (fd_set *) lua_touserdata(L, 2); 135 fd_set *set = (fd_set *) lua_topointer(L, 2);
133 if (fd >= 0 && FD_ISSET(fd, set)) lua_pushnumber(L, 1); 136 if (fd >= 0 && FD_ISSET(fd, set)) lua_pushnumber(L, 1);
134 else lua_pushnil(L); 137 else lua_pushnil(L);
135 return 1; 138 return 1;
diff --git a/src/smtp.lua b/src/smtp.lua
index 72a0e5a..774dddb 100644
--- a/src/smtp.lua
+++ b/src/smtp.lua
@@ -19,7 +19,7 @@ Public.TIMEOUT = 180
19Public.PORT = 25 19Public.PORT = 25
20-- domain used in HELO command and default sendmail 20-- domain used in HELO command and default sendmail
21-- If we are under a CGI, try to get from environment 21-- If we are under a CGI, try to get from environment
22Public.DOMAIN = getenv("SERVER_NAME") or "localhost" 22Public.DOMAIN = os.getenv("SERVER_NAME") or "localhost"
23-- default server used to send e-mails 23-- default server used to send e-mails
24Public.SERVER = "localhost" 24Public.SERVER = "localhost"
25 25
diff --git a/src/timeout.c b/src/timeout.c
index 266a86e..fdbc47a 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -125,9 +125,9 @@ void tm_open(lua_State *L)
125 (void) L; 125 (void) L;
126#ifdef _DEBUG 126#ifdef _DEBUG
127 lua_pushcfunction(L, tm_lua_time); 127 lua_pushcfunction(L, tm_lua_time);
128 lua_setglobal(L, "_time"); 128 priv_newglobal(L, "_time");
129 lua_pushcfunction(L, tm_lua_sleep); 129 lua_pushcfunction(L, tm_lua_sleep);
130 lua_setglobal(L, "_sleep"); 130 priv_newglobal(L, "_sleep");
131#endif 131#endif
132} 132}
133 133
diff --git a/src/udp.c b/src/udp.c
index 0dc0df8..29004fd 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -45,7 +45,7 @@ void udp_open(lua_State *L)
45 udp_inherit(L, UDP_CLASS); 45 udp_inherit(L, UDP_CLASS);
46 /* declare global functions */ 46 /* declare global functions */
47 lua_pushcfunction(L, udp_global_udpsocket); 47 lua_pushcfunction(L, udp_global_udpsocket);
48 lua_setglobal(L, "udpsocket"); 48 priv_newglobal(L, "udp");
49 for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++) 49 for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++)
50 priv_newglobalmethod(L, funcs[i].name); 50 priv_newglobalmethod(L, funcs[i].name);
51 /* make class selectable */ 51 /* make class selectable */
@@ -162,12 +162,12 @@ static int udp_lua_receivefrom(lua_State *L)
162 p_udp udp = (p_udp) lua_touserdata(L, 1); 162 p_udp udp = (p_udp) lua_touserdata(L, 1);
163 p_tm tm = &udp->base_tm; 163 p_tm tm = &udp->base_tm;
164 struct sockaddr_in peer; 164 struct sockaddr_in peer;
165 size_t peer_len = sizeof(peer); 165 int peer_len = sizeof(peer);
166 unsigned char buffer[UDP_DATAGRAMSIZE]; 166 unsigned char buffer[UDP_DATAGRAMSIZE];
167 size_t wanted = (size_t) luaL_opt_number(L, 2, sizeof(buffer)); 167 size_t wanted = (size_t) luaL_opt_number(L, 2, sizeof(buffer));
168 size_t got; 168 size_t got;
169 int err; 169 int err;
170 if (udp->udp_connected) lua_error(L, "receivefrom on connected socket"); 170 if (udp->udp_connected) luaL_error(L, "receivefrom on connected socket");
171 tm_markstart(tm); 171 tm_markstart(tm);
172 wanted = MIN(wanted, sizeof(buffer)); 172 wanted = MIN(wanted, sizeof(buffer));
173 err = compat_recvfrom(udp->fd, buffer, wanted, &got, tm_getremaining(tm), 173 err = compat_recvfrom(udp->fd, buffer, wanted, &got, tm_getremaining(tm),
@@ -201,7 +201,7 @@ static int udp_lua_send(lua_State *L)
201 size_t wanted, sent = 0; 201 size_t wanted, sent = 0;
202 int err; 202 int err;
203 cchar *data = luaL_check_lstr(L, 2, &wanted); 203 cchar *data = luaL_check_lstr(L, 2, &wanted);
204 if (!udp->udp_connected) lua_error(L, "send on unconnected socket"); 204 if (!udp->udp_connected) luaL_error(L, "send on unconnected socket");
205 tm_markstart(tm); 205 tm_markstart(tm);
206 err = compat_send(udp->fd, data, wanted, &sent, tm_getremaining(tm)); 206 err = compat_send(udp->fd, data, wanted, &sent, tm_getremaining(tm));
207 priv_pusherror(L, err == PRIV_CLOSED ? PRIV_REFUSED : err); 207 priv_pusherror(L, err == PRIV_CLOSED ? PRIV_REFUSED : err);
@@ -230,9 +230,9 @@ static int udp_lua_sendto(lua_State *L)
230 p_tm tm = &udp->base_tm; 230 p_tm tm = &udp->base_tm;
231 struct sockaddr_in peer; 231 struct sockaddr_in peer;
232 int err; 232 int err;
233 if (udp->udp_connected) lua_error(L, "sendto on connected socket"); 233 if (udp->udp_connected) luaL_error(L, "sendto on connected socket");
234 memset(&peer, 0, sizeof(peer)); 234 memset(&peer, 0, sizeof(peer));
235 if (!inet_aton(ip, &peer.sin_addr)) lua_error(L, "invalid ip address"); 235 if (!inet_aton(ip, &peer.sin_addr)) luaL_error(L, "invalid ip address");
236 peer.sin_family = AF_INET; 236 peer.sin_family = AF_INET;
237 peer.sin_port = htons(port); 237 peer.sin_port = htons(port);
238 tm_markstart(tm); 238 tm_markstart(tm);
diff --git a/src/unix.c b/src/unix.c
index d50d98c..0fc08bd 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -28,7 +28,7 @@ void compat_open(lua_State *L)
28} 28}
29 29
30COMPAT_FD compat_accept(COMPAT_FD s, struct sockaddr *addr, 30COMPAT_FD compat_accept(COMPAT_FD s, struct sockaddr *addr,
31 socklen_t *len, int deadline) 31 int *len, int deadline)
32{ 32{
33 struct timeval tv; 33 struct timeval tv;
34 fd_set fds; 34 fd_set fds;
@@ -74,7 +74,7 @@ int compat_send(COMPAT_FD c, cchar *data, size_t count, size_t *sent,
74} 74}
75 75
76int compat_sendto(COMPAT_FD c, cchar *data, size_t count, size_t *sent, 76int compat_sendto(COMPAT_FD c, cchar *data, size_t count, size_t *sent,
77 int deadline, SA *addr, socklen_t len) 77 int deadline, SA *addr, int len)
78{ 78{
79 struct timeval tv; 79 struct timeval tv;
80 fd_set fds; 80 fd_set fds;
@@ -134,7 +134,7 @@ int compat_recv(COMPAT_FD c, uchar *data, size_t count, size_t *got,
134} 134}
135 135
136int compat_recvfrom(COMPAT_FD c, uchar *data, size_t count, size_t *got, 136int compat_recvfrom(COMPAT_FD c, uchar *data, size_t count, size_t *got,
137 int deadline, SA *addr, socklen_t *len) 137 int deadline, SA *addr, int *len)
138{ 138{
139 struct timeval tv; 139 struct timeval tv;
140 fd_set fds; 140 fd_set fds;
@@ -290,7 +290,7 @@ cchar *compat_trysetoptions(lua_State *L, COMPAT_FD sock)
290static cchar *try_setbooloption(lua_State *L, COMPAT_FD sock, int name) 290static cchar *try_setbooloption(lua_State *L, COMPAT_FD sock, int name)
291{ 291{
292 int bool, res; 292 int bool, res;
293 if (!lua_isnumber(L, -1)) lua_error(L, "invalid option value"); 293 if (!lua_isnumber(L, -1)) luaL_error(L, "invalid option value");
294 bool = (int) lua_tonumber(L, -1); 294 bool = (int) lua_tonumber(L, -1);
295 res = setsockopt(sock, SOL_SOCKET, name, (char *) &bool, sizeof(bool)); 295 res = setsockopt(sock, SOL_SOCKET, name, (char *) &bool, sizeof(bool));
296 if (res < 0) return "error setting option"; 296 if (res < 0) return "error setting option";
diff --git a/src/unix.h b/src/unix.h
index e317b06..944b471 100644
--- a/src/unix.h
+++ b/src/unix.h
@@ -46,15 +46,15 @@ void compat_open(lua_State *L);
46#define compat_select select 46#define compat_select select
47 47
48COMPAT_FD compat_socket(int domain, int type, int protocol); 48COMPAT_FD compat_socket(int domain, int type, int protocol);
49COMPAT_FD compat_accept(COMPAT_FD s, SA *addr, socklen_t *len, int deadline); 49COMPAT_FD compat_accept(COMPAT_FD s, SA *addr, int *len, int deadline);
50int compat_send(COMPAT_FD c, cchar *data, size_t count, size_t *done, 50int compat_send(COMPAT_FD c, cchar *data, size_t count, size_t *done,
51 int deadline); 51 int deadline);
52int compat_recv(COMPAT_FD c, uchar *data, size_t count, size_t *done, 52int compat_recv(COMPAT_FD c, uchar *data, size_t count, size_t *done,
53 int deadline); 53 int deadline);
54int compat_sendto(COMPAT_FD c, cchar *data, size_t count, size_t *done, 54int compat_sendto(COMPAT_FD c, cchar *data, size_t count, size_t *done,
55 int deadline, SA *addr, socklen_t len); 55 int deadline, SA *addr, int len);
56int compat_recvfrom(COMPAT_FD c, uchar *data, size_t count, size_t *got, 56int compat_recvfrom(COMPAT_FD c, uchar *data, size_t count, size_t *got,
57 int deadline, SA *addr, socklen_t *len); 57 int deadline, SA *addr, int *len);
58void compat_setnonblocking(COMPAT_FD sock); 58void compat_setnonblocking(COMPAT_FD sock);
59void compat_setblocking(COMPAT_FD sock); 59void compat_setblocking(COMPAT_FD sock);
60void compat_setreuseaddr(COMPAT_FD sock); 60void compat_setreuseaddr(COMPAT_FD sock);
diff --git a/src/url.lua b/src/url.lua
index e17bcf5..0ecec3c 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -34,26 +34,30 @@ function Public.parse_url(url, default)
34 -- empty url is parsed to nil 34 -- empty url is parsed to nil
35 if not url or url == "" then return nil end 35 if not url or url == "" then return nil end
36 -- remove whitespace 36 -- remove whitespace
37 url = gsub(url, "%s", "") 37 url = string.gsub(url, "%s", "")
38 -- get fragment 38 -- get fragment
39 url = gsub(url, "#(.*)$", function(f) parsed.fragment = f end) 39 url = string.gsub(url, "#(.*)$", function(f) parsed.fragment = f end)
40 -- get scheme 40 -- get scheme
41 url = gsub(url, "^([%w][%w%+%-%.]*)%:", function(s) parsed.scheme = s end) 41 url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
42 function(s) parsed.scheme = s end)
42 -- get authority 43 -- get authority
43 url = gsub(url, "^//([^/]*)", function(n) parsed.authority = n end) 44 url = string.gsub(url, "^//([^/]*)", function(n) parsed.authority = n end)
44 -- get query string 45 -- get query stringing
45 url = gsub(url, "%?(.*)", function(q) parsed.query = q end) 46 url = string.gsub(url, "%?(.*)", function(q) parsed.query = q end)
46 -- get params 47 -- get params
47 url = gsub(url, "%;(.*)", function(p) parsed.params = p end) 48 url = string.gsub(url, "%;(.*)", function(p) parsed.params = p end)
48 if url ~= "" then parsed.path = url end 49 if url ~= "" then parsed.path = url end
49 local authority = parsed.authority 50 local authority = parsed.authority
50 if not authority then return parsed end 51 if not authority then return parsed end
51 authority = gsub(authority,"^([^@]*)@",function(u) parsed.userinfo = u end) 52 authority = string.gsub(authority,"^([^@]*)@",
52 authority = gsub(authority, ":([^:]*)$", function(p) parsed.port = p end) 53 function(u) parsed.userinfo = u end)
54 authority = string.gsub(authority, ":([^:]*)$",
55 function(p) parsed.port = p end)
53 if authority ~= "" then parsed.host = authority end 56 if authority ~= "" then parsed.host = authority end
54 local userinfo = parsed.userinfo 57 local userinfo = parsed.userinfo
55 if not userinfo then return parsed end 58 if not userinfo then return parsed end
56 userinfo = gsub(userinfo, ":([^:]*)$", function(p) parsed.password = p end) 59 userinfo = string.gsub(userinfo, ":([^:]*)$",
60 function(p) parsed.password = p end)
57 parsed.user = userinfo 61 parsed.user = userinfo
58 return parsed 62 return parsed
59end 63end
@@ -64,7 +68,7 @@ end
64-- Input 68-- Input
65-- parsed: parsed URL, as returned by Public.parse 69-- parsed: parsed URL, as returned by Public.parse
66-- Returns 70-- Returns
67-- a string with the corresponding URL 71-- a stringing with the corresponding URL
68----------------------------------------------------------------------------- 72-----------------------------------------------------------------------------
69function Public.build_url(parsed) 73function Public.build_url(parsed)
70 local url = parsed.path or "" 74 local url = parsed.path or ""
@@ -86,7 +90,7 @@ function Public.build_url(parsed)
86 if authority then url = "//" .. authority .. url end 90 if authority then url = "//" .. authority .. url end
87 if parsed.scheme then url = parsed.scheme .. ":" .. url end 91 if parsed.scheme then url = parsed.scheme .. ":" .. url end
88 if parsed.fragment then url = url .. "#" .. parsed.fragment end 92 if parsed.fragment then url = url .. "#" .. parsed.fragment end
89 url = gsub(url, "%s", "") 93 url = string.gsub(url, "%s", "")
90 return url 94 return url
91end 95end
92 96
@@ -134,13 +138,13 @@ end
134function Public.parse_path(path) 138function Public.parse_path(path)
135 local parsed = {} 139 local parsed = {}
136 path = path or "" 140 path = path or ""
137 path = gsub(path, "%s", "") 141 path = string.gsub(path, "%s", "")
138 gsub(path, "([^/]+)", function (s) tinsert(parsed, s) end) 142 string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end)
139 for i = 1, getn(parsed) do 143 for i = 1, table.getn(parsed) do
140 parsed[i] = Code.unescape(parsed[i]) 144 parsed[i] = Code.unescape(parsed[i])
141 end 145 end
142 if strsub(path, 1, 1) == "/" then parsed.is_absolute = 1 end 146 if stringsub(path, 1, 1) == "/" then parsed.is_absolute = 1 end
143 if strsub(path, -1, -1) == "/" then parsed.is_directory = 1 end 147 if stringsub(path, -1, -1) == "/" then parsed.is_directory = 1 end
144 return parsed 148 return parsed
145end 149end
146 150
@@ -150,11 +154,11 @@ end
150-- parsed: path segments 154-- parsed: path segments
151-- unsafe: if true, segments are not protected before path is built 155-- unsafe: if true, segments are not protected before path is built
152-- Returns 156-- Returns
153-- path: correspondin path string 157-- path: correspondin path stringing
154----------------------------------------------------------------------------- 158-----------------------------------------------------------------------------
155function Public.build_path(parsed, unsafe) 159function Public.build_path(parsed, unsafe)
156 local path = "" 160 local path = ""
157 local n = getn(parsed) 161 local n = table.getn(parsed)
158 if unsafe then 162 if unsafe then
159 for i = 1, n-1 do 163 for i = 1, n-1 do
160 path = path .. parsed[i] 164 path = path .. parsed[i]
@@ -178,10 +182,10 @@ function Public.build_path(parsed, unsafe)
178 return path 182 return path
179end 183end
180 184
181function Private.make_set(table) 185function Private.make_set(t)
182 local s = {} 186 local s = {}
183 for i = 1, getn(table) do 187 for i = 1, table.getn(t) do
184 s[table[i]] = 1 188 s[t[i]] = 1
185 end 189 end
186 return s 190 return s
187end 191end
@@ -195,7 +199,7 @@ Private.segment_set = Private.make_set {
195 199
196function Private.protect_segment(s) 200function Private.protect_segment(s)
197 local segment_set = Private.segment_set 201 local segment_set = Private.segment_set
198 return gsub(s, "(%W)", function (c) 202 return string.gsub(s, "(%W)", function (c)
199 if segment_set[c] then return c 203 if segment_set[c] then return c
200 else return Code.escape(c) end 204 else return Code.escape(c) end
201 end) 205 end)
@@ -210,21 +214,21 @@ end
210-- corresponding absolute path 214-- corresponding absolute path
211----------------------------------------------------------------------------- 215-----------------------------------------------------------------------------
212function Private.absolute_path(base_path, relative_path) 216function Private.absolute_path(base_path, relative_path)
213 if strsub(relative_path, 1, 1) == "/" then return relative_path end 217 if stringsub(relative_path, 1, 1) == "/" then return relative_path end
214 local path = gsub(base_path, "[^/]*$", "") 218 local path = string.gsub(base_path, "[^/]*$", "")
215 path = path .. relative_path 219 path = path .. relative_path
216 path = gsub(path, "([^/]*%./)", function (s) 220 path = string.gsub(path, "([^/]*%./)", function (s)
217 if s ~= "./" then return s else return "" end 221 if s ~= "./" then return s else return "" end
218 end) 222 end)
219 path = gsub(path, "/%.$", "/") 223 path = string.gsub(path, "/%.$", "/")
220 local reduced 224 local reduced
221 while reduced ~= path do 225 while reduced ~= path do
222 reduced = path 226 reduced = path
223 path = gsub(reduced, "([^/]*/%.%./)", function (s) 227 path = string.gsub(reduced, "([^/]*/%.%./)", function (s)
224 if s ~= "../../" then return "" else return s end 228 if s ~= "../../" then return "" else return s end
225 end) 229 end)
226 end 230 end
227 path = gsub(reduced, "([^/]*/%.%.)$", function (s) 231 path = string.gsub(reduced, "([^/]*/%.%.)$", function (s)
228 if s ~= "../.." then return "" else return s end 232 if s ~= "../.." then return "" else return s end
229 end) 233 end)
230 return path 234 return path