aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2011-05-25 20:57:22 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2011-05-25 20:57:22 +0000
commit3a8ba90dfb0c2eb224f317dd692ede426691e72a (patch)
treefe1cc5379a2e0e031663fe9c15d908653844bc73 /src
parentbce60be30fe8e9c1b0eb33128c23c93d7bca5303 (diff)
downloadluasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.tar.gz
luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.tar.bz2
luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.zip
Saving before big changes to support IPv6.
Diffstat (limited to 'src')
-rw-r--r--src/buffer.c14
-rw-r--r--src/buffer.h4
-rw-r--r--src/ftp.lua4
-rw-r--r--src/inet.c3
-rw-r--r--src/makefile173
-rw-r--r--src/mbox.lua40
-rw-r--r--src/tp.lua2
-rw-r--r--src/url.lua122
-rw-r--r--src/usocket.c6
-rw-r--r--src/wsocket.c8
10 files changed, 219 insertions, 157 deletions
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) {
42* Initializes C structure 42* Initializes C structure
43\*-------------------------------------------------------------------------*/ 43\*-------------------------------------------------------------------------*/
44void buffer_init(p_buffer buf, p_io io, p_timeout tm) { 44void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
45 buf->first = buf->last = 0; 45 buf->first = buf->last = 0;
46 buf->io = io; 46 buf->io = io;
47 buf->tm = tm; 47 buf->tm = tm;
48 buf->received = buf->sent = 0; 48 buf->received = buf->sent = 0;
@@ -122,9 +122,15 @@ int buffer_meth_receive(lua_State *L, p_buffer buf) {
122 if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b); 122 if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b);
123 else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b); 123 else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b);
124 else luaL_argcheck(L, 0, 2, "invalid receive pattern"); 124 else luaL_argcheck(L, 0, 2, "invalid receive pattern");
125 /* get a fixed number of bytes (minus what was already partially 125 /* get a fixed number of bytes (minus what was already partially
126 * received) */ 126 * received) */
127 } else err = recvraw(buf, (size_t) lua_tonumber(L, 2)-size, &b); 127 } else {
128 double n = lua_tonumber(L, 2);
129 size_t wanted = (size_t) n;
130 luaL_argcheck(L, n >= 0, 2, "invalid receive pattern");
131 if (size == 0 || wanted > size)
132 err = recvraw(buf, wanted-size, &b);
133 }
128 /* check if there was an error */ 134 /* check if there was an error */
129 if (err != IO_DONE) { 135 if (err != IO_DONE) {
130 /* we can't push anyting in the stack before pushing the 136 /* 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_ {
29 size_t sent, received; /* bytes sent, and bytes received */ 29 size_t sent, received; /* bytes sent, and bytes received */
30 p_io io; /* IO driver used for this buffer */ 30 p_io io; /* IO driver used for this buffer */
31 p_timeout tm; /* timeout management for this buffer */ 31 p_timeout tm; /* timeout management for this buffer */
32 size_t first, last; /* index of first and last bytes of stored data */ 32 size_t first, last; /* index of first and last bytes of stored data */
33 char data[BUF_SIZE]; /* storage space for buffer data */ 33 char data[BUF_SIZE]; /* storage space for buffer data */
34} t_buffer; 34} t_buffer;
35typedef t_buffer *p_buffer; 35typedef t_buffer *p_buffer;
36 36
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)
212end 212end
213 213
214local default = { 214local default = {
215 path = "/", 215 path = "/",
216 scheme = "ftp" 216 scheme = "ftp"
217} 217}
218 218
219local function parse(u) 219local 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,
213 memset(&remote, 0, sizeof(remote)); 213 memset(&remote, 0, sizeof(remote));
214 remote.sin_family = AF_INET; 214 remote.sin_family = AF_INET;
215 remote.sin_port = htons(port); 215 remote.sin_port = htons(port);
216 if (strcmp(address, "*")) { 216 if (strcmp(address, "*")) {
217 if (!inet_aton(address, &remote.sin_addr)) { 217 if (!inet_aton(address, &remote.sin_addr)) {
218 struct hostent *hp = NULL; 218 struct hostent *hp = NULL;
219 struct in_addr **addr; 219 struct in_addr **addr;
@@ -248,7 +248,6 @@ const char *inet_trybind(p_socket ps, const char *address, unsigned short port)
248 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); 248 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
249 } 249 }
250 err = socket_bind(ps, (SA *) &local, sizeof(local)); 250 err = socket_bind(ps, (SA *) &local, sizeof(local));
251 if (err != IO_DONE) socket_destroy(ps);
252 return socket_strerror(err); 251 return socket_strerror(err);
253} 252}
254 253
diff --git a/src/makefile b/src/makefile
index 3351997..701feb3 100644
--- a/src/makefile
+++ b/src/makefile
@@ -1,8 +1,13 @@
1PLAT = none 1PLAT?=macosx
2
2INSTALL_DATA=cp 3INSTALL_DATA=cp
3INSTALL_EXEC=cp 4INSTALL_EXEC=cp
4INSTALL_TOP= /opt/local 5INSTALL_TOP=/opt/local
5LUAINC= $(LUAINC_$(PLAT)) 6
7LUAINC_macosx=/opt/local/include
8LUAINC_linux=/usr/include/lua5.1
9LUAINC_win32="../../lua-5.1.3/src"
10LUALIB_win32="../../lua-5.1.3"
6 11
7#------ 12#------
8# Install directories 13# Install directories
@@ -15,40 +20,76 @@ INSTALL_MIME_SHARE=$(INSTALL_TOP_SHARE)/mime
15INSTALL_MIME_LIB=$(INSTALL_TOP_LIB)/mime 20INSTALL_MIME_LIB=$(INSTALL_TOP_LIB)/mime
16 21
17#------ 22#------
18# Output file names 23# Supported platforms
19# 24#
20EXT=so 25PLATS= macosx linux win32
21SOCKET_V=2.0.3
22MIME_V=1.0.3
23SOCKET_SO=socket.$(EXT).$(SOCKET_V)
24MIME_SO=mime.$(EXT).$(MIME_V)
25UNIX_SO=unix.$(EXT)
26 26
27#------ 27#------
28# Compiler and linker settings 28# Compiler and linker settings
29# for Mac OS X 29# for Mac OS X
30LUAINC_macosx= -I/opt/local/include 30SO_macosx=so
31O_macosx=o
31CC_macosx=gcc 32CC_macosx=gcc
32DEF_macosx= -DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN \ 33DEF_macosx= -DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN \
33 -DLUASOCKET_API='__attribute__((visibility("default")))' \ 34 -DLUASOCKET_API='__attribute__((visibility("default")))' \
34 -DMIME_API='__attribute__((visibility("default")))' 35 -DMIME_API='__attribute__((visibility("default")))'
35CFLAGS_macosx= $(LUAINC) $(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common \ 36CFLAGS_macosx= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fno-common \
36 -fvisibility=hidden 37 -fvisibility=hidden
37LDFLAGS_macosx= -bundle -undefined dynamic_lookup 38LDFLAGS_macosx= -bundle -undefined dynamic_lookup -o
38LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc 39LD_macosx= export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc
40SOCKET_macosx=usocket.o
39 41
40#------ 42#------
41# Compiler and linker settings 43# Compiler and linker settings
42# for Linux 44# for Linux
43LUAINC_linux= -I/usr/local/include/lua5.1 45SO_linux=so
46O_linux=o
44CC_linux=gcc 47CC_linux=gcc
45DEF_linux=-DLUASOCKET_DEBUG \ 48DEF_linux=-DLUASOCKET_DEBUG \
46 -DLUASOCKET_API='__attribute__((visibility("default")))' \ 49 -DLUASOCKET_API='__attribute__((visibility("default")))' \
47 -DMIME_API='__attribute__((visibility("default")))' 50 -DMIME_API='__attribute__((visibility("default")))'
48CFLAGS_linux= $(LUAINC) $(DEF) -pedantic -Wall -O2 -fpic \ 51CFLAGS_linux= -I$(LUAINC) $(DEF) -pedantic -Wall -O2 -fpic \
49 -fvisibility=hidden 52 -fvisibility=hidden
50LDFLAGS_linux=-O -shared -fpic 53LDFLAGS_linux=-O -shared -fpic -o
51LD_linux= gcc 54LD_linux=gcc
55SOCKET_linux=usocket.o
56
57#------
58# Compiler and linker settings
59# for Win32
60SO_win32=dll
61O_win32=obj
62CC_win32=cl
63DEF_win32= /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_USRDLL" \
64 /D "LUASOCKET_API=__declspec(dllexport)" /D "LUASOCKET_DEBUG" \
65 /D "_CRT_SECURE_NO_WARNINGS" /D "_WINDLL"
66CFLAGS_win32=/I$(LUAINC) $(DEF) /O2 /Ot /MD /W3 /nologo
67LDFLAGS_win32= /nologo /link /NOLOGO /DLL /INCREMENTAL:NO \
68 /LIBPATH:$(LUALIB) \
69 /MANIFEST \
70 /MANIFESTFILE:"intermediate.manifest" \
71 /MANIFESTUAC:"level='asInvoker' uiAccess='false'" \
72 /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /DYNAMICBASE:NO \
73 /MACHINE:X86 ws2_32.lib lua5.1.lib /OUT:
74LD_win32=cl
75SOCKET_win32=wsocket.obj
76
77.SUFFIXES: .obj
78
79.c.obj:
80 $(CC) $(CFLAGS) /Fo"$@" /c $<
81
82#------
83# Output file names
84#
85SO=$(SO_$(PLAT))
86O=$(O_$(PLAT))
87SOCKET_V=2.0.3
88MIME_V=1.0.3
89SOCKET_SO=socket.$(SO).$(SOCKET_V)
90MIME_SO=mime.$(SO).$(MIME_V)
91UNIX_SO=unix.$(SO)
92SOCKET=$(SOCKET_$(PLAT))
52 93
53#------ 94#------
54# Settings selected for platform 95# Settings selected for platform
@@ -58,46 +99,48 @@ DEF=$(DEF_$(PLAT))
58CFLAGS=$(CFLAGS_$(PLAT)) 99CFLAGS=$(CFLAGS_$(PLAT))
59LDFLAGS=$(LDFLAGS_$(PLAT)) 100LDFLAGS=$(LDFLAGS_$(PLAT))
60LD=$(LD_$(PLAT)) 101LD=$(LD_$(PLAT))
102LUAINC= $(LUAINC_$(PLAT))
103LUALIB= $(LUALIB_$(PLAT))
61 104
62#------ 105#------
63# Modules belonging to socket-core 106# Modules belonging to socket-core
64# 107#
65SOCKET_OBJS= \ 108SOCKET_OBJS= \
66 luasocket.o \ 109 luasocket.$(O) \
67 timeout.o \ 110 timeout.$(O) \
68 buffer.o \ 111 buffer.$(O) \
69 io.o \ 112 io.$(O) \
70 auxiliar.o \ 113 auxiliar.$(O) \
71 options.o \ 114 options.$(O) \
72 inet.o \ 115 inet.$(O) \
73 usocket.o \ 116 $(SOCKET) \
74 except.o \ 117 except.$(O) \
75 select.o \ 118 select.$(O) \
76 tcp.o \ 119 tcp.$(O) \
77 udp.o 120 udp.$(O)
78 121
79#------ 122#------
80# Modules belonging mime-core 123# Modules belonging mime-core
81# 124#
82MIME_OBJS= \ 125MIME_OBJS= \
83 mime.o 126 mime.$(O)
84 127
85#------ 128#------
86# Modules belonging unix (local domain sockets) 129# Modules belonging unix (local domain sockets)
87# 130#
88UNIX_OBJS:=\ 131UNIX_OBJS=\
89 buffer.o \ 132 buffer.$(O) \
90 auxiliar.o \ 133 auxiliar.$(O) \
91 options.o \ 134 options.$(O) \
92 timeout.o \ 135 timeout.$(O) \
93 io.o \ 136 io.$(O) \
94 usocket.o \ 137 usocket.$(O) \
95 unix.o 138 unix.$(O)
96 139
97#------ 140#------
98# Files to install 141# Files to install
99# 142#
100TO_SOCKET_SHARE:= \ 143TO_SOCKET_SHARE= \
101 http.lua \ 144 http.lua \
102 url.lua \ 145 url.lua \
103 tp.lua \ 146 tp.lua \
@@ -105,33 +148,41 @@ TO_SOCKET_SHARE:= \
105 headers.lua \ 148 headers.lua \
106 smtp.lua 149 smtp.lua
107 150
108TO_TOP_SHARE:= \ 151TO_TOP_SHARE= \
109 ltn12.lua \ 152 ltn12.lua \
110 socket.lua \ 153 socket.lua \
111 mime.lua 154 mime.lua
112 155
156#------
157# Targets
158#
113default: $(PLAT) 159default: $(PLAT)
114 160
115macosx: 161macosx:
116 $(MAKE) all PLAT=macosx 162 $(MAKE) all PLAT=macosx
117 163
164win32:
165 $(MAKE) all PLAT=win32
166
118linux: 167linux:
119 $(MAKE) all PLAT=linux 168 $(MAKE) all PLAT=linux
120 169
121none: 170none:
122 @echo "Please choose a platform:" 171 @echo "Please run"
172 @echo " make PLATFORM"
173 @echo "where PLATFORM is one of these:"
123 @echo " $(PLATS)" 174 @echo " $(PLATS)"
124 175
125all: $(SOCKET_SO) $(MIME_SO) 176all: $(SOCKET_SO) $(MIME_SO)
126 177
127$(SOCKET_SO): $(SOCKET_OBJS) 178$(SOCKET_SO): $(SOCKET_OBJS)
128 $(LD) $(LDFLAGS) -o $@ $(SOCKET_OBJS) 179 $(LD) $(SOCKET_OBJS) $(LDFLAGS)$@
129 180
130$(MIME_SO): $(MIME_OBJS) 181$(MIME_SO): $(MIME_OBJS)
131 $(LD) $(LDFLAGS) -o $@ $(MIME_OBJS) 182 $(LD) $(MIME_OBJS) $(LDFLAGS)$@
132 183
133$(UNIX_SO): $(UNIX_OBJS) 184$(UNIX_SO): $(UNIX_OBJS)
134 $(LD) $(LDFLAGS) -o $@ $(UNIX_OBJS) 185 $(LD) $(UNIX_OBJS) $(LDFLAGS)$@
135 186
136install: 187install:
137 mkdir -p $(INSTALL_TOP_SHARE) 188 mkdir -p $(INSTALL_TOP_SHARE)
@@ -139,9 +190,9 @@ install:
139 mkdir -p $(INSTALL_SOCKET_SHARE) 190 mkdir -p $(INSTALL_SOCKET_SHARE)
140 $(INSTALL_DATA) $(TO_SOCKET_SHARE) $(INSTALL_SOCKET_SHARE) 191 $(INSTALL_DATA) $(TO_SOCKET_SHARE) $(INSTALL_SOCKET_SHARE)
141 mkdir -p $(INSTALL_SOCKET_LIB) 192 mkdir -p $(INSTALL_SOCKET_LIB)
142 $(INSTALL_EXEC) $(SOCKET_SO) $(INSTALL_SOCKET_LIB)/core.$(EXT) 193 $(INSTALL_EXEC) $(SOCKET_SO) $(INSTALL_SOCKET_LIB)/core.$(SO)
143 mkdir -p $(INSTALL_MIME_LIB) 194 mkdir -p $(INSTALL_MIME_LIB)
144 $(INSTALL_EXEC) $(MIME_SO) $(INSTALL_MIME_LIB)/core.$(EXT) 195 $(INSTALL_EXEC) $(MIME_SO) $(INSTALL_MIME_LIB)/core.$(SO)
145 196
146local: 197local:
147 $(MAKE) install INSTALL_TOP_LIB=.. INSTALL_TOP_SHARE=.. 198 $(MAKE) install INSTALL_TOP_LIB=.. INSTALL_TOP_SHARE=..
@@ -155,24 +206,24 @@ clean:
155#------ 206#------
156# List of dependencies 207# List of dependencies
157# 208#
158auxiliar.o: auxiliar.c auxiliar.h 209auxiliar.$(O): auxiliar.c auxiliar.h
159buffer.o: buffer.c buffer.h io.h timeout.h 210buffer.$(O): buffer.c buffer.h io.h timeout.h
160except.o: except.c except.h 211except.$(O): except.c except.h
161inet.o: inet.c inet.h socket.h io.h timeout.h usocket.h 212inet.$(O): inet.c inet.h socket.h io.h timeout.h usocket.h
162io.o: io.c io.h timeout.h 213io.$(O): io.c io.h timeout.h
163luasocket.o: luasocket.c luasocket.h auxiliar.h except.h \ 214luasocket.$(O): luasocket.c luasocket.h auxiliar.h except.h \
164 timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \ 215 timeout.h buffer.h io.h inet.h socket.h usocket.h tcp.h \
165 udp.h select.h 216 udp.h select.h
166mime.o: mime.c mime.h 217mime.$(O): mime.c mime.h
167options.o: options.c auxiliar.h options.h socket.h io.h \ 218options.$(O): options.c auxiliar.h options.h socket.h io.h \
168 timeout.h usocket.h inet.h 219 timeout.h usocket.h inet.h
169select.o: select.c socket.h io.h timeout.h usocket.h select.h 220select.$(O): select.c socket.h io.h timeout.h usocket.h select.h
170tcp.o: tcp.c auxiliar.h socket.h io.h timeout.h usocket.h \ 221tcp.$(O): tcp.c auxiliar.h socket.h io.h timeout.h usocket.h \
171 inet.h options.h tcp.h buffer.h 222 inet.h options.h tcp.h buffer.h
172timeout.o: timeout.c auxiliar.h timeout.h 223timeout.$(O): timeout.c auxiliar.h timeout.h
173udp.o: udp.c auxiliar.h socket.h io.h timeout.h usocket.h \ 224udp.$(O): udp.c auxiliar.h socket.h io.h timeout.h usocket.h \
174 inet.h options.h udp.h 225 inet.h options.h udp.h
175unix.o: unix.c auxiliar.h socket.h io.h timeout.h usocket.h \ 226unix.$(O): unix.c auxiliar.h socket.h io.h timeout.h usocket.h \
176 options.h unix.h buffer.h 227 options.h unix.h buffer.h
177usocket.o: usocket.c socket.h io.h timeout.h usocket.h 228usocket.$(O): usocket.c socket.h io.h timeout.h usocket.h
178wsocket.o: wsocket.c socket.h io.h timeout.h usocket.h 229wsocket.$(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
5function Public.split_message(message_s) 5function Public.split_message(message_s)
6 local message = {} 6 local message = {}
7 message_s = string.gsub(message_s, "\r\n", "\n") 7 message_s = string.gsub(message_s, "\r\n", "\n")
8 string.gsub(message_s, "^(.-\n)\n", function (h) message.headers = h end) 8 string.gsub(message_s, "^(.-\n)\n", function (h) message.headers = h end)
9 string.gsub(message_s, "^.-\n\n(.*)", function (b) message.body = b end) 9 string.gsub(message_s, "^.-\n\n(.*)", function (b) message.body = b end)
10 if not message.body then 10 if not message.body then
11 string.gsub(message_s, "^\n(.*)", function (b) message.body = b end) 11 string.gsub(message_s, "^\n(.*)", function (b) message.body = b end)
12 end 12 end
13 if not message.headers and not message.body then 13 if not message.headers and not message.body then
14 message.headers = message_s 14 message.headers = message_s
@@ -54,30 +54,30 @@ function Public.parse_from(from)
54 name = name or "" 54 name = name or ""
55 address = address or "" 55 address = address or ""
56 if name == "" then name = address end 56 if name == "" then name = address end
57 name = string.gsub(name, '"', "") 57 name = string.gsub(name, '"', "")
58 return name, address 58 return name, address
59end 59end
60 60
61function Public.split_mbox(mbox_s) 61function Public.split_mbox(mbox_s)
62 mbox = {} 62 mbox = {}
63 mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" 63 mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n"
64 local nj, i, j = 1, 1, 1 64 local nj, i, j = 1, 1, 1
65 while 1 do 65 while 1 do
66 i, nj = string.find(mbox_s, "\n\nFrom .-\n", j) 66 i, nj = string.find(mbox_s, "\n\nFrom .-\n", j)
67 if not i then break end 67 if not i then break end
68 local message = string.sub(mbox_s, j, i-1) 68 local message = string.sub(mbox_s, j, i-1)
69 table.insert(mbox, message) 69 table.insert(mbox, message)
70 j = nj+1 70 j = nj+1
71 end 71 end
72 return mbox 72 return mbox
73end 73end
74 74
75function Public.parse(mbox_s) 75function Public.parse(mbox_s)
76 local mbox = Public.split_mbox(mbox_s) 76 local mbox = Public.split_mbox(mbox_s)
77 for i = 1, table.getn(mbox) do 77 for i = 1, table.getn(mbox) do
78 mbox[i] = Public.parse_message(mbox[i]) 78 mbox[i] = Public.parse_message(mbox[i])
79 end 79 end
80 return mbox 80 return mbox
81end 81end
82 82
83function Public.parse_message(message_s) 83function 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
106-- closes the underlying c 106-- closes the underlying c
107function metat.__index:close() 107function metat.__index:close()
108 self.c:close() 108 self.c:close()
109 return 1 109 return 1
110end 110end
111 111
112-- connect with server and return c object 112-- 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
40-- escaped representation of string binary 40-- escaped representation of string binary
41----------------------------------------------------------------------------- 41-----------------------------------------------------------------------------
42local function make_set(t) 42local function make_set(t)
43 local s = {} 43 local s = {}
44 for i,v in base.ipairs(t) do 44 for i,v in base.ipairs(t) do
45 s[t[i]] = 1 45 s[t[i]] = 1
46 end 46 end
47 return s 47 return s
48end 48end
49 49
50-- these are allowed withing a path segment, along with alphanum 50-- these are allowed withing a path segment, along with alphanum
51-- other characters must be escaped 51-- other characters must be escaped
52local segment_set = make_set { 52local segment_set = make_set {
53 "-", "_", ".", "!", "~", "*", "'", "(", 53 "-", "_", ".", "!", "~", "*", "'", "(",
54 ")", ":", "@", "&", "=", "+", "$", ",", 54 ")", ":", "@", "&", "=", "+", "$", ",",
55} 55}
56 56
57local function protect_segment(s) 57local function protect_segment(s)
58 return string.gsub(s, "([^A-Za-z0-9_])", function (c) 58 return string.gsub(s, "([^A-Za-z0-9_])", function (c)
59 if segment_set[c] then return c 59 if segment_set[c] then return c
60 else return string.format("%%%02x", string.byte(c)) end 60 else return string.format("%%%02x", string.byte(c)) end
61 end) 61 end)
62end 62end
63 63
64----------------------------------------------------------------------------- 64-----------------------------------------------------------------------------
@@ -182,20 +182,26 @@ function build(parsed)
182 local url = build_path(ppath) 182 local url = build_path(ppath)
183 if parsed.params then url = url .. ";" .. parsed.params end 183 if parsed.params then url = url .. ";" .. parsed.params end
184 if parsed.query then url = url .. "?" .. parsed.query end 184 if parsed.query then url = url .. "?" .. parsed.query end
185 local authority = parsed.authority 185 local authority = parsed.authority
186 if parsed.host then 186 if parsed.host then
187 authority = parsed.host 187 authority = parsed.host
188 if parsed.port then authority = authority .. ":" .. parsed.port end 188 if parsed.port then authority = authority .. ":" .. parsed.port end
189 local userinfo = parsed.userinfo 189 local userinfo = parsed.userinfo
190 if parsed.user then 190 if parsed.user then
191 userinfo = parsed.user 191 userinfo = parsed.user
192 if parsed.password then 192 if parsed.password then
193 userinfo = userinfo .. ":" .. parsed.password 193 userinfo = userinfo .. ":" .. parsed.password
194 end 194 end
195 end 195 end
196 if userinfo then authority = userinfo .. "@" .. authority end 196 if userinfo then authority = userinfo .. "@" .. authority end
197 end 197 end
198 if authority then url = "//" .. authority .. url end 198 if authority then
199 if string.sub(url, 1, 1) == "/" then
200 url = "//" .. authority .. url
201 else
202 url = "//" .. authority .. "/" .. url
203 end
204 end
199 if parsed.scheme then url = parsed.scheme .. ":" .. url end 205 if parsed.scheme then url = parsed.scheme .. ":" .. url end
200 if parsed.fragment then url = url .. "#" .. parsed.fragment end 206 if parsed.fragment then url = url .. "#" .. parsed.fragment end
201 -- url = string.gsub(url, "%s", "") 207 -- url = string.gsub(url, "%s", "")
@@ -211,8 +217,8 @@ end
211-- corresponding absolute url 217-- corresponding absolute url
212----------------------------------------------------------------------------- 218-----------------------------------------------------------------------------
213function absolute(base_url, relative_url) 219function absolute(base_url, relative_url)
220 local base_parsed = base_url
214 if base.type(base_url) == "table" then 221 if base.type(base_url) == "table" then
215 base_parsed = base_url
216 base_url = build(base_parsed) 222 base_url = build(base_parsed)
217 else 223 else
218 base_parsed = parse(base_url) 224 base_parsed = parse(base_url)
@@ -250,16 +256,16 @@ end
250-- segment: a table with one entry per segment 256-- segment: a table with one entry per segment
251----------------------------------------------------------------------------- 257-----------------------------------------------------------------------------
252function parse_path(path) 258function parse_path(path)
253 local parsed = {} 259 local parsed = {}
254 path = path or "" 260 path = path or ""
255 --path = string.gsub(path, "%s", "") 261 --path = string.gsub(path, "%s", "")
256 string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) 262 string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end)
257 for i = 1, table.getn(parsed) do 263 for i = 1, table.getn(parsed) do
258 parsed[i] = unescape(parsed[i]) 264 parsed[i] = unescape(parsed[i])
259 end 265 end
260 if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end 266 if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end
261 if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end 267 if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end
262 return parsed 268 return parsed
263end 269end
264 270
265----------------------------------------------------------------------------- 271-----------------------------------------------------------------------------
@@ -271,27 +277,27 @@ end
271-- path: corresponding path stringing 277-- path: corresponding path stringing
272----------------------------------------------------------------------------- 278-----------------------------------------------------------------------------
273function build_path(parsed, unsafe) 279function build_path(parsed, unsafe)
274 local path = "" 280 local path = ""
275 local n = table.getn(parsed) 281 local n = table.getn(parsed)
276 if unsafe then 282 if unsafe then
277 for i = 1, n-1 do 283 for i = 1, n-1 do
278 path = path .. parsed[i] 284 path = path .. parsed[i]
279 path = path .. "/" 285 path = path .. "/"
280 end 286 end
281 if n > 0 then 287 if n > 0 then
282 path = path .. parsed[n] 288 path = path .. parsed[n]
283 if parsed.is_directory then path = path .. "/" end 289 if parsed.is_directory then path = path .. "/" end
284 end 290 end
285 else 291 else
286 for i = 1, n-1 do 292 for i = 1, n-1 do
287 path = path .. protect_segment(parsed[i]) 293 path = path .. protect_segment(parsed[i])
288 path = path .. "/" 294 path = path .. "/"
289 end 295 end
290 if n > 0 then 296 if n > 0 then
291 path = path .. protect_segment(parsed[n]) 297 path = path .. protect_segment(parsed[n])
292 if parsed.is_directory then path = path .. "/" end 298 if parsed.is_directory then path = path .. "/" end
293 end 299 end
294 end 300 end
295 if parsed.is_absolute then path = "/" .. path end 301 if parsed.is_absolute then path = "/" .. path end
296 return path 302 return path
297end 303end
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) {
30 pfd.revents = 0; 30 pfd.revents = 0;
31 if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ 31 if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
32 do { 32 do {
33 int t = (int)(timeout_getretry(tm)*1e3); 33 int t = (int)(timeout_getretry(tm)*1e3);
34 ret = poll(&pfd, 1, t >= 0? t: -1); 34 ret = poll(&pfd, 1, t >= 0? t: -1);
35 } while (ret == -1 && errno == EINTR); 35 } while (ret == -1 && errno == EINTR);
36 if (ret == -1) return errno; 36 if (ret == -1) return errno;
37 if (ret == 0) return IO_TIMEOUT; 37 if (ret == 0) return IO_TIMEOUT;
38 if (sw == WAITFD_C && (pfd.revents & (POLLIN|POLLERR))) return IO_CLOSED; 38 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) {
54 if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ 54 if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */
55 if (sw & WAITFD_R) { 55 if (sw & WAITFD_R) {
56 FD_ZERO(&rfds); 56 FD_ZERO(&rfds);
57 FD_SET(*ps, &rfds); 57 FD_SET(*ps, &rfds);
58 rp = &rfds; 58 rp = &rfds;
59 } 59 }
60 if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } 60 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,
207 /* loop until we send something or we give up on error */ 207 /* loop until we send something or we give up on error */
208 for ( ;; ) { 208 for ( ;; ) {
209 /* try to send something */ 209 /* try to send something */
210 int put = send(*ps, data, (int) count, 0); 210 int put = send(*ps, data, (int) count, 0);
211 /* if we sent something, we are done */ 211 /* if we sent something, we are done */
212 if (put > 0) { 212 if (put > 0) {
213 *sent = put; 213 *sent = put;
@@ -346,8 +346,8 @@ const char *socket_strerror(int err) {
346} 346}
347 347
348const char *socket_ioerror(p_socket ps, int err) { 348const char *socket_ioerror(p_socket ps, int err) {
349 (void) ps; 349 (void) ps;
350 return socket_strerror(err); 350 return socket_strerror(err);
351} 351}
352 352
353static const char *wstrerror(int err) { 353static const char *wstrerror(int err) {