-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalache.js
248 lines (236 loc) · 8.34 KB
/
malache.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
/*This simple http server is made by malpower
* for testing web applications for mobile.
* This porgram is based on Node.js which is
* a platform from joyent, inc. It allows user
* building backend service programs with JS.
* Node.js uses Google V8 engine, it means that
* it will be fast.
* You can also share your files on http with
* this simple http server.Conf.js is a configure
* file for this simple server.Anthor named
* malpower who is web programmer in Chengdu.
* Email: [email protected]
*/
var conf=require("./conf");
var cluster=require("cluster");
var http=require("http");
var fs=require("fs");
var os=require("os");
var Ex=require("./extends");
var Serv=require("./services");
var Active=require("./actives");
var util=require("util");
var cp=require("child_process");
var cwd=process.cwd();
if (process.argv.length==3)
{
if (parseInt(process.argv[2])>0 && parseInt(process.argv[2])<65536)
{
conf.port=parseInt(process.argv[2]);
}
}
var sys=new Object;
sys.root=cwd;
var ex;
(function() //initialize the environment of active pages.
{
for (var i=0;i<conf.activeModules.length;i++)
{
sys[conf.activeModules[i]]=require(conf.activeModules[i]);
}
sys.conf=conf;
ex=new Ex(sys);
Serv(ex);
})();
var server=http.createServer(function(req,res) //http requesting handler
{
process.chdir(cwd);
var folder=new String;
console.log("\r\n~~~~~~~~~~~~~~~~~~~~~REQUEST~~~~~~~~~~~~~~~~~~~~~~"); //show request information on every requesting.
console.log("request time: "+Date());
console.log("connected from client: "+req.socket.remoteAddress+":"+req.socket.remotePort+"\r\nrequested file: "+req.url+"\r\nfrom: "+req.headers.host);
console.log("request method: "+req.method);
console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
res.setHeader("Server","Malache HTTP server, made by malpower([email protected])");
if (typeof(conf.domains[req.headers.host])!="undefined") //redirect into domain directories.
{
folder=conf.domains[req.headers.host].folder;
if (folder==undefined)
{
folder=conf.folder;
}
}
else
{
folder=conf.folder;
}
if (req.url.substring(req.url.length-1,req.url.length)=="/") //set default page for directories
{
if (conf.domains[req.headers.host]!=undefined && typeof(conf.domains[req.headers.host].defaultPage)=="string") //set default page of in this domain.
{
req.url+=conf.domains[req.headers.host].defaultPage;
}
else
{
req.url+=conf.defaultIndex;
}
}
var uri=req.url.substring(0,req.url.indexOf("?"));
if (uri=="")
{
uri=req.url;
}
if (uri.substring(uri.lastIndexOf(".")+1)==conf.protect) //check if the request is for a active page.requesting protected file will be changed into active page.
{
res.setHeader("content-type","text/html");
res.statusCode=404;
res.end("<h1>Error 404<br />File not found!</h1><br /><span style='font-size: 11px'>Malache simple http server is made by malpower.</span>");
return false;
}
if (ex.runService(req,res)) //check and run if the requesting is for plugin.
{
return false;
}
if (Active(req,res,sys)) //check and run if the requesting is for active page.
{
return false;
}
try
{
req.url=decodeURIComponent(req.url);
}
catch (e)
{
console.log("DECODE URI ERROR");
}
if (req.method=="POST")
{
res.statusCode=400;
res.setHeader("content-type","text/html");
res.end("<h1>400</h1><br /><br />wrong requesting method");
req.socket.destroy();
return false;
}
var realFilePath=req.url.substring(0,(req.url.lastIndexOf("?")>1?req.url.lastIndexOf("?"):req.url.length));
fs.open(folder+realFilePath,"r",function(err,fd) //read file and response data.
{
if (err) //response 404 if the file is not existing.
{
res.setHeader("content-type","text/html");
res.statusCode=404;
res.end("<h1>Error 404<br />File not found!</h1><br /><span style='font-size: 11px'>Malache simple http server is made by malpower.</span>");
return false;
}
var stat=fs.fstatSync(fd); //read details of requesting file.
if (!stat.isFile()) //response 403 if the requesting file is a folder.
{
res.statusCode=403;
res.end("<h1>Error 403</h1><br />folder cannot be listed.");
return false;
}
if (req.headers["if-modified-since"]==stat.mtime) //about cache.
{
res.statusCode=304;
res.setHeader("last-modified",stat.mtime);
res.setHeader("cache-control","private");
res.end(); //does not response data body, browser will read this file in it's cache.
return;
}
res.statusCode=200; //response data normally.
res.setHeader("cache-control","private");
res.setHeader("last-modified",stat.mtime);
var efn=req.url.substring(realFilePath.lastIndexOf(".")+1,realFilePath.length);
res.setHeader("content-type","unknow/*"); //set default content-type.
res.setHeader("content-length",stat.size);
for (var i=0;i<conf.contentTypes.length;i++)
{
if (conf.contentTypes[i].type==efn)
{
res.setHeader("content-type",conf.contentTypes[i].value);
break;
}
}
var rs=fs.createReadStream(folder+realFilePath); //read file.
rs.on("data",function(data)
{
res.write(data); //response data.
});
rs.on("end",function() //finish responsing.
{
res.end();
fs.close(fd);
delete rs;
});
rs.on("error",function(e) //response 500 if get any error on reading file.
{
res.statusCode=500;
res.end("500 ERROR");
fs.close(fd);
console.log(e.message);
delete rs;
});
});
});
var ips=new Array; //an array for keeping local ip addresses.
(function() //for getting local ip address.
{
var ifaces=os.networkInterfaces();
for (var x in ifaces)
{
for (var y in ifaces[x])
{
if (ifaces[x][y]["family"]=="IPv4")
{
if (ifaces[x][y]["address"]!="127.0.0.1")
{
ips.push(ifaces[x][y]["address"]);
}
}
}
}
})();
try
{
if (conf.bindingIp)
{
server.listen(conf.port,conf.bindingIp); //restart listenning port.
}
else
{
server.listen(conf.port);
}
server.on("error",function(e)
{
console.log(e);
console.log("Maybe you can change http port in conf.js.This error captured because that port "+conf.port+" is in use.");
process.exit(1);
});
}
catch(e) //error on listenning.
{
console.log(e);
console.log("Maybe you can change http port in conf.js.This error captured because that port "+conf.port+" is in use.");
process.exit(1);
}
var version="D201405121859s";
process.title="Malache HTTP Server["+version+"]"; //set title text
console.log("============Malache Http Server by malpower=========="); //show malache informations.
console.log("Version: "+version);
console.log("HTTP server is now running on port: "+conf.port);
console.log("Default WEB base folder: "+conf.folder);
console.log("Domain list: ");
for (var x in conf.domains) //list domains which has set in conf.js
{
console.log(" "+x+" : "+conf.domains[x].folder);
}
console.log("Address list: ");
for (var i=0;i<ips.length;i++)
{
console.log(" "+ips[i]);
}
console.log("-----------------------------------------------------\r\n");
process.on("uncaughtException",function(e) //prevent exiting program.
{
console.log("FINAL ERROR========MAIN");
console.log(e.stack);
});