@@ -119,7 +119,7 @@ export abstract class Server {
119
119
protected readonly server : http . Server | https . Server ;
120
120
protected rootPath = path . resolve ( __dirname , "../../../.." ) ;
121
121
private listenPromise : Promise < string > | undefined ;
122
- public readonly protocol : string ;
122
+ public readonly protocol : "http" | "https" ;
123
123
public readonly options : ServerOptions ;
124
124
125
125
public constructor ( options : ServerOptions ) {
@@ -157,17 +157,12 @@ export abstract class Server {
157
157
}
158
158
159
159
/**
160
- * The local address of the server. If you pass in a request, it will use the
161
- * request's host if listening on a port (rather than a socket). This enables
162
- * setting the webview endpoint to the same host the browser is using.
160
+ * The *local* address of the server.
163
161
*/
164
- public address ( request ?: http . IncomingMessage ) : string {
162
+ public address ( ) : string {
165
163
const address = this . server . address ( ) ;
166
164
const endpoint = typeof address !== "string"
167
- ? ( request
168
- ? request . headers . host ! . split ( ":" , 1 ) [ 0 ]
169
- : ( address . address === "::" ? "localhost" : address . address )
170
- ) + ":" + address . port
165
+ ? ( address . address === "::" ? "localhost" : address . address ) + ":" + address . port
171
166
: address ;
172
167
return `${ this . protocol } ://${ endpoint } ` ;
173
168
}
@@ -189,15 +184,17 @@ export abstract class Server {
189
184
return { content : await util . promisify ( fs . readFile ) ( filePath ) , filePath } ;
190
185
}
191
186
187
+ protected withBase ( request : http . IncomingMessage , path : string ) : string {
188
+ return `${ this . protocol } ://${ request . headers . host } ${ this . options . basePath } ${ path } ` ;
189
+ }
190
+
192
191
private onRequest = async ( request : http . IncomingMessage , response : http . ServerResponse ) : Promise < void > => {
193
192
try {
194
193
const payload = await this . preHandleRequest ( request ) ;
195
194
response . writeHead ( payload . redirect ? HttpCode . Redirect : payload . code || HttpCode . Ok , {
196
195
"Cache-Control" : "max-age=86400" , // TODO: ETag?
197
196
"Content-Type" : getMediaMime ( payload . filePath ) ,
198
- ...( payload . redirect ? {
199
- Location : `${ this . protocol } ://${ request . headers . host } ${ this . options . basePath } ${ payload . redirect } ` ,
200
- } : { } ) ,
197
+ ...( payload . redirect ? { Location : this . withBase ( request , payload . redirect ) } : { } ) ,
201
198
...payload . headers ,
202
199
} ) ;
203
200
response . end ( payload . content ) ;
@@ -464,11 +461,11 @@ export class MainServer extends Server {
464
461
] ) ;
465
462
const environment = this . services . get ( IEnvironmentService ) as IEnvironmentService ;
466
463
const locale = environment . args . locale || await getLocaleFromConfig ( environment . userDataPath ) ;
467
- const webviewEndpoint = this . address ( request ) + "/webview/" ;
468
464
const cwd = process . env . VSCODE_CWD || process . cwd ( ) ;
469
465
const workspacePath = parsedUrl . query . workspace as string | undefined ;
470
466
const folderPath = ! workspacePath ? parsedUrl . query . folder as string | undefined || this . options . folderUri || cwd : undefined ;
471
467
const remoteAuthority = request . headers . host as string ;
468
+ const webviewEndpoint = this . withBase ( request , "/webview/" ) ;
472
469
const transformer = getUriTransformer ( remoteAuthority ) ;
473
470
const options : Options = {
474
471
WORKBENCH_WEB_CONGIGURATION : {
0 commit comments