Skip to content

Commit

Permalink
8299018: java/net/httpclient/HttpsTunnelAuthTest.java fails with java…
Browse files Browse the repository at this point in the history
….io.IOException: HTTP/1.1 header parser received no bytes

Backport-of: e5edb10dc56d9edac8e050e0f8e6c116743975d6
  • Loading branch information
GoeLin committed Feb 13, 2023
1 parent da75daf commit a800231
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions test/jdk/java/net/httpclient/ProxyServer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2022, 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
Expand Down Expand Up @@ -331,8 +331,10 @@ public void init() {
// check authorization credentials, if required by the server
if (credentials != null) {
if (!authorized(credentials, headers)) {
boolean shouldClose = shouldCloseAfter407(headers);
var closestr = shouldClose ? "Connection: close\r\n" : "";
String resp = "HTTP/1.1 407 Proxy Authentication Required\r\n" +
"Content-Length: 0\r\n" +
"Content-Length: 0\r\n" + closestr +
"Proxy-Authenticate: Basic realm=\"proxy realm\"\r\n\r\n";
clientSocket.setOption(StandardSocketOptions.TCP_NODELAY, true);
clientSocket.setOption(StandardSocketOptions.SO_LINGER, 2);
Expand All @@ -344,7 +346,7 @@ public void init() {
System.out.printf("Proxy: unauthorized; 407 sent (%s/%s), linger: %s, nodelay: %s%n",
buffer.position(), buffer.position() + buffer.remaining(), linger, nodelay);
}
if (shouldCloseAfter407(headers)) {
if (shouldClose) {
closeConnection();
return;
}
Expand Down Expand Up @@ -445,6 +447,11 @@ void closeConnection() throws IOException {
// If the client sends a request body we will need to close the connection
// otherwise, we can keep it open.
private boolean shouldCloseAfter407(List<String> headers) throws IOException {
var cmdline = headers.get(0);
int m = cmdline.indexOf(' ');
var method = (m > 0) ? cmdline.substring(0, m) : null;
var nobody = List.of("GET", "HEAD");

var te = findFirst(headers, "transfer-encoding");
if (te != null) {
// processing transfer encoding not implemented
Expand All @@ -455,28 +462,37 @@ private boolean shouldCloseAfter407(List<String> headers) throws IOException {
}
var cl = findFirst(headers, "content-length");
int n = -1;
try {
n = Integer.parseInt(cl);
if (debug) {
System.out.printf("Proxy: content-length: %d%n", cl);
if (cl == null) {
if (nobody.contains(method)) {
n = 0;
System.out.printf("Proxy: no content length for %s, assuming 0%n", method);
} else {
System.out.printf("Proxy: no content-length for %s, closing connection%n", method);
return true;
}
} catch (IllegalFormatException x) {
if (debug) {
System.out.println("Proxy: bad content-length, closing connection");
} else {
try {
n = Integer.parseInt(cl);
if (debug) {
System.out.printf("Proxy: content-length: %d%n", n);
}
} catch (NumberFormatException x) {
if (debug) {
System.out.println("Proxy: bad content-length, closing connection");
System.out.println("Proxy: \tcontent-length: " + cl);
System.out.println("Proxy: \theaders: " + headers);
System.out.println("Proxy: \t" + x);
}
return true; // should close
}
return true; // should close
}
if (n > 0 || n < -1) {
if (n > 0) {
if (debug) {
System.out.println("Proxy: request body with 407, closing connection");
}
return true; // should close
}
var cmdline = headers.get(0);
int m = cmdline.indexOf(' ');
var method = (m > 0) ? cmdline.substring(0, m) : null;
var nobody = List.of("GET", "HEAD");
if (n == 0 || nobody.contains(m)) {
if (n == 0) {
var available = clientIn.available();
var drained = drain(clientSocket);
if (drained > 0 || available > 0) {
Expand Down

1 comment on commit a800231

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.