- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpserver.js
More file actions
Latest commit
executable file
·132 lines (115 loc) · 4.19 KB
/
Copy pathhttpserver.js
File metadata and controls
executable file
·132 lines (115 loc) · 4.19 KB
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
#! /usr/bin/env node
/**
* Created by Yuchi on 2015/10/17.
*/
varfs=require("fs");
//var process = require("process");
varchild_process=require('child_process');
varhttp=require("http");
varpath=require("path");
varmime=require("mime-types");
varFilesMgn=require("./filesmgn.js");
varargv=require("minimist")(process.argv.slice(2),
{"boolean":["h","m","d"],"string":["i"],alias:{"help":"h","md5":"m","date":"d","index":"i","port":"p"}});
functionisnumber(o){
returntypeofo=="number";
}
functionfixedEncodeURI(str){
returnencodeURI(str).replace(/%5B/g,'[').replace(/%5D/g,']');
}
if(argv.h||argv._.length==0){
console.log(""+
"Usage: mdhs [options] [file_paths]\n"+
" mdhs -p 8000 ~/*\n"+
"Options:\n"+
" -h, --help Help\n"+
" -p, --port port Default port number is 8000\n"+
" \n"+
" -m, --md5 Enable md5sum, show in web page\n"+
" -d, --date Last modified date\n"+
" -i, --index index.html Default folder's index file name\n"+
"File paths:\n"+
" At least one\n"
);
process.exit();
}
varindexs_filename=[];
if(argv.i){
argv.i.split(",").forEach(function(val){
varv=val.trim();
if(v.length>0)indexs_filename.push(v);
});
}
varfilesmgn=newFilesMgn(argv._,indexs_filename);
port=isnumber(argv.p)?argv.p:8000;
md5sum_cmd={
'darwin': "md5 -r",
'freebsd': "md5sum",
'linux': "md5sum",
'sunos': "md5sum",
//'win32': "FCIV-md5-sha1", // not sure
}[process.platform];
http.createServer(function(request,response){
varparams=require("url").parse(request.url);
varpathname=decodeURI(params.pathname);
varbody;
varresult=filesmgn.find_url_path(pathname);
if(result==null){
body="File not found.";
response.writeHead(404,{
'Content-Length': body.length,
'Content-Type': "text/plain"});
response.end(body);
}elseif(typeofresult=="string"){
varsta=fs.statSync(result);
response.writeHead(200,{
'Content-Type': mime.lookup(result)||"application/octet-stream",
'Content-Length': sta.size
});
varreadStream=fs.createReadStream(result);
readStream.pipe(response);
vardate=newDate();
console.log("[%d:%d:%d] Download file: %s",date.getHours(),date.getMinutes(),date.getSeconds(),result);
}elseif(typeofresult=="object"&&result.files_map!=null){
body="<html><body>";
variter_fun=function(file){
varfile_path;
if(result.base_folder==null){
file_path=result.files_map[file];
}else{
file_path=path.join(result.base_folder,file);
}
varsta=fs.statSync(file_path);
varmd5sum="";
varfile_date="";
if(argv.m&&!sta.isDirectory()&&md5sum_cmd!=null){
md5sum=" ("+child_process.execSync(md5sum_cmd+" \""+file_path+"\"").toString().split(" ")[0]+")";
}
if(argv.d){
file_date=" "+sta.mtime;
}
body+="<a href='"+fixedEncodeURI(result.base_url+"/"+file)+"'>"+file+"</a>"+file_date+md5sum+"<p>";
};
if(result.base_folder==null){
// map
for(varfileinresult.files_map){
iter_fun(file);
}
}else{
// array
result.files_map.forEach(iter_fun);
}
body+="</body></html>";
response.writeHead(404,{
'Content-Length': body.length,
'Content-Type': "text/html"});
response.end(body);
}else{
body="What are you looking for?";
response.writeHead(404,{
'Content-Length': body.length,
'Content-Type': "text/plain"});
response.end(body);
}
}).listen(port);
console.log('Server running at http://127.0.0.1:'+port+"/");