message: Call vscode.env.asExternalUri to obaint a valid origin when starting a http server. Close
diff: @@ -41,18 +41,43 @@ export class Server {
  private readonly httpServer: http.Server
  private address?: AddressInfo
  readonly pdfFilePathEncoder: PdfFilePathEncoder
  + private validOriginUri: vscode.Uri | undefined
  constructor(extension: Extension) {
  this.extension = extension
  this.pdfFilePathEncoder = new PdfFilePathEncoder(extension)
  this.httpServer = http.createServer((request, response) => this.handler(request, response))
  + this.initializeHttpServer()
  + this.extension.logger.addLogMessage('[Server] Creating LaTeX Workshop http and websocket server.')
  + }
  +
  + get port(): number {
  + const portNum = this.address?.port
  + if (portNum === undefined) {
  + this.extension.logger.addLogMessage('Server port number is undefined.')
  + throw new Error('Server port number is undefined.')
  + }
  + return portNum
  + }
  +
  + private get validOrigin(): string {
  + if (this.validOriginUri) {
  + return `http://${this.validOriginUri.authority}`
  + } else {
  + throw new Error('[Server] validOrigin is undefined')
  + }
  + }
  +
  + private initializeHttpServer() {
  const configuration = vscode.workspace.getConfiguration('latex-workshop')
  const viewerPort = configuration.get('viewer.pdf.internal.port') as number
  - this.httpServer.listen(viewerPort, '127.0.0.1', undefined, () => {
  + this.httpServer.listen(viewerPort, '127.0.0.1', undefined, async () => {
  const address = this.httpServer.address()
  if (address && typeof address !== 'string') {
  this.address = address
  this.extension.logger.addLogMessage(`[Server] Server successfully started: ${JSON.stringify(address)}`)
  + this.validOriginUri = await this.obtainValidOrigin(address.port)
  + this.extension.logger.addLogMessage(`[Server] valdOrigin is ${this.validOrigin}`)
  this.initializeWsServer()
  } else {
  this.extension.logger.addLogMessage(`[Server] Server failed to start. Address is invalid: ${JSON.stringify(address)}`)
  @@ -61,20 +86,12 @@ export class Server {
  this.httpServer.on('error', (err) => {
  this.extension.logger.addLogMessage(`[Server] Error creating LaTeX Workshop http server: ${JSON.stringify(err)}.`)
  })
  - this.extension.logger.addLogMessage('[Server] Creating LaTeX Workshop http and websocket server.')
  - }
  -
  - get port(): number {
  - const portNum = this.address?.port
  - if (portNum === undefined) {
  - this.extension.logger.addLogMessage('Server port number is undefined.')
  - throw new Error('Server port number is undefined.')
  - }
  - return portNum
  }
  - private get validOrigin(): string {
  - return `http://127.0.0.1:${this.port}`
  + private async obtainValidOrigin(serverPort: number): Promise<vscode.Uri> {
  + const origUrl = `http://127.0.0.1:${serverPort}/`
  + const uri = await vscode.env.asExternalUri(vscode.Uri.parse(origUrl, true))
  + return uri
  }
  private initializeWsServer() {
  