diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-08-12 05:56:32 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2005-08-12 05:56:32 +0000 |
| commit | 0c3cdd5ef2485a79d6fec9261f2850c41577d5b3 (patch) | |
| tree | d69164c9f815e2d0308ba3f0d15b18e67163d879 /src | |
| parent | 37f7af4b9f1250e3c3439df03d43cf291a4d6f37 (diff) | |
| download | luasocket-0c3cdd5ef2485a79d6fec9261f2850c41577d5b3.tar.gz luasocket-0c3cdd5ef2485a79d6fec9261f2850c41577d5b3.tar.bz2 luasocket-0c3cdd5ef2485a79d6fec9261f2850c41577d5b3.zip | |
Final push for release...
Diffstat (limited to 'src')
| -rw-r--r-- | src/ftp.lua | 12 | ||||
| -rw-r--r-- | src/http.lua | 45 | ||||
| -rw-r--r-- | src/ltn12.lua | 12 | ||||
| -rw-r--r-- | src/luasocket.c | 2 | ||||
| -rw-r--r-- | src/makefile | 87 | ||||
| -rw-r--r-- | src/mime.lua | 3 | ||||
| -rw-r--r-- | src/smtp.lua | 29 | ||||
| -rw-r--r-- | src/socket.lua | 3 | ||||
| -rw-r--r-- | src/ssl.c | 70 | ||||
| -rw-r--r-- | src/ssl.h | 29 | ||||
| -rw-r--r-- | src/tp.lua | 9 | ||||
| -rw-r--r-- | src/url.lua | 2 | ||||
| -rw-r--r-- | src/wsocket.c | 5 |
13 files changed, 145 insertions, 163 deletions
diff --git a/src/ftp.lua b/src/ftp.lua index 841df5f..226e04c 100644 --- a/src/ftp.lua +++ b/src/ftp.lua | |||
| @@ -17,6 +17,7 @@ local url = require("socket.url") | |||
| 17 | local tp = require("socket.tp") | 17 | local tp = require("socket.tp") |
| 18 | local ltn12 = require("ltn12") | 18 | local ltn12 = require("ltn12") |
| 19 | module("socket.ftp") | 19 | module("socket.ftp") |
| 20 | getmetatable(_M).__index = nil | ||
| 20 | 21 | ||
| 21 | ----------------------------------------------------------------------------- | 22 | ----------------------------------------------------------------------------- |
| 22 | -- Program constants | 23 | -- Program constants |
| @@ -35,8 +36,8 @@ PASSWORD = "anonymous@anonymous.org" | |||
| 35 | ----------------------------------------------------------------------------- | 36 | ----------------------------------------------------------------------------- |
| 36 | local metat = { __index = {} } | 37 | local metat = { __index = {} } |
| 37 | 38 | ||
| 38 | function open(server, port) | 39 | function open(server, port, create) |
| 39 | local tp = socket.try(tp.connect(server, port or PORT, TIMEOUT)) | 40 | local tp = socket.try(tp.connect(server, port or PORT, create, TIMEOUT)) |
| 40 | local f = base.setmetatable({ tp = tp }, metat) | 41 | local f = base.setmetatable({ tp = tp }, metat) |
| 41 | -- make sure everything gets closed in an exception | 42 | -- make sure everything gets closed in an exception |
| 42 | f.try = socket.newtry(function() f:close() end) | 43 | f.try = socket.newtry(function() f:close() end) |
| @@ -199,7 +200,7 @@ end | |||
| 199 | local function tput(putt) | 200 | local function tput(putt) |
| 200 | putt = override(putt) | 201 | putt = override(putt) |
| 201 | socket.try(putt.host, "missing hostname") | 202 | socket.try(putt.host, "missing hostname") |
| 202 | local f = open(putt.host, putt.port) | 203 | local f = open(putt.host, putt.port, putt.create) |
| 203 | f:greet() | 204 | f:greet() |
| 204 | f:login(putt.user, putt.password) | 205 | f:login(putt.user, putt.password) |
| 205 | if putt.type then f:type(putt.type) end | 206 | if putt.type then f:type(putt.type) end |
| @@ -242,7 +243,7 @@ end) | |||
| 242 | local function tget(gett) | 243 | local function tget(gett) |
| 243 | gett = override(gett) | 244 | gett = override(gett) |
| 244 | socket.try(gett.host, "missing hostname") | 245 | socket.try(gett.host, "missing hostname") |
| 245 | local f = open(gett.host, gett.port) | 246 | local f = open(gett.host, gett.port, gett.create) |
| 246 | f:greet() | 247 | f:greet() |
| 247 | f:login(gett.user, gett.password) | 248 | f:login(gett.user, gett.password) |
| 248 | if gett.type then f:type(gett.type) end | 249 | if gett.type then f:type(gett.type) end |
| @@ -264,7 +265,7 @@ command = socket.protect(function(cmdt) | |||
| 264 | cmdt = override(cmdt) | 265 | cmdt = override(cmdt) |
| 265 | socket.try(cmdt.host, "missing hostname") | 266 | socket.try(cmdt.host, "missing hostname") |
| 266 | socket.try(cmdt.command, "missing command") | 267 | socket.try(cmdt.command, "missing command") |
| 267 | local f = open(cmdt.host, cmdt.port) | 268 | local f = open(cmdt.host, cmdt.port, cmdt.create) |
| 268 | f:greet() | 269 | f:greet() |
| 269 | f:login(cmdt.user, cmdt.password) | 270 | f:login(cmdt.user, cmdt.password) |
| 270 | f.try(f.tp:command(cmdt.command, cmdt.argument)) | 271 | f.try(f.tp:command(cmdt.command, cmdt.argument)) |
| @@ -278,4 +279,3 @@ get = socket.protect(function(gett) | |||
| 278 | else return tget(gett) end | 279 | else return tget(gett) end |
| 279 | end) | 280 | end) |
| 280 | 281 | ||
| 281 | --getmetatable(_M).__index = nil | ||
diff --git a/src/http.lua b/src/http.lua index 91c52da..9434d97 100644 --- a/src/http.lua +++ b/src/http.lua | |||
| @@ -16,6 +16,7 @@ local string = require("string") | |||
| 16 | local base = _G | 16 | local base = _G |
| 17 | local table = require("table") | 17 | local table = require("table") |
| 18 | module("socket.http") | 18 | module("socket.http") |
| 19 | getmetatable(_M).__index = nil | ||
| 19 | 20 | ||
| 20 | ----------------------------------------------------------------------------- | 21 | ----------------------------------------------------------------------------- |
| 21 | -- Program constants | 22 | -- Program constants |
| @@ -105,26 +106,16 @@ end | |||
| 105 | ----------------------------------------------------------------------------- | 106 | ----------------------------------------------------------------------------- |
| 106 | local metat = { __index = {} } | 107 | local metat = { __index = {} } |
| 107 | 108 | ||
| 108 | -- default connect function, respecting the timeout | ||
| 109 | local function connect(host, port, create) | ||
| 110 | local c, e = (create or socket.tcp)() | ||
| 111 | if not c then return nil, e end | ||
| 112 | c:settimeout(TIMEOUT) | ||
| 113 | local r, e = c:connect(host, port or PORT) | ||
| 114 | if not r then | ||
| 115 | c:close() | ||
| 116 | return nil, e | ||
| 117 | end | ||
| 118 | return c | ||
| 119 | end | ||
| 120 | |||
| 121 | function open(host, port, create) | 109 | function open(host, port, create) |
| 122 | -- create socket with user connect function, or with default | 110 | -- create socket with user connect function, or with default |
| 123 | local c = socket.try(connect(host, port, create)) | 111 | local c = socket.try(create or socket.tcp)() |
| 124 | -- create our http request object, pointing to the socket | ||
| 125 | local h = base.setmetatable({ c = c }, metat) | 112 | local h = base.setmetatable({ c = c }, metat) |
| 126 | -- make sure the object close gets called on exception | 113 | -- create finalized try |
| 127 | h.try = socket.newtry(function() h:close() end) | 114 | h.try = socket.newtry(function() h:close() end) |
| 115 | -- set timeout before connecting | ||
| 116 | h.try(c:settimeout(TIMEOUT)) | ||
| 117 | h.try(c:connect(host, port or PORT)) | ||
| 118 | -- here everything worked | ||
| 128 | return h | 119 | return h |
| 129 | end | 120 | end |
| 130 | 121 | ||
| @@ -134,11 +125,11 @@ function metat.__index:sendrequestline(method, uri) | |||
| 134 | end | 125 | end |
| 135 | 126 | ||
| 136 | function metat.__index:sendheaders(headers) | 127 | function metat.__index:sendheaders(headers) |
| 128 | local h = "\r\n" | ||
| 137 | for i, v in base.pairs(headers) do | 129 | for i, v in base.pairs(headers) do |
| 138 | self.try(self.c:send(i .. ": " .. v .. "\r\n")) | 130 | h = i .. ": " .. v .. "\r\n" .. h |
| 139 | end | 131 | end |
| 140 | -- mark end of request headers | 132 | self.try(self.c:send(h)) |
| 141 | self.try(self.c:send("\r\n")) | ||
| 142 | return 1 | 133 | return 1 |
| 143 | end | 134 | end |
| 144 | 135 | ||
| @@ -213,7 +204,7 @@ local function adjustheaders(headers, host) | |||
| 213 | ["te"] = "trailers" | 204 | ["te"] = "trailers" |
| 214 | } | 205 | } |
| 215 | -- override with user headers | 206 | -- override with user headers |
| 216 | for i,v in pairs(headers or lower) do | 207 | for i,v in base.pairs(headers or lower) do |
| 217 | lower[string.lower(i)] = v | 208 | lower[string.lower(i)] = v |
| 218 | end | 209 | end |
| 219 | return lower | 210 | return lower |
| @@ -232,7 +223,7 @@ local function adjustrequest(reqt) | |||
| 232 | local nreqt = reqt.url and url.parse(reqt.url, default) or {} | 223 | local nreqt = reqt.url and url.parse(reqt.url, default) or {} |
| 233 | local t = url.parse(reqt.url, default) | 224 | local t = url.parse(reqt.url, default) |
| 234 | -- explicit components override url | 225 | -- explicit components override url |
| 235 | for i,v in pairs(reqt) do nreqt[i] = v end | 226 | for i,v in base.pairs(reqt) do nreqt[i] = v end |
| 236 | socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'") | 227 | socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'") |
| 237 | -- compute uri if user hasn't overriden | 228 | -- compute uri if user hasn't overriden |
| 238 | nreqt.uri = reqt.uri or adjusturi(nreqt) | 229 | nreqt.uri = reqt.uri or adjusturi(nreqt) |
| @@ -276,11 +267,11 @@ function tauthorize(reqt) | |||
| 276 | return trequest(reqt) | 267 | return trequest(reqt) |
| 277 | end | 268 | end |
| 278 | 269 | ||
| 279 | function tredirect(reqt, headers) | 270 | function tredirect(reqt, location) |
| 280 | return trequest { | 271 | local result, code, headers, status = trequest { |
| 281 | -- the RFC says the redirect URL has to be absolute, but some | 272 | -- the RFC says the redirect URL has to be absolute, but some |
| 282 | -- servers do not respect that | 273 | -- servers do not respect that |
| 283 | url = url.absolute(reqt, headers["location"]), | 274 | url = url.absolute(reqt, location), |
| 284 | source = reqt.source, | 275 | source = reqt.source, |
| 285 | sink = reqt.sink, | 276 | sink = reqt.sink, |
| 286 | headers = reqt.headers, | 277 | headers = reqt.headers, |
| @@ -288,6 +279,9 @@ function tredirect(reqt, headers) | |||
| 288 | nredirects = (reqt.nredirects or 0) + 1, | 279 | nredirects = (reqt.nredirects or 0) + 1, |
| 289 | connect = reqt.connect | 280 | connect = reqt.connect |
| 290 | } | 281 | } |
| 282 | -- pass location header back as a hint we redirected | ||
| 283 | headers.location = headers.location or location | ||
| 284 | return result, code, headers, status | ||
| 291 | end | 285 | end |
| 292 | 286 | ||
| 293 | function trequest(reqt) | 287 | function trequest(reqt) |
| @@ -301,7 +295,7 @@ function trequest(reqt) | |||
| 301 | headers = h:receiveheaders() | 295 | headers = h:receiveheaders() |
| 302 | if shouldredirect(reqt, code, headers) then | 296 | if shouldredirect(reqt, code, headers) then |
| 303 | h:close() | 297 | h:close() |
| 304 | return tredirect(reqt, headers) | 298 | return tredirect(reqt, headers.location) |
| 305 | elseif shouldauthorize(reqt, code) then | 299 | elseif shouldauthorize(reqt, code) then |
| 306 | h:close() | 300 | h:close() |
| 307 | return tauthorize(reqt) | 301 | return tauthorize(reqt) |
| @@ -332,4 +326,3 @@ request = socket.protect(function(reqt, body) | |||
| 332 | else return trequest(reqt) end | 326 | else return trequest(reqt) end |
| 333 | end) | 327 | end) |
| 334 | 328 | ||
| 335 | --getmetatable(_M).__index = nil | ||
diff --git a/src/ltn12.lua b/src/ltn12.lua index fbc9dce..633e0d7 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
| @@ -12,6 +12,7 @@ local string = require("string") | |||
| 12 | local table = require("table") | 12 | local table = require("table") |
| 13 | local base = _G | 13 | local base = _G |
| 14 | module("ltn12") | 14 | module("ltn12") |
| 15 | getmetatable(_M).__index = nil | ||
| 15 | 16 | ||
| 16 | filter = {} | 17 | filter = {} |
| 17 | source = {} | 18 | source = {} |
| @@ -134,8 +135,6 @@ function source.rewind(src) | |||
| 134 | end | 135 | end |
| 135 | end | 136 | end |
| 136 | 137 | ||
| 137 | local print = print | ||
| 138 | |||
| 139 | -- chains a source with a filter | 138 | -- chains a source with a filter |
| 140 | function source.chain(src, f) | 139 | function source.chain(src, f) |
| 141 | base.assert(src and f) | 140 | base.assert(src and f) |
| @@ -258,7 +257,8 @@ end | |||
| 258 | function pump.step(src, snk) | 257 | function pump.step(src, snk) |
| 259 | local chunk, src_err = src() | 258 | local chunk, src_err = src() |
| 260 | local ret, snk_err = snk(chunk, src_err) | 259 | local ret, snk_err = snk(chunk, src_err) |
| 261 | return chunk and ret and not src_err and not snk_err, src_err or snk_err | 260 | if chunk and ret then return 1 |
| 261 | else return nil, src_err or snk_err end | ||
| 262 | end | 262 | end |
| 263 | 263 | ||
| 264 | -- pumps all data from a source to a sink, using a step function | 264 | -- pumps all data from a source to a sink, using a step function |
| @@ -267,8 +267,10 @@ function pump.all(src, snk, step) | |||
| 267 | step = step or pump.step | 267 | step = step or pump.step |
| 268 | while true do | 268 | while true do |
| 269 | local ret, err = step(src, snk) | 269 | local ret, err = step(src, snk) |
| 270 | if not ret then return not err, err end | 270 | if not ret then |
| 271 | if err then return nil, err | ||
| 272 | else return 1 end | ||
| 273 | end | ||
| 271 | end | 274 | end |
| 272 | end | 275 | end |
| 273 | 276 | ||
| 274 | --getmetatable(_M).__index = nil | ||
diff --git a/src/luasocket.c b/src/luasocket.c index ed26b1f..94ea05b 100644 --- a/src/luasocket.c +++ b/src/luasocket.c | |||
| @@ -108,7 +108,7 @@ static int base_open(lua_State *L) { | |||
| 108 | /*-------------------------------------------------------------------------*\ | 108 | /*-------------------------------------------------------------------------*\ |
| 109 | * Initializes all library modules. | 109 | * Initializes all library modules. |
| 110 | \*-------------------------------------------------------------------------*/ | 110 | \*-------------------------------------------------------------------------*/ |
| 111 | LUASOCKET_API int luaopen_socketcore(lua_State *L) { | 111 | LUASOCKET_API int luaopen_socket_core(lua_State *L) { |
| 112 | int i; | 112 | int i; |
| 113 | base_open(L); | 113 | base_open(L); |
| 114 | for (i = 0; mod[i].name; i++) mod[i].func(L); | 114 | for (i = 0; mod[i].name; i++) mod[i].func(L); |
diff --git a/src/makefile b/src/makefile new file mode 100644 index 0000000..7ef18bf --- /dev/null +++ b/src/makefile | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | #------ | ||
| 2 | # Load configuration | ||
| 3 | # | ||
| 4 | include ../config | ||
| 5 | |||
| 6 | #------ | ||
| 7 | # Hopefully no need to change anything below this line | ||
| 8 | # | ||
| 9 | |||
| 10 | #------ | ||
| 11 | # Modules belonging to socket-core | ||
| 12 | # | ||
| 13 | SOCKET_OBJS:= \ | ||
| 14 | luasocket.o \ | ||
| 15 | timeout.o \ | ||
| 16 | buffer.o \ | ||
| 17 | io.o \ | ||
| 18 | auxiliar.o \ | ||
| 19 | options.o \ | ||
| 20 | inet.o \ | ||
| 21 | tcp.o \ | ||
| 22 | udp.o \ | ||
| 23 | except.o \ | ||
| 24 | select.o \ | ||
| 25 | $(COMPAT)/compat-5.1.o \ | ||
| 26 | usocket.o | ||
| 27 | |||
| 28 | #------ | ||
| 29 | # Modules belonging mime-core | ||
| 30 | # | ||
| 31 | MIME_OBJS:=\ | ||
| 32 | mime.o \ | ||
| 33 | $(COMPAT)/compat-5.1.o | ||
| 34 | |||
| 35 | #------ | ||
| 36 | # Modules belonging unix (local domain sockets) | ||
| 37 | # | ||
| 38 | UNIX_OBJS:=\ | ||
| 39 | buffer.o \ | ||
| 40 | auxiliar.o \ | ||
| 41 | options.o \ | ||
| 42 | timeout.o \ | ||
| 43 | io.o \ | ||
| 44 | usocket.o \ | ||
| 45 | unix.o | ||
| 46 | |||
| 47 | all: $(SOCKET_SO) $(MIME_SO) | ||
| 48 | |||
| 49 | $(SOCKET_SO): $(SOCKET_OBJS) | ||
| 50 | $(LD) $(LDFLAGS) -o $@ $^ | ||
| 51 | |||
| 52 | $(MIME_SO): $(MIME_OBJS) | ||
| 53 | $(LD) $(LDFLAGS) -o $@ $^ | ||
| 54 | |||
| 55 | $(UNIX_SO): $(UNIX_OBJS) | ||
| 56 | $(LD) $(LDFLAGS) -o $@ $^ | ||
| 57 | |||
| 58 | #------ | ||
| 59 | # List of dependencies | ||
| 60 | # | ||
| 61 | auxiliar.o: auxiliar.c auxiliar.h | ||
| 62 | buffer.o: buffer.c buffer.h io.h timeout.h | ||
| 63 | except.o: except.c except.h | ||
| 64 | inet.o: inet.c inet.h socket.h io.h timeout.h usocket.h | ||
| 65 | io.o: io.c io.h timeout.h | ||
| 66 | luasocket.o: luasocket.c luasocket.h auxiliar.h except.h timeout.h \ | ||
| 67 | buffer.h io.h inet.h socket.h usocket.h tcp.h udp.h select.h | ||
| 68 | mime.o: mime.c mime.h | ||
| 69 | options.o: options.c auxiliar.h options.h socket.h io.h timeout.h \ | ||
| 70 | usocket.h inet.h | ||
| 71 | select.o: select.c socket.h io.h timeout.h usocket.h select.h | ||
| 72 | tcp.o: tcp.c auxiliar.h socket.h io.h timeout.h usocket.h inet.h \ | ||
| 73 | options.h tcp.h buffer.h | ||
| 74 | timeout.o: timeout.c auxiliar.h timeout.h | ||
| 75 | udp.o: udp.c auxiliar.h socket.h io.h timeout.h usocket.h inet.h \ | ||
| 76 | options.h udp.h | ||
| 77 | unix.o: unix.c auxiliar.h socket.h io.h timeout.h usocket.h options.h \ | ||
| 78 | unix.h buffer.h | ||
| 79 | usocket.o: usocket.c socket.h io.h timeout.h usocket.h | ||
| 80 | |||
| 81 | clean: | ||
| 82 | rm -f $(SOCKET_SO) $(SOCKET_OBJS) | ||
| 83 | rm -f $(MIME_SO) $(UNIX_SO) $(MIME_OBJS) $(UNIX_OBJS) | ||
| 84 | |||
| 85 | #------ | ||
| 86 | # End of makefile configuration | ||
| 87 | # | ||
diff --git a/src/mime.lua b/src/mime.lua index e112f8a..af42dcd 100644 --- a/src/mime.lua +++ b/src/mime.lua | |||
| @@ -14,6 +14,7 @@ local mime = require("mime.core") | |||
| 14 | local io = require("io") | 14 | local io = require("io") |
| 15 | local string = require("string") | 15 | local string = require("string") |
| 16 | module("mime") | 16 | module("mime") |
| 17 | getmetatable(_M).__index = nil | ||
| 17 | 18 | ||
| 18 | -- encode, decode and wrap algorithm tables | 19 | -- encode, decode and wrap algorithm tables |
| 19 | encodet = {} | 20 | encodet = {} |
| @@ -84,5 +85,3 @@ end | |||
| 84 | function stuff() | 85 | function stuff() |
| 85 | return ltn12.filter.cycle(dot, 2) | 86 | return ltn12.filter.cycle(dot, 2) |
| 86 | end | 87 | end |
| 87 | |||
| 88 | --getmetatable(_M).__index = nil | ||
diff --git a/src/smtp.lua b/src/smtp.lua index 6850f7f..46df1ab 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
| @@ -18,6 +18,7 @@ local tp = require("socket.tp") | |||
| 18 | local ltn12 = require("ltn12") | 18 | local ltn12 = require("ltn12") |
| 19 | local mime = require("mime") | 19 | local mime = require("mime") |
| 20 | module("socket.smtp") | 20 | module("socket.smtp") |
| 21 | getmetatable(_M).__index = nil | ||
| 21 | 22 | ||
| 22 | ----------------------------------------------------------------------------- | 23 | ----------------------------------------------------------------------------- |
| 23 | -- Program constants | 24 | -- Program constants |
| @@ -111,12 +112,12 @@ function metat.__index:send(mailt) | |||
| 111 | self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step) | 112 | self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step) |
| 112 | end | 113 | end |
| 113 | 114 | ||
| 114 | function open(server, port) | 115 | function open(server, port, create) |
| 115 | local tp = socket.try(tp.connect(server or SERVER, port or PORT, TIMEOUT)) | 116 | local tp = socket.try(tp.connect(server or SERVER, port or PORT, |
| 117 | create, TIMEOUT)) | ||
| 116 | local s = base.setmetatable({tp = tp}, metat) | 118 | local s = base.setmetatable({tp = tp}, metat) |
| 117 | -- make sure tp is closed if we get an exception | 119 | -- make sure tp is closed if we get an exception |
| 118 | s.try = socket.newtry(function() | 120 | s.try = socket.newtry(function() |
| 119 | if s.tp:command("QUIT") then s.tp:check("2..") end | ||
| 120 | s:close() | 121 | s:close() |
| 121 | end) | 122 | end) |
| 122 | return s | 123 | return s |
| @@ -165,10 +166,9 @@ end | |||
| 165 | local function send_source(mesgt) | 166 | local function send_source(mesgt) |
| 166 | -- set content-type if user didn't override | 167 | -- set content-type if user didn't override |
| 167 | if not mesgt.headers or not mesgt.headers["content-type"] then | 168 | if not mesgt.headers or not mesgt.headers["content-type"] then |
| 168 | coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n') | 169 | coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n\r\n') |
| 169 | end | 170 | else coroutine.yield("\r\n") end |
| 170 | -- finish headers | 171 | -- finish headers |
| 171 | coroutine.yield("\r\n") | ||
| 172 | -- send body from source | 172 | -- send body from source |
| 173 | while true do | 173 | while true do |
| 174 | local chunk, err = mesgt.body() | 174 | local chunk, err = mesgt.body() |
| @@ -182,21 +182,20 @@ end | |||
| 182 | local function send_string(mesgt) | 182 | local function send_string(mesgt) |
| 183 | -- set content-type if user didn't override | 183 | -- set content-type if user didn't override |
| 184 | if not mesgt.headers or not mesgt.headers["content-type"] then | 184 | if not mesgt.headers or not mesgt.headers["content-type"] then |
| 185 | coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n') | 185 | coroutine.yield('content-type: text/plain; charset="iso-8859-1"\r\n\r\n') |
| 186 | end | 186 | else coroutine.yield("\r\n") end |
| 187 | -- finish headers | ||
| 188 | coroutine.yield("\r\n") | ||
| 189 | -- send body from string | 187 | -- send body from string |
| 190 | coroutine.yield(mesgt.body) | 188 | coroutine.yield(mesgt.body) |
| 191 | |||
| 192 | end | 189 | end |
| 193 | 190 | ||
| 194 | -- yield the headers one by one | 191 | -- yield the headers all at once |
| 195 | local function send_headers(mesgt) | 192 | local function send_headers(mesgt) |
| 196 | if mesgt.headers then | 193 | if mesgt.headers then |
| 194 | local h = "" | ||
| 197 | for i,v in base.pairs(mesgt.headers) do | 195 | for i,v in base.pairs(mesgt.headers) do |
| 198 | coroutine.yield(i .. ':' .. v .. "\r\n") | 196 | h = i .. ': ' .. v .. "\r\n" .. h |
| 199 | end | 197 | end |
| 198 | coroutine.yield(h) | ||
| 200 | end | 199 | end |
| 201 | end | 200 | end |
| 202 | 201 | ||
| @@ -237,12 +236,10 @@ end | |||
| 237 | -- High level SMTP API | 236 | -- High level SMTP API |
| 238 | ----------------------------------------------------------------------------- | 237 | ----------------------------------------------------------------------------- |
| 239 | send = socket.protect(function(mailt) | 238 | send = socket.protect(function(mailt) |
| 240 | local s = open(mailt.server, mailt.port) | 239 | local s = open(mailt.server, mailt.port, mailt.create) |
| 241 | local ext = s:greet(mailt.domain) | 240 | local ext = s:greet(mailt.domain) |
| 242 | s:auth(mailt.user, mailt.password, ext) | 241 | s:auth(mailt.user, mailt.password, ext) |
| 243 | s:send(mailt) | 242 | s:send(mailt) |
| 244 | s:quit() | 243 | s:quit() |
| 245 | return s:close() | 244 | return s:close() |
| 246 | end) | 245 | end) |
| 247 | |||
| 248 | --getmetatable(_M).__index = nil | ||
diff --git a/src/socket.lua b/src/socket.lua index 13b474d..be01667 100644 --- a/src/socket.lua +++ b/src/socket.lua | |||
| @@ -12,6 +12,7 @@ local string = require("string") | |||
| 12 | local math = require("math") | 12 | local math = require("math") |
| 13 | local socket = require("socket.core") | 13 | local socket = require("socket.core") |
| 14 | module("socket") | 14 | module("socket") |
| 15 | getmetatable(_M).__index = nil | ||
| 15 | 16 | ||
| 16 | ----------------------------------------------------------------------------- | 17 | ----------------------------------------------------------------------------- |
| 17 | -- Exported auxiliar functions | 18 | -- Exported auxiliar functions |
| @@ -131,5 +132,3 @@ sourcet["default"] = sourcet["until-closed"] | |||
| 131 | 132 | ||
| 132 | source = choose(sourcet) | 133 | source = choose(sourcet) |
| 133 | 134 | ||
| 134 | -- clear globals from namespace | ||
| 135 | getmetatable(_M).__index = nil | ||
diff --git a/src/ssl.c b/src/ssl.c deleted file mode 100644 index 8e2ce00..0000000 --- a/src/ssl.c +++ /dev/null | |||
| @@ -1,70 +0,0 @@ | |||
| 1 | /*=========================================================================*\ | ||
| 2 | * Simple client SSL support | ||
| 3 | * LuaSocket toolkit | ||
| 4 | * | ||
| 5 | * RCS ID: $Id$ | ||
| 6 | \*=========================================================================*/ | ||
| 7 | #include <lua.h> | ||
| 8 | #include <lauxlib.h> | ||
| 9 | |||
| 10 | #include "ssl.h" | ||
| 11 | |||
| 12 | /*=========================================================================*\ | ||
| 13 | * Internal function prototypes | ||
| 14 | \*=========================================================================*/ | ||
| 15 | static int global_wrap(lua_State *L); | ||
| 16 | |||
| 17 | /* functions in library namespace */ | ||
| 18 | static luaL_reg func[] = { | ||
| 19 | {"wrap", global_create}, | ||
| 20 | {NULL, NULL} | ||
| 21 | }; | ||
| 22 | |||
| 23 | static luaL_reg wrap[] = { | ||
| 24 | {"__tostring", aux_tostring}, | ||
| 25 | {"__gc", meth_close}, | ||
| 26 | {"close", meth_close}, | ||
| 27 | {"receive", meth_receive}, | ||
| 28 | {"send", meth_send}, | ||
| 29 | {NULL, NULL} | ||
| 30 | }; | ||
| 31 | |||
| 32 | static luaL_reg owned[] = { | ||
| 33 | {"__tostring", aux_tostring}, | ||
| 34 | {NULL, NULL} | ||
| 35 | }; | ||
| 36 | |||
| 37 | /*-------------------------------------------------------------------------*\ | ||
| 38 | * Initializes module | ||
| 39 | \*-------------------------------------------------------------------------*/ | ||
| 40 | int ssl_open(lua_State *L) | ||
| 41 | { | ||
| 42 | aux_newclass(L, "ssl{wraper}", wrap); | ||
| 43 | aux_newclass(L, "ssl{owned}", owned); | ||
| 44 | lua_pushstring(L, "ssl") | ||
| 45 | lua_newtable(L); | ||
| 46 | luaL_openlib(L, NULL, func, 0); | ||
| 47 | lua_settable(L, -3); | ||
| 48 | return 0; | ||
| 49 | } | ||
| 50 | |||
| 51 | /*=========================================================================*\ | ||
| 52 | * Library functions | ||
| 53 | \*=========================================================================*/ | ||
| 54 | /*-------------------------------------------------------------------------*\ | ||
| 55 | * Wraps a tcp object into an SSL object | ||
| 56 | \*-------------------------------------------------------------------------*/ | ||
| 57 | static int global_wrap(lua_State *L) { | ||
| 58 | p_tcp tcp = (p_tcp) aux_checkclass(L, "tcp{client}", 1); | ||
| 59 | /* change class of tcp object */ | ||
| 60 | aux_setclass(L, "ssl{owned}", 1); | ||
| 61 | /* create wrapper */ | ||
| 62 | p_wrap wrap = (p_wrap) lua_newuserdata(L, sizeof(t_wrap)); | ||
| 63 | /* lock reference */ | ||
| 64 | lua_pushvalue(L, 1); | ||
| 65 | wrap->ref = lua_ref(L, 1); | ||
| 66 | /* initialize wrapper */ | ||
| 67 | wrap->tcp = tcp; | ||
| 68 | io_init(&tcp->io, wrap_send, wrap_recv, wrap); | ||
| 69 | return 1; | ||
| 70 | } | ||
diff --git a/src/ssl.h b/src/ssl.h deleted file mode 100644 index 13ce97b..0000000 --- a/src/ssl.h +++ /dev/null | |||
| @@ -1,29 +0,0 @@ | |||
| 1 | #ifndef SSL_H | ||
| 2 | #define SSL_H | ||
| 3 | /*=========================================================================*\ | ||
| 4 | * Simple client SSL support | ||
| 5 | * LuaSocket toolkit | ||
| 6 | * | ||
| 7 | * This is just a simple example to show how to extend LuaSocket | ||
| 8 | * | ||
| 9 | * RCS ID: $Id$ | ||
| 10 | \*=========================================================================*/ | ||
| 11 | #include <lua.h> | ||
| 12 | #include <openssl/ssl.h> | ||
| 13 | |||
| 14 | #include "buffer.h" | ||
| 15 | #include "timeout.h" | ||
| 16 | #include "socket.h" | ||
| 17 | #include "tcp.h" | ||
| 18 | |||
| 19 | typedef struct t_wrap_ { | ||
| 20 | p_tcp tcp; | ||
| 21 | SSL* ssl; | ||
| 22 | int ref; | ||
| 23 | } t_wrap; | ||
| 24 | |||
| 25 | typedef t_wrap *p_wrap; | ||
| 26 | |||
| 27 | int ssl_open(lua_State *L); | ||
| 28 | |||
| 29 | #endif /* SSL_H */ | ||
| @@ -13,6 +13,7 @@ local string = require("string") | |||
| 13 | local socket = require("socket") | 13 | local socket = require("socket") |
| 14 | local ltn12 = require("ltn12") | 14 | local ltn12 = require("ltn12") |
| 15 | module("socket.tp") | 15 | module("socket.tp") |
| 16 | getmetatable(_M).__index = nil | ||
| 16 | 17 | ||
| 17 | ----------------------------------------------------------------------------- | 18 | ----------------------------------------------------------------------------- |
| 18 | -- Program constants | 19 | -- Program constants |
| @@ -98,7 +99,8 @@ end | |||
| 98 | 99 | ||
| 99 | function metat.__index:source(source, step) | 100 | function metat.__index:source(source, step) |
| 100 | local sink = socket.sink("keep-open", self.c) | 101 | local sink = socket.sink("keep-open", self.c) |
| 101 | return ltn12.pump.all(source, sink, step or ltn12.pump.step) | 102 | local ret, err = ltn12.pump.all(source, sink, step or ltn12.pump.step) |
| 103 | return ret, err | ||
| 102 | end | 104 | end |
| 103 | 105 | ||
| 104 | -- closes the underlying c | 106 | -- closes the underlying c |
| @@ -108,8 +110,8 @@ function metat.__index:close() | |||
| 108 | end | 110 | end |
| 109 | 111 | ||
| 110 | -- connect with server and return c object | 112 | -- connect with server and return c object |
| 111 | function connect(host, port, timeout) | 113 | function connect(host, port, create, timeout) |
| 112 | local c, e = socket.tcp() | 114 | local c, e = (create or socket.tcp()) |
| 113 | if not c then return nil, e end | 115 | if not c then return nil, e end |
| 114 | c:settimeout(timeout or TIMEOUT) | 116 | c:settimeout(timeout or TIMEOUT) |
| 115 | local r, e = c:connect(host, port) | 117 | local r, e = c:connect(host, port) |
| @@ -120,4 +122,3 @@ function connect(host, port, timeout) | |||
| 120 | return base.setmetatable({c = c}, metat) | 122 | return base.setmetatable({c = c}, metat) |
| 121 | end | 123 | end |
| 122 | 124 | ||
| 123 | --getmetatable(_M).__index = nil | ||
diff --git a/src/url.lua b/src/url.lua index 0db111b..bd39d98 100644 --- a/src/url.lua +++ b/src/url.lua | |||
| @@ -12,6 +12,7 @@ local string = require("string") | |||
| 12 | local base = _G | 12 | local base = _G |
| 13 | local table = require("table") | 13 | local table = require("table") |
| 14 | module("socket.url") | 14 | module("socket.url") |
| 15 | getmetatable(_M).__index = nil | ||
| 15 | 16 | ||
| 16 | ----------------------------------------------------------------------------- | 17 | ----------------------------------------------------------------------------- |
| 17 | -- Encodes a string into its escaped hexadecimal representation | 18 | -- Encodes a string into its escaped hexadecimal representation |
| @@ -279,4 +280,3 @@ function build_path(parsed, unsafe) | |||
| 279 | return path | 280 | return path |
| 280 | end | 281 | end |
| 281 | 282 | ||
| 282 | --getmetatable(_M).__index = nil | ||
diff --git a/src/wsocket.c b/src/wsocket.c index 2cbd073..c4c51b4 100644 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
| @@ -74,7 +74,10 @@ int sock_select(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_tm tm) { | |||
| 74 | double t = tm_get(tm); | 74 | double t = tm_get(tm); |
| 75 | tv.tv_sec = (int) t; | 75 | tv.tv_sec = (int) t; |
| 76 | tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); | 76 | tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); |
| 77 | return select(0, rfds, wfds, efds, t >= 0.0? &tv: NULL); | 77 | if (n <= 0) { |
| 78 | Sleep(1000*t); | ||
| 79 | return 0; | ||
| 80 | } else return select(0, rfds, wfds, efds, t >= 0.0? &tv: NULL); | ||
| 78 | } | 81 | } |
| 79 | 82 | ||
| 80 | /*-------------------------------------------------------------------------*\ | 83 | /*-------------------------------------------------------------------------*\ |
