undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,119 @@
/*
* Copyright (c) 2004, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 6181108
* @library /test/lib
* @summary double encoded URL passed to ResponseCache
* @author Edward Wang
*/
import java.net.*;
import java.util.*;
import java.io.*;
import jdk.test.lib.net.URIBuilder;
import static java.net.Proxy.NO_PROXY;
public class B6181108 implements Runnable {
ServerSocket ss;
static String urlWithSpace;
/*
* "Our" http server just return 200
*/
public void run() {
try {
Socket s = ss.accept();
InputStream is = s.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String x;
while ((x=r.readLine()) != null) {
if (x.length() ==0) {
break;
}
}
PrintStream out = new PrintStream(
new BufferedOutputStream(
s.getOutputStream() ));
/* response 200 */
out.print("HTTP/1.1 200 OK\r\n");
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
out.print("Content-Length: 0\r\n");
out.print("Connection: close\r\n");
out.print("\r\n");
out.print("\r\n");
out.flush();
s.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try { ss.close(); } catch (IOException unused) {}
}
}
static class ResponseCache extends java.net.ResponseCache {
public CacheResponse get(URI uri, String method, Map<String,List<String>> hdrs) {
System.out.println("get uri = " + uri);
if (!urlWithSpace.equals(uri.toString())) {
throw new RuntimeException("test failed");
}
return null;
}
public CacheRequest put(URI uri, URLConnection urlc) {
System.out.println("put uri = " + uri);
return null;
}
}
B6181108() throws Exception {
/* start the server */
InetAddress loopback = InetAddress.getLoopbackAddress();
ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));
(new Thread(this)).start();
ResponseCache.setDefault(new ResponseCache());
String base = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(ss.getLocalPort())
.build()
.toString();
urlWithSpace = base + "/space%20test/page1.html";
URL url = new URL(urlWithSpace);
URLConnection urlc = url.openConnection(NO_PROXY);
int i = ((HttpURLConnection)(urlc)).getResponseCode();
System.out.println("response code = " + i);
ResponseCache.setDefault(null);
}
public static void main(String args[]) throws Exception {
new B6181108();
}
}

View file

