/
HttpImpl.hx
307 lines (255 loc) · 7.68 KB
/
HttpImpl.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
hxc: -D nodejs
*/
package cloudshift.http;
import cloudshift.Core;
using cloudshift.Mixin;
import cloudshift.Http;
import cloudshift.http.Mime;
import js.Node;
private typedef Cache = {
var mtime:Float;
var buf:NodeBuffer;
}
class HttpImpl implements HttpServer,implements Part<HostPort,String,HttpServer,HttpEvents> {
public var part_:Part_<HostPort,String,HttpServer,HttpEvents>;
var _server:NodeHttpServer;
var _cache:Hash<Cache>;
var _getHandler:String->NodeHttpServerReq->NodeHttpServerResp->Int->Void;
var _routes:Array<{re:EReg,handler:THandler}>;
var _notFound:NodeHttpServerReq->NodeHttpServerResp->Void;
var _index:String;
var _root:String;
var _serverName:String;
static var readStreamOpt:ReadStreamOpt = cast {
flags: 'r',
mode: 0666
};
static var _formidable:Dynamic ;
static function __init__() {
_formidable = js.Node.require('formidable');
}
public function
new() {
_routes = [];
_index = "index.html";
_root = null;
_serverName = "Cloudshift 0.2.3";
_getHandler = defaultGetHandler;
_cache = new Hash();
part_ = Core.part(this);
}
public function
start_(d:HostPort,?oc:Outcome<String,HttpServer>) {
if (oc == null)
oc = Core.outcome();
_server = Node.http.createServer(function(req,resp) {
var
url = req.url,
match = false;
if (_routes != null) {
for (r in _routes) {
if (r.re.match(url)) {
match = true;
try {
r.handler(r.re,req,resp);
} catch(ex:Dynamic) {
Core.log(E("handler exp:"+ex));
}
break;
}
}
}
if (!match && _root != null) {
if (req.method == "GET") {
if (url == "/") url = _index;
_getHandler(url,req,resp,200);
}
}
});
Core.log(I("Starting "+_serverName+" on "+d.host+":"+d.port));
/*
_server.addListener(NodeC.EVENT_HTTPSERVER_REQUEST,function(request,response) {
notify(Request(request,response));
});
*/
_server.listen(d.port,d.host,function() {
oc.resolve(Right(cast(this,HttpServer)));
});
return oc;
}
public function stop_(?d) {
var p = Core.outcome();
_server.close();
//_server.on("close",function() {
p.resolve(Right(cast this));
// });
return p;
}
public function
handler(r:EReg,handler:THandler):HttpServer {
_routes.push({re:r,handler:handler});
return this;
}
public function
notFound(nf:NodeHttpServerReq->NodeHttpServerResp->Void):HttpServer {
_notFound = nf;
return this;
}
public function
index(indexFile:String):HttpServer {
_index = indexFile;
return this;
}
public function
serverName(serverName:String):HttpServer {
_serverName = serverName;
return this;
}
public function
root(rootDir:String):HttpServer {
_root = if (!rootDir.endsWith("/")) rootDir + "/" ;
_getHandler = serve;
return this;
}
function
defaultGetHandler(path:String,req:NodeHttpServerReq,resp:NodeHttpServerResp,statusCode:Int) {
do404(req,resp);
}
public function
fields(req:NodeHttpServerReq,cb:TFields,uploadDir="/tmp") {
parseFields(req,cb,uploadDir);
}
public static function
parseFields(req:NodeHttpServerReq,cb:TFields,?uploadDir:String) {
var
form:Dynamic = untyped __js__('new cloudshift.http.HttpImpl._formidable.IncomingForm()'),
fields = new Hash<String>(),
files:Array<{field:String,file:TUploadFile}> = null;
if (uploadDir != null) form.uploadDir = uploadDir;
form.on('field',function(field,value) {
fields.set(Std.string(field),Std.string(value));
})
.on('file',function(field,file) {
if (files == null) files = [];
files.push({field:field,file:file});
})
.on('end', function() {
cb(fields,files);
});
form.parse(req);
}
public function
serve(path:String,req:NodeHttpServerReq,resp:NodeHttpServerResp,statusCode=200) {
var fileToServe = if (_root != null ) _root+path else path;
Node.fs.stat(fileToServe,function (e, stat:NodeStat) {
if (e != null) {
do404(req,resp);
return;
}
var
mtime = Date.fromString(new String(stat.mtime)),
fmtime = mtime.getTime(),
size = stat.size,
eTag = Node.stringify([stat.ino, size, mtime].join('-')),
// since = Reflect.field(req.headers,"if-modified-since"),
modified = false;
/*
if (since != null) {
modified = (parse(since) < fmtime) ;
}*/
if (Reflect.field(req.headers,"if-none-match") == eTag ){
resp.statusCode = 304;
headers(resp,size,path,eTag,mtime);
resp.end();
return;
}
if (stat.isFile()) {
resp.statusCode = statusCode;
headers(resp,size,path,eTag,mtime);
serveFromCache(resp,fileToServe,stat,fmtime);
} else if (stat.isDirectory()) {
do404(req,resp);
} else {
do404(req,resp);
}
});
}
public function
serveNoCache(path:String,req:NodeHttpServerReq,resp:NodeHttpServerResp,statusCode=200) {
var fileToServe = if (_root != null ) _root+path else path;
Node.fs.stat(fileToServe,function (e, stat:NodeStat) {
if (e != null) {
do404(req,resp);
return;
}
var
mtime = Date.fromString(new String(stat.mtime)),
size = stat.size;
if (stat.isFile()) {
resp.statusCode = statusCode;
headers(resp,size,path,null,mtime);
resp.setHeader("cache-control","no-cache");
Node.fs.createReadStream(path,readStreamOpt).pipe(resp);
} else if (stat.isDirectory()) {
do404(req,resp);
} else {
do404(req,resp);
}
});
}
function do404(req:NodeHttpServerReq,resp:NodeHttpServerResp) {
// resp.writeHead(404);
if (_notFound != null)
_notFound(req,resp);
//resp.end();
}
function headers(resp:NodeHttpServerResp,size,path,etag,mtime:Date) {
resp.setHeader("Content-Length",size);
resp.setHeader("Content-Type",Reflect.field(Mime.types,Node.path.extname(path).substr(1)));
resp.setHeader("Date",UTCString(Date.now()));
if (etag != null)
resp.setHeader("ETag",etag);
resp.setHeader("Last-Modified", UTCString(mtime));
resp.setHeader("Server",_serverName);
}
function
serveFromCache(resp:NodeHttpServerResp,path:String,stat:NodeStat,mtime:Float) {
var cached = _cache.get(path) ;
if (cached == null) {
pipeFile(resp,path,stat,mtime);
} else {
if (cached.mtime < mtime) {
pipeFile(resp,path,stat,mtime);
} else {
resp.end(cached.buf);
}
}
}
function
pipeFile(resp:NodeHttpServerResp,path:String,stat:NodeStat,mtime:Float){
var
buf = new NodeBuffer(stat.size),
offset = 0;
_cache.set(path,{mtime:mtime,buf:buf});
Node.fs.createReadStream(path,readStreamOpt)
.on('error', function (err) {
Node.console.error(err);
})
.on('data', function (chunk) {
chunk.copy(buf, offset);
offset += chunk.length;
})
.on('close', function () {
})
.pipe(resp);
}
static function
UTCString(d:Date) : String {
return untyped __js__("d.toUTCString()");
}
static function
parse(d:String):Float {
return untyped __js__("Date.parse(d)");
}
}