mirror of
https://github.com/cypress-io/cypress.git
synced 2026-01-11 09:20:20 -06:00
* chore: convert @packages/https-proxy to TypeScript and use vitest instead of mocha * chore: address comments from code review * Update packages/https-proxy/vitest.config.ts
48 lines
944 B
TypeScript
48 lines
944 B
TypeScript
import { parse } from 'url'
|
|
|
|
export function parseHost (hostString: string, defaultPort: number) {
|
|
let m
|
|
|
|
m = hostString.match(/^http:\/\/(.*)/)
|
|
|
|
if (m) {
|
|
const parsedUrl = parse(hostString)
|
|
|
|
return {
|
|
host: parsedUrl.hostname,
|
|
port: parsedUrl.port,
|
|
}
|
|
}
|
|
|
|
const hostPort = hostString.split(':')
|
|
const host = hostPort[0]
|
|
const port = hostPort.length === 2 ? +hostPort[1] : defaultPort
|
|
|
|
return {
|
|
host,
|
|
port,
|
|
}
|
|
}
|
|
|
|
export function hostAndPort (reqUrl: string, headers: any, defaultPort: number) {
|
|
let m
|
|
const {
|
|
host,
|
|
} = headers
|
|
|
|
const hostPort = parseHost(host, defaultPort)
|
|
|
|
// this handles paths which include the full url. This could happen if it's a proxy
|
|
m = reqUrl.match(/^http:\/\/([^\/]*)\/?(.*)$/)
|
|
|
|
if (m) {
|
|
const parsedUrl = parse(reqUrl)
|
|
|
|
hostPort.host = parsedUrl.hostname
|
|
hostPort.port = parsedUrl.port
|
|
reqUrl = parsedUrl.path
|
|
}
|
|
|
|
return hostPort
|
|
}
|