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

import Foundation

// MARK: ServerResponse

public class ServerResponse : SocketWriter {

    ///
    /// Socket for the ServerResponse
    ///
    private var socket: Socket?
    
    ///
    /// TODO: ???
    ///
    private var startFlushed = false
    
    ///
    /// TODO: ???
    ///
    private var singleHeaders: [String: String] = [:]
    
    ///
    /// TODO: ???
    ///
    private var multiHeaders: [String: [String]] = [:]
    
    ///
    /// Status code
    ///
    public var status = HttpStatusCode.OK.rawValue
    
    ///
    /// Status code
    ///
    public var statusCode: HttpStatusCode? {
        get {
            return HttpStatusCode(rawValue: status)
        }
        set (newValue) {
            if let newValue = newValue where !startFlushed {
                status = newValue.rawValue
            }
        }
    }
    
    ///
    /// Initializes a ServerResponse instance
    ///
    init(socket: Socket) {
        
        self.socket = socket
        setHeader("Date", value: SpiUtils.httpDate())
        
    }
    
    ///
    /// Get a specific headers for the response by key
    ///
    /// - Parameter key: the header key
    ///
    public func getHeader(key: String) -> String? {
        
        return singleHeaders[key]
        
    }
    
    ///
    /// Get all values on a specific key
    ///
    /// - Parameter key: the header key
    ///
    /// - Returns: a list of String values
    ///
    public func getHeaders(key: String) -> [String]? {
        
        return multiHeaders[key]
        
    }
    
    ///
    /// Set the value for a header
    ///
    /// - Parameter key: key 
    /// - Parameter value: the value
    ///
    public func setHeader(key: String, value: String) {
        singleHeaders[key] = value
        multiHeaders.removeValue(forKey: key)
    }

    ///
    /// Set the value for a header (list)
    ///
    /// - Parameter key: key
    /// - Parameter value: the value
    ///
    public func setHeader(key: String, value: [String]) {
        multiHeaders[key] = value
        singleHeaders.removeValue(forKey: key)
    }
    
    ///
    /// Remove a key from the header
    ///
    /// - Parameter key: key
    ///
    public func removeHeader(key: String) {
        singleHeaders.removeValue(forKey: key)<commit_msg>IBM-Swift/Kitura#306 added header append method<commit_after>/**
 * Copyright IBM Corporation 2016
 *
 * 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 Socket

import Foundation

// MARK: ServerResponse

public class ServerResponse : SocketWriter {

    ///
    /// Socket for the ServerResponse
    ///
    private var socket: Socket?
    
    ///
    /// TODO: ???
    ///
    private var startFlushed = false
    
    ///
    /// TODO: ???
    ///
    private var singleHeaders: [String: String] = [:]
    
    ///
    /// TODO: ???
    ///
    private var multiHeaders: [String: [String]] = [:]
    
    ///
    /// Status code
    ///
    public var status = HttpStatusCode.OK.rawValue
    
    ///
    /// Status code
    ///
    public var statusCode: HttpStatusCode? {
        get {
            return HttpStatusCode(rawValue: status)
        }
        set (newValue) {
            if let newValue = newValue where !startFlushed {
                status = newValue.rawValue
            }
        }
    }
    
    ///
    /// Initializes a ServerResponse instance
    ///
    init(socket: Socket) {
        
        self.socket = socket
        setHeader("Date", value: SpiUtils.httpDate())
        
    }
    
    ///
    /// Get a specific headers for the response by key
    ///
    /// - Parameter key: the header key
    ///
    public func getHeader(key: String) -> String? {
        
        return singleHeaders[key]
        
    }
    
    ///
    /// Get all values on a specific key
    ///
    /// - Parameter key: the header key
    ///
    /// - Returns: a list of String values
    ///
    public func getHeaders(key: String) -> [String]? {
        
        return multiHeaders[key]
        
    }
    
    ///
    /// Set the value for a header
    ///
    /// - Parameter key: key 
    /// - Parameter value: the value
    ///
    public func setHeader(key: String, value: String) {
        singleHeaders[key] = value
        multiHeaders.removeValue(forKey: key)
    }

    ///
    /// Set the value for a header (list)
    ///
    /// - Parameter key: key
    /// - Parameter value: the value
    ///
    public func setHeader(key: String, value: [String]) {
        multiHeaders[key] = value
        singleHeaders.removeValue(forKey: key)
    }

    ///
    /// Append a value to the header
    ///
    /// - Parameter key: the header key
    /// - Parameter value: string value
    ///
    public func append(key: String, value: String) {

        if let singleValue = singleHeaders[key] where multiHeaders.count == 0 {
            multiHeaders[key] = [singleValue, value]
            singleHeaders.removeValue(forKey: key)
        } else if let _ = multiHeaders[key] {
            multiHeaders[key]!.append(value)
        } else {
            setHeader(key, value: value)
        }
    }

    ///
    /// Append values to the header
    ///
    /// - Parameter key: the header key
    /// - Parameter value: array of string values
    ///
    public func append(key: String, value: [String]) {

        if let singleValue = singleHeaders[key] where multiHeaders.count == 0 {
            multiHeaders[key] = [singleValue] + value
            singleHeaders.removeValue(forKey: key)
        } else if let _ = multiHeaders[key] {
            multiHeaders[key]! = multiHeaders[key]! + value
        } else {
            if value.count == 1 {
                setHeader(key, value: value.first!)
            } else {
                setHeader(key, value: value)
            }
        }
    }
    
    ///
    /// Remove a key from the header
    ///
    /// - Parameter key: key
    ///
    public func removeHeader(key: String) {
        singleHeaders.removeValue(forKey: key)
{'fraction_non_alphanumeric': 0.11860139860139861, 'fraction_numerical': 0.003076923076923077, 'mean_word_length': 1.6485185185185185, 'pattern_counts': {'":': 0, '<': 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': '3089855', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1951, 'n_words': 623}