|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Hummingbird server framework project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 the Hummingbird authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import HTTPTypes |
| 16 | + |
| 17 | +#if canImport(FoundationEssentials) |
| 18 | +import FoundationEssentials |
| 19 | +#else |
| 20 | +import Foundation |
| 21 | +#endif |
| 22 | + |
| 23 | +extension Router { |
| 24 | + /// Route description |
| 25 | + public struct RouteDescription: CustomStringConvertible { |
| 26 | + /// Route path |
| 27 | + public let path: RouterPath |
| 28 | + /// Route method |
| 29 | + public let method: HTTPRequest.Method |
| 30 | + |
| 31 | + public var description: String { "\(method) \(path)" } |
| 32 | + } |
| 33 | + |
| 34 | + /// List of routes added to router |
| 35 | + public var routes: [RouteDescription] { |
| 36 | + let trieValues = self.trie.root.values() |
| 37 | + return trieValues.flatMap { endpoint in |
| 38 | + endpoint.value.methods.keys |
| 39 | + .sorted { $0.rawValue < $1.rawValue } |
| 40 | + .map { RouteDescription(path: endpoint.path, method: $0) } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// Validate router |
| 45 | + /// |
| 46 | + /// Verify that routes are not clashing |
| 47 | + public func validate() throws { |
| 48 | + try self.trie.root.validate() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +extension RouterPathTrieBuilder.Node { |
| 53 | + func validate(_ root: String = "") throws { |
| 54 | + let sortedChildren = children.sorted { $0.key.priority > $1.key.priority } |
| 55 | + if sortedChildren.count > 1 { |
| 56 | + for index in 1..<sortedChildren.count { |
| 57 | + let exampleElement = |
| 58 | + switch sortedChildren[index].key.value { |
| 59 | + case .path(let path): |
| 60 | + String(path) |
| 61 | + case .capture: |
| 62 | + UUID().uuidString |
| 63 | + case .prefixCapture(let suffix, _): |
| 64 | + "\(UUID().uuidString)\(suffix)" |
| 65 | + case .suffixCapture(let prefix, _): |
| 66 | + "\(prefix)/\(UUID().uuidString)" |
| 67 | + case .wildcard: |
| 68 | + UUID().uuidString |
| 69 | + case .prefixWildcard(let suffix): |
| 70 | + "\(UUID().uuidString)\(suffix)" |
| 71 | + case .suffixWildcard(let prefix): |
| 72 | + "\(prefix)/\(UUID().uuidString)" |
| 73 | + case .recursiveWildcard: |
| 74 | + UUID().uuidString |
| 75 | + case .null: |
| 76 | + "" |
| 77 | + } |
| 78 | + // test path element against all the previous trie entries in this node |
| 79 | + for trieEntry in sortedChildren[0..<index] { |
| 80 | + if case trieEntry.key = exampleElement { |
| 81 | + throw RouterValidationError( |
| 82 | + path: "\(root)/\(sortedChildren[index].key)", |
| 83 | + override: "\(root)/\(trieEntry.key)" |
| 84 | + ) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + for child in self.children { |
| 92 | + try child.validate("\(root)/\(child.key)") |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Router validation error |
| 98 | +public struct RouterValidationError: Error, CustomStringConvertible { |
| 99 | + let path: RouterPath |
| 100 | + let override: RouterPath |
| 101 | + |
| 102 | + public var description: String { |
| 103 | + "Route \(override) overrides \(path)" |
| 104 | + } |
| 105 | +} |
0 commit comments