private _bazelMiddleware()

in bazel/http-server/server.ts [74:114]


  private _bazelMiddleware(req: http.IncomingMessage, res: http.ServerResponse) {
    if (!req.url) {
      res.statusCode = 500;
      res.end('Error: No url specified');
      return;
    }

    // Detect if the url escapes the server's root path
    for (const rootPath of this._rootPaths) {
      const absoluteRootPath = path.resolve(rootPath);
      const absoluteJoinedPath = path.resolve(path.posix.join(rootPath, getManifestPath(req.url)));
      if (!absoluteJoinedPath.startsWith(absoluteRootPath)) {
        res.statusCode = 500;
        res.end('Error: Detected directory traversal');
        return;
      }
    }

    // Implements the HTML history API fallback logic based on the requirements of the
    // "connect-history-api-fallback" package. See the conditions for a request being redirected
    // to the index: https://github.com/bripkens/connect-history-api-fallback#introduction
    if (
      this._historyApiFallback &&
      req.method === 'GET' &&
      !req.url.includes('.') &&
      req.headers.accept &&
      req.headers.accept.includes('text/html')
    ) {
      res.end(this._getIndexHtmlContent());
    } else {
      const resolvedPath = this._resolveUrlFromRunfiles(req.url);

      if (resolvedPath === null) {
        res.statusCode = 404;
        res.end('Not found - Error 404');
        return;
      }

      send(req, resolvedPath).pipe(res);
    }
  }