Skip to content
Permalink
Browse files
8218546: Unable to connect to https://google.com using java.net.HttpC…
…lient

Backport-of: 10a281c
  • Loading branch information
shipilev committed Jan 27, 2022
1 parent b6df150 commit b240c60427e693c4e8afc34fc52b2f9f70584537
Showing 2 changed files with 63 additions and 28 deletions.
@@ -614,10 +614,6 @@ private OutgoingHeaders<Stream<T>> headerFrame(long contentLength) {
if (contentLength > 0) {
h.setHeader("content-length", Long.toString(contentLength));
}
URI uri = request.uri();
if (uri != null) {
h.setHeader("host", Utils.hostString(request));
}
HttpHeaders sysh = filterHeaders(h.build());
HttpHeaders userh = filterHeaders(request.getUserHeaders());
// Filter context restricted from userHeaders
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
* @test
* @summary Verify that some special headers - such as User-Agent
* can be specified by the caller.
* @bug 8203771
* @bug 8203771 8218546
* @modules java.base/sun.net.www.http
* java.net.http/jdk.internal.net.http.common
* java.net.http/jdk.internal.net.http.frame
@@ -64,8 +64,6 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
@@ -74,13 +72,13 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import static java.lang.System.err;
import static java.lang.System.out;
import static java.net.http.HttpClient.Builder.NO_PROXY;
import static java.net.http.HttpClient.Version.HTTP_2;
import static java.nio.charset.StandardCharsets.US_ASCII;
import org.testng.Assert;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class SpecialHeadersTest implements HttpServerAdapters {

@@ -151,7 +149,11 @@ static String userAgent() {
"USER-AGENT", u -> userAgent(), "HOST", u -> u.getRawAuthority());

@Test(dataProvider = "variants")
void test(String uriString, String headerNameAndValue, boolean sameClient) throws Exception {
void test(String uriString,
String headerNameAndValue,
boolean sameClient)
throws Exception
{
out.println("\n--- Starting ");

int index = headerNameAndValue.indexOf(":");
@@ -183,21 +185,41 @@ void test(String uriString, String headerNameAndValue, boolean sameClient) throw
assertEquals(resp.statusCode(), 200,
"Expected 200, got:" + resp.statusCode());

String receivedHeaderString = value == null ? null
: resp.headers().firstValue("X-"+key).get();
out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key));
if (value != null) {
assertEquals(receivedHeaderString, value);
assertEquals(resp.headers().allValues("X-"+key), List.of(value));
boolean isInitialRequest = i == 0;
boolean isSecure = uri.getScheme().equalsIgnoreCase("https");
boolean isHTTP2 = resp.version() == HTTP_2;
boolean isNotH2CUpgrade = isSecure || (sameClient == true && !isInitialRequest);
boolean isDefaultHostHeader = name.equalsIgnoreCase("host") && useDefault;

// By default, HTTP/2 sets the `:authority:` pseudo-header, instead
// of the `Host` header. Therefore, there should be no "X-Host"
// header in the response, except the response to the h2c Upgrade
// request which will have been sent through HTTP/1.1.

if (isDefaultHostHeader && isHTTP2 && isNotH2CUpgrade) {
assertTrue(resp.headers().firstValue("X-" + key).isEmpty());
assertTrue(resp.headers().allValues("X-" + key).isEmpty());
out.println("No X-" + key + " header received, as expected");
} else {
assertEquals(resp.headers().allValues("X-"+key).size(), 0);
String receivedHeaderString = value == null ? null
: resp.headers().firstValue("X-"+key).get();
out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key));
if (value != null) {
assertEquals(receivedHeaderString, value);
assertEquals(resp.headers().allValues("X-"+key), List.of(value));
} else {
assertEquals(resp.headers().allValues("X-"+key).size(), 0);
}
}

}
}

@Test(dataProvider = "variants")
void testHomeMadeIllegalHeader(String uriString, String headerNameAndValue, boolean sameClient) throws Exception {
void testHomeMadeIllegalHeader(String uriString,
String headerNameAndValue,
boolean sameClient)
throws Exception
{
out.println("\n--- Starting ");
final URI uri = URI.create(uriString);

@@ -266,22 +288,39 @@ void testAsync(String uriString, String headerNameAndValue, boolean sameClient)
}
HttpRequest request = requestBuilder.build();

boolean isInitialRequest = i == 0;
boolean isSecure = uri.getScheme().equalsIgnoreCase("https");
boolean isNotH2CUpgrade = isSecure || (sameClient == true && !isInitialRequest);
boolean isDefaultHostHeader = name.equalsIgnoreCase("host") && useDefault;

client.sendAsync(request, BodyHandlers.ofString())
.thenApply(response -> {
out.println("Got response: " + response);
out.println("Got body: " + response.body());
assertEquals(response.statusCode(), 200);
return response;})
.thenAccept(resp -> {
String receivedHeaderString = value == null ? null
: resp.headers().firstValue("X-"+key).get();
out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key));
if (value != null) {
assertEquals(receivedHeaderString, value);
assertEquals(resp.headers().allValues("X-" + key), List.of(value));
// By default, HTTP/2 sets the `:authority:` pseudo-header, instead
// of the `Host` header. Therefore, there should be no "X-Host"
// header in the response, except the response to the h2c Upgrade
// request which will have been sent through HTTP/1.1.

if (isDefaultHostHeader && resp.version() == HTTP_2 && isNotH2CUpgrade) {
assertTrue(resp.headers().firstValue("X-" + key).isEmpty());
assertTrue(resp.headers().allValues("X-" + key).isEmpty());
out.println("No X-" + key + " header received, as expected");
} else {
assertEquals(resp.headers().allValues("X-" + key).size(), 1);
} })
String receivedHeaderString = value == null ? null
: resp.headers().firstValue("X-"+key).get();
out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key));
if (value != null) {
assertEquals(receivedHeaderString, value);
assertEquals(resp.headers().allValues("X-" + key), List.of(value));
} else {
assertEquals(resp.headers().allValues("X-" + key).size(), 1);
}
}
})
.join();
}
}

3 comments on commit b240c60

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on b240c60 Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on b240c60 Feb 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk11u

@openjdk
Copy link

@openjdk openjdk bot commented on b240c60 Feb 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch GoeLin-backport-b240c604 in my personal fork of openjdk/jdk11u. To create a pull request with this backport targeting openjdk/jdk11u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit b240c604 from the openjdk/jdk11u-dev repository.

The commit being backported was authored by Aleksey Shipilev on 27 Jan 2022 and had no reviewers.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u:

$ git fetch https://github.com/openjdk-bots/jdk11u GoeLin-backport-b240c604:GoeLin-backport-b240c604
$ git checkout GoeLin-backport-b240c604
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u GoeLin-backport-b240c604

Please sign in to comment.