<commit_before>// This source file is part of the Swift.org Server APIs open source project
//
// Copyright (c) 2017 Swift Server API project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
//

import CHTTPParser
import Foundation
import Dispatch

public enum StreamingParserError: Error {
    case ConnectionAbortedError
}

/// Class that wraps the CHTTPParser and calls the `HTTPRequestHandler` to get the response
/// :nodoc:
public class StreamingParser: HTTPResponseWriter {

    let handle: HTTPRequestHandler

    /// Time to leave socket open waiting for next request to start
    public let keepAliveTimeout: TimeInterval

    /// Flag to track if the client wants to send consecutive requests on the same TCP connection
    var clientRequestedKeepAlive = false

    /// Tracks when socket should be closed. Needs to have a lock, since it's updated often
    private let _keepAliveUntilLock = DispatchSemaphore(value: 1)
    private var _keepAliveUntil: TimeInterval?
    public var keepAliveUntil: TimeInterval? {
        get {
            _keepAliveUntilLock.wait()
            defer {
                 _keepAliveUntilLock.signal()
            }
            return _keepAliveUntil
        }
        set {
            _keepAliveUntilLock.wait()
            defer {
                _keepAliveUntilLock.signal()
            }
            _keepAliveUntil = newValue
        }
    }

    /// Socket File Descriptor so we can log it for debugging
    private let _socketDebuggingFDLock = DispatchSemaphore(value: 1)
    private var _socketDebuggingFD: Int?
    internal var socketDebuggingFD: Int? {
        get {
            _socketDebuggingFDLock.wait()
            defer {
                _socketDebuggingFDLock.signal()
            }
            return _socketDebuggingFD
        }
        set {
            _socketDebuggingFDLock.wait()
            defer {
                _socketDebuggingFDLock.signal()
            }
            _socketDebuggingFD = newValue
        }
    }
    
    /// Tracks when we've been told socket has been closed. Needs to have a lock, since if we get confused, bat things happen
    private let _abortCalledLock = DispatchSemaphore(value: 1)
    private var _abortCalled: Bool = false
    internal var abortCalled: Bool {
        get {
            _abortCalledLock.wait()
            defer {
                _abortCalledLock.signal()
            }
            return _abortCalled
        }
        set {
            _abortCalledLock.wait()
            defer {
                _abortCalledLock.signal()
            }
            _abortCalled = newValue
        }
    }

    /// Optional delegate that can tell us how many connections are in-flight.
    public weak var connectionCounter: CurrentConnectionCounting?

    /// Holds the bytes that come from the CHTTPParser until we have enough of them to do something with it
    var parserBuffer: Data?
    
    /// Lock for parser buffer. TODO: Figure out how to wrap this without copying it
    private let _parserBufferLock = DispatchSemaphore(value: 1)
<commit_msg>Remove Logging variable no longer used and fixed typo<commit_after>// This source file is part of the Swift.org Server APIs open source project
//
// Copyright (c) 2017 Swift Server API project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
//

import CHTTPParser
import Foundation
import Dispatch

public enum StreamingParserError: Error {
    case ConnectionAbortedError
}

/// Class that wraps the CHTTPParser and calls the `HTTPRequestHandler` to get the response
/// :nodoc:
public class StreamingParser: HTTPResponseWriter {

    let handle: HTTPRequestHandler

    /// Time to leave socket open waiting for next request to start
    public let keepAliveTimeout: TimeInterval

    /// Flag to track if the client wants to send consecutive requests on the same TCP connection
    var clientRequestedKeepAlive = false

    /// Tracks when socket should be closed. Needs to have a lock, since it's updated often
    private let _keepAliveUntilLock = DispatchSemaphore(value: 1)
    private var _keepAliveUntil: TimeInterval?
    public var keepAliveUntil: TimeInterval? {
        get {
            _keepAliveUntilLock.wait()
            defer {
                 _keepAliveUntilLock.signal()
            }
            return _keepAliveUntil
        }
        set {
            _keepAliveUntilLock.wait()
            defer {
                _keepAliveUntilLock.signal()
            }
            _keepAliveUntil = newValue
        }
    }
    
    /// Tracks when we've been told socket has been closed. Needs to have a lock, since if we get confused, bad things happen
    private let _abortCalledLock = DispatchSemaphore(value: 1)
    private var _abortCalled: Bool = false
    internal var abortCalled: Bool {
        get {
            _abortCalledLock.wait()
            defer {
                _abortCalledLock.signal()
            }
            return _abortCalled
        }
        set {
            _abortCalledLock.wait()
            defer {
                _abortCalledLock.signal()
            }
            _abortCalled = newValue
        }
    }

    /// Optional delegate that can tell us how many connections are in-flight.
    public weak var connectionCounter: CurrentConnectionCounting?

    /// Holds the bytes that come from the CHTTPParser until we have enough of them to do something with it
    var parserBuffer: Data?
    
    /// Lock for parser buffer. TODO: Figure out how to wrap this without copying it
    private let _parserBufferLock = DispatchSemaphore(value: 1)

{'fraction_non_alphanumeric': 0.056814220982920875, 'fraction_numerical': 0.0033112582781456954, 'mean_word_length': 2.172470978441128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '7471499', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1460, 'n_words': 539}