-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.js
More file actions
190 lines (157 loc) · 5.33 KB
/
Copy pathwebserver.js
File metadata and controls
190 lines (157 loc) · 5.33 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
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
/**
* Created by Aleksey Chichenkov <a.chichenkov@initi.ru> on 10/23/18.
*/
var fs = require('fs');
var Config = JSON.parse(fs.readFileSync("config.json", "utf8"));
var port = /*Config.port || 3000*/443;
var ROOT_FILE = Config.default_file || "index.html";
var WEB_FOLDER = Config.web_root || __dirname;
var _debug = Config._debug;
var _info = Config.info;
var requestHandler = function(request, response) {
_debug && console.log("");
_debug && console.log("");
_info && console.log("INCOMING URL: ", request.url);
if(valid_url(request.url)) { // check chars in url
var url = process_url(request.url); // get path and query
var info = path_info(url.path); // getr info file or not file
_debug && console.log("URL INFO: ", JSON.stringify(url));
_debug && console.log("PATH INFO: ", JSON.stringify(info));
var rel_path;
var load_file = false;
var type;
if(!info.is_file) {
type = get_type(ROOT_FILE);
_debug && console.log("TYPE: PATH");
rel_path = url.path + ROOT_FILE;
load_file = true;
} else {
_debug && console.log("TYPE: FILE");
type = get_type(info.file);
if(valid_type(type)){
_debug && console.log("VALID TYPE");
rel_path = url.path;
load_file = true;
} else {
_debug && console.log("INVALID TYPE", info.file);
}
}
var fpath = WEB_FOLDER + rel_path;
_debug && console.log("WEB_FOLDER: ", WEB_FOLDER);
_debug && console.log("rel_path: ", rel_path);
_debug && console.log("RESULT PATH: ", fpath);
if(load_file && fs.existsSync(fpath)) {
_debug && console.log("TRY LOAD: ", fpath);
var type_info = Config.types[type];
_debug && console.log("type_info", JSON.stringify(type_info));
if (type_info.binary) {
var contents = fs.readFileSync(fpath);
response.writeHead(200, {
'Content-Type': type_info.mime
});
response.write(contents, 'binary');
response.end();
} else {
fs.readFile(fpath, 'utf8', function (err, contents) {
_debug && console.log("LOAD RESPONSE: ", fpath);
response.writeHead(200, {
'Content-Type': type_info.mime
});
_debug && console.log("BINARY: false");
response.end(contents);
});
}
} else {
_debug && console.log("NO LOADED: ", fpath);
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8" });
response.end("url invalid or file does not exist");
}
} else {
_debug && console.log("INVALID URL: ", request.url);
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8" });
response.end("url invalid");
}
};
var webserver = require('https');
var options = {
key: fs.readFileSync('private.key', "utf8"),
cert: fs.readFileSync('certificate.crt', "utf8"),
ca: fs.readFileSync('ca_bundle.crt', "utf8"),
};
var server = webserver.createServer(options, requestHandler);
server.listen(port, function (err) {
if (err) {
return _info && console.log('something bad happened', err);
}
_info && console.log("https server is listening on port: " + port);
});
var process_url = function (_url) {
var match_query = _url.match(/\?/);
var path;
var has_query = false;
var query;
if(match_query) {
path = _url.substring(0, match_query.index);
query = _url.substring(match_query.index, _url.length);
has_query = true;
} else {
path = _url;
}
return {
path: path,
query: query,
has_query: has_query
}
};
var valid_url = function (_url) {
var match = _url.match(/[^a-zA-Z0-9_\-:%+=?\\/\.&]/);
return !match;
};
var valid_type = function (_type) {
var t = Config.types[_type];
return t != undefined;
};
var get_type = function (_file_name) {
var arr = _file_name.split(".");
var type;
if(arr.length == 1) {
type = "";
} else {
type = arr[arr.length - 1]
}
return type;
};
var path_info = function (_path) {
var info = {
is_file: false
};
var p = WEB_FOLDER.replace(/\\/gm, "/") + _path;
if(fs.existsSync(p)) {
var stats = fs.statSync(p);
var is_file = !stats.isDirectory();
if (is_file) {
var arr = _path.split("/");
var end = arr[arr.length - 1];
info.is_file = true;
info.file = end;
}
}
return info;
};
var http = require('http');
var httpServer = http.createServer(function (request, response) {
var location = "https://" + request.headers.host + request.url;
console.log("redirected to :",location);
// HTTP/1.1 301 Moved Permanently
// Location: http://www.example.org/index.asp
response.writeHead(301, {
"Location": location
});
response.end()
});
httpServer.listen(80, function (err) {
if (err) {
return _info && console.log('something bad happened', err);
}
_info && console.log("http server is listening on port: " + 80);
});