aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-06-12 22:02:21 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-06-12 22:02:21 +0000
commit8b114f3bf4ccea3b065551fa94649a9e45935b5b (patch)
tree06f2faa7e896e9434ba89ec68445ea56e6c8c6dc
parentb22f6f3830515a57a8776e7489b3e2d434abd12f (diff)
downloadluasocket-8b114f3bf4ccea3b065551fa94649a9e45935b5b.tar.gz
luasocket-8b114f3bf4ccea3b065551fa94649a9e45935b5b.tar.bz2
luasocket-8b114f3bf4ccea3b065551fa94649a9e45935b5b.zip
Stupid bug in http.lua.
-rw-r--r--FIX3
-rw-r--r--samples/cddb.lua2
-rw-r--r--src/http.lua53
-rw-r--r--src/mime.lua32
-rw-r--r--src/select.c4
-rw-r--r--src/smtp.lua2
-rw-r--r--src/socket.lua41
-rw-r--r--src/url.lua2
-rw-r--r--test/dicttest.lua2
-rw-r--r--test/httptest.lua4
-rw-r--r--test/smtptest.lua2
-rw-r--r--test/urltest.lua4
12 files changed, 78 insertions, 73 deletions
diff --git a/FIX b/FIX
index ed769d5..a811ec0 100644
--- a/FIX
+++ b/FIX
@@ -7,6 +7,7 @@ get rid of a = socket.try() in the manual, except for protected cases.
7get rid of "base." kludge 7get rid of "base." kludge
8check all "require("http")" etc in the manual. 8check all "require("http")" etc in the manual.
9make sure sock_gethostname.* only return success if the hp is not null! 9make sure sock_gethostname.* only return success if the hp is not null!
10change 'l' prefix in C libraries to 'l-something'... 10change 'l' prefix in C libraries to 'c'
11 don't forget the declarations in luasocket.h and mime.h!!! 11 don't forget the declarations in luasocket.h and mime.h!!!
12setpeername was using udp{unconnected} 12setpeername was using udp{unconnected}
13fixed a bug in http.lua that caused some requests to fail
diff --git a/samples/cddb.lua b/samples/cddb.lua
index 3e48cf6..482dc98 100644
--- a/samples/cddb.lua
+++ b/samples/cddb.lua
@@ -39,7 +39,7 @@ if code == 200 then
39 if not data then 39 if not data then
40 print(error or code) 40 print(error or code)
41 else 41 else
42 for i,v in data do 42 for i,v in pairs(data) do
43 io.write(i, ': ', v, '\n') 43 io.write(i, ': ', v, '\n')
44 end 44 end
45 end 45 end
diff --git a/src/http.lua b/src/http.lua
index 38b93e2..669f54d 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -28,6 +28,51 @@ PORT = 80
28USERAGENT = socket.VERSION 28USERAGENT = socket.VERSION
29 29
30----------------------------------------------------------------------------- 30-----------------------------------------------------------------------------
31-- Extra sources and sinks
32-----------------------------------------------------------------------------
33socket.sourcet["http-chunked"] = function(sock)
34 return base.setmetatable({
35 getfd = function() return sock:getfd() end,
36 dirty = function() return sock:dirty() end
37 }, {
38 __call = function()
39 -- get chunk size, skip extention
40 local line, err = sock:receive()
41 if err then return nil, err end
42 local size = base.tonumber(string.gsub(line, ";.*", ""), 16)
43 if not size then return nil, "invalid chunk size" end
44 -- was it the last chunk?
45 if size <= 0 then
46 -- skip trailer headers, if any
47 local line, err = sock:receive()
48 while not err and line ~= "" do
49 line, err = sock:receive()
50 end
51 return nil, err
52 else
53 -- get chunk and skip terminating CRLF
54 local chunk, err, part = sock:receive(size)
55 if chunk then sock:receive() end
56 return chunk, err
57 end
58 end
59 })
60end
61
62socket.sinkt["http-chunked"] = function(sock)
63 return base.setmetatable({
64 getfd = function() return sock:getfd() end,
65 dirty = function() return sock:dirty() end
66 }, {
67 __call = function(self, chunk, err)
68 if not chunk then return sock:send("0\r\n\r\n") end
69 local size = string.format("%X\r\n", string.len(chunk))
70 return sock:send(size .. chunk .. "\r\n")
71 end
72 })
73end
74
75-----------------------------------------------------------------------------
31-- Low level HTTP API 76-- Low level HTTP API
32----------------------------------------------------------------------------- 77-----------------------------------------------------------------------------
33local metat = { __index = {} } 78local metat = { __index = {} }
@@ -70,7 +115,7 @@ function metat.__index:sendheaders(headers)
70end 115end
71 116
72function metat.__index:sendbody(headers, source, step) 117function metat.__index:sendbody(headers, source, step)
73 source = source or ltn12.source.empty() 118 source = source or ltn12.source.empty()
74 step = step or ltn12.pump.step 119 step = step or ltn12.pump.step
75 -- if we don't know the size in advance, send chunked and hope for the best 120 -- if we don't know the size in advance, send chunked and hope for the best
76 local mode = "http-chunked" 121 local mode = "http-chunked"
@@ -155,7 +200,7 @@ end
155local function adjustheaders(headers, host) 200local function adjustheaders(headers, host)
156 local lower = {} 201 local lower = {}
157 -- override with user values 202 -- override with user values
158 for i,v in (headers or lower) do 203 for i,v in pairs(headers or lower) do
159 lower[string.lower(i)] = v 204 lower[string.lower(i)] = v
160 end 205 end
161 lower["user-agent"] = lower["user-agent"] or USERAGENT 206 lower["user-agent"] = lower["user-agent"] or USERAGENT
@@ -175,7 +220,7 @@ local function adjustrequest(reqt)
175 local nreqt = reqt.url and url.parse(reqt.url, default) or {} 220 local nreqt = reqt.url and url.parse(reqt.url, default) or {}
176 local t = url.parse(reqt.url, default) 221 local t = url.parse(reqt.url, default)
177 -- explicit components override url 222 -- explicit components override url
178 for i,v in reqt do nreqt[i] = reqt[i] end 223 for i,v in pairs(reqt) do nreqt[i] = v end
179 socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'") 224 socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'")
180 -- compute uri if user hasn't overriden 225 -- compute uri if user hasn't overriden
181 nreqt.uri = reqt.uri or adjusturi(nreqt) 226 nreqt.uri = reqt.uri or adjusturi(nreqt)
@@ -238,7 +283,7 @@ function trequest(reqt)
238 local h = open(reqt.host, reqt.port, reqt.connect) 283 local h = open(reqt.host, reqt.port, reqt.connect)
239 h:sendrequestline(reqt.method, reqt.uri) 284 h:sendrequestline(reqt.method, reqt.uri)
240 h:sendheaders(reqt.headers) 285 h:sendheaders(reqt.headers)
241 h:sendbody(reqt.headers, reqt.source, reqt.step) 286 if reqt.source then h:sendbody(reqt.headers, reqt.source, reqt.step) end
242 local code, headers, status 287 local code, headers, status
243 code, status = h:receivestatusline() 288 code, status = h:receivestatusline()
244 headers = h:receiveheaders() 289 headers = h:receiveheaders()
diff --git a/src/mime.lua b/src/mime.lua
index 6492a96..6ef82b8 100644
--- a/src/mime.lua
+++ b/src/mime.lua
@@ -15,9 +15,9 @@ local mime = require("cmime")
15module("mime") 15module("mime")
16 16
17-- encode, decode and wrap algorithm tables 17-- encode, decode and wrap algorithm tables
18mime.encodet = {} 18encodet = {}
19mime.decodet = {} 19decodet = {}
20mime.wrapt = {} 20wrapt = {}
21 21
22-- creates a function that chooses a filter by name from a given table 22-- creates a function that chooses a filter by name from a given table
23local function choose(table) 23local function choose(table)
@@ -32,21 +32,21 @@ local function choose(table)
32end 32end
33 33
34-- define the encoding filters 34-- define the encoding filters
35mime.encodet['base64'] = function() 35encodet['base64'] = function()
36 return ltn12.filter.cycle(b64, "") 36 return ltn12.filter.cycle(b64, "")
37end 37end
38 38
39mime.encodet['quoted-printable'] = function(mode) 39encodet['quoted-printable'] = function(mode)
40 return ltn12.filter.cycle(qp, "", 40 return ltn12.filter.cycle(qp, "",
41 (mode == "binary") and "=0D=0A" or "\r\n") 41 (mode == "binary") and "=0D=0A" or "\r\n")
42end 42end
43 43
44-- define the decoding filters 44-- define the decoding filters
45mime.decodet['base64'] = function() 45decodet['base64'] = function()
46 return ltn12.filter.cycle(unb64, "") 46 return ltn12.filter.cycle(unb64, "")
47end 47end
48 48
49mime.decodet['quoted-printable'] = function() 49decodet['quoted-printable'] = function()
50 return ltn12.filter.cycle(unqp, "") 50 return ltn12.filter.cycle(unqp, "")
51end 51end
52 52
@@ -60,29 +60,29 @@ local function format(chunk)
60end 60end
61 61
62-- define the line-wrap filters 62-- define the line-wrap filters
63mime.wrapt['text'] = function(length) 63wrapt['text'] = function(length)
64 length = length or 76 64 length = length or 76
65 return ltn12.filter.cycle(wrp, length, length) 65 return ltn12.filter.cycle(wrp, length, length)
66end 66end
67mime.wrapt['base64'] = wrapt['text'] 67wrapt['base64'] = wrapt['text']
68mime.wrapt['default'] = wrapt['text'] 68wrapt['default'] = wrapt['text']
69 69
70mime.wrapt['quoted-printable'] = function() 70wrapt['quoted-printable'] = function()
71 return ltn12.filter.cycle(qpwrp, 76, 76) 71 return ltn12.filter.cycle(qpwrp, 76, 76)
72end 72end
73 73
74-- function that choose the encoding, decoding or wrap algorithm 74-- function that choose the encoding, decoding or wrap algorithm
75mime.encode = choose(encodet) 75encode = choose(encodet)
76mime.decode = choose(decodet) 76decode = choose(decodet)
77mime.wrap = choose(wrapt) 77wrap = choose(wrapt)
78 78
79-- define the end-of-line normalization filter 79-- define the end-of-line normalization filter
80function mime.normalize(marker) 80function normalize(marker)
81 return ltn12.filter.cycle(eol, 0, marker) 81 return ltn12.filter.cycle(eol, 0, marker)
82end 82end
83 83
84-- high level stuffing filter 84-- high level stuffing filter
85function mime.stuff() 85function stuff()
86 return ltn12.filter.cycle(dot, 2) 86 return ltn12.filter.cycle(dot, 2)
87end 87end
88 88
diff --git a/src/select.c b/src/select.c
index efdbdff..e2cd91d 100644
--- a/src/select.c
+++ b/src/select.c
@@ -124,7 +124,7 @@ static int collect_fd(lua_State *L, int tab, int max_fd,
124 break; 124 break;
125 } 125 }
126 fd = getfd(L); 126 fd = getfd(L);
127 if (fd > 0) { 127 if (fd >= 0) {
128 FD_SET(fd, set); 128 FD_SET(fd, set);
129 if (max_fd < fd) max_fd = fd; 129 if (max_fd < fd) max_fd = fd;
130 lua_pushnumber(L, fd); 130 lua_pushnumber(L, fd);
@@ -150,7 +150,7 @@ static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) {
150 break; 150 break;
151 } 151 }
152 fd = getfd(L); 152 fd = getfd(L);
153 if (fd > 0 && dirty(L)) { 153 if (fd >= 0 && dirty(L)) {
154 lua_pushnumber(L, ++ndirty); 154 lua_pushnumber(L, ++ndirty);
155 lua_pushvalue(L, -2); 155 lua_pushvalue(L, -2);
156 lua_settable(L, dtab); 156 lua_settable(L, dtab);
diff --git a/src/smtp.lua b/src/smtp.lua
index 12c2195..944a40f 100644
--- a/src/smtp.lua
+++ b/src/smtp.lua
@@ -211,7 +211,7 @@ end
211-- set defaul headers 211-- set defaul headers
212local function adjust_headers(mesgt) 212local function adjust_headers(mesgt)
213 local lower = {} 213 local lower = {}
214 for i,v in (mesgt.headers or lower) do 214 for i,v in base.pairs(mesgt.headers or lower) do
215 lower[string.lower(i)] = v 215 lower[string.lower(i)] = v
216 end 216 end
217 lower["date"] = lower["date"] or 217 lower["date"] = lower["date"] or
diff --git a/src/socket.lua b/src/socket.lua
index f3563e7..9a523fe 100644
--- a/src/socket.lua
+++ b/src/socket.lua
@@ -62,19 +62,6 @@ socket.sinkt = {}
62 62
63socket.BLOCKSIZE = 2048 63socket.BLOCKSIZE = 2048
64 64
65socket.sinkt["http-chunked"] = function(sock)
66 return base.setmetatable({
67 getfd = function() return sock:getfd() end,
68 dirty = function() return sock:dirty() end
69 }, {
70 __call = function(self, chunk, err)
71 if not chunk then return sock:send("0\r\n\r\n") end
72 local size = string.format("%X\r\n", string.len(chunk))
73 return sock:send(size .. chunk .. "\r\n")
74 end
75 })
76end
77
78socket.sinkt["close-when-done"] = function(sock) 65socket.sinkt["close-when-done"] = function(sock)
79 return base.setmetatable({ 66 return base.setmetatable({
80 getfd = function() return sock:getfd() end, 67 getfd = function() return sock:getfd() end,
@@ -140,34 +127,6 @@ socket.sourcet["until-closed"] = function(sock)
140 }) 127 })
141end 128end
142 129
143socket.sourcet["http-chunked"] = function(sock)
144 return base.setmetatable({
145 getfd = function() return sock:getfd() end,
146 dirty = function() return sock:dirty() end
147 }, {
148 __call = function()
149 -- get chunk size, skip extention
150 local line, err = sock:receive()
151 if err then return nil, err end
152 local size = base.tonumber(string.gsub(line, ";.*", ""), 16)
153 if not size then return nil, "invalid chunk size" end
154 -- was it the last chunk?
155 if size <= 0 then
156 -- skip trailer headers, if any
157 local line, err = sock:receive()
158 while not err and line ~= "" do
159 line, err = sock:receive()
160 end
161 return nil, err
162 else
163 -- get chunk and skip terminating CRLF
164 local chunk, err = sock:receive(size)
165 if chunk then sock:receive() end
166 return chunk, err
167 end
168 end
169 })
170end
171 130
172socket.sourcet["default"] = socket.sourcet["until-closed"] 131socket.sourcet["default"] = socket.sourcet["until-closed"]
173 132
diff --git a/src/url.lua b/src/url.lua
index 7aa3760..f29ecc8 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -119,7 +119,7 @@ end
119function parse(url, default) 119function parse(url, default)
120 -- initialize default parameters 120 -- initialize default parameters
121 local parsed = {} 121 local parsed = {}
122 for i,v in (default or parsed) do parsed[i] = v end 122 for i,v in base.pairs(default or parsed) do parsed[i] = v end
123 -- empty url is parsed to nil 123 -- empty url is parsed to nil
124 if not url or url == "" then return nil, "invalid url" end 124 if not url or url == "" then return nil, "invalid url" end
125 -- remove whitespace 125 -- remove whitespace
diff --git a/test/dicttest.lua b/test/dicttest.lua
index 7ac7811..9ab9c41 100644
--- a/test/dicttest.lua
+++ b/test/dicttest.lua
@@ -1,3 +1,3 @@
1local dict = require"socket.dict" 1local dict = require"socket.dict"
2 2
3for i,v in dict.get("dict://localhost/d:teste") do print(v) end 3for i,v in pairs(dict.get("dict://localhost/d:teste")) do print(v) end
diff --git a/test/httptest.lua b/test/httptest.lua
index 2335fcb..963b947 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -32,7 +32,7 @@ index_file = "test/index.html"
32index = readfile(index_file) 32index = readfile(index_file)
33 33
34local check_result = function(response, expect, ignore) 34local check_result = function(response, expect, ignore)
35 for i,v in response do 35 for i,v in pairs(response) do
36 if not ignore[i] then 36 if not ignore[i] then
37 if v ~= expect[i] then 37 if v ~= expect[i] then
38 local f = io.open("err", "w") 38 local f = io.open("err", "w")
@@ -42,7 +42,7 @@ local check_result = function(response, expect, ignore)
42 end 42 end
43 end 43 end
44 end 44 end
45 for i,v in expect do 45 for i,v in pairs(expect) do
46 if not ignore[i] then 46 if not ignore[i] then
47 if v ~= response[i] then 47 if v ~= response[i] then
48 local f = io.open("err", "w") 48 local f = io.open("err", "w")
diff --git a/test/smtptest.lua b/test/smtptest.lua
index e812737..d0c938c 100644
--- a/test/smtptest.lua
+++ b/test/smtptest.lua
@@ -70,7 +70,7 @@ end
70local check_headers = function(sent, got) 70local check_headers = function(sent, got)
71 sent = sent or {} 71 sent = sent or {}
72 got = got or {} 72 got = got or {}
73 for i,v in sent do 73 for i,v in pairs(sent) do
74 if not similar(v, got[i]) then fail("header " .. v .. "failed!") end 74 if not similar(v, got[i]) then fail("header " .. v .. "failed!") end
75 end 75 end
76end 76end
diff --git a/test/urltest.lua b/test/urltest.lua
index 570b52e..ae54db9 100644
--- a/test/urltest.lua
+++ b/test/urltest.lua
@@ -75,7 +75,7 @@ local check_parse_url = function(gaba)
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")
78 for i,v in parsed do print(i,v) end 78 for i,v in pairs(parsed) do print(i,v) end
79 exit() 79 exit()
80 end 80 end
81 end 81 end
@@ -83,7 +83,7 @@ local check_parse_url = function(gaba)
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")
86 for i,v in parsed do print(i,v) end 86 for i,v in pairs(parsed) do print(i,v) end
87 exit() 87 exit()
88 end 88 end
89 end 89 end