|
4 | 4 |
|
5 | 5 | package io.modelcontextprotocol.util; |
6 | 6 |
|
| 7 | +import reactor.util.annotation.Nullable; |
| 8 | + |
| 9 | +import java.net.URI; |
7 | 10 | import java.util.Collection; |
8 | 11 | import java.util.Map; |
9 | 12 |
|
10 | | -import reactor.util.annotation.Nullable; |
11 | | - |
12 | 13 | /** |
13 | 14 | * Miscellaneous utility methods. |
14 | 15 | * |
@@ -52,4 +53,55 @@ public static boolean isEmpty(@Nullable Map<?, ?> map) { |
52 | 53 | return (map == null || map.isEmpty()); |
53 | 54 | } |
54 | 55 |
|
| 56 | + /** |
| 57 | + * Resolves the given endpoint URL against the base URL. |
| 58 | + * <ul> |
| 59 | + * <li>If the endpoint URL is relative, it will be resolved against the base URL.</li> |
| 60 | + * <li>If the endpoint URL is absolute, it will be validated to ensure it matches the |
| 61 | + * base URL's scheme, authority, and path prefix.</li> |
| 62 | + * <li>If validation fails for an absolute URL, an {@link IllegalArgumentException} is |
| 63 | + * thrown.</li> |
| 64 | + * </ul> |
| 65 | + * @param baseUrl The base URL (must be absolute) |
| 66 | + * @param endpointUrl The endpoint URL (can be relative or absolute) |
| 67 | + * @return The resolved endpoint URI |
| 68 | + * @throws IllegalArgumentException If the absolute endpoint URL does not match the |
| 69 | + * base URL or URI is malformed |
| 70 | + */ |
| 71 | + public static URI resolveUri(URI baseUrl, String endpointUrl) { |
| 72 | + URI endpointUri = URI.create(endpointUrl); |
| 73 | + if (endpointUri.isAbsolute() && !isUnderBaseUri(baseUrl, endpointUri)) { |
| 74 | + throw new IllegalArgumentException("Absolute endpoint URL does not match the base URL."); |
| 75 | + } |
| 76 | + else { |
| 77 | + return baseUrl.resolve(endpointUri); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Checks if the given absolute endpoint URI falls under the base URI. It validates |
| 83 | + * the scheme, authority (host and port), and ensures that the base path is a prefix |
| 84 | + * of the endpoint path. |
| 85 | + * @param baseUri The base URI |
| 86 | + * @param endpointUri The endpoint URI to check |
| 87 | + * @return true if endpointUri is within baseUri's hierarchy, false otherwise |
| 88 | + */ |
| 89 | + private static boolean isUnderBaseUri(URI baseUri, URI endpointUri) { |
| 90 | + if (!baseUri.getScheme().equals(endpointUri.getScheme()) |
| 91 | + || !baseUri.getAuthority().equals(endpointUri.getAuthority())) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + URI normalizedBase = baseUri.normalize(); |
| 96 | + URI normalizedEndpoint = endpointUri.normalize(); |
| 97 | + |
| 98 | + String basePath = normalizedBase.getPath(); |
| 99 | + String endpointPath = normalizedEndpoint.getPath(); |
| 100 | + |
| 101 | + if (basePath.endsWith("/")) { |
| 102 | + basePath = basePath.substring(0, basePath.length() - 1); |
| 103 | + } |
| 104 | + return endpointPath.startsWith(basePath); |
| 105 | + } |
| 106 | + |
55 | 107 | } |
0 commit comments