<commit_before>part of bridge.http;

class Server {
  final _BackwardsCompatibilityPipeline _fallbackPipeline;
  final Container _container;
  final HttpConfig _config;
  Pipeline _pipeline;
  _HttpServer _runningServer;

  Server(Container container, this._config)
      : _container = container,
        _fallbackPipeline = new _BackwardsCompatibilityPipeline(container);

  String get hostname => _config.host;

  int get port => _config.port;

  bool get isRunning => _runningServer != null;

  Pipeline get pipeline => _pipeline ?? _fallbackPipeline;

  void usePipeline(Pipeline pipeline) {
    _pipeline = pipeline;
  }

  Future<String> start() async {
    if (isRunning) throw new StateError('The server is already running!');
    _runningServer = new _HttpServer(handle, _config);
    if (_config.useSsl) {
      await _runningServer.startSecure();
      return 'https://$hostname:${_config.sslPort} (http://$hostname:$port)';
    }
    await _runningServer.start();
    return 'http://$hostname:$port';
  }

  Future stop() async {
    if (!isRunning)
      throw new StateError('The server isn\'t running!');
    await _runningServer.stop();
    _runningServer = null;
  }

  Future<shelf.Response> handle(shelf.Request request) {
    return pipeline.handle(request, _container);
  }

  @Deprecated('very soon. Create a middleware instead')
  void modulateRouteReturnValue(modulation(value)) {
    _fallbackPipeline.modulateRouteReturnValue(modulation);
  }

  @Deprecated("very soon. "
      "The app's Pipeline must include the Middleware instead.")
  void addMiddleware(Object middleware, {bool highPriority: false}) {
    _fallbackPipeline.addMiddleware(middleware, highPriority: highPriority);
  }

  @Deprecated('very soon. Create a middleware instead')
  void handleException(Type exceptionType, Function handler) {
    _fallbackPipeline.handleException(exceptionType, handler);
  }

  @Deprecated('very soon. Bind a router in container instead')
  void attachRouter(Router router) {}
}

class _BackwardsCompatibilityPipeline extends Pipeline {
  final Container _container;
  final List middlewareList = [];
  final Map errorHandlersMap = {};

  _BackwardsCompatibilityPipeline(this._container);

  @override get middleware => new List.from(defaultMiddleware)
    ..addAll(middlewareList);

  @override get errorHandlers => errorHandlersMap;

  void addMiddleware(Object middleware, {bool highPriority: false}) {
    if (highPriority) {
      middlewareList.insert(0, middleware);
    } else {
      middlewareList.add(middleware);
    }
  }

  void handleException(Type exceptionType, Function handler) {
    errorHandlersMap[exceptionType] = handler;
  }

  void modulateRouteReturnValue(modulation(value)) {
    addMiddleware(
        new _RouteReturnValueModulationMiddleware(_container, modulation));
  }
}

class _RouteReturnValueModulationMiddleware extends Middleware {
  final Container _container;
  final Function _modulation;

  _RouteReturnValueModulationMiddleware(this._container, this._modulation);

  Future<shelf.Response> handle(shelf.Request request) {
    return super.handle(convert(
        request,
        Object,
        _modulation
    ));
  }
}

class _HttpServer {
  final shelf.Handler handler;
  final HttpConfig config;
  HttpServer server;
  HttpServer secureServer;

  _HttpServer(this.handler, this.config);

  Future start() async {
    await _start(handler);
  }

  Future startSecure() async {
    secureServer = await HttpServer.bindSecure(
        config.host,
        config.sslPort,
        new SecurityContext()
          ..useCertificateChain(config.certificate)
          ..usePrivateKey(
              config.privateKey, password: config.privateKeyPassword)
    );
    if (config.port != config.sslPort)
      await _start(const shelf.Pipeline()
          .addHandler((shelf.Request request) {
        return new shelf.Response.seeOther(
            'https://${config.host}:${config.sslPort}/${request.url}');
      }));
    shelf_io.serveRequests(secureServer, handler);
  }

  Future _start(shelf.Handler handler) async {
    server = await HttpServer.bind(config.host, config.port);
    shelf_io.serveRequests(server, handler);
  }

  Future stop() async {
    await server?.close();
    await secureServer?.close();
  }
}
<commit_msg>Correction of redirection URL generation<commit_after>part of bridge.http;

