Skip to content

Commit 9221440

Browse files
committed
8292876: Do not include the deprecated userinfo component of the URI in HTTP/2 headers
Reviewed-by: phh Backport-of: b30d922009bab114fb8ac200df42b594a72a2942
1 parent 0385265 commit 9221440

File tree

2 files changed

+124
-3
lines changed

2 files changed

+124
-3
lines changed

src/java.net.http/share/classes/jdk/internal/net/http/Stream.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,14 @@ private static HttpHeaders createPseudoHeaders(HttpRequest request) {
842842
hdrs.setHeader(":method", method);
843843
URI uri = request.uri();
844844
hdrs.setHeader(":scheme", uri.getScheme());
845-
// TODO: userinfo deprecated. Needs to be removed
846-
hdrs.setHeader(":authority", uri.getAuthority());
847-
// TODO: ensure header names beginning with : not in user headers
845+
String host = uri.getHost();
846+
int port = uri.getPort();
847+
assert host != null;
848+
if (port != -1) {
849+
hdrs.setHeader(":authority", host + ":" + port);
850+
} else {
851+
hdrs.setHeader(":authority", host);
852+
}
848853
String query = uri.getRawQuery();
849854
String path = uri.getRawPath();
850855
if (path == null || path.isEmpty()) {
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import jdk.test.lib.net.SimpleSSLContext;
25+
import jdk.test.lib.net.URIBuilder;
26+
import org.junit.jupiter.api.AfterAll;
27+
import org.junit.jupiter.api.BeforeAll;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.TestInstance;
30+
31+
import javax.net.ssl.SSLContext;
32+
import java.io.IOException;
33+
import java.net.URI;
34+
import java.net.http.HttpClient;
35+
import java.net.http.HttpRequest;
36+
import java.net.http.HttpResponse;
37+
import jdk.httpclient.test.lib.http2.Http2TestServer;
38+
import jdk.httpclient.test.lib.http2.Http2TestExchange;
39+
import jdk.httpclient.test.lib.http2.Http2Handler;
40+
41+
42+
import static org.junit.jupiter.api.Assertions.assertEquals;
43+
44+
45+
/**
46+
* @test
47+
* @bug 8292876
48+
* @library /test/lib /test/jdk/java/net/httpclient/lib
49+
* @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext
50+
* @run junit/othervm UserInfoTest
51+
*/
52+
53+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
54+
public class UserInfoTest {
55+
56+
Http2TestServer server;
57+
int port;
58+
SSLContext sslContext;
59+
60+
@BeforeAll
61+
void before() throws Exception {
62+
sslContext = new SimpleSSLContext().get();
63+
server = createServer(sslContext);
64+
port = server.getAddress().getPort();
65+
server.start();
66+
}
67+
68+
@AfterAll
69+
void after() throws Exception {
70+
server.close();
71+
}
72+
73+
static class Http2TestHandler implements Http2Handler {
74+
@Override
75+
public void handle(Http2TestExchange e) throws IOException {
76+
String authorityHeader = e.getRequestHeaders().firstValue(":authority").orElse(null);
77+
if (authorityHeader == null || authorityHeader.contains("user@")) {
78+
e.sendResponseHeaders(500, -1);
79+
} else {
80+
e.sendResponseHeaders(200, -1);
81+
}
82+
}
83+
}
84+
85+
private static Http2TestServer createServer(SSLContext sslContext) throws Exception {
86+
Http2TestServer http2TestServer = new Http2TestServer("localhost", true, sslContext);
87+
Http2TestHandler handler = new Http2TestHandler();
88+
http2TestServer.addHandler(handler, "/");
89+
return http2TestServer;
90+
}
91+
92+
@Test
93+
public void testAuthorityHeader() throws Exception {
94+
HttpClient client = HttpClient
95+
.newBuilder()
96+
.proxy(HttpClient.Builder.NO_PROXY)
97+
.sslContext(sslContext)
98+
.build();
99+
100+
URI uri = URIBuilder.newBuilder()
101+
.scheme("https")
102+
.userInfo("user")
103+
.loopback()
104+
.port(port)
105+
.build();
106+
107+
HttpRequest request = HttpRequest
108+
.newBuilder(uri)
109+
.GET()
110+
.build();
111+
112+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
113+
114+
assertEquals(200, response.statusCode(), "Test Failed : " + response.uri().getAuthority());
115+
}
116+
}

0 commit comments

Comments
 (0)