<commit_before>// Copyright 2015 <NAME>
//
// 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 Foundation


/// The server implementation used when the application is running as a CGI process.
class CGIServer: ServerProtocol {

  /// The environment variable dictionary that contains the headers for the request.
  private let environment: [String: String]

  /// The input stream from which the request body can be read.
  private let requestStream: InputStream

  /// The output stream to which the response body can be written.
  private let responseStream: OutputStream

  /// Creates a new server with the given environment, request stream, and response stream.
  ///
  /// This initializer exists primarily for testing.
  ///
  /// - Parameter environment: The dictionary containing the environment variables from which the
  ///   request headers will be read.
  /// - Parameter requestStream: The input stream from which the request body can be read.
  /// - Parameter responseStream: The output stream to which the response body can be written.
  init(environment: [String: String], requestStream: InputStream, responseStream: OutputStream) {
    self.environment = environment
    self.requestStream = requestStream
    self.responseStream = responseStream
  }

  /// Creates a new server that takes its environment from the process's environment and that uses
  /// the `stdin` and `stdout` file descriptors as its request and response streams, respectively.
  convenience init() {
    // TODO: Eliminate this dependency on Foundation. Would be nice if the POSIX "environ" variable
    // were exposed to Swift.
    let process = NSProcessInfo.processInfo()
    let environment = process.environment

    let requestStream = FileInputStream(fileDescriptor: STDIN_FILENO)
    let responseStream =
        BufferingOutputStream(outputStream: FileOutputStream(fileDescriptor: STDOUT_FILENO))

    self.init(
        environment: environment, requestStream: requestStream, responseStream: responseStream)
  }

  func listen(handler: (HTTPRequest, HTTPResponse) -> Void) {
    let request = CGIHTTPRequest(environment: environment, contentStream: requestStream)
    let response = CGIHTTPResponse(contentStream: responseStream)

    handler(request, response)

    responseStream.flush()
  }
}
<commit_msg>Use `BufferingInputStream` for CGI requests.<commit_after>// Copyright 2015 <NAME>
//
// 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 Foundation


/// The server implementation used when the application is running as a CGI process.
class CGIServer: ServerProtocol {

  /// The environment variable dictionary that contains the headers for the request.
  private let environment: [String: String]

  /// The input stream from which the request body can be read.
  private let requestStream: InputStream

  /// The output stream to which the response body can be written.
  private let responseStream: OutputStream

  /// Creates a new server with the given environment, request stream, and response stream.
  ///
  /// This initializer exists primarily for testing.
  ///
  /// - Parameter environment: The dictionary containing the environment variables from which the
  ///   request headers will be read.
  /// - Parameter requestStream: The input stream from which the request body can be read.
  /// - Parameter responseStream: The output stream to which the response body can be written.
  init(environment: [String: String], requestStream: InputStream, responseStream: OutputStream) {
    self.environment = environment
    self.requestStream = requestStream
    self.responseStream = responseStream
  }

  /// Creates a new server that takes its environment from the process's environment and that uses
  /// the `stdin` and `stdout` file descriptors as its request and response streams, respectively.
  convenience init() {
    // TODO: Eliminate this dependency on Foundation. Would be nice if the POSIX "environ" variable
    // were exposed to Swift.
    let process = NSProcessInfo.processInfo()
    let environment = process.environment

    let requestStream =
        BufferingInputStream(inputStream: FileInputStream(fileDescriptor: STDIN_FILENO))
    let responseStream =
        BufferingOutputStream(outputStream: FileOutputStream(fileDescriptor: STDOUT_FILENO))

    self.init(
        environment: environment, requestStream: requestStream, responseStream: responseStream)
  }

  func listen(handler: (HTTPRequest, HTTPResponse) -> Void) {
    let request = CGIHTTPRequest(environment: environment, contentStream: requestStream)
    let response = CGIHTTPResponse(contentStream: responseStream)

    handler(request, response)

    responseStream.flush()
  }
}

{'fraction_non_alphanumeric': 0.07517482517482517, 'fraction_numerical': 0.002797202797202797, 'mean_word_length': 3.7164056059356967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3394341', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1330, 'n_words': 621}