<commit_before>/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as http from 'http';
import fs from 'fs';
import path from 'path';
import { mime, wsServer } from '../utilsBundle';
import type { WebSocketServer } from '../utilsBundle';
import { assert } from './';

export type ServerRouteHandler = (request: http.IncomingMessage, response: http.ServerResponse) => boolean;

export class HttpServer {
  private _server: http.Server;
  private _urlPrefix: string;
  private _port: number = 0;
  private _started = false;
  private _routes: { prefix?: string, exact?: string, handler: ServerRouteHandler }[] = [];
  private _activeSockets = new Set<import('net').Socket>();
  constructor(address: string = '') {
    this._urlPrefix = address;
    this._server = http.createServer(this._onRequest.bind(this));
  }

  createWebSocketServer(): WebSocketServer {
    return new wsServer({ server: this._server });
  }

  routePrefix(prefix: string, handler: ServerRouteHandler) {
    this._routes.push({ prefix, handler });
  }

  routePath(path: string, handler: ServerRouteHandler) {
    this._routes.push({ exact: path, handler });
  }

  port(): number {
    return this._port;
  }

  async start(port?: number): Promise<string> {
    assert(!this._started, 'server already started');
    this._started = true;
    this._server.on('connection', socket => {
      this._activeSockets.add(socket);
      socket.once('close', () => this._activeSockets.delete(socket));
    });
    this._server.listen(port);
    await new Promise(cb => this._server!.once('listening', cb));<commit_msg>fix(html-report): listen on 127.0.0.1 only (#15361)<commit_after>/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as http from 'http';
import fs from 'fs';
import path from 'path';
import { mime, wsServer } from '../utilsBundle';
import type { WebSocketServer } from '../utilsBundle';
import { assert } from './';

export type ServerRouteHandler = (request: http.IncomingMessage, response: http.ServerResponse) => boolean;

export class HttpServer {
  private _server: http.Server;
  private _urlPrefix: string;
  private _port: number = 0;
  private _started = false;
  private _routes: { prefix?: string, exact?: string, handler: ServerRouteHandler }[] = [];
  private _activeSockets = new Set<import('net').Socket>();
  constructor(address: string = '') {
    this._urlPrefix = address;
    this._server = http.createServer(this._onRequest.bind(this));
  }

  createWebSocketServer(): WebSocketServer {
    return new wsServer({ server: this._server });
  }

  routePrefix(prefix: string, handler: ServerRouteHandler) {
    this._routes.push({ prefix, handler });
  }

  routePath(path: string, handler: ServerRouteHandler) {
    this._routes.push({ exact: path, handler });
  }

  port(): number {
    return this._port;
  }

  async start(port?: number): Promise<string> {
    assert(!this._started, 'server already started');
    this._started = true;
    this._server.on('connection', socket => {
      this._activeSockets.add(socket);
      socket.once('close', () => this._activeSockets.delete(socket));
    });
    this._server.listen(port, '127.0.0.1');
    await new Promise(cb => this._server!.once('listening', cb));
{'fraction_non_alphanumeric': 0.1284826157034308, 'fraction_numerical': 0.006216900759843426, 'mean_word_length': 4.027777777777778, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1453606', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1201, 'n_words': 401}