forked from shimondoodkin/nodejs-mongodb-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httputils_old-multiprartjs.js
executable file
·511 lines (475 loc) · 14.8 KB
/
httputils_old-multiprartjs.js
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//thanks to creationix - large protion of the code here is learned from his connect library.
var querystring = require('querystring');
var sys = require('sys');
var multipart = require("deps/multipart-js/lib/multipart");
var fs = require('fs');
var _ = require('deps/nodejs-clone-extend/merger');
this.temp=__dirname+'/temp/';
var lastrandom=[]
var lastrandom_time=0;
function uniquerandom()
{
var newtime=(new Date()).getTime(),random;
if(lastrandom_time!=newtime&&lastrandom.length>0) lastrandom=[];
do
{
random=Math.floor(Math.random() * 9999);
} while (lastrandom.indexOf(random)!=-1);
lastrandom.push(random);
return (newtime*10000)+random;
} this.uniquerandom=uniquerandom();
this.postdecoders =
{
'application/x-www-form-urlencoded': querystring.parse,
'application/json': JSON.parse,
};
var mylog='';
function upload_files(request, response, folder, callback)
{
request.setEncoding('binary');
// request.setEncoding('binary'); // do not set encoding to binary it needs it as buffer
// the StringDecoder is initilized lazyly to Stream._decoder and if it is existes it decodes strings to String from Buffer.
// i need buffer so it is bad to set encoding.
//
// console.log(sys.inspect(request)); // see the request stream.
//
//if i will whish to sue decoder it goes like this:
//
//var StringDecoder = require("string_decoder").StringDecoder; // lazy load
//this._decoder = new StringDecoder(encoding);
//var string = self._decoder.write(pool.slice(start, end));
//if (string.length) self.emit('data', string);
// matches .xxxxx or [xxxxx] or ['xxxxx'] or ["xxxxx"] with optional [] at the end
var chunks = /(?:(?:^|\.)([^\[\(\.]+)(?=\[|\.|$|\()|\[([^"'][^\]]*?)\]|\["([^\]"]*?)"\]|\['([^\]']*?)'\])(\[\])?/g;
// Parse a key=val string.
function add_to_object_by_name (obj, key, value) // a modified version of QueryString.parse = QueryString.decode
{
try
{
var res=obj,next;
key.replace(chunks, function (all, name, nameInBrackets, nameIn2Quotes, nameIn1Quotes, isArray, offset) {
var end = offset + all.length == key.length;
name = name || nameInBrackets || nameIn2Quotes || nameIn1Quotes;
next = end ? value : {};
//next = next && (+next == next ? +next : next);
if (Array.isArray(res[name])) {
res[name].push(next);
res = next;
} else {
if (name in res) {
if (isArray || end) {
res = (res[name] = [res[name], next])[1];
} else {
res = res[name];
}
} else {
if (isArray) {
res = (res[name] = [next])[0];
} else {
res = res[name] = next;
}
}
}
});
}
catch(err)
{
err.message="error parsing variable names of post , form elements.\r\n"+err.message;
err.stack="error parsing variable names of post , form elements.\r\n"+err.stack;
throw err
}
return obj;
};
var parser = multipart.parser();
parser.headers = request.headers;
var request_vars=[];
var name, savename, filename, text, file;
var inquee=0,finished=false;
if(folder.charAt(folder.length-1)!='/') folder+='/';
parser.addListener('onPartBegin', function(part)
{
inquee++;
name = part.name;
//console.log('begin name '+ name)
if(!part.filename)
{
text="";
file=false;
inquee--;
if(finished&&inquee==0)
{
callback(null,request_vars); // if file writing goes beyond parsing, call callback it later when done.
}
}
else
{
var exten = part.filename.substring( part.filename.lastIndexOf(".") , part.filename.length ).toLowerCase();
var randomname='upload_'+uniquerandom()+exten;
savename=folder+randomname;
file = fs.createWriteStream(savename,{ 'flags': 'w', 'encoding': 'binary', 'mode': 0666 });
file.addListener('close', function(part)
{
inquee--;
if(finished&&inquee==0)
{
callback(null,request_vars); // if file writing goes beyond parsing, call callback it later when done.
}
});
}
// instead onData and onPartEnd, a pump should be set up here
// like: sys.pump(part, file, function() { sys.debug('file pump end'); });
});
/*
function(part)
{
inquee--;
if(finished&&inquee==0)
callback(null,request_vars); // if file writing goes beyond parsing, call callback it later when done.
}
*/
parser.addListener('onData', function(chunk) { if(file) file.write(chunk); else text+=chunk; });
parser.addListener('onPartEnd', function(part)
{
//console.log(sys.inspect(part.headers['content-type']));
//console.log(sys.inspect(part.headers['content-disposition']));
if(file)
{
add_to_object_by_name(request_vars,part.name,{filename:part.filename,path:savename,mime:part.headers['content-type']});
file.end();
}
else
{
add_to_object_by_name(request_vars,part.name,decodeURIComponent( escape( text ) ));
}
});
parser.addListener('onEnd', function(part) { finished=true; if(inquee==0) {callback(null,request_vars);} });
parser.addListener('onError', function(err) { console.log(' parser on end '); callback(err,request_vars); });
sys.pump(request, parser, function() { console.log('request end'); });
}
this.post=function (req, res, callback )
{
//console.log("post received:");
//console.log(sys.inspect(req.post));
callback(req.post);
}
this.realpost=function (req, res, callback )
// called first of all to overcome a bug in node js
// (it could be called any time in any page.
// but there is a bug: the request object dies at the end of a function,
// for example if the later work is done in a settimeout
// so to overcome i have renamed post to realpost
// called realpost as first thing in process request
// it continues to the callback
// later it uses the "post" merthod to get what was posted
// and fetched at the begining with realpost)
// when this bug is fixed i can rename back realpost to post.
{
if(
req.method==='POST'
// || req.method=='PUT'
)
{
var content_type='';
var decoder=false;
if(req.headers['content-type'])
{
content_type=req.headers['content-type'].split(';')[0];
if(this.postdecoders[content_type])
decoder=this.postdecoders[content_type];
}
var data = '';
if(req.headers['content-type'].indexOf('multipart/form-data')==-1)
{
try
{
//console.log("POST6");
req.setEncoding('utf8');
//console.log(sys.inspect(req._events));
req.on('data', function(chunk)
{
// console.log("POST7");
data += chunk;
});
req.on('end', function()
{
//console.log("POST8");
//req.postmime = content_type;
//req.postraw = data;
if(decoder)
{
try
{
req.post = decoder(data);
callback(req.post);
}
catch (err)
{
callback({});
}
}
else
{
//console.log("POST9");
callback({});
}
});
}catch(e){console.log(e.stack);}
//console.log("post readble:"+(req.connection.readable));
//req.resume();
}
else
{
//console.log("GONE TO UPLOAD FILES");
upload_files(req, res,this.temp,function(error,data)
{
req.post = data;
if(error!==null) callback({});
else callback(data);
});
}
}
else
{
callback({});
}
}
this.referrer = function(req, res, callback)
{
// actualy no need call back here because it is not transfering any thing
if(callback)
callback(req, res,req.headers.referrer || req.headers.referer || "");
else
return req.headers.referrer || req.headers.referer || "";
}
this.redirect = function(req, res, url, callback, code )
{
res.writeHead( code || 302, {'Location': url } );
res.end();
if(callback)callback();
};
function parseCookie(str)
{
var obj = {},
pairs = str.split(/[;,] */);
for (var i = 0, len = pairs.length; i < len; ++i) {
var pair = pairs[i],
eqlIndex = pair.indexOf('='),
key = pair.substr(0, eqlIndex).trim().toLowerCase(),
val = pair.substr(++eqlIndex, pair.length).trim();
// Quoted values
if (val[0] === '"') {
val = val.slice(1, -1);
}
// Only assign once
if (obj[key] === undefined) {
obj[key] = querystring.unescape(val, true);
}
}
return obj;
} this.parseCookie=parseCookie;
function cookie(req)
{
if(req.headers.cookie)
{
req.parsedcookie=parseCookie(req.headers.cookie);
return req.parsedcookie;
}
return {};
};this.cookie = cookie;
function get_user(req)
{
var parsedcookie=cookie(req); // cookie names are lowercase
if(parsedcookie['user_id'])
{
req.user_id=parsedcookie['user_id'];
//req.user_hash=parsedcookie['user_hash'];
}
return parsedcookie;
} this.get_user=get_user;
function save_user(req,header,user_id)
{
var pid=upid// add hash later
req.user_id=user_id;
//req.user_pass=user_hash;
// header['Set-Cookie']='user_id="'+user_id+'"; path=/'+(req.secure?';');
/*
document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
document.cookie ='user_id="'+uid+'";user_hash="'+pid+'"; path=/'+(req.secure?'; secure:':'')
*/
} this.save_user=save_user;
function clear_user(req,header)
{
req.user_id='';
header['Set-Cookie']='user_id=;user_hash=; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'+(req.secure?'; secure:':'');
} this.clear_user=clear_user;
/*
now i have a session module
function session_start(req,header)
{
var parsedcookie=cookie(req),sid; // cookie names are lowercase
if(parsedcookie['sid'])
{
req.session_id=parsedcookie['sid'];
}
else
{
sid=uniquerandom();//((newtime*10000)+(Math.floor(Math.random() * 9999)))
req.session_id=sid;
header['Set-Cookie']='sid="'+sid+'"; path=/'+(req.secure?'; secure:':'');
}
return parsedcookie;
} this.session_start=session_start;
function session_save(req)
{
if(req.session_id);
} this.session_save=session_save;
function session_load(req)
{
if(req.session_id);
} this.session_load=session_load;
*/
this.contentTypes = { // types from node paperboy
"aiff": "audio/x-aiff",
"arj": "application/x-arj-compressed",
"asf": "video/x-ms-asf",
"asx": "video/x-ms-asx",
"au": "audio/ulaw",
"avi": "video/x-msvideo",
"bcpio": "application/x-bcpio",
"ccad": "application/clariscad",
"cod": "application/vnd.rim.cod",
"com": "application/x-msdos-program",
"cpio": "application/x-cpio",
"cpt": "application/mac-compactpro",
"csh": "application/x-csh",
"css": "text/css",
"deb": "application/x-debian-package",
"dl": "video/dl",
"doc": "application/msword",
"drw": "application/drafting",
"dvi": "application/x-dvi",
"dwg": "application/acad",
"dxf": "application/dxf",
"dxr": "application/x-director",
"etx": "text/x-setext",
"ez": "application/andrew-inset",
"fli": "video/x-fli",
"flv": "video/x-flv",
"gif": "image/gif",
"gl": "video/gl",
"gtar": "application/x-gtar",
"gz": "application/x-gzip",
"hdf": "application/x-hdf",
"hqx": "application/mac-binhex40",
"html": "text/html",
"ico": "image/x-icon",
"ice": "x-conference/x-cooltalk",
"ief": "image/ief",
"igs": "model/iges",
"ips": "application/x-ipscript",
"ipx": "application/x-ipix",
"jad": "text/vnd.sun.j2me.app-descriptor",
"jar": "application/java-archive",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"latex": "application/x-latex",
"lsp": "application/x-lisp",
"lzh": "application/octet-stream",
"m": "text/plain",
"m3u": "audio/x-mpegurl",
"man": "application/x-troff-man",
"me": "application/x-troff-me",
"midi": "audio/midi",
"mif": "application/x-mif",
"mime": "www/mime",
"movie": "video/x-sgi-movie",
"mp4": "video/mp4",
"mpg": "video/mpeg",
"mpga": "audio/mpeg",
"ms": "application/x-troff-ms",
"nc": "application/x-netcdf",
"oda": "application/oda",
"ogm": "application/ogg",
"pbm": "image/x-portable-bitmap",
"pdf": "application/pdf",
"pgm": "image/x-portable-graymap",
"pgn": "application/x-chess-pgn",
"pgp": "application/pgp",
"pm": "application/x-perl",
"png": "image/png",
"pnm": "image/x-portable-anymap",
"ppm": "image/x-portable-pixmap",
"ppz": "application/vnd.ms-powerpoint",
"pre": "application/x-freelance",
"prt": "application/pro_eng",
"ps": "application/postscript",
"qt": "video/quicktime",
"ra": "audio/x-realaudio",
"rar": "application/x-rar-compressed",
"ras": "image/x-cmu-raster",
"rgb": "image/x-rgb",
"rm": "audio/x-pn-realaudio",
"rpm": "audio/x-pn-realaudio-plugin",
"rtf": "text/rtf",
"rtx": "text/richtext",
"scm": "application/x-lotusscreencam",
"set": "application/set",
"sgml": "text/sgml",
"sh": "application/x-sh",
"shar": "application/x-shar",
"silo": "model/mesh",
"sit": "application/x-stuffit",
"skt": "application/x-koan",
"smil": "application/smil",
"snd": "audio/basic",
"sol": "application/solids",
"spl": "application/x-futuresplash",
"src": "application/x-wais-source",
"stl": "application/SLA",
"stp": "application/STEP",
"sv4cpio": "application/x-sv4cpio",
"sv4crc": "application/x-sv4crc",
"swf": "application/x-shockwave-flash",
"tar": "application/x-tar",
"tcl": "application/x-tcl",
"tex": "application/x-tex",
"texinfo": "application/x-texinfo",
"tgz": "application/x-tar-gz",
"tiff": "image/tiff",
"tr": "application/x-troff",
"tsi": "audio/TSP-audio",
"tsp": "application/dsptype",
"tsv": "text/tab-separated-values",
"txt": "text/plain",
"unv": "application/i-deas",
"ustar": "application/x-ustar",
"vcd": "application/x-cdlink",
"vda": "application/vda",
"vivo": "video/vnd.vivo",
"vrm": "x-world/x-vrml",
"wav": "audio/x-wav",
"wax": "audio/x-ms-wax",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"wmx": "video/x-ms-wmx",
"wrl": "model/vrml",
"wvx": "video/x-ms-wvx",
"xbm": "image/x-xbitmap",
"xlw": "application/vnd.ms-excel",
"xml": "text/xml",
"xpm": "image/x-xpixmap",
"xwd": "image/x-xwindowdump",
"xyz": "chemical/x-pdb",
"zip": "application/zip"
};
this.charsets = {
'text/javascript': 'UTF-8',
'text/html': 'UTF-8',
'text/css': 'UTF-8'
};