/** * @fileoverview Simple HTTP server used in hint's tests to mimick certain scenarios. */ import * as http from 'http'; import * as https from 'https'; import forEach = require('lodash/forEach'); import random = require('lodash/random'); import * as express from 'express'; // to work with option 'strict', we can't use import. const onHeaders = require('on-headers'); import { normalizeString } from '@hint/utils-string'; import { normalizeHeaderValue } from '@hint/utils-network'; import { IServer, Message, ServerConfiguration, WebhintMessage } from './types'; import { replacer, reviver } from './buffer-serialization'; /** * List of ports in our range that are unsafe for Chrome. * See: https://src.chromium.org/viewvc/chrome/trunk/src/net/base/net_util.cc?view=markup */ const unsafePorts = [3659, 4045, 6000, 6665, 6666, 6667, 6668, 6669]; /** A testing server for webhint's hints */ /* istanbul ignore next */ export class SameThreadServer implements IServer { private static usedPorts = new Set(unsafePorts); private static maxPort: number = 65535; private static minPort: number = 3000; private _app: express.Application; private _server: https.Server | http.Server = {} as http.Server; private _port: number; private _isHTTPS: boolean | undefined; private static readonly _cert: string = `-----BEGIN CERTIFICATE----- MIIDBTCCAe2gAwIBAgIJAPrIPsJ7DJ0SMA0GCSqGSIb3DQEBBQUAMBkxFzAVBgNV BAMMDmxvY2FsaG9zdDo4NDQzMB4XDTE3MDcwNzE3MTI0OVoXDTI3MDcwNTE3MTI0 OVowGTEXMBUGA1UEAwwObG9jYWxob3N0Ojg0NDMwggEiMA0GCSqGSIb3DQEBAQUA A4IBDwAwggEKAoIBAQDmIlQAv6ZAGrEg3cHP+NpfqQV+LS0WxSczPAcZmDXJea5e UAt7Gc9gI7ULIHW68TJ8rGVdZKKriPqXmG/vY9+u/UZ1IwcQZWMTPTDL8Ku1/lDj 0zxO3avujEnHGcgp1RdfS0TsigS5rSWmx+dt9a7zbn1oCh2l6e1HeFd0NF15tXZG shL5CXxAip6hhZWCkZ5XjOm8iF4Bn2vCO00gb41iIoD7+2NaGBZkQa3L1NfuTpVf pkW30tRi7QiMOVr7W9I1FNPZAXKvi1pH8nlvYflKrG6XN6V8LzOW3xrYLs3HqbAg NCFNa2s/elcKOAIZFqN5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DY1PUJQ -----END RSA PRIVATE KEY-----`; private getNewValidPort(): number { let port = random(SameThreadServer.minPort, SameThreadServer.maxPort); while (SameThreadServer.usedPorts.has(port)) { port = random(SameThreadServer.minPort, SameThreadServer.maxPort); } return port; } public constructor(isHTTPS?: boolean) { this._port = this.getNewValidPort(); this._app = express(); this._app.disable('x-powered-by'); this._app.use((req, res, next) => { res.setHeader('Cache-Control', 'no-cache'); next(); }); this._isHTTPS = isHTTPS; } /** * Because we don't know the port until we start the server, we need to update * the references to http://localhost in the HTML to http://localhost:finalport. */ private updateLocalhost(html: string): string { return html.replace(/\/\/localhost\//g, `//localhost:${this._port}/`); } private handleHeaders(res: express.Response, headers: any) { onHeaders(res, () => { Object.entries(headers).forEach(([header, value]) => { if (value !== null) { res.setHeader(header, value as string); } else { res.removeHeader(header); } }); }); } private getContent(value: any): string { if (typeof value === 'string') { return this.updateLocalhost(value); } else if (value && typeof value.content !== 'undefined') { return typeof value.content === 'string' ? this.updateLocalhost(value.content) : value.content; } return ''; } private getNumberOfMatches(req: express.Request, requestConditions: any) { const headers = requestConditions.request && requestConditions.request.headers; /* * Matching is done only based on headers, as for the time * beeing there is no need to match based on other things. */ if (!headers) { return 0; } let numberOfMatches = 0; for (const [header, value] of Object.entries(headers)) { // TODO: handle `string[]` in `req.headers` const headerValue = normalizeHeaderValue(req.headers as any, header); if ((headerValue !== normalizeString(value as string)) || (!headerValue && (value === null))) { return 0; } numberOfMatches++; } return numberOfMatches; } private getValue(req: express.Request, config: any) { let bestNumberOfMatches = 1; let bestMatch = null; for (const [key, value] of Object.entries(config)) { let requestConditions; try { requestConditions = JSON.parse(key); const newNumberOfMatches = this.getNumberOfMatches(req, requestConditions); if (newNumberOfMatches >= bestNumberOfMatches) { bestMatch = value; bestNumberOfMatches = newNumberOfMatches; } } catch (e) { // Ignore invalid keys. } } return bestMatch; } private isConditionalConfig(configuration: any): boolean { /* * The following is done to quickly determine the type of * configuration. Possible options are: * * 1) Simple config * (server response is always the same) * * { * name: ..., * reports: [{ message: ... }], * serverConfig: { * '/': { * content: ..., * headers: ... * }, * ... * } * } * * * 2) Conditional config * (server response depends on the request) * * { * name: ... * reports: [{ message: ... }], * serverConfig: { * [JSON.stringify({ * headers: { * ... * } * })]: { * '/': { * content: ..., * headers: ... * }, * }, * ... * } * } */ try { return typeof JSON.parse(Object.entries(configuration)[0][0]) === 'object'; } catch (e) { // Ignore. } return false; } private normalizeConfig(configuration: any) { const config: any = {}; /* * This function convers something such as: * * { * '{"request":{"headers":{"Accept-Encoding":"gzip"}}}': { * '/': { * content: ... * headers: ... * }, * ... * } * '{"request":{"headers":{"Accept-Encoding":"br"}}}': { * '/': { * content: ... * headers: ... * }, * ... * } * ... * } * * to * * { * '/': { * '{"request":{"headers":{"Accept-Encoding":"gzip"}}}': { * content: ... * headers: ... * }, * '{"request":{"headers":{"Accept-Encoding":"br"}}}': { * content: ... * headers: ... * }, * ... * } * ... * } * */ for (const [k, v] of Object.entries(configuration)) { for (const [key, value] of Object.entries(v as {})) { config[key] = { ...config[key], ...{ [k]: value } }; } } return config; } /** Applies the configuration for routes to the server. */ public configure(configuration: ServerConfiguration) { let customFavicon = false; if (typeof configuration === 'string') { this._app.get('/', (req, res) => { res.send(configuration); }); return; } const conditionalConfig = this.isConditionalConfig(configuration); const config = conditionalConfig ? this.normalizeConfig(configuration) : configuration; forEach(config, (val, key) => { customFavicon = customFavicon || key === '/favicon.ico'; this._app.get(key, (req, res) => { const value: any = conditionalConfig ? this.getValue(req, val) : val; const content = this.getContent(value); /* * Hacky way to make `request` fail, but required * for testing cases such as the internet connection * being down when a particular request is made. */ if (value === null) { res.redirect(301, 'test://fa.il'); return; } const redirects = [301, 302, 303, 307, 308]; if (value && redirects.includes(value.status)) { res.redirect(value.status, content); return; } if (value && value.auth) { if (req.headers.authorization !== value.auth) { res.setHeader('WWW-Authorization', 'Basic realm="testing", charset="utf-8"'); res.send(); return; } } if (value && value.status) { res.status(value.status); } if (value && value.headers) { this.handleHeaders(res, value.headers); } if (content !== 'timeout') { // if `content === 'timeout'`, leaves the server hanging to test the implementation of timeout. res.send(content); } }); }); if (!customFavicon) { this._app.get('/favicon.ico', (req, res) => { res.status(200); res.setHeader('Content-Length', '0'); res.setHeader('Content-Type', 'image/x-icon'); res.end(); }); } } /** Starts listening on the given port. */ public start(): Promise { return new Promise((resolve, reject) => { let options; if (this._isHTTPS) { options = { cert: SameThreadServer._cert, key: SameThreadServer._key }; this._server = https.createServer(options, this._app); } else { this._server = http.createServer(this._app); } // TODO: need to find a way to cast `err` to a [System Error](https://nodejs.org/dist/latest-v7.x/docs/api/errors.html#errors_system_errors) this._server.on('error', (err: any) => { // Need to use `console.log` because otherwise we don't get the output console.log(`Server Error: ${err.code}`); if (err.code === 'EADDRINUSE' || err.code === 'EACCES') { setImmediate(() => { this._port = this.getNewValidPort(); this._server.close(); this._server.listen(this._port); }); } else { reject(err); } }); this._server.once('listening', () => { resolve(); }); this._server.listen(this._port); }); } /** Stops the server and frees the port. */ public stop() { this._server.close(); return Promise.resolve(null); } public getPort() { return this._port; } public get port() { return this._port; } } /** * We assume that if there's something it means it should be https: * * `node server.js https` == `node server.js something` */ const useHttps = !!process.argv[2]; const server = new SameThreadServer(useHttps); type Action = (message: WebhintMessage) => Promise; /** Wrapper to do the filtering of actions based on the type */ /* istanbul ignore next */ const action = (fn: Action) => { const type = fn.name; return async (message: Message) => { if (message.webhint.type !== type) { return false; } const payload = await fn(message.webhint); if (process.send) { const message = { webhint: { payload, type } } as Message; const serializedMessage = JSON.stringify(message, replacer); process.send(serializedMessage); } return true; }; }; /* istanbul ignore next */ const start = (message: WebhintMessage) => { return server.start(); }; /* istanbul ignore next */ const stop = (message: WebhintMessage) => { // TODO: Disconnect and stop process? return server.stop(); }; /* istanbul ignore next */ const port = (message: WebhintMessage) => { return Promise.resolve(server.port); }; /* istanbul ignore next */ const configure = (message: WebhintMessage) => { const { payload } = message; server.configure(payload as ServerConfiguration); return Promise.resolve(); }; /* istanbul ignore next */ const serverActions = [ action(port), action(start), action(configure), action(stop) ]; process.on('message', /* istanbul ignore next */ async (serializedMessage: string) => { const message = typeof serializedMessage === 'string' ? JSON.parse(serializedMessage, reviver) : serializedMessage; if (!message.webhint) { // Message is not for us return; } const dupe = Array.from(serverActions); let handled = false; while (!handled) { const action = dupe.shift(); if (!action) { break; } handled = await action(message); if (handled) { break; } } });