@ -0,0 +1,259 @@
/*
* Copyright (c) 2003, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @summary Unit test for java.net.ResponseCache
* @bug 4837267
* @library /test/lib
* @author Yingxian Wang
*/
import java.net.*;
import java.util.*;
import java.io.*;
import javax.net.ssl.*;
import jdk.test.lib.net.URIBuilder;
import static java.net.Proxy.NO_PROXY;
/**
* Request should get serviced by the cache handler. Response get
* saved through the cache handler.
*/
public class ResponseCacheTest implements Runnable {
ServerSocket ss;
static URL url1;
static URL url2;
static String FNPrefix, OutFNPrefix;
static List<Closeable> streams = new ArrayList<>();
static List<File> files = new ArrayList<>();
/*
* Our "http" server to return a 404 */
public void run() {
Socket s = null;
FileInputStream fis = null;
try {
s = ss.accept();
InputStream is = s.getInputStream ();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String x;
while ((x=r.readLine()) != null) {
if (x.length() ==0) {
break;
}
}
PrintStream out = new PrintStream(
new BufferedOutputStream(
s.getOutputStream() ));
/* send file2.1 */
File file2 = new File(FNPrefix+"file2.1");
out.print("HTTP/1.1 200 OK\r\n");
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
out.print("Content-Length: "+file2.length()+"\r\n");
out.print("Connection: close\r\n");
out.print("\r\n");
fis = new FileInputStream(file2);
byte[] buf = new byte[(int)file2.length()];
int len;
while ((len = fis.read(buf)) != -1) {
out.print(new String(buf));
}
out.flush();
s.close();
ss.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try { ss.close(); } catch (IOException unused) {}
try { s.close(); } catch (IOException unused) {}
try { fis.close(); } catch (IOException unused) {}
}
}
static class NameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
ResponseCacheTest() throws Exception {
/* start the server */
InetAddress loopback = InetAddress.getLoopbackAddress();
ss = new ServerSocket();
ss.bind(new InetSocketAddress(loopback, 0));
(new Thread(this)).start();
/* establish http connection to server */
url1 = new URL("http://localhost/file1.cache");
HttpURLConnection http = (HttpURLConnection)url1.openConnection();
InputStream is = null;
System.out.println("request headers: "+http.getRequestProperties());
System.out.println("responsecode is :"+http.getResponseCode());
Map<String,List<String>> headers1 = http.getHeaderFields();
try {
is = http.getInputStream();
} catch (IOException ioex) {
throw new RuntimeException(ioex.getMessage());
}
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String x;
File fileout = new File(OutFNPrefix+"file1");
PrintStream out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream(fileout)));
while ((x=r.readLine()) != null) {
out.print(x+"\n");
}
out.flush();
out.close();
http.disconnect();
// testing ResponseCacheHandler.put()
url2 = URIBuilder.newBuilder()
.scheme("http")
.host(ss.getInetAddress())
.port(ss.getLocalPort())
.path("/file2.1")
.toURL();
http = (HttpURLConnection)url2.openConnection(NO_PROXY);
System.out.println("responsecode2 is :"+http.getResponseCode());
Map<String,List<String>> headers2 = http.getHeaderFields();
try {
is = http.getInputStream();
} catch (IOException ioex) {
throw new RuntimeException(ioex.getMessage());
}
r = new BufferedReader(new InputStreamReader(is));
fileout = new File(OutFNPrefix+"file2.2");
out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream(fileout)));
while ((x=r.readLine()) != null) {
out.print(x+"\n");
}
out.flush();
out.close();
// assert (headers1 == headers2 && file1 == file2.2)
File file1 = new File(OutFNPrefix+"file1");
File file2 = new File(OutFNPrefix+"file2.2");
files.add(file1);
files.add(file2);
System.out.println("headers1"+headers1+"\nheaders2="+headers2);
if (!headers1.equals(headers2) || file1.length() != file2.length()) {
throw new RuntimeException("test failed");
}
}
public static void main(String args[]) throws Exception {
try {
ResponseCache.setDefault(new MyResponseCache());
FNPrefix = System.getProperty("test.src", ".")+"/";
OutFNPrefix = System.getProperty("test.scratch", ".")+"/";
new ResponseCacheTest();
} finally{
ResponseCache.setDefault(null);
for (Closeable c: streams) {
try { c.close(); } catch (IOException unused) {}
}
for (File f: files) {
f.delete();
}
}
}
static class MyResponseCache extends ResponseCache {
public CacheResponse get(URI uri, String rqstMethod,
Map<String,List<String>> rqstHeaders)
throws IOException
{
try {
if (uri.equals(url1.toURI())) {
return new MyCacheResponse(FNPrefix+"file1.cache");
}
} catch (URISyntaxException ex) {
throw new RuntimeException (ex);
}
return null;
}
public CacheRequest put(URI uri, URLConnection conn) throws IOException {
// save cache to file2.cache
// 1. serialize headers into file2.cache
// 2. write data to file2.cache
return new MyCacheRequest(OutFNPrefix+"file2.cache", conn.getHeaderFields());
}
}
static class MyCacheResponse extends CacheResponse {
FileInputStream fis;
Map<String,List<String>> headers;
public MyCacheResponse(String filename) {
try {
fis = new FileInputStream(new File(filename));
streams.add(fis);
ObjectInputStream ois = new ObjectInputStream(fis);
headers = (Map<String,List<String>>)ois.readObject();
} catch (Exception ex) {
// throw new RuntimeException(ex.getMessage());
}
}
public InputStream getBody() throws IOException {
return fis;
}
public Map<String,List<String>> getHeaders() throws IOException {
return headers;
}
}
static class MyCacheRequest extends CacheRequest {
FileOutputStream fos;
public MyCacheRequest(String filename, Map<String,List<String>> rspHeaders) {
try {
File file = new File(filename);
fos = new FileOutputStream(file);
streams.add(fos);
files.add(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(rspHeaders);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage());
}
}
public OutputStream getBody() throws IOException {
return fos;
}
public void abort() {
// no op
}
}
}

