<commit_before>//
//  Connect.swift
//  SwiftyHTTP
//
//  Created by <NAME> on 6/26/14.
//  Copyright (c) 2014 Always Right Institute. All rights reserved.
//

/**
 * NodeJS style Connect, well, bitz of it ;-)
 *
 * http://stephensugden.com/middleware_guide/
 *
 * Note: no error handling
 */
public class Connect : HTTPServer {
  
  var middlewarez = [MiddlewareEntry]()
  
  override public init() {
    super.init()
    self.onRequest { [unowned self] in
      self.doRequest($0, response: $1, connection: $2)
    }
  }
  
  public func use(cb: Middleware) -> Self {
    middlewarez.append(MiddlewareEntry(middleware: cb))
    return self
  }
  public func use(prefix: String, _ cb: Middleware) -> Self {
    middlewarez.append(MiddlewareEntry(urlPrefix: prefix, middleware: cb))
    return self
  }
  
  // FIXME: complete me
  
  func doRequest(request: HTTPRequest, response: HTTPResponse,
                 connection: HTTPConnection) -> Void
  {
    // first lookup all middleware matching the request (i.e. the URL prefix
    // matches)
    let matchingMiddleware = middlewarez.filter { $0.matchesRequest(request) }
    
    let endNext : Next = { _ in } // a noop middleware next-block handle
    var next    : Next = { _ in } // cannot be let as it's self-referencing
    
    var i = 0 // capture position in matching-middleware array (shared)
    next = {
      args in
      <commit_msg>Remove an outdated comment.<commit_after>//
//  Connect.swift
//  SwiftyHTTP
//
//  Created by <NAME> on 6/26/14.
//  Copyright (c) 2014 Always Right Institute. All rights reserved.
//

/**
 * NodeJS style Connect, well, bitz of it ;-)
 *
 * http://stephensugden.com/middleware_guide/
 *
 * Note: no error handling
 */
public class Connect : HTTPServer {
  
  var middlewarez = [MiddlewareEntry]()
  
  override public init() {
    super.init()
    self.onRequest { [unowned self] in
      self.doRequest($0, response: $1, connection: $2)
    }
  }
  
  public func use(cb: Middleware) -> Self {
    middlewarez.append(MiddlewareEntry(middleware: cb))
    return self
  }
  public func use(prefix: String, _ cb: Middleware) -> Self {
    middlewarez.append(MiddlewareEntry(urlPrefix: prefix, middleware: cb))
    return self
  }
  
  func doRequest(request: HTTPRequest, response: HTTPResponse,
                 connection: HTTPConnection) -> Void
  {
    // first lookup all middleware matching the request (i.e. the URL prefix
    // matches)
    let matchingMiddleware = middlewarez.filter { $0.matchesRequest(request) }
    
    let endNext : Next = { _ in } // a noop middleware next-block handle
    var next    : Next = { _ in } // cannot be let as it's self-referencing
    
    var i = 0 // capture position in matching-middleware array (shared)
    next = {
      args in
      
{'fraction_non_alphanumeric': 0.1140193617784152, 'fraction_numerical': 0.010039440659734672, 'mean_word_length': 2.642297650130548, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '4877755', 'n_tokens_mistral': 874, 'n_tokens_neox': 859, 'n_words': 260}