From 3a8ba90dfb0c2eb224f317dd692ede426691e72a Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Wed, 25 May 2011 20:57:22 +0000 Subject: Saving before big changes to support IPv6. --- src/buffer.c | 14 +++-- src/buffer.h | 4 +- src/ftp.lua | 4 +- src/inet.c | 3 +- src/makefile | 173 +++++++++++++++++++++++++++++++++++++--------------------- src/mbox.lua | 40 +++++++------- src/tp.lua | 2 +- src/url.lua | 122 +++++++++++++++++++++-------------------- src/usocket.c | 6 +- src/wsocket.c | 8 +-- 10 files changed, 219 insertions(+), 157 deletions(-) (limited to 'src') diff --git a/src/buffer.c b/src/buffer.c index 363da3d..5be0faf 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -42,7 +42,7 @@ int buffer_open(lua_State *L) { * Initializes C structure \*-------------------------------------------------------------------------*/ void buffer_init(p_buffer buf, p_io io, p_timeout tm) { - buf->first = buf->last = 0; + buf->first = buf->last = 0; buf->io = io; buf->tm = tm; buf->received = buf->sent = 0; @@ -122,9 +122,15 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) { if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b); else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b); else luaL_argcheck(L, 0, 2, "invalid receive pattern"); - /* get a fixed number of bytes (minus what was already partially - * received) */ - } else err = recvraw(buf, (size_t) lua_tonumber(L, 2)-size, &b); + /* get a fixed number of bytes (minus what was already partially + * received) */ + } else { + double n = lua_tonumber(L, 2); + size_t wanted = (size_t) n; + luaL_argcheck(L, n >= 0, 2, "invalid receive pattern"); + if (size == 0 || wanted > size) + err = recvraw(buf, wanted-size, &b); + } /* check if there was an error */ if (err != IO_DONE) { /* we can't push anyting in the stack before pushing the diff --git a/src/buffer.h b/src/buffer.h index 58838d1..1281bb3 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -29,8 +29,8 @@ typedef struct t_buffer_ { size_t sent, received; /* bytes sent, and bytes received */ p_io io; /* IO driver used for this buffer */ p_timeout tm; /* timeout management for this buffer */ - size_t first, last; /* index of first and last bytes of stored data */ - char data[BUF_SIZE]; /* storage space for buffer data */ + size_t first, last; /* index of first and last bytes of stored data */ + char data[BUF_SIZE]; /* storage space for buffer data */ } t_buffer; typedef t_buffer *p_buffer; diff --git a/src/ftp.lua b/src/ftp.lua index f27e838..c90a65c 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -212,8 +212,8 @@ local function tput(putt) end local default = { - path = "/", - scheme = "ftp" + path = "/", + scheme = "ftp" } local function parse(u) diff --git a/src/inet.c b/src/inet.c index 32f0cd2..862288c 100644 --- a/src/inet.c +++ b/src/inet.c @@ -213,7 +213,7 @@ const char *inet_tryconnect(p_socket ps, const char *address, memset(&remote, 0, sizeof(remote)); remote.sin_family = AF_INET; remote.sin_port = htons(port); - if (strcmp(address, "*")) { + if (strcmp(address, "*")) { if (!inet_aton(address, &remote.sin_addr)) { struct hostent *hp = NULL; struct in_addr **addr; @@ -248,7 +248,6 @@ const char *inet_trybind(p_socket ps, const char *address, unsigned short port) memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); } err = socket_bind(ps, (SA *) &local, sizeof(local)); - if (err != IO_DONE) socket_destroy(ps); return socket_strerror(err); } diff --git a/src/makefile b/src/makefile index 3351997..701feb3 100644 --- a/src/makefile +++ b/src/makefile @@ -1,8 +1,13 @@ -PLAT = none +PLAT?=macosx + INSTALL_DATA=cp INSTALL_EXEC=cp -INSTALL_TOP= /opt/local -LUAINC= $(LUAINC_$(PLAT)) +INSTALL_TOP=/opt/local + +LUAINC_macosx=/opt/local/include +LUAINC_linux=/usr/include/lua5.1 +LUAINC_win32="../../lua-5.1.3/src" +LUALIB_win32="../../lua-5.1.3" #------ # Install directories @@ -15,40 +20,76 @@ INSTALL_MIME_SHARE=$(INSTALL_TOP_SHARE)/mime INSTALL_MIME_LIB=$(INSTALL_TOP_LIB)/mime #------ -# Output file names +# Supported platforms # -EXT=so -SOCKET_V=2.0.3 -MIME_V=1.0.3 -SOCKET_SO=socket.$(EXT).$(SOCKET_V) -MIME_SO=mime.$(EXT).$(MIME_V) -UNIX_SO=unix.$(EXT) +PLATS= macosx linux win32 #------ # Compiler and linker settings # for Mac OS X -LUAINC_macosx= -I/opt/local/include +SO_macosx=so +O_macosx=o CC_macosx=gcc DEF_macosx= -DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN \ -DLUASOCKET_API='__attribute__((visibility("default")))' \ -DMIME_API='__attribute__((visibility("default")))' -CFLAGS_macosx= $(LUAINC) $(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common \ +CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \ -fvisibility=hidden -LDFLAGS_macosx= -bundle -undefined dynamic_lookup +LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc +SOCKET_macosx=usocket.o #------ # Compiler and linker settings # for Linux -LUAINC_linux= -I/usr/local/include/lua5.1 +SO_linux=so +O_linux=o CC_linux=gcc DEF_linux=-DLUASOCKET_DEBUG \ -DLUASOCKET_API='__attribute__((visibility("default")))' \ -DMIME_API='__attribute__((visibility("default")))' -CFLAGS_linux= $(LUAINC) $(DEF) -pedantic -Wall -O2 -fpic \ +CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fpic \ -fvisibility=hidden -LDFLAGS_linux=-O -shared -fpic -LD_linux= gcc +LDFLAGS_linux=-O -shared -fpic -o +LD_linux=gcc +SOCKET_linux=usocket.o + +#------ +# Compiler and linker settings +# for Win32 +SO_win32=dll +O_win32=obj +CC_win32=cl +DEF_win32= /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_USRDLL" \ + /D "LUASOCKET_API=__declspec(dllexport)" /D "LUASOCKET_DEBUG" \ + /D "_CRT_SECURE_NO_WARNINGS" /D "_WINDLL" +CFLAGS_win32=/I$(LUAINC) $(DEF) /O2 /Ot /MD /W3 /nologo +LDFLAGS_win32= /nologo /link /NOLOGO /DLL /INCREMENTAL:NO \ + /LIBPATH:$(LUALIB) \ + /MANIFEST \ + /MANIFESTFILE:"intermediate.manifest" \ + /MANIFESTUAC:"level='asInvoker' uiAccess='false'" \ + /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /DYNAMICBASE:NO \ + /MACHINE:X86 ws2_32.lib lua5.1.lib /OUT: +LD_win32=cl +SOCKET_win32=wsocket.obj + +.SUFFIXES: .obj + +.c.obj: + $(CC) $(CFLAGS) /Fo"$@" /c $< + +#------ +# Output file names +# +SO=$(SO_$(PLAT)) +O=$(O_$(PLAT)) +SOCKET_V=2.0.3 +MIME_V=1.0.3 +SOCKET_SO=socket.$(SO).$(SOCKET_V) +MIME_SO=mime.$(SO).$(MIME_V) +UNIX_SO=unix.$(SO) +SOCKET=$(SOCKET_$(PLAT)) #------ # Settings selected for platform @@ -58,46 +99,48 @@ DEF=$(DEF_$(PLAT)) CFLAGS=$(CFLAGS_$(PLAT)) LDFLAGS=$(LDFLAGS_$(PLAT)) LD=$(LD_$(PLAT)) +LUAINC= $(LUAINC_$(PLAT)) +LUALIB= $(LUALIB_$(PLAT)) #------ # Modules belonging to socket-core # SOCKET_OBJS= \ - luasocket.o \ - timeout.o \ - buffer.o \ - io.o \ - auxiliar.o \ - options.o \ - inet.o \ - usocket.o \ - except.o \ - select.o \ - tcp.o \ - udp.o + luasocket.$(O) \ + timeout.$(O) \ + buffer.$(O) \ + io.$(O) \ + auxiliar.$(O) \ + options.$(O) \ + inet.$(O) \ + $(SOCKET) \ + except.$(O) \ + select.$(O) \ + tcp.$(O) \ + udp.$(O) #------ # Modules belonging mime-core # MIME_OBJS= \ - mime.o + mime.$(O) #------ # Modules belonging unix (local domain sockets) # -UNIX_OBJS:=\ - buffer.o \ - auxiliar.o \ - options.o \ - timeout.o \ - io.o \ - usocket.o \ - unix.o +UNIX_OBJS=\ + buffer.$(O) \ + auxiliar.$(O) \ + options.$(O) \ + timeout.$(O) \ + io.$(O) \ + usocket.$(O) \ + unix.$(O) #------ # Files to install # -TO_SOCKET_SHARE:= \ +TO_SOCKET_SHARE= \ http.lua \ url.lua \ tp.lua \ @@ -105,33 +148,41 @@ TO_SOCKET_SHARE:= \ headers.lua \ smtp.lua -TO_TOP_SHARE:= \ +TO_TOP_SHARE= \ ltn12.lua \ socket.lua \ mime.lua +#------ +# Targets +# default: $(PLAT) macosx: $(MAKE) all PLAT=macosx +win32: + $(MAKE) all PLAT=win32 + linux: $(MAKE) all PLAT=linux none: - @echo "Please choose a platform:" + @echo "Please run" + @echo " make PLATFORM" + @echo "where PLATFORM is one of these:" @echo " $(PLATS)" all: $(SOCKET_SO) $(MIME_SO) $(SOCKET_SO): $(SOCKET_OBJS) - $(LD) $(LDFLAGS) -o $@ $(SOCKET_OBJS) + $(LD) $(SOCKET_OBJS) $(LDFLAGS)$@ $(MIME_SO): $(MIME_OBJS) - $(LD) $(LDFLAGS) -o $@ $(MIME_OBJS) + $(LD) $(MIME_OBJS) $(LDFLAGS)$@ $(UNIX_SO): $(UNIX_OBJS) - $(LD) $(LDFLAGS) -o $@ $(UNIX_OBJS) + $(LD) $(UNIX_OBJS) $(LDFLAGS)$@ install: mkdir -p $(INSTALL_TOP_SHARE) @@ -139,9 +190,9 @@ install: mkdir -p $(INSTALL_SOCKET_SHARE) $(INSTALL_DATA) $(TO_SOCKET_SHARE) $(INSTALL_SOCKET_SHARE) mkdir -p $(INSTALL_SOCKET_LIB) - $(INSTALL_EXEC) $(SOCKET_SO) $(INSTALL_SOCKET_LIB)/core.$(EXT) + $(INSTALL_EXEC) $(SOCKET_SO) $(INSTALL_SOCKET_LIB)/core.$(SO) mkdir -p $(INSTALL_MIME_LIB) - $(INSTALL_EXEC) $(MIME_SO) $(INSTALL_MIME_LIB)/core.$(EXT) + $(INSTALL_EXEC) $(MIME_SO) $(INSTALL_MIME_LIB)/core.$(SO) local: $(MAKE) install INSTALL_TOP_LIB=.. INSTALL_TOP_SHARE=.. @@ -155,24 +206,24 @@ clean: #------ # List of dependencies # -auxiliar.o: auxiliar.c auxiliar.h -buffer.o: buffer.c buffer.h io.h timeout.h -except.o: except.c except.h -inet.o: inet.c inet.h socket.h io.h timeout.h usocket.h -io.o: io.c io.h timeout.h -luasocket.o: luasocket.c luasocket.h auxiliar.h except.h \ +auxiliar.$(O): auxiliar.c auxiliar.h +buffer.$(O): buffer.c buffer.h io.h timeout.h +except.$(O): except.c except.h +inet.$(O): inet.c inet.h socket.h io.h timeout.h usocket.h +io.$(O): io.c io.h timeout.h +luasocket.$(O): luasocket.c luasocket.h auxiliar.h except.h \ timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \ udp.h select.h -mime.o: mime.c mime.h -options.o: options.c auxiliar.h options.h socket.h io.h \ +mime.$(O): mime.c mime.h +options.$(O): options.c auxiliar.h options.h socket.h io.h \ timeout.h usocket.h inet.h -select.o: select.c socket.h io.h timeout.h usocket.h select.h -tcp.o: tcp.c auxiliar.h socket.h io.h timeout.h usocket.h \ +select.$(O): select.c socket.h io.h timeout.h usocket.h select.h +tcp.$(O): tcp.c auxiliar.h socket.h io.h timeout.h usocket.h \ inet.h options.h tcp.h buffer.h -timeout.o: timeout.c auxiliar.h timeout.h -udp.o: udp.c auxiliar.h socket.h io.h timeout.h usocket.h \ +timeout.$(O): timeout.c auxiliar.h timeout.h +udp.$(O): udp.c auxiliar.h socket.h io.h timeout.h usocket.h \ inet.h options.h udp.h -unix.o: unix.c auxiliar.h socket.h io.h timeout.h usocket.h \ +unix.$(O): unix.c auxiliar.h socket.h io.h timeout.h usocket.h \ options.h unix.h buffer.h -usocket.o: usocket.c socket.h io.h timeout.h usocket.h -wsocket.o: wsocket.c socket.h io.h timeout.h usocket.h +usocket.$(O): usocket.c socket.h io.h timeout.h usocket.h +wsocket.$(O): wsocket.c socket.h io.h timeout.h usocket.h diff --git a/src/mbox.lua b/src/mbox.lua index ce6537c..b7d4a2a 100644 --- a/src/mbox.lua +++ b/src/mbox.lua @@ -5,10 +5,10 @@ mbox = Public function Public.split_message(message_s) local message = {} message_s = string.gsub(message_s, "\r\n", "\n") - string.gsub(message_s, "^(.-\n)\n", function (h) message.headers = h end) - string.gsub(message_s, "^.-\n\n(.*)", function (b) message.body = b end) + string.gsub(message_s, "^(.-\n)\n", function (h) message.headers = h end) + string.gsub(message_s, "^.-\n\n(.*)", function (b) message.body = b end) if not message.body then - string.gsub(message_s, "^\n(.*)", function (b) message.body = b end) + string.gsub(message_s, "^\n(.*)", function (b) message.body = b end) end if not message.headers and not message.body then message.headers = message_s @@ -54,30 +54,30 @@ function Public.parse_from(from) name = name or "" address = address or "" if name == "" then name = address end - name = string.gsub(name, '"', "") + name = string.gsub(name, '"', "") return name, address end function Public.split_mbox(mbox_s) - mbox = {} - mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" - local nj, i, j = 1, 1, 1 - while 1 do - i, nj = string.find(mbox_s, "\n\nFrom .-\n", j) - if not i then break end - local message = string.sub(mbox_s, j, i-1) - table.insert(mbox, message) - j = nj+1 - end - return mbox + mbox = {} + mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" + local nj, i, j = 1, 1, 1 + while 1 do + i, nj = string.find(mbox_s, "\n\nFrom .-\n", j) + if not i then break end + local message = string.sub(mbox_s, j, i-1) + table.insert(mbox, message) + j = nj+1 + end + return mbox end function Public.parse(mbox_s) - local mbox = Public.split_mbox(mbox_s) - for i = 1, table.getn(mbox) do - mbox[i] = Public.parse_message(mbox[i]) - end - return mbox + local mbox = Public.split_mbox(mbox_s) + for i = 1, table.getn(mbox) do + mbox[i] = Public.parse_message(mbox[i]) + end + return mbox end function Public.parse_message(message_s) diff --git a/src/tp.lua b/src/tp.lua index 5fd8d22..46f7f64 100644 --- a/src/tp.lua +++ b/src/tp.lua @@ -106,7 +106,7 @@ end -- closes the underlying c function metat.__index:close() self.c:close() - return 1 + return 1 end -- connect with server and return c object diff --git a/src/url.lua b/src/url.lua index 18e5ab2..7623557 100644 --- a/src/url.lua +++ b/src/url.lua @@ -40,25 +40,25 @@ end -- escaped representation of string binary ----------------------------------------------------------------------------- local function make_set(t) - local s = {} - for i,v in base.ipairs(t) do - s[t[i]] = 1 - end - return s + local s = {} + for i,v in base.ipairs(t) do + s[t[i]] = 1 + end + return s end -- these are allowed withing a path segment, along with alphanum -- other characters must be escaped local segment_set = make_set { "-", "_", ".", "!", "~", "*", "'", "(", - ")", ":", "@", "&", "=", "+", "$", ",", + ")", ":", "@", "&", "=", "+", "$", ",", } local function protect_segment(s) - return string.gsub(s, "([^A-Za-z0-9_])", function (c) - if segment_set[c] then return c - else return string.format("%%%02x", string.byte(c)) end - end) + return string.gsub(s, "([^A-Za-z0-9_])", function (c) + if segment_set[c] then return c + else return string.format("%%%02x", string.byte(c)) end + end) end ----------------------------------------------------------------------------- @@ -182,20 +182,26 @@ function build(parsed) local url = build_path(ppath) if parsed.params then url = url .. ";" .. parsed.params end if parsed.query then url = url .. "?" .. parsed.query end - local authority = parsed.authority - if parsed.host then - authority = parsed.host - if parsed.port then authority = authority .. ":" .. parsed.port end - local userinfo = parsed.userinfo - if parsed.user then - userinfo = parsed.user - if parsed.password then - userinfo = userinfo .. ":" .. parsed.password - end - end - if userinfo then authority = userinfo .. "@" .. authority end - end - if authority then url = "//" .. authority .. url end + local authority = parsed.authority + if parsed.host then + authority = parsed.host + if parsed.port then authority = authority .. ":" .. parsed.port end + local userinfo = parsed.userinfo + if parsed.user then + userinfo = parsed.user + if parsed.password then + userinfo = userinfo .. ":" .. parsed.password + end + end + if userinfo then authority = userinfo .. "@" .. authority end + end + if authority then + if string.sub(url, 1, 1) == "/" then + url = "//" .. authority .. url + else + url = "//" .. authority .. "/" .. url + end + end if parsed.scheme then url = parsed.scheme .. ":" .. url end if parsed.fragment then url = url .. "#" .. parsed.fragment end -- url = string.gsub(url, "%s", "") @@ -211,8 +217,8 @@ end -- corresponding absolute url ----------------------------------------------------------------------------- function absolute(base_url, relative_url) + local base_parsed = base_url if base.type(base_url) == "table" then - base_parsed = base_url base_url = build(base_parsed) else base_parsed = parse(base_url) @@ -250,16 +256,16 @@ end -- segment: a table with one entry per segment ----------------------------------------------------------------------------- function parse_path(path) - local parsed = {} - path = path or "" - --path = string.gsub(path, "%s", "") - string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) - for i = 1, table.getn(parsed) do - parsed[i] = unescape(parsed[i]) - end - if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end - if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end - return parsed + local parsed = {} + path = path or "" + --path = string.gsub(path, "%s", "") + string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) + for i = 1, table.getn(parsed) do + parsed[i] = unescape(parsed[i]) + end + if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end + if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end + return parsed end ----------------------------------------------------------------------------- @@ -271,27 +277,27 @@ end -- path: corresponding path stringing ----------------------------------------------------------------------------- function build_path(parsed, unsafe) - local path = "" - local n = table.getn(parsed) - if unsafe then - for i = 1, n-1 do - path = path .. parsed[i] - path = path .. "/" - end - if n > 0 then - path = path .. parsed[n] - if parsed.is_directory then path = path .. "/" end - end - else - for i = 1, n-1 do - path = path .. protect_segment(parsed[i]) - path = path .. "/" - end - if n > 0 then - path = path .. protect_segment(parsed[n]) - if parsed.is_directory then path = path .. "/" end - end - end - if parsed.is_absolute then path = "/" .. path end - return path + local path = "" + local n = table.getn(parsed) + if unsafe then + for i = 1, n-1 do + path = path .. parsed[i] + path = path .. "/" + end + if n > 0 then + path = path .. parsed[n] + if parsed.is_directory then path = path .. "/" end + end + else + for i = 1, n-1 do + path = path .. protect_segment(parsed[i]) + path = path .. "/" + end + if n > 0 then + path = path .. protect_segment(parsed[n]) + if parsed.is_directory then path = path .. "/" end + end + end + if parsed.is_absolute then path = "/" .. path end + return path end diff --git a/src/usocket.c b/src/usocket.c index 2c30b9a..1ba1043 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -30,9 +30,9 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) { pfd.revents = 0; if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ do { - int t = (int)(timeout_getretry(tm)*1e3); - ret = poll(&pfd, 1, t >= 0? t: -1); - } while (ret == -1 && errno == EINTR); + int t = (int)(timeout_getretry(tm)*1e3); + ret = poll(&pfd, 1, t >= 0? t: -1); + } while (ret == -1 && errno == EINTR); if (ret == -1) return errno; if (ret == 0) return IO_TIMEOUT; if (sw == WAITFD_C && (pfd.revents & (POLLIN|POLLERR))) return IO_CLOSED; diff --git a/src/wsocket.c b/src/wsocket.c index e247777..2d07904 100644 --- a/src/wsocket.c +++ b/src/wsocket.c @@ -54,7 +54,7 @@ int socket_waitfd(p_socket ps, int sw, p_timeout tm) { if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ if (sw & WAITFD_R) { FD_ZERO(&rfds); - FD_SET(*ps, &rfds); + FD_SET(*ps, &rfds); rp = &rfds; } if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } @@ -207,7 +207,7 @@ int socket_send(p_socket ps, const char *data, size_t count, /* loop until we send something or we give up on error */ for ( ;; ) { /* try to send something */ - int put = send(*ps, data, (int) count, 0); + int put = send(*ps, data, (int) count, 0); /* if we sent something, we are done */ if (put > 0) { *sent = put; @@ -346,8 +346,8 @@ const char *socket_strerror(int err) { } const char *socket_ioerror(p_socket ps, int err) { - (void) ps; - return socket_strerror(err); + (void) ps; + return socket_strerror(err); } static const char *wstrerror(int err) { -- cgit v1.2.3-55-g6feb