class Server {
  final _BackwardsCompatibilityPipeline _fallbackPipeline;
  final Container _container;
  final HttpConfig _config;
  Pipeline _pipeline;
  _HttpServer _runningServer;

  Server(Container container, this._config)
      : _container = container,
        _fallbackPipeline = new _BackwardsCompatibilityPipeline(container);

  String get hostname => _config.host;

  int get port => _config.port;

  bool get isRunning => _runningServer != null;

  Pipeline get pipeline => _pipeline ?? _fallbackPipeline;

  void usePipeline(Pipeline pipeline) {
    _pipeline = pipeline;
  }

  Future<String> start() async {
    if (isRunning) throw new StateError('The server is already running!');
    _runningServer = new _HttpServer(handle, _config);
    if (_config.useSsl) {
      await _runningServer.startSecure();
      return 'https://$hostname:${_config.sslPort} (http://$hostname:$port)';
    }
    await _runningServer.start();
    return 'http://$hostname:$port';
  }

  Future stop() async {
    if (!isRunning)
      throw new StateError('The server isn\'t running!');
    await _runningServer.stop();
    _runningServer = null;
  }

  Future<shelf.Response> handle(shelf.Request request) {
    return pipeline.handle(request, _container);
  }

  @Deprecated('very soon. Create a middleware instead')
  void modulateRouteReturnValue(modulation(value)) {
    _fallbackPipeline.modulateRouteReturnValue(modulation);
  }

  @Deprecated("very soon. "
      "The app's Pipeline must include the Middleware instead.")
  void addMiddleware(Object middleware, {bool highPriority: false}) {
    _fallbackPipeline.addMiddleware(middleware, highPriority: highPriority);
  }

  @Deprecated('very soon. Create a middleware instead')
  void handleException(Type exceptionType, Function handler) {
    _fallbackPipeline.handleException(exceptionType, handler);
  }

  @Deprecated('very soon. Bind a router in container instead')
  void attachRouter(Router router) {}
}

class _BackwardsCompatibilityPipeline extends Pipeline {
  final Container _container;
  final List middlewareList = [];
  final Map errorHandlersMap = {};

  _BackwardsCompatibilityPipeline(this._container);

  @override get middleware => new List.from(defaultMiddleware)
    ..addAll(middlewareList);

  @override get errorHandlers => errorHandlersMap;

  void addMiddleware(Object middleware, {bool highPriority: false}) {
    if (highPriority) {
      middlewareList.insert(0, middleware);
    } else {
      middlewareList.add(middleware);
    }
  }

  void handleException(Type exceptionType, Function handler) {
    errorHandlersMap[exceptionType] = handler;
  }

  void modulateRouteReturnValue(modulation(value)) {
    addMiddleware(
        new _RouteReturnValueModulationMiddleware(_container, modulation));
  }
}

class _RouteReturnValueModulationMiddleware extends Middleware {
  final Container _container;
  final Function _modulation;

  _RouteReturnValueModulationMiddleware(this._container, this._modulation);

  Future<shelf.Response> handle(shelf.Request request) {
    return super.handle(convert(
        request,
        Object,
        _modulation
    ));
  }
}

class _HttpServer {
  final shelf.Handler handler;
  final HttpConfig config;
  HttpServer server;
  HttpServer secureServer;

  _HttpServer(this.handler, this.config);

  Future start() async {
    await _start(handler);
  }

  Future startSecure() async {
    secureServer = await HttpServer.bindSecure(
        config.host,
        config.sslPort,
        new SecurityContext()
          ..useCertificateChain(config.certificate)
          ..usePrivateKey(
              config.privateKey, password: config.privateKeyPassword)
    );
    if (config.port != config.sslPort)
      await _start(const shelf.Pipeline()
          .addHandler((shelf.Request request) {
        return new shelf.Response.seeOther(
            'https://${request.headers['host']}:${config.sslPort}/${request.url}');
      }));
    shelf_io.serveRequests(secureServer, handler);
  }

  Future _start(shelf.Handler handler) async {
    server = await HttpServer.bind(config.host, config.port);
    shelf_io.serveRequests(server, handler);
  }

  Future stop() async {
    await server?.close();
    await secureServer?.close();
  }
}

{'fraction_non_alphanumeric': 0.10153810570139933, 'fraction_numerical': 0.00023129409043598937, 'mean_word_length': 3.89417091114884, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5866815', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2427, 'n_words': 570}