Path: blob/master/test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java
41159 views
/*1* Copyright (c) 2005, 2019, 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* @modules java.base/sun.net.www28* @library ../../httptest/29* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction30* @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream31* @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load32*/3334import java.net.*;35import java.io.*;36import java.util.*;37import jdk.test.lib.net.URIBuilder;3839public class ResponseCacheStream implements HttpCallback {4041void okReply (HttpTransaction req) throws IOException {42req.setResponseEntityBody ("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");43req.sendResponse (200, "Ok");44System.out.println ("Server: sent response");45req.orderlyClose();46}4748public void request (HttpTransaction req) {49try {50okReply (req);51} catch (IOException e) {52e.printStackTrace();53}54}5556static class MyCacheRequest extends CacheRequest {57private OutputStream buf = null;5859public MyCacheRequest(OutputStream out) {60buf = out;61}6263public OutputStream getBody() throws IOException {64return buf;65}6667/**68* Aborts the attempt to cache the response. If an IOException is69* encountered while reading the response or writing to the cache,70* the current cache store operation will be abandoned.71*/72public void abort() {73}7475}7677static class MyResponseCache extends ResponseCache {78private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);7980public MyResponseCache() {81}8283public CacheRequest put(URI uri, URLConnection conn) throws IOException {84return new MyCacheRequest(buf);85}8687public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException {88return null;89}9091public byte[] getBuffer() {92return buf.toByteArray();93}94}9596static TestHttpServer server;9798public static void main(String[] args) throws Exception {99MyResponseCache cache = new MyResponseCache();100try {101InetAddress loopback = InetAddress.getLoopbackAddress();102ResponseCache.setDefault(cache);103server = new TestHttpServer (new ResponseCacheStream(), loopback, 0);104System.out.println ("Server: listening on port: " + server.getLocalPort());105URL url = URIBuilder.newBuilder()106.scheme("http")107.loopback()108.port(server.getLocalPort())109.path("/")110.toURL();111System.out.println ("Client: connecting to " + url);112HttpURLConnection urlc = (HttpURLConnection)url.openConnection();113InputStream is = urlc.getInputStream();114System.out.println("is is " + is.getClass() + ". And markSupported: " + is.markSupported());115if (is.markSupported()) {116byte[] b = new byte[1024];117byte[] b2 = new byte[32];118int len;119int count;120is.mark(10);121len = is.read(b, 0, 10);122is.reset();123len = 0;124count = 0;125do {126len = is.read(b, count, 40 - count);127if (len > 0)128count += len;129} while (len > 0);130is.mark(20);131len = is.read(b2, 0, 20);132is.reset();133len = is.read(b, count, 10);134count += len;135is.mark(20);136len = is.read(b2, 0, 20);137is.reset();138do {139len = is.read(b, count, 1024 - count);140if (len > 0)141count += len;142} while (len > 0);143is.close();144String s1 = new String(b, 0 , count);145String s2 = new String(cache.getBuffer(), 0 , count);146if (! s1.equals(s2))147throw new RuntimeException("cache got corrupted!");148}149} catch (Exception e) {150if (server != null) {151server.terminate();152}153throw e;154}155server.terminate();156}157}158159160