View file

@ -0,0 +1,92 @@
/*
* Copyright (c) 2012, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @summary Fixed a potential NullPointerException when setting a ResponseCache that returns a null CacheRequest
* @bug 4837267
* @modules jdk.httpserver
* @library /test/lib
* @run main Test
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test
* @author Michael McMahon
*/
import com.sun.net.httpserver.*;
import java.net.*;
import java.io.*;
import java.util.*;
import jdk.test.lib.net.URIBuilder;
public class Test
{
static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
byte[] b = new byte[1024];
int r = 0;
InputStream is = t.getRequestBody();
while (is.read(b) != -1) ;
String response = "This is the response";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
public static void main(String args[]) throws Exception {
InetAddress loopback = InetAddress.getLoopbackAddress();
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
server.createContext("/", new MyHandler());
server.start();
ResponseCache bak = ResponseCache.getDefault();
try {
ResponseCache.setDefault(new ResponseCache() {
public CacheResponse get(URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)
throws IOException {
return null;
}
public CacheRequest put(URI uri, URLConnection conn) throws IOException
{
return null;
}
});
URL url = URIBuilder.newBuilder()
.scheme("http")
.host(server.getAddress().getAddress())
.port(server.getAddress().getPort())
.path("/")
.toURL();
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
InputStream is = urlc.getInputStream();
while (is.read() != -1) ;
is.close();
} finally {
ResponseCache.setDefault(bak);
server.stop(0);
}
}
}

View file

@ -0,0 +1,151 @@
/*
* Copyright (c) 2014, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8042622
* @library /test/lib
* @summary Check for CRL results in IllegalArgumentException "white space not allowed"
* @modules jdk.httpserver
* @run main/othervm Test2
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test2
*/
import com.sun.net.httpserver.*;
import java.util.*;
import java.util.concurrent.*;
import java.io.*;
import java.net.*;
import java.security.*;
import javax.security.auth.callback.*;
import javax.net.ssl.*;
import jdk.test.lib.net.URIBuilder;
public class Test2 {
static volatile boolean failed = false;
static class Cache extends ResponseCache {
public CacheResponse get(URI uri, String method, Map<String,List<String>> headers) {
Set<String> keys = headers.keySet();
for (String key : keys) {
if (key.indexOf(' ') != -1 || key.indexOf('\t') != -1
|| key.indexOf(':') != -1)
{
failed = true;
}
}
return null;
}
public CacheRequest put(URI uri, URLConnection c) throws IOException {
return null;
}
}
static int port;
static URL url, redir;
public static void main (String[] args) throws Exception {
Handler handler = new Handler();
InetAddress loopback = InetAddress.getLoopbackAddress();
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
HttpServer server = HttpServer.create (addr, 0);
port = server.getAddress().getPort();
HttpContext ctx = server.createContext ("/test", handler);
System.out.println ("Server: " + server.getAddress());
ResponseCache.setDefault(new Cache());
ExecutorService executor = Executors.newCachedThreadPool();
server.setExecutor (executor);
server.start ();
url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.path("/test/foo")
.toURLUnchecked();
System.out.println("URL: " + url);
redir = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(port)
.path("/test/foo/redirect/bar")
.toURLUnchecked();
System.out.println("Redir URL: " + redir);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
urlc.addRequestProperty("X-Foo", "bar");
urlc.setInstanceFollowRedirects(true);
System.out.println(urlc.getResponseCode());
InputStream i = urlc.getInputStream();
int count=0;
for (int c=i.read(); c!=-1; c=i.read()) {
//System.out.write(c);
count++;
}
System.out.println("Read " + count);
System.out.println("FINISHED");
server.stop(0);
executor.shutdownNow();
if (failed) {
throw new RuntimeException("Test failed");
}
}
public static boolean error = false;
public static int count = 0;
static class Handler implements HttpHandler {
int invocation = 0;
public void handle (HttpExchange t)
throws IOException
{
InputStream is = t.getRequestBody();
Headers map = t.getRequestHeaders();
Headers rmap = t.getResponseHeaders();
invocation ++;
if (invocation == 1) {
rmap.add("Location", redir.toString());
while (is.read () != -1) ;
is.close();
System.out.println ("sending response");
t.sendResponseHeaders (301, 0);
} else {
byte[] buf = "Hello world".getBytes();
t.sendResponseHeaders (200, buf.length);
OutputStream os = t.getResponseBody();
try {
os.write(buf);
} catch (IOException e) {
System.out.println ("EX 1 " + e);
}
}
System.out.println ("Closing");
t.close();
}
}
}

