|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.http.server.reactive; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.net.URISyntaxException; |
| 21 | + |
| 22 | +import reactor.netty.http.server.HttpServerRequest; |
| 23 | + |
| 24 | +import org.springframework.util.Assert; |
| 25 | + |
| 26 | +/** |
| 27 | + * Helper class for creating a {@link URI} from a reactor {@link HttpServerRequest}. |
| 28 | + * |
| 29 | + * @author Arjen Poutsma |
| 30 | + * @since 6.0.8 |
| 31 | + */ |
| 32 | +abstract class ReactorUriHelper { |
| 33 | + |
| 34 | + public static URI createUri(HttpServerRequest request) throws URISyntaxException { |
| 35 | + Assert.notNull(request, "HttpServerRequest must not be null"); |
| 36 | + |
| 37 | + StringBuilder builder = new StringBuilder(); |
| 38 | + String scheme = request.scheme(); |
| 39 | + builder.append(scheme); |
| 40 | + builder.append("://"); |
| 41 | + |
| 42 | + appendHostName(request, builder); |
| 43 | + |
| 44 | + int port = request.hostPort(); |
| 45 | + if ((scheme.equals("http") || scheme.equals("ws")) && port != 80 || |
| 46 | + (scheme.equals("https") || scheme.equals("wss")) && port != 443) { |
| 47 | + builder.append(':'); |
| 48 | + builder.append(port); |
| 49 | + } |
| 50 | + |
| 51 | + appendRequestUri(request, builder); |
| 52 | + |
| 53 | + return new URI(builder.toString()); |
| 54 | + } |
| 55 | + |
| 56 | + private static void appendHostName(HttpServerRequest request, StringBuilder builder) { |
| 57 | + String hostName = request.hostName(); |
| 58 | + boolean ipv6 = hostName.indexOf(':') != -1; |
| 59 | + boolean brackets = ipv6 && !hostName.startsWith("[") && !hostName.endsWith("]"); |
| 60 | + if (brackets) { |
| 61 | + builder.append('['); |
| 62 | + } |
| 63 | + if (encoded(hostName, ipv6)) { |
| 64 | + builder.append(hostName); |
| 65 | + } |
| 66 | + else { |
| 67 | + for (int i=0; i < hostName.length(); i++) { |
| 68 | + char c = hostName.charAt(i); |
| 69 | + if (isAllowedInHost(c, ipv6)) { |
| 70 | + builder.append(c); |
| 71 | + } |
| 72 | + else { |
| 73 | + builder.append('%'); |
| 74 | + char hex1 = Character.toUpperCase(Character.forDigit((c >> 4) & 0xF, 16)); |
| 75 | + char hex2 = Character.toUpperCase(Character.forDigit(c & 0xF, 16)); |
| 76 | + builder.append(hex1); |
| 77 | + builder.append(hex2); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + if (brackets) { |
| 82 | + builder.append(']'); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static boolean encoded(String hostName, boolean ipv6) { |
| 87 | + int length = hostName.length(); |
| 88 | + for (int i = 0; i < length; i++) { |
| 89 | + char c = hostName.charAt(i); |
| 90 | + if (c == '%') { |
| 91 | + if ((i + 2) < length) { |
| 92 | + char hex1 = hostName.charAt(i + 1); |
| 93 | + char hex2 = hostName.charAt(i + 2); |
| 94 | + int u = Character.digit(hex1, 16); |
| 95 | + int l = Character.digit(hex2, 16); |
| 96 | + if (u == -1 || l == -1) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + i += 2; |
| 100 | + } |
| 101 | + else { |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + else if (!isAllowedInHost(c, ipv6)) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + private static boolean isAllowedInHost(char c, boolean ipv6) { |
| 113 | + return (c >= 'a' && c <= 'z') || // alpha |
| 114 | + (c >= 'A' && c <= 'Z') || // alpha |
| 115 | + (c >= '0' && c <= '9') || // digit |
| 116 | + '-' == c || '.' == c || '_' == c || '~' == c || // unreserved |
| 117 | + '!' == c || '$' == c || '&' == c || '\'' == c || '(' == c || ')' == c || // sub-delims |
| 118 | + '*' == c || '+' == c || ',' == c || ';' == c || '=' == c || |
| 119 | + (ipv6 && ('[' == c || ']' == c || ':' == c)); // ipv6 |
| 120 | + } |
| 121 | + |
| 122 | + private static void appendRequestUri(HttpServerRequest request, StringBuilder builder) { |
| 123 | + String uri = request.uri(); |
| 124 | + int length = uri.length(); |
| 125 | + for (int i = 0; i < length; i++) { |
| 126 | + char c = uri.charAt(i); |
| 127 | + if (c == '/' || c == '?' || c == '#') { |
| 128 | + break; |
| 129 | + } |
| 130 | + if (c == ':' && (i + 2 < length)) { |
| 131 | + if (uri.charAt(i + 1) == '/' && uri.charAt(i + 2) == '/') { |
| 132 | + for (int j = i + 3; j < length; j++) { |
| 133 | + c = uri.charAt(j); |
| 134 | + if (c == '/' || c == '?' || c == '#') { |
| 135 | + builder.append(uri, j, length); |
| 136 | + return; |
| 137 | + } |
| 138 | + } |
| 139 | + return; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + builder.append(uri); |
| 144 | + } |
| 145 | +} |
0 commit comments