2017-06-15 15:15:25 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-08-02 19:06:47 +00:00
|
|
|
const http = require('http');
|
|
|
|
const https = require('https');
|
|
|
|
const url = require('url');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const mime = require('mime');
|
|
|
|
const WebSocketServer = require('ws').Server;
|
2017-06-15 15:15:25 +00:00
|
|
|
|
2018-02-09 03:59:46 +00:00
|
|
|
const fulfillSymbol = Symbol('fullfil callback');
|
2017-06-28 05:02:46 +00:00
|
|
|
const rejectSymbol = Symbol('reject callback');
|
|
|
|
|
2018-09-24 19:46:39 +00:00
|
|
|
class TestServer {
|
2017-07-07 06:45:18 +00:00
|
|
|
/**
|
|
|
|
* @param {string} dirPath
|
|
|
|
* @param {number} port
|
2018-09-24 19:46:39 +00:00
|
|
|
* @return {!TestServer}
|
2017-07-07 06:45:18 +00:00
|
|
|
*/
|
|
|
|
static async create(dirPath, port) {
|
2018-09-24 19:46:39 +00:00
|
|
|
const server = new TestServer(dirPath, port);
|
2017-07-07 06:45:18 +00:00
|
|
|
await new Promise(x => server._server.once('listening', x));
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {string} dirPath
|
|
|
|
* @param {number} port
|
2018-09-24 19:46:39 +00:00
|
|
|
* @return {!TestServer}
|
2017-07-10 22:09:52 +00:00
|
|
|
*/
|
|
|
|
static async createHTTPS(dirPath, port) {
|
2018-09-24 19:46:39 +00:00
|
|
|
const server = new TestServer(dirPath, port, {
|
2017-07-10 22:09:52 +00:00
|
|
|
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
|
|
|
|
cert: fs.readFileSync(path.join(__dirname, 'cert.pem')),
|
|
|
|
passphrase: 'aaaa',
|
|
|
|
});
|
|
|
|
await new Promise(x => server._server.once('listening', x));
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} dirPath
|
|
|
|
* @param {number} port
|
|
|
|
* @param {!Object=} sslOptions
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-07-10 22:09:52 +00:00
|
|
|
constructor(dirPath, port, sslOptions) {
|
|
|
|
if (sslOptions)
|
|
|
|
this._server = https.createServer(sslOptions, this._onRequest.bind(this));
|
|
|
|
else
|
|
|
|
this._server = http.createServer(this._onRequest.bind(this));
|
2017-07-07 15:55:30 +00:00
|
|
|
this._server.on('connection', socket => this._onSocket(socket));
|
2017-06-28 21:39:37 +00:00
|
|
|
this._wsServer = new WebSocketServer({server: this._server});
|
|
|
|
this._wsServer.on('connection', this._onWebSocketConnection.bind(this));
|
2017-06-21 20:51:06 +00:00
|
|
|
this._server.listen(port);
|
|
|
|
this._dirPath = dirPath;
|
2017-06-28 05:02:46 +00:00
|
|
|
|
2018-02-05 22:59:07 +00:00
|
|
|
this._startTime = new Date();
|
|
|
|
this._cachedPathPrefix = null;
|
|
|
|
|
2017-07-07 15:55:30 +00:00
|
|
|
/** @type {!Set<!net.Socket>} */
|
|
|
|
this._sockets = new Set();
|
|
|
|
|
2017-06-28 05:02:46 +00:00
|
|
|
/** @type {!Map<string, function(!IncomingMessage, !ServerResponse)>} */
|
|
|
|
this._routes = new Map();
|
2017-09-11 23:32:13 +00:00
|
|
|
/** @type {!Map<string, !{username:string, password:string}>} */
|
|
|
|
this._auths = new Map();
|
2018-04-03 22:21:08 +00:00
|
|
|
/** @type {!Map<string, string>} */
|
|
|
|
this._csp = new Map();
|
2019-02-25 03:31:35 +00:00
|
|
|
/** @type {!Set<string>} */
|
|
|
|
this._gzipRoutes = new Set();
|
2017-06-28 05:02:46 +00:00
|
|
|
/** @type {!Map<string, !Promise>} */
|
|
|
|
this._requestSubscribers = new Map();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-15 15:15:25 +00:00
|
|
|
|
2017-07-07 15:55:30 +00:00
|
|
|
_onSocket(socket) {
|
|
|
|
this._sockets.add(socket);
|
2017-07-19 07:40:52 +00:00
|
|
|
// ECONNRESET is a legit error given
|
|
|
|
// that tab closing simply kills process.
|
|
|
|
socket.on('error', error => {
|
|
|
|
if (error.code !== 'ECONNRESET')
|
|
|
|
throw error;
|
|
|
|
});
|
2017-07-07 15:55:30 +00:00
|
|
|
socket.once('close', () => this._sockets.delete(socket));
|
|
|
|
}
|
|
|
|
|
2018-02-05 22:59:07 +00:00
|
|
|
/**
|
|
|
|
* @param {string} pathPrefix
|
|
|
|
*/
|
|
|
|
enableHTTPCache(pathPrefix) {
|
|
|
|
this._cachedPathPrefix = pathPrefix;
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:32:13 +00:00
|
|
|
/**
|
|
|
|
* @param {string} path
|
|
|
|
* @param {string} username
|
|
|
|
* @param {string} password
|
|
|
|
*/
|
|
|
|
setAuth(path, username, password) {
|
|
|
|
this._auths.set(path, {username, password});
|
|
|
|
}
|
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
enableGzip(path) {
|
|
|
|
this._gzipRoutes.add(path);
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:21:08 +00:00
|
|
|
/**
|
|
|
|
* @param {string} path
|
|
|
|
* @param {string} csp
|
|
|
|
*/
|
|
|
|
setCSP(path, csp) {
|
|
|
|
this._csp.set(path, csp);
|
|
|
|
}
|
|
|
|
|
2017-07-07 15:55:30 +00:00
|
|
|
async stop() {
|
|
|
|
this.reset();
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const socket of this._sockets)
|
2017-07-07 15:55:30 +00:00
|
|
|
socket.destroy();
|
|
|
|
this._sockets.clear();
|
|
|
|
await new Promise(x => this._server.close(x));
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-15 15:15:25 +00:00
|
|
|
|
2017-06-28 05:02:46 +00:00
|
|
|
/**
|
|
|
|
* @param {string} path
|
|
|
|
* @param {function(!IncomingMessage, !ServerResponse)} handler
|
|
|
|
*/
|
|
|
|
setRoute(path, handler) {
|
|
|
|
this._routes.set(path, handler);
|
|
|
|
}
|
|
|
|
|
2017-06-30 01:54:01 +00:00
|
|
|
/**
|
2018-04-23 17:01:16 +00:00
|
|
|
* @param {string} from
|
|
|
|
* @param {string} to
|
2017-06-30 01:54:01 +00:00
|
|
|
*/
|
|
|
|
setRedirect(from, to) {
|
|
|
|
this.setRoute(from, (req, res) => {
|
|
|
|
res.writeHead(302, { location: to });
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-28 05:02:46 +00:00
|
|
|
/**
|
|
|
|
* @param {string} path
|
|
|
|
* @return {!Promise<!IncomingMessage>}
|
|
|
|
*/
|
|
|
|
waitForRequest(path) {
|
|
|
|
let promise = this._requestSubscribers.get(path);
|
|
|
|
if (promise)
|
|
|
|
return promise;
|
|
|
|
let fulfill, reject;
|
|
|
|
promise = new Promise((f, r) => {
|
|
|
|
fulfill = f;
|
|
|
|
reject = r;
|
|
|
|
});
|
|
|
|
promise[fulfillSymbol] = fulfill;
|
|
|
|
promise[rejectSymbol] = reject;
|
|
|
|
this._requestSubscribers.set(path, promise);
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this._routes.clear();
|
2017-09-11 23:32:13 +00:00
|
|
|
this._auths.clear();
|
2018-04-03 22:21:08 +00:00
|
|
|
this._csp.clear();
|
2019-02-25 03:31:35 +00:00
|
|
|
this._gzipRoutes.clear();
|
2017-08-21 23:39:04 +00:00
|
|
|
const error = new Error('Static Server has been reset');
|
|
|
|
for (const subscriber of this._requestSubscribers.values())
|
2017-06-28 05:02:46 +00:00
|
|
|
subscriber[rejectSymbol].call(null, error);
|
|
|
|
this._requestSubscribers.clear();
|
|
|
|
}
|
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
_onRequest(request, response) {
|
2017-07-10 21:13:44 +00:00
|
|
|
request.on('error', error => {
|
|
|
|
if (error.code === 'ECONNRESET')
|
|
|
|
response.end();
|
|
|
|
else
|
|
|
|
throw error;
|
|
|
|
});
|
2019-03-05 22:57:15 +00:00
|
|
|
request.postBody = new Promise(resolve => {
|
|
|
|
let body = '';
|
|
|
|
request.on('data', chunk => body += chunk);
|
|
|
|
request.on('end', () => resolve(body));
|
|
|
|
});
|
2017-08-21 23:39:04 +00:00
|
|
|
const pathName = url.parse(request.url).path;
|
2017-09-11 23:32:13 +00:00
|
|
|
if (this._auths.has(pathName)) {
|
|
|
|
const auth = this._auths.get(pathName);
|
2018-04-17 20:49:01 +00:00
|
|
|
const credentials = Buffer.from((request.headers.authorization || '').split(' ')[1] || '', 'base64').toString();
|
2017-09-11 23:32:13 +00:00
|
|
|
if (credentials !== `${auth.username}:${auth.password}`) {
|
|
|
|
response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Secure Area"' });
|
|
|
|
response.end('HTTP Error 401 Unauthorized: Access is denied');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 05:02:46 +00:00
|
|
|
// Notify request subscriber.
|
2018-09-20 18:31:19 +00:00
|
|
|
if (this._requestSubscribers.has(pathName)) {
|
2017-06-28 05:02:46 +00:00
|
|
|
this._requestSubscribers.get(pathName)[fulfillSymbol].call(null, request);
|
2018-09-20 18:31:19 +00:00
|
|
|
this._requestSubscribers.delete(pathName);
|
|
|
|
}
|
2017-08-21 23:39:04 +00:00
|
|
|
const handler = this._routes.get(pathName);
|
2018-02-07 17:31:53 +00:00
|
|
|
if (handler) {
|
2017-06-28 05:02:46 +00:00
|
|
|
handler.call(null, request, response);
|
2018-02-07 17:31:53 +00:00
|
|
|
} else {
|
|
|
|
const pathName = url.parse(request.url).path;
|
|
|
|
this.serveFile(request, response, pathName);
|
|
|
|
}
|
2017-06-28 05:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!IncomingMessage} request
|
|
|
|
* @param {!ServerResponse} response
|
2018-02-07 17:31:53 +00:00
|
|
|
* @param {string} pathName
|
2017-06-28 05:02:46 +00:00
|
|
|
*/
|
2018-02-07 17:31:53 +00:00
|
|
|
serveFile(request, response, pathName) {
|
2017-06-21 20:51:06 +00:00
|
|
|
if (pathName === '/')
|
|
|
|
pathName = '/index.html';
|
2018-02-05 22:59:07 +00:00
|
|
|
const filePath = path.join(this._dirPath, pathName.substring(1));
|
|
|
|
|
|
|
|
if (this._cachedPathPrefix !== null && filePath.startsWith(this._cachedPathPrefix)) {
|
|
|
|
if (request.headers['if-modified-since']) {
|
|
|
|
response.statusCode = 304; // not modified
|
|
|
|
response.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
response.setHeader('Cache-Control', 'public, max-age=31536000');
|
2018-06-14 17:18:02 +00:00
|
|
|
response.setHeader('Last-Modified', this._startTime.toISOString());
|
2018-02-05 22:59:07 +00:00
|
|
|
} else {
|
|
|
|
response.setHeader('Cache-Control', 'no-cache, no-store');
|
|
|
|
}
|
2018-04-03 22:21:08 +00:00
|
|
|
if (this._csp.has(pathName))
|
|
|
|
response.setHeader('Content-Security-Policy', this._csp.get(pathName));
|
2017-06-28 21:39:37 +00:00
|
|
|
|
2019-02-25 03:31:35 +00:00
|
|
|
fs.readFile(filePath, (err, data) => {
|
2017-06-21 20:51:06 +00:00
|
|
|
if (err) {
|
|
|
|
response.statusCode = 404;
|
2018-02-05 22:59:07 +00:00
|
|
|
response.end(`File not found: ${filePath}`);
|
2017-06-21 20:51:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-17 21:51:03 +00:00
|
|
|
response.setHeader('Content-Type', mime.getType(filePath));
|
2019-02-25 03:31:35 +00:00
|
|
|
if (this._gzipRoutes.has(pathName)) {
|
|
|
|
response.setHeader('Content-Encoding', 'gzip');
|
|
|
|
const zlib = require('zlib');
|
|
|
|
zlib.gzip(data, (_, result) => {
|
|
|
|
response.end(result);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
response.end(data);
|
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
});
|
|
|
|
}
|
2017-06-28 21:39:37 +00:00
|
|
|
|
|
|
|
_onWebSocketConnection(connection) {
|
|
|
|
connection.send('opened');
|
|
|
|
}
|
2017-06-15 15:15:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 19:46:39 +00:00
|
|
|
module.exports = {TestServer};
|