Binary file not shown.

View file

@ -0,0 +1,184 @@
/* @test @(#)file2.1 1.1 03/08/09
* @summary Unit test for java.net.ResponseCacheHandler
* @bug 4837267
* @author Yingxian Wang
*/
import java.net.*;
import java.util.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.ReadableByteChannel;
import sun.net.www.ParseUtil;
/**
* Request should get serviced by the cache handler. Response get
* saved through the cache handler.
*/
public class ResponseCacheHandlerTest implements Runnable {
ServerSocket ss;
static URL url1;
static URL url2;
static boolean flag = false;
/*
* Our "http" server to return a 404 */
public void run() {
try {
Socket s = ss.accept();
// check request contains "Cookie"
InputStream is = s.getInputStream ();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
boolean flag = false;
String x;
while ((x=r.readLine()) != null) {
if (x.length() ==0) {
break;
}
}
PrintStream out = new PrintStream(
new BufferedOutputStream(
s.getOutputStream() ));
/* send file2.1 */
File file2 = new File("file2.1");
out.print("HTTP/1.1 200 OK\r\n");
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
out.print("Content-Length: "+file2.length()+"\r\n");
out.print("Connection: close\r\n");
out.print("\r\n");
FileInputStream fis = new FileInputStream(file2);
byte[] buf = new byte[(int)file2.length()];
int len;
while ((len = fis.read(buf)) != -1) {
out.print(new String(buf));
}
out.flush();
s.close();
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
ResponseCacheHandlerTest() throws Exception {
/* start the server */
ss = new ServerSocket(0);
(new Thread(this)).start();
/* establish http connection to server */
url1 = new URL("http://localhost/file1.cache");
HttpURLConnection http = (HttpURLConnection)url1.openConnection();
InputStream is = null;
System.out.println("headers1:"+http.getHeaderFields());
try {
is = http.getInputStream();
} catch (IOException ioex) {
throw new RuntimeException(ioex.getMessage());
}
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String x;
File fileout = new File("file1");
PrintStream out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream(fileout)));
while ((x=r.readLine()) != null) {
out.print(x+"\n");
}
out.flush();
out.close();
// if !(file1.out == file1.cache)
// throw new RuntimeException("testing ResponseCacheHandler.get() failed");
http.disconnect();
// testing ResponseCacheHandler.put()
url2 = new URL("http://localhost:" +
Integer.toString(ss.getLocalPort())+"/file2.1");
http = (HttpURLConnection)url2.openConnection();
System.out.println("headers2:"+http.getHeaderFields());
try {
is = http.getInputStream();
} catch (IOException ioex) {
throw new RuntimeException(ioex.getMessage());
}
DataInputStream dis = new DataInputStream(is);
fileout = new File("file2.2");
byte[] buf = new byte[(int)new File("file2.1").length()];
DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileout));
int len;
while ((len=dis.read(buf)) != -1) {
dos.write(buf, 0, len);
}
dos.flush();
dos.close();
// if !(file2.1 == file2.2 == file2.cache)
// throw new RuntimeException("testing ResponseCacheHandler.put() failed");
}
public static void main(String args[]) throws Exception {
ResponseCacheHandler.setDefault(new MyResponseCacheHandler());
new ResponseCacheHandlerTest();
}
static class MyResponseCacheHandler extends ResponseCacheHandler {
public Response
get(URI uri, Map requestHeaders)
throws IOException {
if (uri.equals(ParseUtil.toURI(url1))) {
return new MyResponse("file1.cache");
} if (uri.equals(ParseUtil.toURI(url2)) && !flag) {
flag = true;
return null;
} else {
return new MyResponse("file2.cache");
}
}
public boolean put(URI uri, Response response) throws IOException {
// save cache to file2.cache
// 1. serialize headers into file2.cache
// 2. write data to file2.cache
File file = new File("file2.cache");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(response.getHeaders());
ReadableByteChannel rbc = response.getBody();
int len = (int)new File("file2.1").length();
ByteBuffer buf = ByteBuffer.allocate(len);
byte[] b = new byte[len];
while ((len = rbc.read(buf)) != -1) {
buf.flip();
buf.get(b, 0, len);
buf.compact();
oos.write(b, 0, len);
}
oos.close();
return true;
}
}
static class MyResponse extends Response {
FileInputStream fis;
Map headers;
public MyResponse(String filename) {
try {
fis = new FileInputStream(new File(filename));
headers = (Map)new ObjectInputStream(fis).readObject();
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage());
}
}
public ReadableByteChannel getBody() throws IOException {
return fis.getChannel();
}
public Map getHeaders() throws IOException {
return headers;
}
}
}

