test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java
author aeubanks
Wed, 27 Mar 2019 09:06:43 -0700
changeset 54314 46cf212cdcca
parent 47216 71c04702a3d5
child 54681 edd709e64ea1
permissions -rw-r--r--
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13788
diff changeset
     2
 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6262486
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    27
 * @library /test/lib
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 14342
diff changeset
    28
 * @modules java.base/sun.net.www
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ../../httptest/
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    30
 * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    38
import jdk.test.lib.net.URIBuilder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class ResponseCacheStream implements HttpCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    void okReply (HttpTransaction req) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        req.setResponseEntityBody ("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            System.out.println ("Server: sent response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static class MyCacheRequest extends CacheRequest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        private OutputStream buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        public MyCacheRequest(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            buf = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public OutputStream getBody() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         * Aborts the attempt to cache the response. If an IOException is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         * encountered while reading the response or writing to the cache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         * the current cache store operation will be abandoned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public void abort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static class MyResponseCache extends ResponseCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public MyResponseCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public CacheRequest put(URI uri, URLConnection conn) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return new MyCacheRequest(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public byte[] getBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return buf.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    97
    static TestHttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        MyResponseCache cache = new MyResponseCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            ResponseCache.setDefault(cache);
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
   103
            server = new TestHttpServer (new ResponseCacheStream());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.out.println ("Server: listening on port: " + server.getLocalPort());
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   105
            URL url = URIBuilder.newBuilder()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   106
                .scheme("http")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   107
                .loopback()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   108
                .port(server.getLocalPort())
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   109
                .path("/")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   110
                .toURL();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.out.println ("Client: connecting to " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.out.println("is is " + is.getClass() + ". And markSupported: " + is.markSupported());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (is.markSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                byte[] b = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                byte[] b2 = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                is.mark(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                len = is.read(b, 0, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    len = is.read(b, count, 40 - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    if (len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                } while (len > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                is.mark(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                len = is.read(b2, 0, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                len = is.read(b, count, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                is.mark(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                len = is.read(b2, 0, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    len = is.read(b, count, 1024 - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    if (len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                } while (len > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                String s1 = new String(b, 0 , count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                String s2 = new String(cache.getBuffer(), 0 , count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (! s1.equals(s2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    throw new RuntimeException("cache got corrupted!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}