<commit_before>/*
 * Copyright IBM Corporation 2015
 *
 * 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

public class HeadersContainer {
    
    ///
    /// The header storage
    ///
    internal var headers: [String: [String]] = [:]
    
    ///
    /// The map of case insensitive header fields to their actual names
    ///
    private var caseInsensitiveMap: [String: String] = [:]
    
    public subscript(key: String) -> [String]? {
        get {
            return get(key)
        }
        
        set(newValue) {
            if let newValue = newValue {
                set(key, value: newValue)
            }
            else {
                remove(key)
            }
        }
    }
    
    ///
    /// Append values to the header
    ///
    /// - Parameter key: the key
    /// - Parameter value: the value
    ///
    public func append(_ key: String, value: [String]) {
        
        // Determine how to handle the header (append or merge)
        switch(key.lowercased()) {
            
        // Headers with an array value (can appear multiple times, but can't be merged)
        case "set-cookie":
            if let headerKey = caseInsensitiveMap[key.lowercased()] {
                headers[headerKey]? += value
            } else {
                set(key, value: value)
            }
            
            
        // Headers with a simple value that can be merged
        default:
            guard let headerKey = caseInsensitiveMap[key.lowercased()], let oldValue = headers[headerKey]?.first else {
                set(key, value: value)
                return
            }
            let newValue = oldValue + ", " + value.joined(separator: ", ")
            headers[headerKey]?[0] = newValue
        }
    }
    
    ///
    /// Append values to the header
    ///
    /// - Parameter key: the key
    /// - Parameter value: the value
    ///
    public func append(_ key: String, value: String) {

        append(key, value: [value])
    }

    ///
    /// Gets the header (case insensitive)
    ///
    /// - Parameter key: the key
    ///
    /// - Returns: the value for the key
    ///
    private func get(_ key: String) -> [String]? {
        if let headerKey = caseInsensitiveMap[key.lowercased()] {
            return headers[headerKey]
        }
        
        return nil
    }
    
    ///
    /// Remove all of the headers
    ///
    func removeAll() {
        headers = [:]
        caseInsensitiveMap = [:]
    }
    
    ///
    /// Set the header value
    ///
    /// - Parameter key: the key
    /// - Parameter value: the value
    ///<commit_msg>Improve performance of HeadersContainer.removeAll (#89)<commit_after>/*
 * Copyright IBM Corporation 2015
 *
 * 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

public class HeadersContainer {
    
    ///
    /// The header storage
    ///
    internal var headers: [String: [String]] = [:]
    
    ///
    /// The map of case insensitive header fields to their actual names
    ///
    private var caseInsensitiveMap: [String: String] = [:]
    
    public subscript(key: String) -> [String]? {
        get {
            return get(key)
        }
        
        set(newValue) {
            if let newValue = newValue {
                set(key, value: newValue)
            }
            else {
                remove(key)
            }
        }
    }
    
    ///
    /// Append values to the header
    ///
    /// - Parameter key: the key
    /// - Parameter value: the value
    ///
    public func append(_ key: String, value: [String]) {
        
        // Determine how to handle the header (append or merge)
        switch(key.lowercased()) {
            
        // Headers with an array value (can appear multiple times, but can't be merged)
        case "set-cookie":
            if let headerKey = caseInsensitiveMap[key.lowercased()] {
                headers[headerKey]? += value
            } else {
                set(key, value: value)
            }
            
            
        // Headers with a simple value that can be merged
        default:
            guard let headerKey = caseInsensitiveMap[key.lowercased()], let oldValue = headers[headerKey]?.first else {
                set(key, value: value)
                return
            }
            let newValue = oldValue + ", " + value.joined(separator: ", ")
            headers[headerKey]?[0] = newValue
        }
    }
    
    ///
    /// Append values to the header
    ///
    /// - Parameter key: the key
    /// - Parameter value: the value
    ///
    public func append(_ key: String, value: String) {

        append(key, value: [value])
    }

    ///
    /// Gets the header (case insensitive)
    ///
    /// - Parameter key: the key
    ///
    /// - Returns: the value for the key
    ///
    private func get(_ key: String) -> [String]? {
        if let headerKey = caseInsensitiveMap[key.lowercased()] {
            return headers[headerKey]
        }
        
        return nil
    }
    
    ///
    /// Remove all of the headers
    ///
    func removeAll() {
        headers.removeAll(keepingCapacity: true)
        caseInsensitiveMap.removeAll(keepingCapacity: true)
    }
    
    ///
    /// Set the header value
    ///
    /// - Parameter key: the key
    /// - Parameter value: the value
    ///
{'fraction_non_alphanumeric': 0.10926216640502355, 'fraction_numerical': 0.0031397174254317113, 'mean_word_length': 1.5919446704637916, 'pattern_counts': {'":': 2, '<': 3, '<?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': '7378015', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1671, 'n_words': 576}