lahttp.lua

Sat, 02 Jan 2010 05:57:36 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 02 Jan 2010 05:57:36 +0000
changeset 1
7ef4b95f77b5
parent 0
6e60da4625db
permissions
-rw-r--r--

Add demo.lua to demonstrate usage

0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 package.preload['util.logger']=(function(...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 if os.getenv("LAHTTP_DEBUG")then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 t=_G.print;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 t=function()end;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local i,a=select,tostring;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 module"logger"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local function e(t,...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local e,o=0,#arg;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 return(t:gsub("%%(.)",function(t)if t~="%"and e<=o then e=e+1;return a(arg[e]);end end));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local function s(n,...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local e,t=0,i('#',...);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local o={...};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return(n:gsub("%%(.)",function(i)if e<=t then e=e+1;return a(o[e]);end end));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 function init(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return function(a,e,...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 t(a,s(e,...));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return _M;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 package.preload['net.server']=(function(...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local l=function(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return _G[e]
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local J=function(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 for t,a in pairs(e)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 e[t]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local w,e=require("util.logger").init("socket"),table.concat;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local n=function(...)return w("debug",e{...});end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local H=function(...)return w("warn",e{...});end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local e=collectgarbage
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local ie=1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local A=l"type"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local T=l"pairs"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local he=l"ipairs"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local h=l"tostring"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local e=l"collectgarbage"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local o=l"os"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local a=l"table"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local t=l"string"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local e=l"coroutine"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local V=o.time
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local R=o.difftime
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 local te=a.concat
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local a=a.remove
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local B=t.len
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local de=t.sub
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local ue=e.wrap
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local le=e.yield
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local I=select(2,pcall(require,"ssl"))
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local L=require"socket"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local ee=(I and I.wrap)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local fe=L.bind
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local me=L.sleep
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local ce=L.select
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local e=(I and I.newcontext)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local G
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local X
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local Z
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 local W
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local Y
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 local ne
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local re
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 local se
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local ae
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 local oe
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local P
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local d
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local Q
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local D
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 local K
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 local p
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 local s
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local C
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local r
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 local i
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 local k
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local b
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local f
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local c
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local a
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local o
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local v
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 local U
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 local S
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local _
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 local j
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 local M
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local z
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 local x
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 local O
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local E
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local q
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local N
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local F
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 local g
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 p={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 s={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 r={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 C={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 i={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 b={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 f={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 k={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 a=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 o=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 v=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 U=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 S=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 _=1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 j=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 z=51e3*1024
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 x=25e3*1024
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 O=12e5
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 E=6e4
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 q=6*60*60
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 N=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 g=1e3
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 _maxsslhandshake=30
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 ae=function(y,l,k,u,b,t,p,v)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 p=p or g
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 local f=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local g,c=y.onincoming,y.ondisconnect
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 local m
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 local c=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 if t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 c=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 if not e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 H"luasec not found"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 c=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 if A(t)~="table"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 H"server.lua: wrong server sslctx"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 c=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 local o;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 o,m=e(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 if not o then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 m=m or"wrong sslctx parameters"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 local a;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 a=m:match("^error loading (.-) %(");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 if a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 if a=="private key"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 a=t.key or"your private key";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 elseif a=="certificate"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 a=t.certificate or"your certificate file";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 local e=m:match("%((.+)%)$")or"some reason";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 if e=="Permission denied"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 e="Check that the permissions allow Prosody to read this file.";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 elseif e=="No such file or directory"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 e="Check that the path is correct, and the file exists.";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 elseif e=="system lib"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 e="Previous error (see logs), or other system error.";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 e="Reason: "..h(e or"unknown"):lower();
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 w("error","SSL/TLS: Failed to load %s: %s",a,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 w("error","SSL/TLS: Error initialising for port %d: %s",u,m);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 c=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 t=o;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 if not c then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 t=false;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 if v then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 w("error","Failed to listen on port %d due to SSL/TLS to SSL/TLS initialisation errors (see logs)",u)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 return nil,"Cannot start ssl, see log for details"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 local m=l.accept
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 local e={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 e.shutdown=function()end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 e.ssl=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 return c
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 e.sslctx=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 return t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 e.remove=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 f=f-1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 e.close=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 for t,e in T(i)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 if e.serverport==u then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 e.disconnect(e,"server closed")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 e:close(true)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 l:close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 o=d(r,l,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 a=d(s,l,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 i[l]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 e=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 l=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 n"server.lua: closed server handler and removed sockets from list"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 e.ip=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 return k
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 e.serverport=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 return u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 e.socket=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 return l
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 e.readbuffer=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 if f>p then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 n("server.lua: refused new client connection: server full")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 local a,s=m(l)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 if a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 local i,o=a:getpeername()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 a:settimeout(0)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 local e,a,t=D(e,y,a,i,u,o,b,t,v)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 if t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 f=f+1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 n("server.lua: accepted new client connection from ",h(i),":",h(o)," to ",h(u))
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 return g(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 elseif s then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 n("server.lua: error with new client connection: ",h(s))
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 return e
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 D=function(M,e,t,I,Y,L,_,p,P)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 t:settimeout(0)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 local w
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 local T
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 local q
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 local v
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 local O=e.onincoming
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 local F=e.status
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 local g=e.ondisconnect
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 local y={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 local m=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 local C
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 local A
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 local D
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 local l=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 local j=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 local E=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 local H,R=0,0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 local z=z
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 local x=x
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 local e=y
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 e.dispatch=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 return O
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 e.disconnect=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 return g
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 e.setlistener=function(a,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 O=t.onincoming
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 g=t.ondisconnect
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 e.getstats=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 return R,H
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 e.ssl=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 return v
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 e.sslctx=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 return p
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 e.send=function(n,i,o,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 return w(t,i,o,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 e.receive=function(a,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 return T(t,a,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 e.shutdown=function(a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 return q(t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 e.close=function(u,h)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 if not e then return true;end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 a=d(s,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 b[e]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 if m~=0 then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 if not(h or A)then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 e.sendbuffer()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 if m~=0 then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 if e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 e.write=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 C=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 w(t,te(y,"",1,m),1,l)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 if t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 c=q and q(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 t:close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 o=d(r,t,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 i[t]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 t=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 n"server.lua: socket already closed"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 if e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 f[e]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 k[e]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 e=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 if M then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 M.remove()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 n"server.lua: closed client handler and removed socket from list"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 return true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 e.ip=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 return I
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 e.serverport=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 return Y
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 e.clientport=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 return L
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 local k=function(i,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 l=l+B(a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 if l>z then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 k[e]="send buffer exceeded"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 e.write=W
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 elseif t and not r[t]then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 o=addsocket(r,t,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 m=m+1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 y[m]=a
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 if e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 f[e]=f[e]or u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 return true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 e.write=k
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 e.bufferqueue=function(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 return y
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 e.socket=function(a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 return t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 e.pattern=function(a,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 _=t or _
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 return _
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 e.set_send=function(a,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 w=t or w
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 return w
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 e.bufferlen=function(o,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 z=a or z
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 x=t or x
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 return l,x,z
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 e.lock_read=function(i,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 if o==true then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 local o=a
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 a=d(s,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 b[e]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 if a~=o then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 j=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 elseif o==false then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 if j then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 j=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 a=addsocket(s,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 b[e]=u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 return j
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390 e.lock=function(i,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 e.lock_read(a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 if a==true then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 e.write=W
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 local a=o
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 o=d(r,t,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 f[e]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 if o~=a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398 E=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 elseif a==false then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 e.write=k
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 if E then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 E=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 k("")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 return j,E
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409 local b=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
410 local o,t,a=T(t,_)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 if not t or(t=="wantread"or t=="timeout")or B(a)>0 then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
412 local o=o or a or""
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413 local a=B(o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414 if a>x then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415 g(e,"receive buffer exceeded")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 e.close(true)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
419 local a=a*ie
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
420 R=R+a
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 S=S+a
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 b[e]=u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 return O(e,o,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425 n("server.lua: client ",h(I),":",h(L)," read error: ",h(t))
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
426 A=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
427 g(e,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 c=e and e.close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
430 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432 local f=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
433 local p,a,i,s,v;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
434 local v;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435 if t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
436 s=te(y,"",1,m)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 p,a,i=w(t,s,1,l)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438 v=(p or i or 0)*ie
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
439 H=H+v
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 U=U+v
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441 c=N and J(y)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
442 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
443 p,a,v=false,"closed",0;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
444 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445 if p then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446 m=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
447 l=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
448 o=d(r,t,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
449 c=D and e:starttls(true)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450 f[e]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451 c=C and e.close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 return true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453 elseif i and(a=="timeout"or a=="wantwrite")then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 s=de(s,i+1,l)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
455 y[1]=s
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456 m=1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 l=l-i
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458 f[e]=u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459 return true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 n("server.lua: client ",h(I),":",h(L)," write error: ",h(a))
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462 A=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 g(e,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 c=e and e.close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
468 local l;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469 function e.set_sslctx(i,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470 v=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
471 p=t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 local u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473 local m
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474 l=ue(function(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 local i
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 for l=1,_maxsslhandshake do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 o=(u and d(r,t,o))or o
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478 a=(m and d(s,t,a))or a
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479 m,u=nil,nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
480 c,i=t:dohandshake()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
481 if not i then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
482 n("server.lua: ssl handshake done")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
483 e.readbuffer=b
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
484 e.sendbuffer=f
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
485 c=F and F(e,"ssl-handshake-complete")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
486 a=addsocket(s,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
487 return true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
488 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
489 n("server.lua: error during ssl handshake: ",h(i))
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
490 if i=="wantwrite"and not u then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
491 o=addsocket(r,t,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
492 u=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
493 elseif i=="wantread"and not m then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
494 a=addsocket(s,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
495 m=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497 break;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499 le()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
500 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 g(e,"ssl handshake failed")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503 c=e and e:close(true)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
505 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
506 )
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
507 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
508 if p then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
509 e:set_sslctx(p);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
510 if P then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
511 local a
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
512 t,a=ee(t,p)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
513 if a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
514 n("server.lua: ssl error: ",h(a))
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
515 return nil,nil,a
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
516 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
517 t:settimeout(0)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
518 e.readbuffer=l
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
519 e.sendbuffer=l
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
520 l(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
521 if not t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
522 return nil,nil,"ssl handshake failed";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
523 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
524 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
525 v=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
526 e.starttls=function(c,u)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
527 if not u then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
528 D=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
529 return
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
530 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
531 local c,u=t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
532 t,u=ee(t,p)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
533 if u then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
534 n("server.lua: error while starting tls on client: ",h(u))
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
535 return nil,u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
536 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
537 t:settimeout(0)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
538 w=t.send
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
539 T=t.receive
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
540 q=G
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
541 i[t]=e
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
542 a=addsocket(s,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
543 a=d(s,c,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
544 o=d(r,c,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
545 i[c]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
546 e.starttls=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
547 D=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
548 v=true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
549 e.readbuffer=l
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
550 e.sendbuffer=l
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
551 l(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
552 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
553 e.readbuffer=b
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
554 e.sendbuffer=f
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
555 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
556 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
557 v=false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
558 e.readbuffer=b
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
559 e.sendbuffer=f
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
560 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
561 w=t.send
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
562 T=t.receive
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
563 q=(v and G)or t.shutdown
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
564 i[t]=e
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
565 a=addsocket(s,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
566 return e,t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
567 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
568 G=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
569 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
570 W=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
571 return false
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
572 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
573 addsocket=function(a,t,e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
574 if not a[t]then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
575 e=e+1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
576 a[e]=t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
577 a[t]=e
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
578 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
579 return e;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
580 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
581 d=function(e,a,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
582 local o=e[a]
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
583 if o then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
584 e[a]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
585 local i=e[t]
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
586 e[t]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
587 if i~=a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
588 e[i]=o
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
589 e[o]=i
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
590 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
591 return t-1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
592 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
593 return t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
594 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
595 P=function(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
596 o=d(r,e,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
597 a=d(s,e,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
598 i[e]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
599 e:close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
600 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
601 re=function(o,e,d,u,r,l)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
602 local t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
603 if A(d)~="table"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
604 t="invalid listener table"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
605 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
606 if not A(e)=="number"or not(e>=0 and e<=65535)then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
607 t="invalid port"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
608 elseif p[e]then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
609 t="listeners on port '"..e.."' already exist"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
610 elseif r and not I then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
611 t="luasec not found"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
612 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
613 if t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
614 H("server.lua, port ",e,": ",t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
615 return nil,t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
616 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
617 o=o or"*"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
618 local t,h=fe(o,e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
619 if h then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
620 H("server.lua, port ",e,": ",h)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
621 return nil,h
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
622 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
623 local h,r=ae(d,t,o,e,u,r,g,l)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
624 if not h then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
625 t:close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
626 return nil,r
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
627 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
628 t:settimeout(0)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
629 a=addsocket(s,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
630 p[e]=h
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
631 i[t]=h
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
632 n("server.lua: new server listener on '",o,":",e,"'")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
633 return h
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
634 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
635 se=function(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
636 return p[e];
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
637 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
638 Q=function(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
639 local t=p[e]
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
640 if not t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
641 return nil,"no server found on port '"..h(e).."'"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
642 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
643 t:close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
644 p[e]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
645 return true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
646 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
647 ne=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
648 for t,e in T(i)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
649 e:close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
650 i[t]=nil
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
651 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
652 a=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
653 o=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
654 v=0
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
655 p={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
656 s={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
657 r={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
658 C={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
659 i={}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
660 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
661 oe=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
662 return _,j,z,x,O,E,q,N,g,_maxsslhandshake
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
663 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
664 K=function(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
665 if A(e)~="table"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
666 return nil,"invalid settings table"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
667 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
668 _=tonumber(e.timeout)or _
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
669 j=tonumber(e.sleeptime)or j
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
670 z=tonumber(e.maxsendlen)or z
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
671 x=tonumber(e.maxreadlen)or x
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
672 O=tonumber(e.checkinterval)or O
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
673 E=tonumber(e.sendtimeout)or E
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
674 q=tonumber(e.readtimeout)or q
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
675 N=e.cleanqueue
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
676 g=e._maxclientsperserver or g
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
677 _maxsslhandshake=e._maxsslhandshake or _maxsslhandshake
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
678 return true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
679 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
680 Y=function(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
681 if A(e)~="function"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
682 return nil,"invalid listener function"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
683 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
684 v=v+1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
685 C[v]=e
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
686 return true
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
687 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
688 Z=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
689 return S,U,a,o,v
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
690 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
691 local e=true;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
692 setquitting=function(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
693 e=not t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
694 return;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
695 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
696 X=function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
697 while e do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
698 local a,e,t=ce(s,r,_)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
699 for e,t in he(e)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
700 local e=i[t]
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
701 if e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
702 e.sendbuffer()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
703 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
704 P(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
705 n"server.lua: found no handler and closed socket (writelist)"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
706 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
707 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
708 for t,e in he(a)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
709 local t=i[e]
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
710 if t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
711 t.readbuffer()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
712 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
713 P(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
714 n"server.lua: found no handler and closed socket (readlist)"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
715 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
716 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
717 for e,t in T(k)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
718 e.disconnect()(e,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
719 e:close(true)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
720 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
721 J(k)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
722 u=V()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
723 if R(u-F)>=1 then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
724 for e=1,v do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
725 C[e](u)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
726 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
727 F=u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
728 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
729 me(j)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
730 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
731 return"quitting"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
732 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
733 local function s()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
734 return"select";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
735 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
736 local n=function(e,s,n,t,a,h,d)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
737 local t=D(nil,t,e,s,n,"clientport",a,h,d)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
738 i[e]=t
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
739 o=addsocket(r,e,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
740 return t,e
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
741 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
742 local t=function(a,o,i,s,r,h)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
743 local t,e=L.tcp()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
744 if e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
745 return nil,e
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
746 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
747 t:settimeout(0)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
748 c,e=t:connect(a,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
749 if e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
750 local e=n(t,a,o,i)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
751 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
752 D(nil,i,t,a,o,"clientport",s,r,h)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
753 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
754 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
755 l"setmetatable"(i,{__mode="k"})
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
756 l"setmetatable"(b,{__mode="k"})
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
757 l"setmetatable"(f,{__mode="k"})
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
758 F=V()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
759 M=V()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
760 Y(function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
761 local e=R(u-M)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
762 if e>O then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
763 M=u
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
764 for e,t in T(f)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
765 if R(u-t)>E then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
766 e.disconnect()(e,"send timeout")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
767 e:close(true)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
768 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
769 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
770 for e,t in T(b)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
771 if R(u-t)>q then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
772 e.disconnect()(e,"read timeout")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
773 e:close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
774 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
775 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
776 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
777 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
778 )
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
779 return{
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
780 addclient=t,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
781 wrapclient=n,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
782 loop=X,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
783 stats=Z,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
784 closeall=ne,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
785 addtimer=Y,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
786 addserver=re,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
787 getserver=se,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
788 getsettings=oe,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
789 setquitting=setquitting,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
790 removeserver=Q,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
791 get_backend=s,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
792 changesettings=K,
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
793 }
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
794 end)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
795 package.preload['net.httpclient_listener']=(function(...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
796 local a=require"util.logger".init("httpclient_listener");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
797 local i=require"net.connlisteners".register;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
798 local e={};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
799 local t={};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
800 local t={default_port=80,default_mode="*a"};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
801 function t.onincoming(t,o)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
802 local e=e[t];
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
803 if not e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
804 a("warn","Received response from connection %s with no request attached!",tostring(t));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
805 return;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
806 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
807 if o and e.reader then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
808 e:reader(o);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
809 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
810 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
811 function t.ondisconnect(a,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
812 local t=e[a];
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
813 if t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
814 t:reader(nil);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
815 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
816 e[a]=nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
817 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
818 function t.register_request(o,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
819 a("debug","Attaching request %s to connection %s",tostring(t.id or t),tostring(o));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
820 e[o]=t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
821 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
822 i("httpclient",t);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
823 end)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
824 package.preload['net.connlisteners']=(function(...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
825 local r=(CFG_SOURCEDIR or".").."/net/";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
826 local h=require"net.server";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
827 local o=require"util.logger".init("connlisteners");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
828 local i=tostring;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
829 local s,n,a=
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
830 dofile,pcall,error
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
831 module"connlisteners"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
832 local e={};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
833 function register(t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
834 if e[t]and e[t]~=a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
835 o("debug","Listener %s is already registered, not registering any more",t);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
836 return false;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
837 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
838 e[t]=a;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
839 o("debug","Registered connection listener %s",t);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
840 return true;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
841 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
842 function deregister(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
843 e[t]=nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
844 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
845 function get(t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
846 local a=e[t];
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
847 if not a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
848 local s,n=n(s,r..t:gsub("[^%w%-]","_").."_listener.lua");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
849 if not s then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
850 o("error","Error while loading listener '%s': %s",i(t),i(n));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
851 return nil,n;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
852 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
853 a=e[t];
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
854 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
855 return a;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
856 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
857 function start(o,e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
858 local t,n=get(o);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
859 if not t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
860 a("No such connection module: "..o..(n and(" ("..n..")")or""),0);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
861 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
862 if e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
863 if(e.type=="ssl"or e.type=="tls")and not e.ssl then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
864 a("No SSL context supplied for a "..i(e.type):upper().." connection!",0);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
865 elseif e.ssl and e.type=="tcp"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
866 a("SSL context supplied for a TCP connection!",0);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
867 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
868 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
869 local i=(e and e.interface)or t.default_interface or"*";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
870 local o=(e and e.port)or t.default_port or a("Can't start listener "..o.." because no port was specified, and it has no default port",0);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
871 local a=(e and e.mode)or t.default_mode or 1;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
872 local n=(e and e.ssl)or nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
873 local s=99999999;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
874 local e=e and e.type=="ssl";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
875 return h.addserver(i,o,t,a,n,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
876 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
877 return _M;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
878 end)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
879 package.preload['net.http']=(function(...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
880 local b=require"socket"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
881 local v=require"mime"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
882 local y=require"socket.url"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
883 local p=require"net.server"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
884 local e=require"net.connlisteners".get;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
885 local d=e("httpclient")or error("No httpclient listener!");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
886 local r,i=table.insert,table.concat;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
887 local s,u,c,m,f,l,t,a=
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
888 tonumber,tostring,pairs,xpcall,select,debug.traceback,string.char,string.format;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
889 local n=require"util.logger".init("http");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
890 local o=function()end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
891 module"http"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
892 function urlencode(e)return e and(e:gsub("%W",function(e)return a("%%%02x",e:byte());end));end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
893 function urldecode(e)return e and(e:gsub("%%(%x%x)",function(e)return t(s(e,16));end));end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
894 local function w(t,e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
895 if t.method=="HEAD"then return nil end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
896 if e==204 or e==304 or e==301 then return nil end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
897 if e>=100 and e<200 then return nil end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
898 return 1
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
899 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
900 local function h(e,t,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
901 if not t then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
902 if e.body then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
903 n("debug","Connection closed, but we have data, calling callback...");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
904 e.callback(i(e.body),e.code,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
905 elseif e.state~="completed"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
906 e.callback("connection-closed",0,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
907 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
908 destroy_request(e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
909 e.body=nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
910 e.state="completed";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
911 return;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
912 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
913 if e.state=="body"and e.state~="completed"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
914 o("Reading body...")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
915 if not e.body then e.body={};e.havebodylength,e.bodylength=0,s(e.responseheaders["content-length"]);end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
916 if a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
917 t=t:sub(a,-1)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
918 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
919 r(e.body,t);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
920 if e.bodylength then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
921 e.havebodylength=e.havebodylength+#t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
922 if e.havebodylength>=e.bodylength then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
923 n("debug","Have full body, calling callback");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
924 if e.callback then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
925 e.callback(i(e.body),e.code,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
926 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
927 e.body=nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
928 e.state="completed";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
929 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
930 o("","Have "..e.havebodylength.." bytes out of "..e.bodylength);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
931 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
932 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
933 elseif e.state=="headers"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
934 o("Reading headers...")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
935 local i=a;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
936 local n=e.responseheaders or{};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
937 for t in t:sub(a,-1):gmatch("(.-)\r\n")do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
938 a=a+#t+2;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
939 local a,i=t:match("(%S+): (.+)");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
940 if a and i then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
941 n[a:lower()]=i;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
942 o("Header: "..a:lower().." = "..i);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
943 elseif#t==0 then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
944 e.responseheaders=n;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
945 break;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
946 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
947 o("Unhandled header line: "..t);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
948 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
949 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
950 e.state="body";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
951 if#t>a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
952 return h(e,t,a);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
953 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
954 elseif e.state=="status"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
955 o("Reading status...")
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
956 local i,a,n,o=t:match("^HTTP/(%S+) (%d+) (.-)\r\n()",a);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
957 a=s(a);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
958 if not a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
959 return e.callback("invalid-status-line",0,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
960 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
961 e.code,e.responseversion=a,i;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
962 if e.onlystatus or not w(e,a)then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
963 if e.callback then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
964 e.callback(nil,a,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
965 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
966 destroy_request(e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
967 return;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
968 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
969 e.state="headers";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
970 if#t>o then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
971 return h(e,t,o);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
972 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
973 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
974 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
975 local function w(e)n("error","Traceback[http]: %s: %s",u(e),l());end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
976 function request(e,a,s)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
977 local e=y.parse(e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
978 if not(e and e.host)then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
979 s(nil,0,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
980 return nil,"invalid-url";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
981 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
982 if not e.path then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
983 e.path="/";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
984 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
985 local l,o;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
986 local t={["Host"]=e.host,["User-Agent"]="Prosody XMPP Server"}
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
987 if e.userinfo then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
988 t["Authorization"]="Basic "..v.b64(e.userinfo);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
989 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
990 if a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
991 l=a.headers;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
992 e.onlystatus=a.onlystatus;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
993 o=a.body;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
994 if o then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
995 e.method="POST ";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
996 t["Content-Length"]=u(#o);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
997 t["Content-Type"]="application/x-www-form-urlencoded";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
998 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
999 if a.method then e.method=a.method;end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1000 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1001 e.handler,e.conn=p.wrapclient(b.tcp(),e.host,e.port or 80,d,"*a");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1002 e.write=function(...)return e.handler:write(...);end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1003 e.conn:settimeout(0);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1004 local u,a=e.conn:connect(e.host,e.port or 80);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1005 if not u and a~="timeout"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1006 s(nil,0,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1007 return nil,a;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1008 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1009 local a={e.method or"GET"," ",e.path," HTTP/1.1\r\n"};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1010 if e.query then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1011 r(a,4,"?");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1012 r(a,5,e.query);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1013 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1014 e.write(i(a));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1015 local a={[2]=": ",[4]="\r\n"};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1016 if l then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1017 for o,n in c(l)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1018 a[1],a[3]=o,n;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1019 e.write(i(a));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1020 t[o]=nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1021 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1022 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1023 for o,n in c(t)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1024 a[1],a[3]=o,n;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1025 e.write(i(a));
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1026 t[o]=nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1027 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1028 e.write("\r\n");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1029 if o then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1030 e.write(o);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1031 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1032 e.callback=function(a,t,o)n("debug","Calling callback, status %s",t or"---");return f(2,m(function()return s(a,t,o)end,w));end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1033 e.reader=h;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1034 e.state="status";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1035 d.register_request(e.handler,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1036 return e;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1037 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1038 function destroy_request(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1039 if e.conn then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1040 e.handler.close()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1041 d.ondisconnect(e.conn,"closed");
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1042 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1043 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1044 _M.urlencode=urlencode;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1045 return _M;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1046 end)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1047 package.preload['util.timer']=(function(...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1048 local h=require"net.server".addtimer;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1049 local a=require"net.server".event;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1050 local d=require"net.server".event_base;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1051 local s=os.time;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1052 local n=table.insert;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1053 local e=table.remove;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1054 local e,i=ipairs,pairs;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1055 local r=type;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1056 local o={};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1057 local t={};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1058 module"timer"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1059 local e;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1060 if not a then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1061 function e(e,a)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1062 local o=s();
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1063 e=e+o;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1064 if e>=o then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1065 n(t,{e,a});
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1066 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1067 a();
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1068 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1069 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1070 h(function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1071 local s=s();
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1072 if#t>0 then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1073 for a,e in i(t)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1074 n(o,e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1075 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1076 t={};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1077 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1078 for i,t in i(o)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1079 local t,a=t[1],t[2];
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1080 if t<=s then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1081 o[i]=nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1082 local t=a(s);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1083 if r(t)=="number"then e(t,a);end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1084 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1085 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1086 end);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1087 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1088 local t=(a.core and a.core.LEAVE)or-1;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1089 function e(a,e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1090 d:addevent(nil,0,function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1091 local e=e();
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1092 if e then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1093 return 0,e;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1094 else
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1095 return t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1096 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1097 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1098 ,a);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1099 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1100 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1101 add_task=e;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1102 return _M;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1103 end)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1104 package.preload['multihttp']=(function(...)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1105 local s=require"net.http";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1106 local o=require"net.server";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1107 local i=require"util.timer";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1108 local n,h,r,d=
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1109 pairs,ipairs,type,setmetatable;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1110 module"multihttp"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1111 __index=_M;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1112 function set_callback(t,e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1113 t.callback=e;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1114 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1115 function set_progress_callback(e,a,t)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1116 t=t or 1;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1117 e.progress_callback=a;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1118 i.add_task(t,function()
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1119 if e.progress_callback then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1120 e.progress_callback(e);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1121 if e.progress_callback then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1122 return t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1123 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1124 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1125 end);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1126 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1127 function add_url(t,e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1128 t.urls[e]=true;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1129 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1130 function remove_url(t,e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1131 t.urls[e]=nil;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1132 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1133 function download(e,i)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1134 e.status="downloading";
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1135 local t=0;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1136 for a in n(e.urls)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1137 t=t+1;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1138 e.urls[a]=
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1139 s.request(a,nil,function(n,t,s)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1140 if e.callback then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1141 e.downloading_count=e.downloading_count-1;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1142 e.callback(a,t,n,s);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1143 if e.downloading_count==0 and i then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1144 o.setquitting(true);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1145 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1146 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1147 end);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1148 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1149 e.download_count=t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1150 e.downloading_count=t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1151 if i then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1152 o.loop();
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1153 o.setquitting(false);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1154 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1155 return t;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1156 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1157 function progress(e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1158 local a={total={}};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1159 for o,t in n(e.urls)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1160 local e={};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1161 e.bytes_downloaded=t.havebodylength;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1162 e.bytes_total=t.bodylength;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1163 if e.bytes_total then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1164 e.percent=100/(e.bytes_total/e.bytes_downloaded);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1165 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1166 a[o]=e;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1167 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1168 return a;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1169 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1170 function new(t,e)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1171 local t={callback=t,urls={}};
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1172 if r(e)=="table"then
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1173 for a,e in h(e)do
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1174 t.urls[e]=true;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1175 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1176 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1177 return d(t,_M);
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1178 end
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1179 return _M;
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1180 end)
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1181 require"net.connlisteners"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1182 require"net.httpclient_listener"
6e60da4625db Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1183 return require"net.http";

mercurial