ADDITIONAL SYSTEM INFORMATION :
java -version
openjdk version "11.0.26" 2025-01-21
OpenJDK Runtime Environment (build 11.0.26+4-post-Ubuntu-1ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.26+4-post-Ubuntu-1ubuntu120.04, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
1. When can responseMessage be null :-
Key Steps in expect100Continue()
a. The method attempts to parse the initial response using:
http.parseHTTP(responses, this);
This reads the response headers and updates the responses object.
b. If the response is not 100 Continue, the method throws an exception before resetting responseCode:
throw new ProtocolException("Server rejected operation");
Key Observation: At this point, responseCode retains the non-100 value received from the server, and the method exits without resetting responseCode to -1.
What Happens When getResponseCode() is called?
Since expect100Continue() does not reset responseCode in case of a non-100 response, the behavior in getResponseCode() is as follows:
1. The first check in getResponseCode() returns immediately if responseCode is already set:
if (responseCode != -1) {
return responseCode;
}
Because responseCode is still set to the non-100 response (e.g., 403 Forbidden), the method returns immediately without further processing.
2. Why does responseMessage remain null?
Since getResponseCode() exits early, it does not attempt to extract the reason phrase from the status line.
If responseMessage was not explicitly set earlier, it remains null.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. For calling server which is responding with non 100 statusCode.
On an httpUrlConnection instance created for upload, make the getOutputStream() method fails with expect100 error and return any status code such as 4xx or 5xx.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
//Lets assume that server return non-100 statusCode everytime server call is invoked.
HttpUrlConnection conn = url.openConnection();
try {
conn.getOutputStream();
} catch(Exception e) {}
See the value of responseCode set in expect100continue method, should be non 100 and not -1.
2. Now call getResponseCode() method and verify that it exits early as responseCode is not -1, and responseMessage fails to get set.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
1. The responseMessage should be same as what was set by the server irrespective of 100 continue assertion passes or fails.
ACTUAL -
1. The responseMessage is coming as null.
---------- BEGIN SOURCE ----------
public class Tests {
public static void main(String[] args) throws IOException {
URL url = new URL(
"http://www.oracle.com"); //server be that is giving non-100 status response for each server call.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
conn.setRequestProperty("Expect", "100-continue");
sendRequest(conn);
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
}
private static void sendRequest ( final HttpURLConnection conn)
throws IOException {
OutputStream os = null;
conn.setDoOutput(true);
conn.setFixedLengthStreamingMode(10);
byte[] bytes = new byte[10];
try {
os = conn.getOutputStream();
os.write(bytes);
} catch (Exception e) {}
if (os != null) {
os.close();
}
}
}
---------- END SOURCE ----------
java -version
openjdk version "11.0.26" 2025-01-21
OpenJDK Runtime Environment (build 11.0.26+4-post-Ubuntu-1ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.26+4-post-Ubuntu-1ubuntu120.04, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
1. When can responseMessage be null :-
Key Steps in expect100Continue()
a. The method attempts to parse the initial response using:
http.parseHTTP(responses, this);
This reads the response headers and updates the responses object.
b. If the response is not 100 Continue, the method throws an exception before resetting responseCode:
throw new ProtocolException("Server rejected operation");
Key Observation: At this point, responseCode retains the non-100 value received from the server, and the method exits without resetting responseCode to -1.
What Happens When getResponseCode() is called?
Since expect100Continue() does not reset responseCode in case of a non-100 response, the behavior in getResponseCode() is as follows:
1. The first check in getResponseCode() returns immediately if responseCode is already set:
if (responseCode != -1) {
return responseCode;
}
Because responseCode is still set to the non-100 response (e.g., 403 Forbidden), the method returns immediately without further processing.
2. Why does responseMessage remain null?
Since getResponseCode() exits early, it does not attempt to extract the reason phrase from the status line.
If responseMessage was not explicitly set earlier, it remains null.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. For calling server which is responding with non 100 statusCode.
On an httpUrlConnection instance created for upload, make the getOutputStream() method fails with expect100 error and return any status code such as 4xx or 5xx.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
//Lets assume that server return non-100 statusCode everytime server call is invoked.
HttpUrlConnection conn = url.openConnection();
try {
conn.getOutputStream();
} catch(Exception e) {}
See the value of responseCode set in expect100continue method, should be non 100 and not -1.
2. Now call getResponseCode() method and verify that it exits early as responseCode is not -1, and responseMessage fails to get set.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
1. The responseMessage should be same as what was set by the server irrespective of 100 continue assertion passes or fails.
ACTUAL -
1. The responseMessage is coming as null.
---------- BEGIN SOURCE ----------
public class Tests {
public static void main(String[] args) throws IOException {
URL url = new URL(
"http://www.oracle.com"); //server be that is giving non-100 status response for each server call.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
conn.setRequestProperty("Expect", "100-continue");
sendRequest(conn);
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
}
private static void sendRequest ( final HttpURLConnection conn)
throws IOException {
OutputStream os = null;
conn.setDoOutput(true);
conn.setFixedLengthStreamingMode(10);
byte[] bytes = new byte[10];
try {
os = conn.getOutputStream();
os.write(bytes);
} catch (Exception e) {}
if (os != null) {
os.close();
}
}
}
---------- END SOURCE ----------