2017-06-21 20:56:19 +00:00
|
|
|
import {parse as urlParse} from 'url';
|
|
|
|
import certificateUrlParse from './certificate-url-parse';
|
2017-08-04 16:54:11 +00:00
|
|
|
import {escapeRegex, setDefaultProtocol} from '../common/misc';
|
2017-06-21 20:56:19 +00:00
|
|
|
|
|
|
|
const DEFAULT_PORT = 443;
|
|
|
|
|
2017-09-17 14:04:56 +00:00
|
|
|
export function urlMatchesCertHost (certificateHost, requestUrl) {
|
2017-06-21 20:56:19 +00:00
|
|
|
const cHostWithProtocol = setDefaultProtocol(certificateHost, 'https:');
|
|
|
|
const {hostname, port} = urlParse(requestUrl);
|
|
|
|
const {hostname: cHostname, port: cPort} = certificateUrlParse(cHostWithProtocol);
|
|
|
|
|
|
|
|
const assumedPort = parseInt(port) || DEFAULT_PORT;
|
|
|
|
const assumedCPort = parseInt(cPort) || DEFAULT_PORT;
|
|
|
|
|
2017-08-04 16:54:11 +00:00
|
|
|
const cHostnameRegex = escapeRegex(cHostname || '').replace(/\\\*/g, '.*');
|
2017-06-21 20:56:19 +00:00
|
|
|
|
2017-09-17 14:04:56 +00:00
|
|
|
if (assumedCPort !== assumedPort) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!(hostname || '').match(`^${cHostnameRegex}$`);
|
2017-06-21 20:56:19 +00:00
|
|
|
}
|