|
| 1 | +package com.fongmi.android.tv.server; |
| 2 | + |
| 3 | +import fi.iki.elonen.NanoHTTPD.IHTTPSession; |
| 4 | +import fi.iki.elonen.NanoHTTPD.Response; |
| 5 | + |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class Route { |
| 9 | + |
| 10 | + public interface Handler { |
| 11 | + Response handle(IHTTPSession session, Map<String, String> params); |
| 12 | + } |
| 13 | + |
| 14 | + private static class Node { |
| 15 | + Map<String, Node> children = new HashMap<>(); |
| 16 | + Handler handler; |
| 17 | + boolean isParam; |
| 18 | + boolean isWildcard; |
| 19 | + String paramName; |
| 20 | + } |
| 21 | + |
| 22 | + private final Node root; |
| 23 | + |
| 24 | + public Route() { |
| 25 | + root = new Node(); |
| 26 | + } |
| 27 | + |
| 28 | + // ====================== Fastify 方法 ====================== |
| 29 | + public Route get(String path, Handler handler) { |
| 30 | + addRoute(path, handler); |
| 31 | + return this; |
| 32 | + } |
| 33 | + |
| 34 | + public Route post(String path, Handler handler) { |
| 35 | + addRoute(path, handler); |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + public Route put(String path, Handler handler) { |
| 40 | + addRoute(path, handler); |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public Route delete(String path, Handler handler) { |
| 45 | + addRoute(path, handler); |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public Route head(String path, Handler handler) { |
| 50 | + addRoute(path, handler); |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public Route options(String path, Handler handler) { |
| 55 | + addRoute(path, handler); |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + public Route all(String path, Handler handler) { |
| 60 | + get(path, handler); |
| 61 | + post(path, handler); |
| 62 | + put(path, handler); |
| 63 | + delete(path, handler); |
| 64 | + head(path, handler); |
| 65 | + options(path, handler); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + // ====================== 核心路由注册 ====================== |
| 70 | + private void addRoute(String path, Handler handler) { |
| 71 | + if (path == null || path.isEmpty()) path = "/"; |
| 72 | + if (!path.startsWith("/")) path = "/" + path; |
| 73 | + |
| 74 | + Node node = root; |
| 75 | + String[] parts = path.split("/"); |
| 76 | + |
| 77 | + for (String part : parts) { |
| 78 | + if (part.isEmpty()) continue; |
| 79 | + |
| 80 | + if (part.equals("*")) { |
| 81 | + node.children.putIfAbsent("*", new Node()); |
| 82 | + Node wild = node.children.get("*"); |
| 83 | + wild.isWildcard = true; |
| 84 | + wild.paramName = "*"; |
| 85 | + node = wild; |
| 86 | + break; |
| 87 | + } else if (part.startsWith(":") && part.endsWith("*") && part.length() > 2) { |
| 88 | + String name = part.substring(1, part.length() - 1); |
| 89 | + node.children.putIfAbsent(":*", new Node()); |
| 90 | + Node pw = node.children.get(":*"); |
| 91 | + pw.isParam = true; |
| 92 | + pw.isWildcard = true; |
| 93 | + pw.paramName = name; |
| 94 | + node = pw; |
| 95 | + break; |
| 96 | + } else if (part.startsWith(":")) { |
| 97 | + node.children.putIfAbsent(":", new Node()); |
| 98 | + Node param = node.children.get(":"); |
| 99 | + param.isParam = true; |
| 100 | + param.paramName = part.substring(1); |
| 101 | + node = param; |
| 102 | + } else { |
| 103 | + node.children.putIfAbsent(part, new Node()); |
| 104 | + node = node.children.get(part); |
| 105 | + } |
| 106 | + } |
| 107 | + node.handler = handler; |
| 108 | + } |
| 109 | + |
| 110 | + // ====================== 路由匹配 ====================== |
| 111 | + private Match match(String path) { |
| 112 | + Node node = root; |
| 113 | + Map<String, String> params = new HashMap<>(); |
| 114 | + String[] parts = path.split("/"); |
| 115 | + int i = 0; |
| 116 | + |
| 117 | + while (i < parts.length) { |
| 118 | + if (node.isWildcard) break; |
| 119 | + |
| 120 | + String part = parts[i]; |
| 121 | + if (part.isEmpty()) { |
| 122 | + i++; |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if (node.children.containsKey(part)) { |
| 127 | + node = node.children.get(part); |
| 128 | + } else if (node.children.containsKey(":")) { |
| 129 | + node = node.children.get(":"); |
| 130 | + params.put(node.paramName, part); |
| 131 | + } else if (node.children.containsKey(":*")) { |
| 132 | + node = node.children.get(":*"); |
| 133 | + params.put(node.paramName, joinRest(parts, i)); |
| 134 | + break; |
| 135 | + } else if (node.children.containsKey("*")) { |
| 136 | + node = node.children.get("*"); |
| 137 | + params.put("*", joinRest(parts, i)); |
| 138 | + break; |
| 139 | + } else { |
| 140 | + return null; |
| 141 | + } |
| 142 | + i++; |
| 143 | + } |
| 144 | + |
| 145 | + if (node.handler == null) return null; |
| 146 | + Match m = new Match(); |
| 147 | + m.handler = node.handler; |
| 148 | + m.params = params; |
| 149 | + return m; |
| 150 | + } |
| 151 | + |
| 152 | + private static class Match { |
| 153 | + Handler handler; |
| 154 | + Map<String, String> params; |
| 155 | + } |
| 156 | + |
| 157 | + private String joinRest(String[] parts, int index) { |
| 158 | + StringBuilder sb = new StringBuilder(); |
| 159 | + for (int j = index; j < parts.length; j++) { |
| 160 | + if (!parts[j].isEmpty()) { |
| 161 | + if (sb.length() > 0) sb.append("/"); |
| 162 | + sb.append(parts[j]); |
| 163 | + } |
| 164 | + } |
| 165 | + return sb.toString(); |
| 166 | + } |
| 167 | + |
| 168 | + // ====================== 原有接口不动 ====================== |
| 169 | + public Response handle(IHTTPSession session) { |
| 170 | + String path = session.getUri().trim(); |
| 171 | + if (path.contains("?")) path = path.substring(0, path.indexOf("?")); |
| 172 | + Match match = match(path); |
| 173 | + return match == null ? null : match.handler.handle(session, match.params); |
| 174 | + } |
| 175 | + |
| 176 | + public boolean hasRoute(String method, String path) { |
| 177 | + if (path.contains("?")) path = path.substring(0, path.indexOf("?")); |
| 178 | + return match(path) != null; |
| 179 | + } |
| 180 | +} |
0 commit comments