Path: blob/master/test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java
66646 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 626248626* @library /test/lib27* @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream28* @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load29*/3031import java.io.ByteArrayOutputStream;32import java.io.IOException;33import java.io.InputStream;34import java.io.OutputStream;35import java.io.PrintWriter;36import java.net.CacheRequest;37import java.net.CacheResponse;38import java.net.HttpURLConnection;39import java.net.InetAddress;40import java.net.InetSocketAddress;41import java.net.ResponseCache;42import java.net.URI;43import java.net.URL;44import java.net.URLConnection;45import java.util.List;46import java.util.Map;47import java.util.concurrent.Executors;4849import com.sun.net.httpserver.HttpExchange;50import com.sun.net.httpserver.HttpHandler;51import com.sun.net.httpserver.HttpServer;52import jdk.test.lib.net.URIBuilder;5354public class ResponseCacheStream implements HttpHandler {5556void okReply (HttpExchange req) throws IOException {57req.sendResponseHeaders(200, 0);58try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {59pw.print("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");60}61System.out.println ("Server: sent response");62}636465@Override66public void handle(HttpExchange exchange) throws IOException {67okReply(exchange);68exchange.close();69}7071static class MyCacheRequest extends CacheRequest {72private OutputStream buf = null;7374public MyCacheRequest(OutputStream out) {75buf = out;76}7778public OutputStream getBody() throws IOException {79return buf;80}8182/**83* Aborts the attempt to cache the response. If an IOException is84* encountered while reading the response or writing to the cache,85* the current cache store operation will be abandoned.86*/87public void abort() {88}8990}9192static class MyResponseCache extends ResponseCache {93private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);9495public MyResponseCache() {96}9798public CacheRequest put(URI uri, URLConnection conn) throws IOException {99return new MyCacheRequest(buf);100}101102public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException {103return null;104}105106public byte[] getBuffer() {107return buf.toByteArray();108}109}110111static HttpServer server;112113public static void main(String[] args) throws Exception {114MyResponseCache cache = new MyResponseCache();115try {116InetAddress loopback = InetAddress.getLoopbackAddress();117ResponseCache.setDefault(cache);118server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);119server.createContext("/", new ResponseCacheStream());120server.setExecutor(Executors.newSingleThreadExecutor());121server.start();122System.out.println("Server: listening on port: " + server.getAddress().getPort());123URL url = URIBuilder.newBuilder()124.scheme("http")125.loopback()126.port(server.getAddress().getPort())127.path("/")128.toURL();129System.out.println ("Client: connecting to " + url);130HttpURLConnection urlc = (HttpURLConnection)url.openConnection();131InputStream is = urlc.getInputStream();132System.out.println("is is " + is.getClass() + ". And markSupported: " + is.markSupported());133if (is.markSupported()) {134byte[] b = new byte[1024];135byte[] b2 = new byte[32];136int len;137int count;138is.mark(10);139len = is.read(b, 0, 10);140is.reset();141len = 0;142count = 0;143do {144len = is.read(b, count, 40 - count);145if (len > 0)146count += len;147} while (len > 0);148is.mark(20);149len = is.read(b2, 0, 20);150is.reset();151len = is.read(b, count, 10);152count += len;153is.mark(20);154len = is.read(b2, 0, 20);155is.reset();156do {157len = is.read(b, count, 1024 - count);158if (len > 0)159count += len;160} while (len > 0);161is.close();162String s1 = new String(b, 0 , count);163String s2 = new String(cache.getBuffer(), 0 , count);164if (! s1.equals(s2))165throw new RuntimeException("cache got corrupted!");166}167} catch (Exception e) {168if (server != null) {169server.stop(1);170}171throw e;172}173server.stop(1);174}175}176177178