<commit_before>//
//  HTTP.swift
//  Edge
//
//  Created by <NAME> on 6/22/16.
//
//

import Dispatch
import Reactive
import POSIX
import POSIXExtensions
import TCP

public final class Server {
    
    public let parser: RequestParser
    
    init() {
        self.parser = RequestParser()
    }
    
    public func listen(host: String, port: Port) -> ColdSignal<Request, SystemError> {
        return ColdSignal { observer in
            
            let tcpServer = try! TCP.Server()
            try! tcpServer.bind(host: host, port: port)
            
            let listen = tcpServer.listen()
            listen.onNext { connection in
                /*
                let read = connection.read()
                read.onNext { data in
                    if let request = try! self.parser.parse(Data(data)) {
                        let response = try! middleware.chain(to: responder).respond(to: request)
                        try! serializer.serialize(response, to: stream)

                        if let upgrade = response.didUpgrade {
                            try! upgrade(request, stream)
                            try! stream.close()
                        }

                        if !request.isKeepAlive {
                            try! stream.close()
                        }
                    }
                    
                }
                read.start()
                */
            }
            listen.start()

            return ActionDisposable {
                listen.stop()
            }
        }

    }

}
<commit_msg>Added HTTP parsing to the server.<commit_after>//
//  HTTP.swift
//  Edge
//
//  Created by <NAME> on 6/22/16.
//
//

import Dispatch
import Reactive
import POSIX
import POSIXExtensions
import TCP

public final class Server {
    
    private var observer: Observer<Request, SystemError>! = nil
    
    init() {

    }
    
    public func listen(host: String, port: Port) -> ColdSignal<Request, SystemError> {
        return ColdSignal { observer in
            self.observer = observer
            
            let tcpServer = try! TCP.Server()
            try! tcpServer.bind(host: host, port: port)
            
            let listen = tcpServer.listen()
            listen.onNext { connection in
                
                // Create parser for connection/
                let parser = RequestParser { request in
                    self.observer.sendNext(request)
                }
                
                // Read from connection and parse data.
                let read = connection.read()
                read.onNext { data in
                    try! parser.parse(data)
                }
                read.start()
            }
            listen.onFailed { error in
                observer.sendFailed(error)
            }
            listen.start()

            return ActionDisposable {
                listen.stop()
            }
        }

    }

}

{'fraction_non_alphanumeric': 0.0835306053432533, 'fraction_numerical': 0.0033818058843422386, 'mean_word_length': 1.1341991341991342, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, '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': '3917073', 'n_tokens_mistral': 758, 'n_tokens_neox': 704, 'n_words': 191}