aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-09-29 06:11:42 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-09-29 06:11:42 +0000
commita32c6d91402329adb769c4fe8bbb24f0d4e99051 (patch)
tree8b3f574b479e84bd9fe086db49cbab551eac75c8
parent773e35ced30fa2c03ddb2a332bf8a9aebb56aa44 (diff)
downloadluasocket-a32c6d91402329adb769c4fe8bbb24f0d4e99051.tar.gz
luasocket-a32c6d91402329adb769c4fe8bbb24f0d4e99051.tar.bz2
luasocket-a32c6d91402329adb769c4fe8bbb24f0d4e99051.zip
Almost ready to release.
-rw-r--r--TODO12
-rw-r--r--config2
-rw-r--r--doc/installation.html54
-rw-r--r--etc/check-links-nb.lua277
-rw-r--r--etc/check-links.lua2
-rw-r--r--etc/check-memory.lua4
-rw-r--r--etc/get.lua2
-rw-r--r--src/auxiliar.h4
-rw-r--r--src/buffer.c4
-rw-r--r--src/buffer.h2
-rw-r--r--src/except.c5
-rw-r--r--src/except.h2
-rw-r--r--src/inet.c4
-rw-r--r--src/inet.h2
-rw-r--r--src/io.c1
-rw-r--r--src/io.h5
-rw-r--r--src/luasocket.c10
-rw-r--r--src/luasocket.h4
-rw-r--r--src/makefile7
-rw-r--r--src/mime.c11
-rw-r--r--src/mime.h11
-rw-r--r--src/options.c3
-rw-r--r--src/options.h2
-rw-r--r--src/select.c4
-rw-r--r--src/socket.h1
-rw-r--r--src/tcp.c4
-rw-r--r--src/tcp.h2
-rw-r--r--src/timeout.c4
-rw-r--r--src/timeout.h2
-rw-r--r--src/udp.c4
-rw-r--r--src/udp.h2
-rw-r--r--src/unix.c4
-rw-r--r--src/unix.h2
-rw-r--r--src/url.lua5
-rw-r--r--src/usocket.c11
-rw-r--r--src/wsocket.c16
-rw-r--r--test/testclnt.lua3
-rw-r--r--test/urltest.lua4
38 files changed, 115 insertions, 383 deletions
diff --git a/TODO b/TODO
index bd60aaa..1d7a1db 100644
--- a/TODO
+++ b/TODO
@@ -1,3 +1,7 @@
1
2get rid of setmetatable(, nil) since packages don't need this anymore in
35.1
4
1new instalation scheme??? 5new instalation scheme???
2 6
3test empty socket.select no windows. 7test empty socket.select no windows.
@@ -15,14 +19,7 @@ change ltn13 to make sure drawbacks are obvious
15use mike's "don't set to blocking before closing unless needed" patch? 19use mike's "don't set to blocking before closing unless needed" patch?
16take a look at DB's smtp patch (add "extra argument" table) 20take a look at DB's smtp patch (add "extra argument" table)
17 21
18optmize aux_getgroupudata (Mike idea)
19 make aux_newclass receive upvalues
20 use one upvalue per string name of class/group
21 make aux_checkgroup by upvalue (faster)
22
23add error message stuff to the manual
24 22
25make sure all modules that can use it actually use socket.newtry
26adicionar exemplos de expansão: pipe, local, named pipe 23adicionar exemplos de expansão: pipe, local, named pipe
27testar os options! 24testar os options!
28- Thread-safe 25- Thread-safe
@@ -30,6 +27,7 @@ testar os options!
30 - proteger ou atomizar o conjunto (timedout, receive), (timedout, send) 27 - proteger ou atomizar o conjunto (timedout, receive), (timedout, send)
31 - inet_ntoa também é uma merda. 28 - inet_ntoa também é uma merda.
32 29
30* protect doesn't catch errors by error and assert
33* BUG NO SET DO TINYIRC!!! SINISTRO. 31* BUG NO SET DO TINYIRC!!! SINISTRO.
34* _VERSION, _DEBUG, etc. 32* _VERSION, _DEBUG, etc.
35* talk about new create field in HTTP, FTP and SMTP 33* talk about new create field in HTTP, FTP and SMTP
diff --git a/config b/config
index a85e8e6..55a9a63 100644
--- a/config
+++ b/config
@@ -27,8 +27,6 @@ COMPAT=compat-5.1r4
27# Top of your Lua installation 27# Top of your Lua installation
28# Relative paths will be inside the src tree 28# Relative paths will be inside the src tree
29# 29#
30#INSTALL_TOP_LUA=/usr/local/share/lua/5.0
31#INSTALL_TOP_LIB=/usr/local/lib/lua/5.0
32INSTALL_TOP_LUA=share 30INSTALL_TOP_LUA=share
33INSTALL_TOP_LIB=lib 31INSTALL_TOP_LIB=lib
34 32
diff --git a/doc/installation.html b/doc/installation.html
index 62604ed..d30e919 100644
--- a/doc/installation.html
+++ b/doc/installation.html
@@ -63,24 +63,28 @@ the package scheme will likely already have been answered.
63 63
64<h3>Directory structure</h3> 64<h3>Directory structure</h3>
65 65
66<p> The standard distribution reserves a directory to be the root of 66<p> On Unix systems, the standard distribution uses two base
67the libraries installed 67directories, one for system dependent files, and another for system
68on a given system. Let's call this directory <tt>&lt;ROOT&gt;</tt>. 68independent files. Let's call these directories <tt>&lt;LIB&gt;</tt>
69On my system, this is the <tt>/usr/local/share/lua/5.0</tt> directory. 69and <tt>&lt;SHARE&gt;</tt>, respectively.
70Here is the standard LuaSocket distribution directory structure:</p> 70For instance, in my laptop, I use '<tt>/usr/local/lib/lua/5.0</tt>' for
71<tt>&lt;LIB&gt;</tt> and '<tt>/usr/local/share/lua/5.0</tt>' for
72<tt>&lt;SHARE&gt;</tt>. On Windows, only one directory is used, say
73'<tt>c:\program files\lua\5.0</tt>'. Here is the standard LuaSocket
74distribution directory structure:</p>
71 75
72<pre class=example> 76<pre class=example>
73&lt;ROOT&gt;/compat-5.1.lua 77&lt;SHARE&gt;/compat-5.1.lua
74&lt;ROOT&gt;/ltn12.lua 78&lt;SHARE&gt;/ltn12.lua
75&lt;ROOT&gt;/mime/init.lua 79&lt;SHARE&gt;/mime/init.lua
76&lt;ROOT&gt;/mime/core.dll 80&lt;LIB&gt;/mime/core.dll
77&lt;ROOT&gt;/socket/init.lua 81&lt;SHARE&gt;/socket/init.lua
78&lt;ROOT&gt;/socket/core.dll 82&lt;LIB&gt;/socket/core.dll
79&lt;ROOT&gt;/socket/http.lua 83&lt;SHARE&gt;/socket/http.lua
80&lt;ROOT&gt;/socket/tp.lua 84&lt;SHARE&gt;/socket/tp.lua
81&lt;ROOT&gt;/socket/ftp.lua 85&lt;SHARE&gt;/socket/ftp.lua
82&lt;ROOT&gt;/socket/smtp.lua 86&lt;SHARE&gt;/socket/smtp.lua
83&lt;ROOT&gt;/socket/url.lua 87&lt;SHARE&gt;/socket/url.lua
84</pre> 88</pre>
85 89
86<p> Naturally, on Unix systems, <tt>core.dll</tt> 90<p> Naturally, on Unix systems, <tt>core.dll</tt>
@@ -91,7 +95,7 @@ environment variables need to be set. The first environment variable tells
91the interpreter to load the <tt>compat-5.1.lua</tt> module at startup: </p> 95the interpreter to load the <tt>compat-5.1.lua</tt> module at startup: </p>
92 96
93<pre class=example> 97<pre class=example>
94LUA_INIT=@&lt;ROOT&gt;/compat-5.1.lua 98LUA_INIT=@&lt;SHARE&gt;/compat-5.1.lua
95</pre> 99</pre>
96 100
97<p> 101<p>
@@ -101,13 +105,12 @@ directories and with the appropriate filename extensions.
101</p> 105</p>
102 106
103<pre class=example> 107<pre class=example>
104LUA_PATH=&lt;ROOT&gt;/?.lua;?.lua 108LUA_PATH=&lt;SHARE&gt;/?.lua;?.lua
105LUA_CPATH=&lt;ROOT&gt;/?.dll;?.dll 109LUA_CPATH=&lt;LIB&gt;/?.dll;?.dll
106</pre> 110</pre>
107 111
108<p> Again, naturally, in Unix the shared library extension would be 112<p> Again, naturally, on Unix systmems the shared library extension would be
109<tt>.so</tt> instead of <tt>.dll</tt> and on Mac OS X it would be 113<tt>.so</tt> instead of <tt>.dll</tt></p>
110<tt>.dylib</tt></p>
111 114
112<h3>Using LuaSocket</h3> 115<h3>Using LuaSocket</h3>
113 116
@@ -118,8 +121,8 @@ it should be easy to use LuaSocket. Just fire the interpreter and use the
118<pre class=example> 121<pre class=example>
119Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio 122Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio
120&gt; socket = require("socket") 123&gt; socket = require("socket")
121&gt; print(socket.VERSION) 124&gt; print(socket._VERSION)
122--&gt; LuaSocket 2.0 (beta3) 125--&gt; LuaSocket 2.0
123</pre> 126</pre>
124 127
125<p> Each module loads their dependencies automatically, so you only need to 128<p> Each module loads their dependencies automatically, so you only need to
@@ -128,7 +131,8 @@ load the modues you directly depend upon: <p>
128<pre class=example> 131<pre class=example>
129Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio 132Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio
130&gt; http = require("socket.http") 133&gt; http = require("socket.http")
131&gt; print(http.request("http://www.tecgraf.puc-rio.br/luasocket")) 134&gt;
135print(http.request("http://www.cs.princeton.edu/~diego/professional/luasocket"))
132--&gt; homepage gets dumped to terminal 136--&gt; homepage gets dumped to terminal
133</pre> 137</pre>
134 138
diff --git a/etc/check-links-nb.lua b/etc/check-links-nb.lua
deleted file mode 100644
index c379e9a..0000000
--- a/etc/check-links-nb.lua
+++ /dev/null
@@ -1,277 +0,0 @@
1-----------------------------------------------------------------------------
2-- Little program that checks links in HTML files, using coroutines and
3-- non-blocking I/O. Thus, faster than simpler version of same program
4-- LuaSocket sample files
5-- Author: Diego Nehab
6-- RCS ID: $$
7-----------------------------------------------------------------------------
8local socket = require("socket")
9
10TIMEOUT = 10
11
12-- we need to yield across calls to protect, so we can't use pcall
13-- we borrow and simplify code from coxpcall to reimplement socket.protect
14-- before loading http
15function socket.protect(f)
16 return function(...)
17 local co = coroutine.create(f)
18 while true do
19 local results = {coroutine.resume(co, unpack(arg))}
20 local status = results[1]
21 table.remove(results, 1)
22 if not status then
23 return nil, results[1][1]
24 end
25 if coroutine.status(co) == "suspended" then
26 arg = {coroutine.yield(unpack(results))}
27 else
28 return unpack(results)
29 end
30 end
31 end
32end
33
34local http = require("socket.http")
35local url = require("socket.url")
36
37-- creates a new set data structure
38function newset()
39 local reverse = {}
40 local set = {}
41 return setmetatable(set, {__index = {
42 insert = function(set, value)
43 if not reverse[value] then
44 table.insert(set, value)
45 reverse[value] = table.getn(set)
46 end
47 end,
48 remove = function(set, value)
49 local index = reverse[value]
50 if index then
51 reverse[value] = nil
52 local top = table.remove(set)
53 if top ~= value then
54 reverse[top] = index
55 set[index] = top
56 end
57 end
58 end
59 }})
60end
61
62local context = {}
63local sending = newset()
64local receiving = newset()
65local nthreads = 0
66
67-- socket.tcp() replacement for non-blocking I/O
68-- implements enough functionality to be used with http.request
69-- in Lua 5.1, we have coroutine.running to simplify things...
70function newcreate(thread)
71 return function()
72 -- try to create underlying socket
73 local tcp, error = socket.tcp()
74 if not tcp then return nil, error end
75 -- put it in non-blocking mode right away
76 tcp:settimeout(0)
77 local trap = {
78 -- we ignore settimeout to preserve our 0 timeout
79 settimeout = function(self, mode, value)
80 return 1
81 end,
82 -- send in non-blocking mode and yield on timeout
83 send = function(self, data, first, last)
84 first = (first or 1) - 1
85 local result, error
86 while true do
87 -- tell dispatcher we want to keep sending before we
88 -- yield control
89 sending:insert(tcp)
90 -- return control to dispatcher
91 -- if upon return the dispatcher tells us we timed out,
92 -- return an error to whoever called us
93 if coroutine.yield() == "timeout" then
94 return nil, "timeout"
95 end
96 -- mark time we started waiting
97 context[tcp].last = socket.gettime()
98 -- try sending
99 result, error, first = tcp:send(data, first+1, last)
100 -- if we are done, or there was an unexpected error,
101 -- break away from loop
102 if error ~= "timeout" then return result, error, first end
103 end
104 end,
105 -- receive in non-blocking mode and yield on timeout
106 receive = function(self, pattern)
107 local error, partial = "timeout", ""
108 local value
109 while true do
110 -- tell dispatcher we want to keep receiving before we
111 -- yield control
112 receiving:insert(tcp)
113 -- return control to dispatcher
114 -- if upon return the dispatcher tells us we timed out,
115 -- return an error to whoever called us
116 if coroutine.yield() == "timeout" then
117 return nil, "timeout"
118 end
119 -- mark time we started waiting
120 context[tcp].last = socket.gettime()
121 -- try receiving
122 value, error, partial = tcp:receive(pattern, partial)
123 -- if we are done, or there was an unexpected error,
124 -- break away from loop
125 if error ~= "timeout" then return value, error, partial end
126 end
127 end,
128 -- connect in non-blocking mode and yield on timeout
129 connect = function(self, host, port)
130 local result, error = tcp:connect(host, port)
131 -- mark time we started waiting
132 context[tcp].last = socket.gettime()
133 if error == "timeout" then
134 -- tell dispatcher we will be able to write uppon connection
135 sending:insert(tcp)
136 -- return control to dispatcher
137 -- if upon return the dispatcher tells us we have a
138 -- timeout, just abort
139 if coroutine.yield() == "timeout" then
140 return nil, "timeout"
141 end
142 -- when we come back, check if connection was successful
143 result, error = tcp:connect(host, port)
144 if result or error == "already connected" then return 1
145 else return nil, "non-blocking connect failed" end
146 else return result, error end
147 end,
148 close = function(self)
149 context[tcp] = nil
150 return tcp:close()
151 end
152 }
153 -- add newly created socket to context
154 context[tcp] = {
155 thread = thread,
156 trap = trap
157 }
158 return trap
159 end
160end
161
162-- get the status of a URL, non-blocking
163function getstatus(link)
164 local parsed = url.parse(link, {scheme = "file"})
165 if parsed.scheme == "http" then
166 local thread = coroutine.create(function(thread, link)
167 local r, c, h, s = http.request{
168 method = "HEAD",
169 url = link,
170 create = newcreate(thread)
171 }
172 if c == 200 then io.write('\t', link, '\n')
173 else io.write('\t', link, ': ', c, '\n') end
174 nthreads = nthreads - 1
175 end)
176 nthreads = nthreads + 1
177 assert(coroutine.resume(thread, thread, link))
178 end
179end
180
181-- dispatch all threads until we are done
182function dispatch()
183 while nthreads > 0 do
184 -- check which sockets are interesting and act on them
185 local readable, writable = socket.select(receiving, sending, 1)
186 -- for all readable connections, resume their threads
187 for _, who in ipairs(readable) do
188 if context[who] then
189 receiving:remove(who)
190 assert(coroutine.resume(context[who].thread))
191 end
192 end
193 -- for all writable connections, do the same
194 for _, who in ipairs(writable) do
195 if context[who] then
196 sending:remove(who)
197 assert(coroutine.resume(context[who].thread))
198 end
199 end
200 -- politely ask replacement I/O functions in idle threads to
201 -- return reporting a timeout
202 local now = socket.gettime()
203 for who, data in pairs(context) do
204 if data.last and now - data.last > TIMEOUT then
205 sending:remove(who)
206 receiving:remove(who)
207 assert(coroutine.resume(context[who].thread, "timeout"))
208 end
209 end
210 end
211end
212
213function readfile(path)
214 path = url.unescape(path)
215 local file, error = io.open(path, "r")
216 if file then
217 local body = file:read("*a")
218 file:close()
219 return body
220 else return nil, error end
221end
222
223function load(u)
224 local parsed = url.parse(u, { scheme = "file" })
225 local body, headers, code, error
226 local base = u
227 if parsed.scheme == "http" then
228 body, code, headers = http.request(u)
229 if code == 200 then
230 -- if there was a redirect, update base to reflect it
231 base = headers.location or base
232 end
233 if not body then
234 error = code
235 end
236 elseif parsed.scheme == "file" then
237 body, error = readfile(parsed.path)
238 else error = string.format("unhandled scheme '%s'", parsed.scheme) end
239 return base, body, error
240end
241
242function getlinks(body, base)
243 -- get rid of comments
244 body = string.gsub(body, "%<%!%-%-.-%-%-%>", "")
245 local links = {}
246 -- extract links
247 body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
248 table.insert(links, url.absolute(base, href))
249 end)
250 body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
251 table.insert(links, url.absolute(base, href))
252 end)
253 string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href)
254 table.insert(links, url.absolute(base, href))
255 end)
256 return links
257end
258
259function checklinks(address)
260 local base, body, error = load(address)
261 if not body then print(error) return end
262 print("Checking ", base)
263 local links = getlinks(body, base)
264 for _, link in ipairs(links) do
265 getstatus(link)
266 end
267end
268
269arg = arg or {}
270if table.getn(arg) < 1 then
271 print("Usage:\n luasocket check-links.lua {<url>}")
272 exit()
273end
274for _, address in ipairs(arg) do
275 checklinks(url.absolute("file:", address))
276end
277dispatch()
diff --git a/etc/check-links.lua b/etc/check-links.lua
index 725cd2a..5c124a8 100644
--- a/etc/check-links.lua
+++ b/etc/check-links.lua
@@ -5,7 +5,7 @@
5-- Author: Diego Nehab 5-- Author: Diego Nehab
6-- RCS ID: $$ 6-- RCS ID: $$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8local url = require("socket.url") 8local url = require("url")
9local dispatch = require("dispatch") 9local dispatch = require("dispatch")
10local http = require("socket.http") 10local http = require("socket.http")
11dispatch.TIMEOUT = 10 11dispatch.TIMEOUT = 10
diff --git a/etc/check-memory.lua b/etc/check-memory.lua
index 7892ba0..7bcdf67 100644
--- a/etc/check-memory.lua
+++ b/etc/check-memory.lua
@@ -7,9 +7,9 @@ function load(s)
7 print(s .. ":\t " .. (b-a) .. "k") 7 print(s .. ":\t " .. (b-a) .. "k")
8end 8end
9 9
10load("socket") 10load("url")
11load("socket.url")
12load("ltn12") 11load("ltn12")
12load("socket")
13load("mime") 13load("mime")
14load("socket.tp") 14load("socket.tp")
15load("socket.smtp") 15load("socket.smtp")
diff --git a/etc/get.lua b/etc/get.lua
index 0c95d54..bd5af28 100644
--- a/etc/get.lua
+++ b/etc/get.lua
@@ -7,7 +7,7 @@
7local socket = require("socket") 7local socket = require("socket")
8local http = require("socket.http") 8local http = require("socket.http")
9local ftp = require("socket.ftp") 9local ftp = require("socket.ftp")
10local url = require("socket.url") 10local url = require("url")
11local ltn12 = require("ltn12") 11local ltn12 = require("ltn12")
12 12
13-- formats a number of seconds into human readable form 13-- formats a number of seconds into human readable form
diff --git a/src/auxiliar.h b/src/auxiliar.h
index 70f4704..ff20b50 100644
--- a/src/auxiliar.h
+++ b/src/auxiliar.h
@@ -31,8 +31,8 @@
31* RCS ID: $Id$ 31* RCS ID: $Id$
32\*=========================================================================*/ 32\*=========================================================================*/
33 33
34#include <lua.h> 34#include "lua.h"
35#include <lauxlib.h> 35#include "lauxlib.h"
36 36
37int aux_open(lua_State *L); 37int aux_open(lua_State *L);
38void aux_newclass(lua_State *L, const char *classname, luaL_reg *func); 38void aux_newclass(lua_State *L, const char *classname, luaL_reg *func);
diff --git a/src/buffer.c b/src/buffer.c
index 1188fda..b69a9b8 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -4,8 +4,8 @@
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id$
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <lua.h> 7#include "lua.h"
8#include <lauxlib.h> 8#include "lauxlib.h"
9 9
10#include "buffer.h" 10#include "buffer.h"
11 11
diff --git a/src/buffer.h b/src/buffer.h
index 8e5fb6c..f43e676 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -17,7 +17,7 @@
17* 17*
18* RCS ID: $Id$ 18* RCS ID: $Id$
19\*=========================================================================*/ 19\*=========================================================================*/
20#include <lua.h> 20#include "lua.h"
21 21
22#include "io.h" 22#include "io.h"
23#include "timeout.h" 23#include "timeout.h"
diff --git a/src/except.c b/src/except.c
index dabaf19..fc4c038 100644
--- a/src/except.c
+++ b/src/except.c
@@ -5,8 +5,9 @@
5* RCS ID: $Id$ 5* RCS ID: $Id$
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <stdio.h> 7#include <stdio.h>
8#include <lua.h> 8
9#include <lauxlib.h> 9#include "lua.h"
10#include "lauxlib.h"
10 11
11#include "except.h" 12#include "except.h"
12 13
diff --git a/src/except.h b/src/except.h
index 2c57b27..03e417d 100644
--- a/src/except.h
+++ b/src/except.h
@@ -28,7 +28,7 @@
28* RCS ID: $Id$ 28* RCS ID: $Id$
29\*=========================================================================*/ 29\*=========================================================================*/
30 30
31#include <lua.h> 31#include "lua.h"
32 32
33int except_open(lua_State *L); 33int except_open(lua_State *L);
34 34
diff --git a/src/inet.c b/src/inet.c
index d102060..81ecd22 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -7,8 +7,8 @@
7#include <stdio.h> 7#include <stdio.h>
8#include <string.h> 8#include <string.h>
9 9
10#include <lua.h> 10#include "lua.h"
11#include <lauxlib.h> 11#include "lauxlib.h"
12 12
13#include "inet.h" 13#include "inet.h"
14 14
diff --git a/src/inet.h b/src/inet.h
index 4b3639a..da95e7e 100644
--- a/src/inet.h
+++ b/src/inet.h
@@ -16,7 +16,7 @@
16* 16*
17* RCS ID: $Id$ 17* RCS ID: $Id$
18\*=========================================================================*/ 18\*=========================================================================*/
19#include <lua.h> 19#include "lua.h"
20#include "socket.h" 20#include "socket.h"
21#include "timeout.h" 21#include "timeout.h"
22 22
diff --git a/src/io.c b/src/io.c
index fe0af76..317cfd4 100644
--- a/src/io.c
+++ b/src/io.c
@@ -27,7 +27,6 @@ const char *io_strerror(int err) {
27 case IO_DONE: return NULL; 27 case IO_DONE: return NULL;
28 case IO_CLOSED: return "closed"; 28 case IO_CLOSED: return "closed";
29 case IO_TIMEOUT: return "timeout"; 29 case IO_TIMEOUT: return "timeout";
30 case IO_CLIPPED: return "clipped";
31 default: return "unknown error"; 30 default: return "unknown error";
32 } 31 }
33} 32}
diff --git a/src/io.h b/src/io.h
index 4f9de57..bcaf416 100644
--- a/src/io.h
+++ b/src/io.h
@@ -15,7 +15,7 @@
15* RCS ID: $Id$ 15* RCS ID: $Id$
16\*=========================================================================*/ 16\*=========================================================================*/
17#include <stdio.h> 17#include <stdio.h>
18#include <lua.h> 18#include "lua.h"
19 19
20#include "timeout.h" 20#include "timeout.h"
21 21
@@ -24,8 +24,7 @@ enum {
24 IO_DONE = 0, /* operation completed successfully */ 24 IO_DONE = 0, /* operation completed successfully */
25 IO_TIMEOUT = -1, /* operation timed out */ 25 IO_TIMEOUT = -1, /* operation timed out */
26 IO_CLOSED = -2, /* the connection has been closed */ 26 IO_CLOSED = -2, /* the connection has been closed */
27 IO_CLIPPED = -3, /* maxium bytes count reached */ 27 IO_UNKNOWN = -3
28 IO_UNKNOWN = -4
29}; 28};
30 29
31/* interface to error message function */ 30/* interface to error message function */
diff --git a/src/luasocket.c b/src/luasocket.c
index 94ea05b..434b5b7 100644
--- a/src/luasocket.c
+++ b/src/luasocket.c
@@ -17,15 +17,17 @@
17/*=========================================================================*\ 17/*=========================================================================*\
18* Standard include files 18* Standard include files
19\*=========================================================================*/ 19\*=========================================================================*/
20#include <lua.h> 20#include "lua.h"
21#include <lauxlib.h> 21#include "lauxlib.h"
22
23#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
22#include "compat-5.1.h" 24#include "compat-5.1.h"
23#include "luasocket.h" 25#endif
24 26
25/*=========================================================================*\ 27/*=========================================================================*\
26* LuaSocket includes 28* LuaSocket includes
27\*=========================================================================*/ 29\*=========================================================================*/
28 30#include "luasocket.h"
29#include "auxiliar.h" 31#include "auxiliar.h"
30#include "except.h" 32#include "except.h"
31#include "timeout.h" 33#include "timeout.h"
diff --git a/src/luasocket.h b/src/luasocket.h
index c7d09d8..0143fa7 100644
--- a/src/luasocket.h
+++ b/src/luasocket.h
@@ -8,10 +8,10 @@
8* 8*
9* RCS ID: $Id$ 9* RCS ID: $Id$
10\*=========================================================================*/ 10\*=========================================================================*/
11#include <lua.h> 11#include "lua.h"
12 12
13/*-------------------------------------------------------------------------*\ 13/*-------------------------------------------------------------------------*\
14* Current luasocket version 14* Current socket library version
15\*-------------------------------------------------------------------------*/ 15\*-------------------------------------------------------------------------*/
16#define LUASOCKET_VERSION "LuaSocket 2.0" 16#define LUASOCKET_VERSION "LuaSocket 2.0"
17#define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab" 17#define LUASOCKET_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab"
diff --git a/src/makefile b/src/makefile
index 7ef18bf..9a97dd3 100644
--- a/src/makefile
+++ b/src/makefile
@@ -11,6 +11,7 @@ include ../config
11# Modules belonging to socket-core 11# Modules belonging to socket-core
12# 12#
13SOCKET_OBJS:= \ 13SOCKET_OBJS:= \
14 $(COMPAT)/compat-5.1.o \
14 luasocket.o \ 15 luasocket.o \
15 timeout.o \ 16 timeout.o \
16 buffer.o \ 17 buffer.o \
@@ -22,15 +23,15 @@ SOCKET_OBJS:= \
22 udp.o \ 23 udp.o \
23 except.o \ 24 except.o \
24 select.o \ 25 select.o \
25 $(COMPAT)/compat-5.1.o \
26 usocket.o 26 usocket.o
27 27
28#------ 28#------
29# Modules belonging mime-core 29# Modules belonging mime-core
30# 30#
31MIME_OBJS:=\ 31MIME_OBJS:=\
32 mime.o \ 32 $(COMPAT)/compat-5.1.o \
33 $(COMPAT)/compat-5.1.o 33 mime.o
34
34 35
35#------ 36#------
36# Modules belonging unix (local domain sockets) 37# Modules belonging unix (local domain sockets)
diff --git a/src/mime.c b/src/mime.c
index 4539e2c..4dfcae5 100644
--- a/src/mime.c
+++ b/src/mime.c
@@ -6,10 +6,13 @@
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
9#include <lua.h> 9#include "lua.h"
10#include <lauxlib.h> 10#include "lauxlib.h"
11 11
12#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
12#include "compat-5.1.h" 13#include "compat-5.1.h"
14#endif
15
13#include "mime.h" 16#include "mime.h"
14 17
15/*=========================================================================*\ 18/*=========================================================================*\
@@ -81,6 +84,10 @@ static UC b64unbase[256];
81MIME_API int luaopen_mime_core(lua_State *L) 84MIME_API int luaopen_mime_core(lua_State *L)
82{ 85{
83 luaL_openlib(L, "mime", func, 0); 86 luaL_openlib(L, "mime", func, 0);
87 /* make version string available to scripts */
88 lua_pushstring(L, "_VERSION");
89 lua_pushstring(L, MIME_VERSION);
90 lua_rawset(L, -3);
84 /* initialize lookup tables */ 91 /* initialize lookup tables */
85 qpsetup(qpclass, qpunbase); 92 qpsetup(qpclass, qpunbase);
86 b64setup(b64unbase); 93 b64setup(b64unbase);
diff --git a/src/mime.h b/src/mime.h
index eda0898..a56751c 100644
--- a/src/mime.h
+++ b/src/mime.h
@@ -1,7 +1,7 @@
1#ifndef MIME_H 1#ifndef MIME_H
2#define MIME_H 2#define MIME_H
3/*=========================================================================*\ 3/*=========================================================================*\
4* MIME support functions 4* Core MIME support
5* LuaSocket toolkit 5* LuaSocket toolkit
6* 6*
7* This module provides functions to implement transfer content encodings 7* This module provides functions to implement transfer content encodings
@@ -10,7 +10,14 @@
10* 10*
11* RCS ID: $Id$ 11* RCS ID: $Id$
12\*=========================================================================*/ 12\*=========================================================================*/
13#include <lua.h> 13#include "lua.h"
14
15/*-------------------------------------------------------------------------*\
16* Current MIME library version
17\*-------------------------------------------------------------------------*/
18#define MIME_VERSION "MIME 1.0"
19#define MIME_COPYRIGHT "Copyright (C) 2004-2005 Diego Nehab"
20#define MIME_AUTHORS "Diego Nehab"
14 21
15/*-------------------------------------------------------------------------*\ 22/*-------------------------------------------------------------------------*\
16* This macro prefixes all exported API functions 23* This macro prefixes all exported API functions
diff --git a/src/options.c b/src/options.c
index c9e69f0..5236a3f 100644
--- a/src/options.c
+++ b/src/options.c
@@ -4,9 +4,10 @@
4* 4*
5* RCS ID: $Id$ 5* RCS ID: $Id$
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <lauxlib.h>
8#include <string.h> 7#include <string.h>
9 8
9#include "lauxlib.h"
10
10#include "auxiliar.h" 11#include "auxiliar.h"
11#include "options.h" 12#include "options.h"
12#include "inet.h" 13#include "inet.h"
diff --git a/src/options.h b/src/options.h
index d57bfaa..6ebf1f6 100644
--- a/src/options.h
+++ b/src/options.h
@@ -10,7 +10,7 @@
10* RCS ID: $Id$ 10* RCS ID: $Id$
11\*=========================================================================*/ 11\*=========================================================================*/
12 12
13#include <lua.h> 13#include "lua.h"
14#include "socket.h" 14#include "socket.h"
15 15
16/* option registry */ 16/* option registry */
diff --git a/src/select.c b/src/select.c
index e2cd91d..d28ade1 100644
--- a/src/select.c
+++ b/src/select.c
@@ -6,8 +6,8 @@
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
9#include <lua.h> 9#include "lua.h"
10#include <lauxlib.h> 10#include "lauxlib.h"
11 11
12#include "socket.h" 12#include "socket.h"
13#include "timeout.h" 13#include "timeout.h"
diff --git a/src/socket.h b/src/socket.h
index 4443bcc..ad12b1b 100644
--- a/src/socket.h
+++ b/src/socket.h
@@ -53,7 +53,6 @@ int sock_waitfd(p_sock ps, int sw, p_tm tm);
53int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm); 53int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm);
54 54
55int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm); 55int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm);
56int sock_connected(p_sock ps, p_tm tm);
57int sock_create(p_sock ps, int domain, int type, int protocol); 56int sock_create(p_sock ps, int domain, int type, int protocol);
58int sock_bind(p_sock ps, SA *addr, socklen_t addr_len); 57int sock_bind(p_sock ps, SA *addr, socklen_t addr_len);
59int sock_listen(p_sock ps, int backlog); 58int sock_listen(p_sock ps, int backlog);
diff --git a/src/tcp.c b/src/tcp.c
index c79ddd1..8b4c7e3 100644
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -6,8 +6,8 @@
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
9#include <lua.h> 9#include "lua.h"
10#include <lauxlib.h> 10#include "lauxlib.h"
11 11
12#include "auxiliar.h" 12#include "auxiliar.h"
13#include "socket.h" 13#include "socket.h"
diff --git a/src/tcp.h b/src/tcp.h
index 708023e..fe4fd8c 100644
--- a/src/tcp.h
+++ b/src/tcp.h
@@ -16,7 +16,7 @@
16* 16*
17* RCS ID: $Id$ 17* RCS ID: $Id$
18\*=========================================================================*/ 18\*=========================================================================*/
19#include <lua.h> 19#include "lua.h"
20 20
21#include "buffer.h" 21#include "buffer.h"
22#include "timeout.h" 22#include "timeout.h"
diff --git a/src/timeout.c b/src/timeout.c
index be68228..4f1d345 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -6,8 +6,8 @@
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <stdio.h> 7#include <stdio.h>
8 8
9#include <lua.h> 9#include "lua.h"
10#include <lauxlib.h> 10#include "lauxlib.h"
11 11
12#include "auxiliar.h" 12#include "auxiliar.h"
13#include "timeout.h" 13#include "timeout.h"
diff --git a/src/timeout.h b/src/timeout.h
index d2f9be0..27e0a8a 100644
--- a/src/timeout.h
+++ b/src/timeout.h
@@ -6,7 +6,7 @@
6* 6*
7* RCS ID: $Id$ 7* RCS ID: $Id$
8\*=========================================================================*/ 8\*=========================================================================*/
9#include <lua.h> 9#include "lua.h"
10 10
11/* timeout control structure */ 11/* timeout control structure */
12typedef struct t_tm_ { 12typedef struct t_tm_ {
diff --git a/src/udp.c b/src/udp.c
index 094e137..4de7248 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -6,8 +6,8 @@
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
9#include <lua.h> 9#include "lua.h"
10#include <lauxlib.h> 10#include "lauxlib.h"
11 11
12#include "auxiliar.h" 12#include "auxiliar.h"
13#include "socket.h" 13#include "socket.h"
diff --git a/src/udp.h b/src/udp.h
index 520573d..3591998 100644
--- a/src/udp.h
+++ b/src/udp.h
@@ -14,7 +14,7 @@
14* 14*
15* RCS ID: $Id$ 15* RCS ID: $Id$
16\*=========================================================================*/ 16\*=========================================================================*/
17#include <lua.h> 17#include "lua.h"
18 18
19#include "timeout.h" 19#include "timeout.h"
20#include "socket.h" 20#include "socket.h"
diff --git a/src/unix.c b/src/unix.c
index c169268..217a600 100644
--- a/src/unix.c
+++ b/src/unix.c
@@ -6,8 +6,8 @@
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8 8
9#include <lua.h> 9#include "lua.h"
10#include <lauxlib.h> 10#include "lauxlib.h"
11 11
12#include "auxiliar.h" 12#include "auxiliar.h"
13#include "socket.h" 13#include "socket.h"
diff --git a/src/unix.h b/src/unix.h
index aaaef3d..60b00fe 100644
--- a/src/unix.h
+++ b/src/unix.h
@@ -9,7 +9,7 @@
9* 9*
10* RCS ID: $Id$ 10* RCS ID: $Id$
11\*=========================================================================*/ 11\*=========================================================================*/
12#include <lua.h> 12#include "lua.h"
13 13
14#include "buffer.h" 14#include "buffer.h"
15#include "timeout.h" 15#include "timeout.h"
diff --git a/src/url.lua b/src/url.lua
index bd39d98..5427cbc 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -15,6 +15,11 @@ module("socket.url")
15getmetatable(_M).__index = nil 15getmetatable(_M).__index = nil
16 16
17----------------------------------------------------------------------------- 17-----------------------------------------------------------------------------
18-- Module version
19-----------------------------------------------------------------------------
20_VERSION = "URL 1.0"
21
22-----------------------------------------------------------------------------
18-- Encodes a string into its escaped hexadecimal representation 23-- Encodes a string into its escaped hexadecimal representation
19-- Input 24-- Input
20-- s: binary string to be encoded 25-- s: binary string to be encoded
diff --git a/src/usocket.c b/src/usocket.c
index dcb40e6..f067fa1 100644
--- a/src/usocket.c
+++ b/src/usocket.c
@@ -169,15 +169,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
169 /* zero timeout case optimization */ 169 /* zero timeout case optimization */
170 if (tm_iszero(tm)) return IO_TIMEOUT; 170 if (tm_iszero(tm)) return IO_TIMEOUT;
171 /* wait until we have the result of the connection attempt or timeout */ 171 /* wait until we have the result of the connection attempt or timeout */
172 return sock_connected(ps, tm); 172 err = sock_waitfd(ps, WAITFD_C, tm);
173} 173 if (err == IO_CLOSED) {
174
175/*-------------------------------------------------------------------------*\
176* Checks if socket is connected, or return reason for failure
177\*-------------------------------------------------------------------------*/
178int sock_connected(p_sock ps, p_tm tm) {
179 int err;
180 if ((err = sock_waitfd(ps, WAITFD_C, tm) == IO_CLOSED)) {
181 if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE; 174 if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE;
182 else return errno; 175 else return errno;
183 } else return err; 176 } else return err;
diff --git a/src/wsocket.c b/src/wsocket.c
index 0f6005f..b533eae 100644
--- a/src/wsocket.c
+++ b/src/wsocket.c
@@ -22,7 +22,8 @@ int sock_open(void) {
22 WORD wVersionRequested = MAKEWORD(2, 0); 22 WORD wVersionRequested = MAKEWORD(2, 0);
23 int err = WSAStartup(wVersionRequested, &wsaData ); 23 int err = WSAStartup(wVersionRequested, &wsaData );
24 if (err != 0) return 0; 24 if (err != 0) return 0;
25 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) { 25 if ((LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) &&
26 (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)) {
26 WSACleanup(); 27 WSACleanup();
27 return 0; 28 return 0;
28 } 29 }
@@ -124,16 +125,8 @@ int sock_connect(p_sock ps, SA *addr, socklen_t len, p_tm tm) {
124 /* zero timeout case optimization */ 125 /* zero timeout case optimization */
125 if (tm_iszero(tm)) return IO_TIMEOUT; 126 if (tm_iszero(tm)) return IO_TIMEOUT;
126 /* we wait until something happens */ 127 /* we wait until something happens */
127 return sock_connected(ps, tm); 128 err = sock_waitfd(ps, WAITFD_C, tm);
128} 129 if (err == IO_CLOSED) {
129
130/*-------------------------------------------------------------------------*\
131* Check if socket is connected
132\*-------------------------------------------------------------------------*/
133int sock_connected(p_sock ps, p_tm tm) {
134 int err;
135 /* give windows time to find out what is up (yes, disgusting) */
136 if ((err = sock_waitfd(ps, WAITFD_C, tm)) == IO_CLOSED) {
137 int len = sizeof(err); 130 int len = sizeof(err);
138 /* give windows time to set the error (yes, disgusting) */ 131 /* give windows time to set the error (yes, disgusting) */
139 Sleep(10); 132 Sleep(10);
@@ -143,6 +136,7 @@ int sock_connected(p_sock ps, p_tm tm) {
143 * "unknown error", but it's not really our fault */ 136 * "unknown error", but it's not really our fault */
144 return err > 0? err: IO_UNKNOWN; 137 return err > 0? err: IO_UNKNOWN;
145 } else return err; 138 } else return err;
139
146} 140}
147 141
148/*-------------------------------------------------------------------------*\ 142/*-------------------------------------------------------------------------*\
diff --git a/test/testclnt.lua b/test/testclnt.lua
index 83f925b..cf71c9f 100644
--- a/test/testclnt.lua
+++ b/test/testclnt.lua
@@ -382,7 +382,8 @@ function connect_timeout()
382 assert(c, e) 382 assert(c, e)
383 c:settimeout(0.1) 383 c:settimeout(0.1)
384 local t = socket.gettime() 384 local t = socket.gettime()
385 local r, e = c:connect("127.0.0.2", 80) 385 local r, e = c:connect("10.0.0.1", 81)
386print(r, e)
386 assert(not r, "should not connect") 387 assert(not r, "should not connect")
387 assert(socket.gettime() - t < 2, "took too long to give up.") 388 assert(socket.gettime() - t < 2, "took too long to give up.")
388 c:close() 389 c:close()
diff --git a/test/urltest.lua b/test/urltest.lua
index ae54db9..8dc0c14 100644
--- a/test/urltest.lua
+++ b/test/urltest.lua
@@ -71,7 +71,7 @@ local check_parse_url = function(gaba)
71 local url = gaba.url 71 local url = gaba.url
72 gaba.url = nil 72 gaba.url = nil
73 local parsed = socket.url.parse(url) 73 local parsed = socket.url.parse(url)
74 for i, v in gaba do 74 for i, v in pairs(gaba) do
75 if v ~= parsed[i] then 75 if v ~= parsed[i] then
76 io.write("parse: In test for '", url, "' expected ", i, " = '", 76 io.write("parse: In test for '", url, "' expected ", i, " = '",
77 v, "' but got '", tostring(parsed[i]), "'\n") 77 v, "' but got '", tostring(parsed[i]), "'\n")
@@ -79,7 +79,7 @@ local check_parse_url = function(gaba)
79 exit() 79 exit()
80 end 80 end
81 end 81 end
82 for i, v in parsed do 82 for i, v in pairs(parsed) do
83 if v ~= gaba[i] then 83 if v ~= gaba[i] then
84 io.write("parse: In test for '", url, "' expected ", i, " = '", 84 io.write("parse: In test for '", url, "' expected ", i, " = '",
85 tostring(gaba[i]), "' but got '", v, "'\n") 85 tostring(gaba[i]), "' but got '", v, "'\n")