View file

@ -0,0 +1,99 @@
/*
* Copyright (c) 2003, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @summary getResponseCode() doesn't return correct value when using cached response
* @bug 4921268
* @author Yingxian Wang
*/
import java.net.*;
import java.util.*;
import java.io.*;
import static java.net.Proxy.NO_PROXY;
/**
* Request should get serviced by the cache handler. Response get
* saved through the cache handler.
*/
public class getResponseCode {
static URL url;
static String FNPrefix;
static List<Closeable> resources = new ArrayList<>();
getResponseCode() throws Exception {
url = new URL("http://localhost/file1.cache");
HttpURLConnection http = (HttpURLConnection)url.openConnection(NO_PROXY);
int respCode = http.getResponseCode();
http.disconnect();
if (respCode != 200) {
throw new RuntimeException("Response code should return 200, but it is returning "+respCode);
}
}
public static void main(String args[]) throws Exception {
try {
ResponseCache.setDefault(new MyResponseCache());
FNPrefix = System.getProperty("test.src", ".")+"/";
new getResponseCode();
} finally{
ResponseCache.setDefault(null);
for (Closeable c : resources) {
try { c.close(); } catch (IOException unused) {}
}
}
}
static class MyResponseCache extends ResponseCache {
public CacheResponse
get(URI uri, String rqstMethod, Map<String,List<String>> requestHeaders)
throws IOException {
return new MyResponse(FNPrefix+"file1.cache");
}
public CacheRequest put(URI uri, URLConnection uconn) throws IOException {;
return null;
}
}
static class MyResponse extends CacheResponse {
FileInputStream fis;
Map<String,List<String>> headers;
public MyResponse(String filename) {
try {
fis = new FileInputStream(new File(filename));
resources.add(fis);
headers = (Map<String,List<String>>)new ObjectInputStream(fis).readObject();
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage());
}
}
public InputStream getBody() throws IOException {
return fis;
}
public Map<String,List<String>> getHeaders() throws IOException {
return headers;
}
}
}