public static URI getUri(HttpServletRequest servletRequest) {
        try {
            String scheme = servletRequest.getScheme();
            String host = servletRequest.getServerName();
            int port = getConnectorLocalPort((Request) servletRequest);
            String path = servletRequest.getRequestURI();
            String query = servletRequest.getQueryString();

            URI uri = URI.create(scheme + "://" +
                                 host + ":" + port +
                                 (path != null ? path : "") +
                                 (query != null ? "?" + query : ""));

            validateSchemeHostPort(scheme, host, port, uri);
            return uri;
        }
        catch (IllegalArgumentException e) {
            throw createBadQueryException(e);
        }
    }
@Test
    void testLegalURIs() {
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("https", "host", null, null));
            assertEquals("https", uri.getScheme());
            assertEquals("host", uri.getHost());
            assertEquals("", uri.getRawPath());
            assertNull(uri.getRawQuery());
        }
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("https", "host", "", ""));
            assertEquals("https", uri.getScheme());
            assertEquals("host", uri.getHost());
            assertEquals("", uri.getRawPath());
            assertEquals("", uri.getRawQuery());
        }
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("http", "host.a1-2-3", "", ""));
            assertEquals("http", uri.getScheme());
            assertEquals("host.a1-2-3", uri.getHost());
            assertEquals("", uri.getRawPath());
            assertEquals("", uri.getRawQuery());
        }
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("https", "host", "/:1/../1=.", ""));
            assertEquals("https", uri.getScheme());
            assertEquals("host", uri.getHost());
            assertEquals("/:1/../1=.", uri.getRawPath());
            assertEquals("", uri.getRawQuery());
        }
        {
            URI uri = HttpRequestFactory.getUri(createMockRequest("https", "host", "", "a=/../&?="));
            assertEquals("https", uri.getScheme());
            assertEquals("host", uri.getHost());
            assertEquals("", uri.getRawPath());
            assertEquals("a=/../&?=", uri.getRawQuery());
        }
    }