aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-03-22 04:15:03 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-03-22 04:15:03 +0000
commit1fa65d89ca5dc64756f7933d7cc3f524e4627dce (patch)
treedf614c8b86b0f7c2f45c2afcacc993ab3c0dcf11
parent4919a83d2271a9e43b83c7d488e3f94c850681e3 (diff)
downloadluasocket-1fa65d89ca5dc64756f7933d7cc3f524e4627dce.tar.gz
luasocket-1fa65d89ca5dc64756f7933d7cc3f524e4627dce.tar.bz2
luasocket-1fa65d89ca5dc64756f7933d7cc3f524e4627dce.zip
Adjusted some details, got rid of old files, added some new.
-rw-r--r--TODO7
-rw-r--r--etc/eol.lua15
-rw-r--r--etc/get.lua4
-rw-r--r--etc/qp.lua24
-rw-r--r--samples/daytimeclnt.lua5
-rw-r--r--src/http.lua41
-rw-r--r--src/ltn12.lua13
-rw-r--r--src/smtp.lua43
-rw-r--r--test/httptest.lua36
-rw-r--r--test/smtptest.lua2
-rw-r--r--test/testsupport.lua37
-rw-r--r--test/urltest.lua2
12 files changed, 132 insertions, 97 deletions
diff --git a/TODO b/TODO
index 110a78c..4fe107b 100644
--- a/TODO
+++ b/TODO
@@ -19,6 +19,13 @@
19* Separar as classes em arquivos 19* Separar as classes em arquivos
20* Retorno de sendto em datagram sockets pode ser refused 20* Retorno de sendto em datagram sockets pode ser refused
21 21
22falar sobre encodet/wrapt/decodet no manual sobre mime
23
24
25RECEIVE MUDOU!!! COLOCAR NO MANUAL.
26HTTP.lua mudou bastante também.
27
28
22change mime.eol to output marker on detection of first candidate, instead 29change mime.eol to output marker on detection of first candidate, instead
23of on the second. that way it works in one pass for strings that end with 30of on the second. that way it works in one pass for strings that end with
24one candidate. 31one candidate.
diff --git a/etc/eol.lua b/etc/eol.lua
index 6b2a8a9..aa43596 100644
--- a/etc/eol.lua
+++ b/etc/eol.lua
@@ -1,9 +1,6 @@
1marker = {['-u'] = '\10', ['-d'] = '\13\10'} 1local marker = '\n'
2arg = arg or {'-u'} 2if arg and arg[1] == '-d' then marker = '\r\n' end
3marker = marker[arg[1]] or marker['-u'] 3local filter = mime.normalize(marker)
4local convert = socket.mime.normalize(marker) 4local source = ltn12.source.chain(ltn12.source.file(io.stdin), filter)
5while 1 do 5local sink = ltn12.sink.file(io.stdout)
6 local chunk = io.read(1) 6ltn12.pump(source, sink)
7 io.write(convert(chunk))
8 if not chunk then break end
9end
diff --git a/etc/get.lua b/etc/get.lua
index 0306b54..eafebda 100644
--- a/etc/get.lua
+++ b/etc/get.lua
@@ -93,7 +93,7 @@ function getbyhttp(url, file)
93 local save = ltn12.sink.file(file or io.stdout) 93 local save = ltn12.sink.file(file or io.stdout)
94 -- only print feedback if output is not stdout 94 -- only print feedback if output is not stdout
95 if file then save = ltn12.sink.chain(stats(gethttpsize(url)), save) end 95 if file then save = ltn12.sink.chain(stats(gethttpsize(url)), save) end
96 local respt = socket.http.request_cb({url = url, sink = save}) 96 local respt = socket.http.request {url = url, sink = save }
97 if respt.code ~= 200 then print(respt.status or respt.error) end 97 if respt.code ~= 200 then print(respt.status or respt.error) end
98end 98end
99 99
@@ -103,7 +103,7 @@ function getbyftp(url, file)
103 -- only print feedback if output is not stdout 103 -- only print feedback if output is not stdout
104 -- and we don't know how big the file is 104 -- and we don't know how big the file is
105 if file then save = ltn12.sink.chain(stats(), save) end 105 if file then save = ltn12.sink.chain(stats(), save) end
106 local ret, err = socket.ftp.get_cb {url = url, sink = save, type = "i"} 106 local ret, err = socket.ftp.get {url = url, sink = save, type = "i"}
107 if err then print(err) end 107 if err then print(err) end
108end 108end
109 109
diff --git a/etc/qp.lua b/etc/qp.lua
index 1ca0ae2..08545db 100644
--- a/etc/qp.lua
+++ b/etc/qp.lua
@@ -2,17 +2,15 @@ local convert
2arg = arg or {} 2arg = arg or {}
3local mode = arg and arg[1] or "-et" 3local mode = arg and arg[1] or "-et"
4if mode == "-et" then 4if mode == "-et" then
5 local normalize = socket.mime.normalize() 5 local normalize = mime.normalize()
6 local qp = socket.mime.encode("quoted-printable") 6 local qp = mime.encode("quoted-printable")
7 local wrap = socket.mime.wrap("quoted-printable") 7 local wrap = mime.wrap("quoted-printable")
8 convert = socket.mime.chain(normalize, qp, wrap) 8 convert = ltn12.filter.chain(normalize, qp, wrap)
9elseif mode == "-eb" then 9elseif mode == "-eb" then
10 local qp = socket.mime.encode("quoted-printable", "binary") 10 local qp = mime.encode("quoted-printable", "binary")
11 local wrap = socket.mime.wrap("quoted-printable") 11 local wrap = mime.wrap("quoted-printable")
12 convert = socket.mime.chain(qp, wrap) 12 convert = ltn12.filter.chain(qp, wrap)
13else convert = socket.mime.decode("quoted-printable") end 13else convert = mime.decode("quoted-printable") end
14while 1 do 14local source = ltn12.source.chain(ltn12.source.file(io.stdin), convert)
15 local chunk = io.read(4096) 15local sink = ltn12.sink.file(io.stdout)
16 io.write(convert(chunk)) 16ltn12.pump(source, sink)
17 if not chunk then break end
18end
diff --git a/samples/daytimeclnt.lua b/samples/daytimeclnt.lua
index 29abe17..63f4017 100644
--- a/samples/daytimeclnt.lua
+++ b/samples/daytimeclnt.lua
@@ -14,8 +14,9 @@ host = socket.dns.toip(host)
14udp = socket.udp() 14udp = socket.udp()
15print("Using host '" ..host.. "' and port " ..port.. "...") 15print("Using host '" ..host.. "' and port " ..port.. "...")
16udp:setpeername(host, port) 16udp:setpeername(host, port)
17udp:settimeout(3)
17sent, err = udp:send("anything") 18sent, err = udp:send("anything")
18if err then print(err) exit() end 19if err then print(err) os.exit() end
19dgram, err = udp:receive() 20dgram, err = udp:receive()
20if not dgram then print(err) exit() end 21if not dgram then print(err) os.exit() end
21io.write(dgram) 22io.write(dgram)
diff --git a/src/http.lua b/src/http.lua
index a10cf50..ab166e3 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -2,7 +2,7 @@
2-- HTTP/1.1 client support for the Lua language. 2-- HTTP/1.1 client support for the Lua language.
3-- LuaSocket toolkit. 3-- LuaSocket toolkit.
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- Conforming to: RFC 2616, LTN7 5-- Conforming to RFC 2616
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded 8-- make sure LuaSocket is loaded
@@ -39,21 +39,18 @@ local function third(a, b, c)
39 return c 39 return c
40end 40end
41 41
42local function shift(a, b, c, d) 42local function receive_headers(reqt, respt)
43 return c, d 43 local headers = {}
44end 44 local sock = respt.tmp.sock
45 45 local line, name, value, _
46-- resquest_p forward declaration 46 -- store results
47local request_p 47 respt.headers = headers
48
49local function receive_headers(sock, headers)
50 local line, name, value
51 -- get first line 48 -- get first line
52 line = socket.try(sock:receive()) 49 line = socket.try(sock:receive())
53 -- headers go until a blank line is found 50 -- headers go until a blank line is found
54 while line ~= "" do 51 while line ~= "" do
55 -- get field-name and value 52 -- get field-name and value
56 name, value = shift(string.find(line, "^(.-):%s*(.*)")) 53 _, _, name, value = string.find(line, "^(.-):%s*(.*)")
57 assert(name and value, "malformed reponse headers") 54 assert(name and value, "malformed reponse headers")
58 name = string.lower(name) 55 name = string.lower(name)
59 -- get next line (value might be folded) 56 -- get next line (value might be folded)
@@ -100,7 +97,10 @@ local function receive_body_bychunks(sock, sink)
100 -- let callback know we are done 97 -- let callback know we are done
101 hand(sink, nil) 98 hand(sink, nil)
102 -- servers shouldn't send trailer headers, but who trusts them? 99 -- servers shouldn't send trailer headers, but who trusts them?
103 receive_headers(sock, {}) 100 local line = socket.try(sock:receive())
101 while line ~= "" do
102 line = socket.try(sock:receive())
103 end
104end 104end
105 105
106local function receive_body_bylength(sock, length, sink) 106local function receive_body_bylength(sock, length, sink)
@@ -245,7 +245,7 @@ local function open(reqt, respt)
245 socket.try(sock:connect(host, port)) 245 socket.try(sock:connect(host, port))
246end 246end
247 247
248function adjust_headers(reqt, respt) 248local function adjust_headers(reqt, respt)
249 local lower = {} 249 local lower = {}
250 local headers = reqt.headers or {} 250 local headers = reqt.headers or {}
251 -- set default headers 251 -- set default headers
@@ -261,7 +261,7 @@ function adjust_headers(reqt, respt)
261 respt.tmp.headers = lower 261 respt.tmp.headers = lower
262end 262end
263 263
264function parse_url(reqt, respt) 264local function parse_url(reqt, respt)
265 -- parse url with default fields 265 -- parse url with default fields
266 local parsed = socket.url.parse(reqt.url, { 266 local parsed = socket.url.parse(reqt.url, {
267 host = "", 267 host = "",
@@ -280,11 +280,16 @@ function parse_url(reqt, respt)
280 respt.tmp.parsed = parsed 280 respt.tmp.parsed = parsed
281end 281end
282 282
283-- forward declaration
284local request_p
285
283local function should_authorize(reqt, respt) 286local function should_authorize(reqt, respt)
284 -- if there has been an authorization attempt, it must have failed 287 -- if there has been an authorization attempt, it must have failed
285 if reqt.headers and reqt.headers["authorization"] then return nil end 288 if reqt.headers and reqt.headers["authorization"] then return nil end
286 -- if we don't have authorization information, we can't retry 289 -- if last attempt didn't fail due to lack of authentication,
287 return respt.tmp.parsed.user and respt.tmp.parsed.password 290 -- or we don't have authorization information, we can't retry
291 return respt.code == 401 and
292 respt.tmp.parsed.user and respt.tmp.parsed.password
288end 293end
289 294
290local function clone(headers) 295local function clone(headers)
@@ -338,14 +343,14 @@ local function redirect(reqt, respt)
338 if respt.headers then respt.headers.location = redirt.url end 343 if respt.headers then respt.headers.location = redirt.url end
339end 344end
340 345
346-- execute a request of through an exception
341function request_p(reqt, respt) 347function request_p(reqt, respt)
342 parse_url(reqt, respt) 348 parse_url(reqt, respt)
343 adjust_headers(reqt, respt) 349 adjust_headers(reqt, respt)
344 open(reqt, respt) 350 open(reqt, respt)
345 send_request(reqt, respt) 351 send_request(reqt, respt)
346 receive_status(reqt, respt) 352 receive_status(reqt, respt)
347 respt.headers = {} 353 receive_headers(reqt, respt)
348 receive_headers(respt.tmp.sock, respt.headers)
349 if should_redirect(reqt, respt) then 354 if should_redirect(reqt, respt) then
350 respt.tmp.sock:close() 355 respt.tmp.sock:close()
351 redirect(reqt, respt) 356 redirect(reqt, respt)
diff --git a/src/ltn12.lua b/src/ltn12.lua
index dc49d80..ed3449b 100644
--- a/src/ltn12.lua
+++ b/src/ltn12.lua
@@ -22,6 +22,7 @@ end
22 22
23-- returns a high level filter that cycles a cycles a low-level filter 23-- returns a high level filter that cycles a cycles a low-level filter
24function filter.cycle(low, ctx, extra) 24function filter.cycle(low, ctx, extra)
25 if type(low) ~= 'function' then error('invalid low-level filter', 2) end
25 return function(chunk) 26 return function(chunk)
26 local ret 27 local ret
27 ret, ctx = low(ctx, chunk, extra) 28 ret, ctx = low(ctx, chunk, extra)
@@ -31,6 +32,8 @@ end
31 32
32-- chains two filters together 33-- chains two filters together
33local function chain2(f1, f2) 34local function chain2(f1, f2)
35 if type(f1) ~= 'function' then error('invalid filter', 2) end
36 if type(f2) ~= 'function' then error('invalid filter', 2) end
34 return function(chunk) 37 return function(chunk)
35 return f2(f1(chunk)) 38 return f2(f1(chunk))
36 end 39 end
@@ -40,6 +43,7 @@ end
40function filter.chain(...) 43function filter.chain(...)
41 local f = arg[1] 44 local f = arg[1]
42 for i = 2, table.getn(arg) do 45 for i = 2, table.getn(arg) do
46 if type(arg[i]) ~= 'function' then error('invalid filter', 2) end
43 f = chain2(f, arg[i]) 47 f = chain2(f, arg[i])
44 end 48 end
45 return f 49 return f
@@ -74,6 +78,7 @@ end
74 78
75-- turns a fancy source into a simple source 79-- turns a fancy source into a simple source
76function source.simplify(src) 80function source.simplify(src)
81 if type(src) ~= 'function' then error('invalid source', 2) end
77 return function() 82 return function()
78 local chunk, err_or_new = src() 83 local chunk, err_or_new = src()
79 src = err_or_new or src 84 src = err_or_new or src
@@ -97,6 +102,7 @@ end
97 102
98-- creates rewindable source 103-- creates rewindable source
99function source.rewind(src) 104function source.rewind(src)
105 if type(src) ~= 'function' then error('invalid source', 2) end
100 local t = {} 106 local t = {}
101 return function(chunk) 107 return function(chunk)
102 if not chunk then 108 if not chunk then
@@ -111,6 +117,8 @@ end
111 117
112-- chains a source with a filter 118-- chains a source with a filter
113function source.chain(src, f) 119function source.chain(src, f)
120 if type(src) ~= 'function' then error('invalid source', 2) end
121 if type(f) ~= 'function' then error('invalid filter', 2) end
114 local co = coroutine.create(function() 122 local co = coroutine.create(function()
115 while true do 123 while true do
116 local chunk, err = src() 124 local chunk, err = src()
@@ -157,6 +165,7 @@ end
157 165
158-- turns a fancy sink into a simple sink 166-- turns a fancy sink into a simple sink
159function sink.simplify(snk) 167function sink.simplify(snk)
168 if type(snk) ~= 'function' then error('invalid sink', 2) end
160 return function(chunk, err) 169 return function(chunk, err)
161 local ret, err_or_new = snk(chunk, err) 170 local ret, err_or_new = snk(chunk, err)
162 if not ret then return nil, err_or_new end 171 if not ret then return nil, err_or_new end
@@ -195,6 +204,8 @@ end
195 204
196-- chains a sink with a filter 205-- chains a sink with a filter
197function sink.chain(f, snk) 206function sink.chain(f, snk)
207 if type(snk) ~= 'function' then error('invalid sink', 2) end
208 if type(f) ~= 'function' then error('invalid filter', 2) end
198 return function(chunk, err) 209 return function(chunk, err)
199 local filtered = f(chunk) 210 local filtered = f(chunk)
200 local done = chunk and "" 211 local done = chunk and ""
@@ -209,6 +220,8 @@ end
209 220
210-- pumps all data from a source to a sink 221-- pumps all data from a source to a sink
211function pump(src, snk) 222function pump(src, snk)
223 if type(src) ~= 'function' then error('invalid source', 2) end
224 if type(snk) ~= 'function' then error('invalid sink', 2) end
212 while true do 225 while true do
213 local chunk, src_err = src() 226 local chunk, src_err = src()
214 local ret, snk_err = snk(chunk, src_err) 227 local ret, snk_err = snk(chunk, src_err)
diff --git a/src/smtp.lua b/src/smtp.lua
index c823c97..ed8bd15 100644
--- a/src/smtp.lua
+++ b/src/smtp.lua
@@ -20,16 +20,17 @@ DOMAIN = os.getenv("SERVER_NAME") or "localhost"
20-- default time zone (means we don't know) 20-- default time zone (means we don't know)
21ZONE = "-0000" 21ZONE = "-0000"
22 22
23function stuff()
24 return ltn12.filter.cycle(dot, 2)
25end
26
27local function shift(a, b, c) 23local function shift(a, b, c)
28 return b, c 24 return b, c
29end 25end
30 26
27-- high level stuffing filter
28function stuff()
29 return ltn12.filter.cycle(dot, 2)
30end
31
31-- send message or throw an exception 32-- send message or throw an exception
32function psend(control, mailt) 33local function send_p(control, mailt)
33 socket.try(control:check("2..")) 34 socket.try(control:check("2.."))
34 socket.try(control:command("EHLO", mailt.domain or DOMAIN)) 35 socket.try(control:command("EHLO", mailt.domain or DOMAIN))
35 socket.try(control:check("2..")) 36 socket.try(control:check("2.."))
@@ -61,11 +62,11 @@ local function newboundary()
61 math.random(0, 99999), seqno) 62 math.random(0, 99999), seqno)
62end 63end
63 64
64-- sendmessage forward declaration 65-- send_message forward declaration
65local sendmessage 66local send_message
66 67
67-- yield multipart message body from a multipart message table 68-- yield multipart message body from a multipart message table
68local function sendmultipart(mesgt) 69local function send_multipart(mesgt)
69 local bd = newboundary() 70 local bd = newboundary()
70 -- define boundary and finish headers 71 -- define boundary and finish headers
71 coroutine.yield('content-type: multipart/mixed; boundary="' .. 72 coroutine.yield('content-type: multipart/mixed; boundary="' ..
@@ -75,7 +76,7 @@ local function sendmultipart(mesgt)
75 -- send each part separated by a boundary 76 -- send each part separated by a boundary
76 for i, m in ipairs(mesgt.body) do 77 for i, m in ipairs(mesgt.body) do
77 coroutine.yield("\r\n--" .. bd .. "\r\n") 78 coroutine.yield("\r\n--" .. bd .. "\r\n")
78 sendmessage(m) 79 send_message(m)
79 end 80 end
80 -- send last boundary 81 -- send last boundary
81 coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n") 82 coroutine.yield("\r\n--" .. bd .. "--\r\n\r\n")
@@ -84,7 +85,7 @@ local function sendmultipart(mesgt)
84end 85end
85 86
86-- yield message body from a source 87-- yield message body from a source
87local function sendsource(mesgt) 88local function send_source(mesgt)
88 -- set content-type if user didn't override 89 -- set content-type if user didn't override
89 if not mesgt.headers or not mesgt.headers["content-type"] then 90 if not mesgt.headers or not mesgt.headers["content-type"] then
90 coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n') 91 coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n')
@@ -101,7 +102,7 @@ local function sendsource(mesgt)
101end 102end
102 103
103-- yield message body from a string 104-- yield message body from a string
104local function sendstring(mesgt) 105local function send_string(mesgt)
105 -- set content-type if user didn't override 106 -- set content-type if user didn't override
106 if not mesgt.headers or not mesgt.headers["content-type"] then 107 if not mesgt.headers or not mesgt.headers["content-type"] then
107 coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n') 108 coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n')
@@ -114,7 +115,7 @@ local function sendstring(mesgt)
114end 115end
115 116
116-- yield the headers one by one 117-- yield the headers one by one
117local function sendheaders(mesgt) 118local function send_headers(mesgt)
118 if mesgt.headers then 119 if mesgt.headers then
119 for i,v in pairs(mesgt.headers) do 120 for i,v in pairs(mesgt.headers) do
120 coroutine.yield(i .. ':' .. v .. "\r\n") 121 coroutine.yield(i .. ':' .. v .. "\r\n")
@@ -123,15 +124,15 @@ local function sendheaders(mesgt)
123end 124end
124 125
125-- message source 126-- message source
126function sendmessage(mesgt) 127function send_message(mesgt)
127 sendheaders(mesgt) 128 send_headers(mesgt)
128 if type(mesgt.body) == "table" then sendmultipart(mesgt) 129 if type(mesgt.body) == "table" then send_multipart(mesgt)
129 elseif type(mesgt.body) == "function" then sendsource(mesgt) 130 elseif type(mesgt.body) == "function" then send_source(mesgt)
130 else sendstring(mesgt) end 131 else send_string(mesgt) end
131end 132end
132 133
133-- set defaul headers 134-- set defaul headers
134local function adjustheaders(mesgt) 135local function adjust_headers(mesgt)
135 mesgt.headers = mesgt.headers or {} 136 mesgt.headers = mesgt.headers or {}
136 mesgt.headers["mime-version"] = "1.0" 137 mesgt.headers["mime-version"] = "1.0"
137 mesgt.headers["date"] = mesgt.headers["date"] or 138 mesgt.headers["date"] = mesgt.headers["date"] or
@@ -140,16 +141,16 @@ local function adjustheaders(mesgt)
140end 141end
141 142
142function message(mesgt) 143function message(mesgt)
143 adjustheaders(mesgt) 144 adjust_headers(mesgt)
144 -- create and return message source 145 -- create and return message source
145 local co = coroutine.create(function() sendmessage(mesgt) end) 146 local co = coroutine.create(function() send_message(mesgt) end)
146 return function() return shift(coroutine.resume(co)) end 147 return function() return shift(coroutine.resume(co)) end
147end 148end
148 149
149function send(mailt) 150function send(mailt)
150 local c, e = socket.tp.connect(mailt.server or SERVER, mailt.port or PORT) 151 local c, e = socket.tp.connect(mailt.server or SERVER, mailt.port or PORT)
151 if not c then return nil, e end 152 if not c then return nil, e end
152 local s, e = pcall(psend, c, mailt) 153 local s, e = pcall(send_p, c, mailt)
153 c:close() 154 c:close()
154 if s then return true 155 if s then return true
155 else return nil, e end 156 else return nil, e end
diff --git a/test/httptest.lua b/test/httptest.lua
index ddeea50..4ec0cc1 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -3,7 +3,7 @@
3-- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi 3-- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi
4-- to "/luasocket-test-cgi" and "/luasocket-test-cgi/" 4-- to "/luasocket-test-cgi" and "/luasocket-test-cgi/"
5-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth 5-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth
6dofile("noglobals.lua") 6dofile("testsupport.lua")
7 7
8local host, proxy, request, response, index_file 8local host, proxy, request, response, index_file
9local ignore, expect, index, prefix, cgiprefix, index_crlf 9local ignore, expect, index, prefix, cgiprefix, index_crlf
@@ -18,33 +18,9 @@ prefix = prefix or "/luasocket-test"
18cgiprefix = cgiprefix or "/luasocket-test-cgi" 18cgiprefix = cgiprefix or "/luasocket-test-cgi"
19index_file = "test/index.html" 19index_file = "test/index.html"
20 20
21local readfile = function(name)
22 local f = io.open(name, "r")
23 if not f then return nil end
24 local s = f:read("*a")
25 f:close()
26 return s
27end
28
29-- read index with CRLF convention 21-- read index with CRLF convention
30index = readfile(index_file) 22index = readfile(index_file)
31 23
32local similar = function(s1, s2)
33 return string.lower(string.gsub(s1 or "", "%s", "")) ==
34 string.lower(string.gsub(s2 or "", "%s", ""))
35end
36
37local fail = function(s)
38 s = s or "failed!"
39 print(s)
40 os.exit()
41end
42
43local check = function (v, e)
44 if v then print("ok")
45 else fail(e) end
46end
47
48local check_result = function(response, expect, ignore) 24local check_result = function(response, expect, ignore)
49 for i,v in response do 25 for i,v in response do
50 if not ignore[i] then 26 if not ignore[i] then
@@ -171,7 +147,7 @@ check_request(request, expect, ignore)
171------------------------------------------------------------------------ 147------------------------------------------------------------------------
172io.write("testing simple post function: ") 148io.write("testing simple post function: ")
173back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index) 149back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index)
174check(back == index) 150assert(back == index)
175 151
176------------------------------------------------------------------------ 152------------------------------------------------------------------------
177io.write("testing ltn12.(sink|source).file: ") 153io.write("testing ltn12.(sink|source).file: ")
@@ -191,7 +167,7 @@ ignore = {
191} 167}
192check_request(request, expect, ignore) 168check_request(request, expect, ignore)
193back = readfile(index_file .. "-back") 169back = readfile(index_file .. "-back")
194check(back == index) 170assert(back == index)
195os.remove(index_file .. "-back") 171os.remove(index_file .. "-back")
196 172
197------------------------------------------------------------------------ 173------------------------------------------------------------------------
@@ -233,7 +209,7 @@ ignore = {
233} 209}
234check_request(request, expect, ignore) 210check_request(request, expect, ignore)
235back = readfile(index_file .. "-back") 211back = readfile(index_file .. "-back")
236check(back == index) 212assert(back == index)
237os.remove(index_file .. "-back") 213os.remove(index_file .. "-back")
238 214
239------------------------------------------------------------------------ 215------------------------------------------------------------------------
@@ -434,7 +410,7 @@ check_request(request, expect, ignore)
434local body 410local body
435io.write("testing simple get function: ") 411io.write("testing simple get function: ")
436body = socket.http.get("http://" .. host .. prefix .. "/index.html") 412body = socket.http.get("http://" .. host .. prefix .. "/index.html")
437check(body == index) 413assert(body == index)
438 414
439------------------------------------------------------------------------ 415------------------------------------------------------------------------
440io.write("testing HEAD method: ") 416io.write("testing HEAD method: ")
@@ -443,7 +419,7 @@ response = socket.http.request {
443 method = "HEAD", 419 method = "HEAD",
444 url = "http://www.cs.princeton.edu/~diego/" 420 url = "http://www.cs.princeton.edu/~diego/"
445} 421}
446check(response and response.headers) 422assert(response and response.headers)
447 423
448------------------------------------------------------------------------ 424------------------------------------------------------------------------
449print("passed all tests") 425print("passed all tests")
diff --git a/test/smtptest.lua b/test/smtptest.lua
index 8468408..e812737 100644
--- a/test/smtptest.lua
+++ b/test/smtptest.lua
@@ -16,7 +16,7 @@ local err
16 16
17dofile("mbox.lua") 17dofile("mbox.lua")
18local parse = mbox.parse 18local parse = mbox.parse
19dofile("noglobals.lua") 19dofile("testsupport.lua")
20 20
21local total = function() 21local total = function()
22 local t = 0 22 local t = 0
diff --git a/test/testsupport.lua b/test/testsupport.lua
new file mode 100644
index 0000000..ca3cd95
--- /dev/null
+++ b/test/testsupport.lua
@@ -0,0 +1,37 @@
1function readfile(name)
2 local f = io.open(name, "r")
3 if not f then return nil end
4 local s = f:read("*a")
5 f:close()
6 return s
7end
8
9function similar(s1, s2)
10 return string.lower(string.gsub(s1 or "", "%s", "")) ==
11 string.lower(string.gsub(s2 or "", "%s", ""))
12end
13
14function fail(msg)
15 msg = msg or "failed"
16 error(msg, 2)
17end
18
19function compare(input, output)
20 local original = readfile(input)
21 local recovered = readfile(output)
22 if original ~= recovered then fail("comparison failed")
23 else print("ok") end
24end
25
26local G = _G
27local set = rawset
28local warn = print
29
30local setglobal = function(table, key, value)
31 warn("changed " .. key)
32 set(table, key, value)
33end
34
35setmetatable(G, {
36 __newindex = setglobal
37})
diff --git a/test/urltest.lua b/test/urltest.lua
index 990a3e5..7e0e73f 100644
--- a/test/urltest.lua
+++ b/test/urltest.lua
@@ -1,4 +1,4 @@
1dofile("noglobals.lua") 1dofile("testsupport.lua")
2 2
3local check_build_url = function(parsed) 3local check_build_url = function(parsed)
4 local built = socket.url.build(parsed) 4 local built = socket.url.build(parsed)