<commit_before>

public struct Route {

	public typealias Middleware = (request: MatchedRequest, next: (MatchedRequest) throws -> Response) throws -> Response
	
	public let method: Method
	public let wildcardPath: WildcardPath
	public let middleware: [Middleware]
	
    let handler: RouteHandlingType
	
	init(method: Method, path: Path, pathTypes: [PathType.Type] = [], middleware: [Middleware] = [], handler: RouteHandlingType) {
		self.method = method
		self.wildcardPath = WildcardPath(path: path, pathTypes: pathTypes)
		self.middleware = middleware
		self.handler = handler
	}
	
	public func matches(request: Request) -> Bool {
		return matches(request.method, path: request.path)
	}
	
	public func matches(method: Method, path: Path) -> Bool {
		guard self.method == method else { return false }
		guard wildcardPath.matches(path) else { return false }
		return true
	}
	
	public func handle(request: Request) throws -> Response {
		let matchedRequest = MatchedRequest(request: request, route: self)
		return try handle(matchedRequest, middlewareGenerator: middleware.generate())
	}
	
	private func handle(request: MatchedRequest, middlewareGenerator: IndexingGenerator<[Middleware]>) throws -> Response {
		var mutableMiddlewareGenerator = middlewareGenerator
		if let middleware = mutableMiddlewareGenerator.next() {
			return try middleware(request: request, next: { (aRequest) -> Response in
				return try self.handle(aRequest, middlewareGenerator: mutableMiddlewareGenerator)
			})
		}
		
        guard let response = handler.perform(request) else {
            throw RoutingError.NotFound(request.request.method, request.request.path)
        }
        
        return response
	}

}


<commit_msg>Update to not use MatchedRequest for middleware.<commit_after>

public struct Route {

	public typealias Middleware = (request: Request, next: (Request) throws -> Response) throws -> Response
	
	public let method: Method
	public let wildcardPath: WildcardPath
	public let middleware: [Middleware]
	
    let handler: RouteHandlingType
	
	init(method: Method, path: Path, pathTypes: [PathType.Type] = [], middleware: [Middleware] = [], handler: RouteHandlingType) {
		self.method = method
		self.wildcardPath = WildcardPath(path: path, pathTypes: pathTypes)
		self.middleware = middleware
		self.handler = handler
	}
	
	public func matches(request: Request) -> Bool {
		return matches(request.method, path: request.path)
	}
	
	public func matches(method: Method, path: Path) -> Bool {
		guard self.method == method else { return false }
		guard wildcardPath.matches(path) else { return false }
		return true
	}
	
	public func handle(request: Request) throws -> Response {
		return try handle(request, middlewareGenerator: middleware.generate())
	}
	
	private func handle(request: Request, middlewareGenerator: IndexingGenerator<[Middleware]>) throws -> Response {
		var mutableMiddlewareGenerator = middlewareGenerator
		if let middleware = mutableMiddlewareGenerator.next() {
			return try middleware(request: request, next: { (aRequest) -> Response in
				return try self.handle(aRequest, middlewareGenerator: mutableMiddlewareGenerator)
			})
		}
		
        let matchedRequest = MatchedRequest(request: request, route: self)
        guard let response = handler.perform(matchedRequest) else {
            throw RoutingError.NotFound(request.method, request.path)
        }
        
        return response
	}

}



{'fraction_non_alphanumeric': 0.09427904261529481, 'fraction_numerical': 0.0, 'mean_word_length': 4.192424242424242, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '3904884', 'n_tokens_mistral': 997, 'n_tokens_neox': 926, 'n_words': 276}