-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathhttp-server.js
210 lines (184 loc) · 5.6 KB
/
http-server.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
'use strict';
var fs = require('fs'),
union = require('union'),
ecstatic = require('ecstatic'),
auth = require('basic-auth'),
httpProxy = require('http-proxy'),
corser = require('corser'),
path = require('path'),
secureCompare = require('secure-compare');
// a hacky and direct workaround to fix https://github.com/http-party/http-server/issues/525
function getCaller() {
try {
var stack = new Error().stack;
var stackLines = stack.split('\n');
var callerStack = stackLines[3];
return callerStack.match(/at (.+) \(/)[1];
}
catch (error) {
return '';
}
}
var _pathNormalize = path.normalize;
path.normalize = function (p) {
var caller = getCaller();
var result = _pathNormalize(p);
// https://github.com/jfhbrook/node-ecstatic/blob/master/lib/ecstatic.js#L20
if (caller === 'decodePathname') {
result = result.replace(/\\/g, '/');
}
return result;
};
//
// Remark: backwards compatibility for previous
// case convention of HTTP
//
exports.HttpServer = exports.HTTPServer = HttpServer;
/**
* Returns a new instance of HttpServer with the
* specified `options`.
*/
exports.createServer = function (options) {
return new HttpServer(options);
};
/**
* Constructor function for the HttpServer object
* which is responsible for serving static files along
* with other HTTP-related features.
*/
function HttpServer(options) {
options = options || {};
if (options.root) {
this.root = options.root;
}
else {
try {
fs.lstatSync('./public');
this.root = './public';
}
catch (err) {
this.root = './';
}
}
this.headers = options.headers || {};
this.cache = (
options.cache === undefined ? 3600 :
// -1 is a special case to turn off caching.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Preventing_caching
options.cache === -1 ? 'no-cache, no-store, must-revalidate' :
options.cache // in seconds.
);
this.showDir = options.showDir !== 'false';
this.autoIndex = options.autoIndex !== 'false';
this.showDotfiles = options.showDotfiles;
this.gzip = options.gzip === true;
this.brotli = options.brotli === true;
if (options.ext) {
this.ext = options.ext === true
? 'html'
: options.ext;
}
this.contentType = options.contentType ||
this.ext === 'html' ? 'text/html' : 'application/octet-stream';
var before = options.before ? options.before.slice() : [];
if (options.logFn) {
before.push(function (req, res) {
options.logFn(req, res);
res.emit('next');
});
}
if (options.username || options.password) {
before.push(function (req, res) {
var credentials = auth(req);
// We perform these outside the if to avoid short-circuiting and giving
// an attacker knowledge of whether the username is correct via a timing
// attack.
if (credentials) {
// if credentials is defined, name and pass are guaranteed to be string
// type
var usernameEqual = secureCompare(options.username.toString(), credentials.name);
var passwordEqual = secureCompare(options.password.toString(), credentials.pass);
if (usernameEqual && passwordEqual) {
return res.emit('next');
}
}
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm=""');
res.end('Access denied');
});
}
if (options.cors) {
this.headers['Access-Control-Allow-Origin'] = '*';
this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Range';
if (options.corsHeaders) {
options.corsHeaders.split(/\s*,\s*/)
.forEach(function (h) { this.headers['Access-Control-Allow-Headers'] += ', ' + h; }, this);
}
before.push(corser.create(options.corsHeaders ? {
requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)
} : null));
}
if (options.robots) {
before.push(function (req, res) {
if (req.url === '/robots.txt') {
res.setHeader('Content-Type', 'text/plain');
var robots = options.robots === true
? 'User-agent: *\nDisallow: /'
: options.robots.replace(/\\n/, '\n');
return res.end(robots);
}
res.emit('next');
});
}
before.push(ecstatic({
root: this.root,
cache: this.cache,
showDir: this.showDir,
showDotfiles: this.showDotfiles,
autoIndex: this.autoIndex,
defaultExt: this.ext,
gzip: this.gzip,
brotli: this.brotli,
contentType: this.contentType,
handleError: typeof options.proxy !== 'string'
}));
if (typeof options.proxy === 'string') {
var proxy = httpProxy.createProxyServer({});
before.push(function (req, res) {
proxy.web(req, res, {
target: options.proxy,
changeOrigin: true
}, function (err, req, res, target) {
if (options.logFn) {
options.logFn(req, res, {
message: err.message,
status: res.statusCode });
}
res.emit('next');
});
});
}
var serverOptions = {
before: before,
headers: this.headers,
onError: function (err, req, res) {
if (options.logFn) {
options.logFn(req, res, err);
}
res.end();
}
};
if (options.https) {
serverOptions.https = options.https;
}
this.server = union.createServer(serverOptions);
if (options.timeout !== undefined) {
this.server.setTimeout(options.timeout);
}
}
HttpServer.prototype.listen = function () {
this.server.listen.apply(this.server, arguments);
};
HttpServer.prototype.close = function () {
return this.server.close.apply(this.server, arguments);
};