@@ -32,7 +32,8 @@ type RequestHandler = (
3232) => Promise < void > ;
3333
3434function createHttpRequestHandler ( options : {
35- acmeChallengeCallback : ( token : string ) => string | undefined
35+ acmeChallengeCallback : ( token : string ) => string | undefined ,
36+ rootDomain : string
3637} ) : RequestHandler {
3738 return async function handleRequest ( req , res ) {
3839 const url = new URL ( req . url ! , `http://${ req . headers . host } ` ) ;
@@ -57,7 +58,11 @@ function createHttpRequestHandler(options: {
5758 return ;
5859 }
5960
60- if ( path === '/' ) {
61+ const hostnamePrefix = url . hostname . endsWith ( options . rootDomain )
62+ ? url . hostname . slice ( 0 , - options . rootDomain . length - 1 )
63+ : undefined ;
64+
65+ if ( path === '/' && ! hostnamePrefix ) {
6166 res . writeHead ( 307 , {
6267 location : 'https://github.com/httptoolkit/testserver/'
6368 } ) ;
@@ -75,7 +80,7 @@ function createHttpRequestHandler(options: {
7580 }
7681
7782 const matchingEndpoint = httpEndpoints . find ( ( endpoint ) =>
78- endpoint . matchPath ( path )
83+ endpoint . matchPath ( path , hostnamePrefix )
7984 ) ;
8085
8186 if ( matchingEndpoint ) {
@@ -94,7 +99,8 @@ function createHttpRequestHandler(options: {
9499}
95100
96101export function createHttp1Handler ( options : {
97- acmeChallengeCallback : ( token : string ) => string | undefined
102+ acmeChallengeCallback : ( token : string ) => string | undefined ,
103+ rootDomain : string
98104} ) {
99105 const handleRequest = createHttpRequestHandler ( options ) ;
100106 const handler = new http . Server ( async ( req , res ) => {
@@ -124,7 +130,8 @@ export function createHttp1Handler(options: {
124130}
125131
126132export function createHttp2Handler ( options : {
127- acmeChallengeCallback : ( token : string ) => string | undefined
133+ acmeChallengeCallback : ( token : string ) => string | undefined ,
134+ rootDomain : string
128135} ) {
129136 const handleRequest = createHttpRequestHandler ( options ) ;
130137 const handler = http2 . createServer ( async ( req , res ) => {
0 commit comments