jdk/test/sun/net/www/protocol/http/ResponseCacheStream.java
author chegar
Fri, 16 Dec 2011 16:09:41 +0000
changeset 11284 2750cfd2352c
parent 5506 202f599c92aa
child 13788 3f38e525f30a
permissions -rw-r--r--
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ../../httptest/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ResponseCacheStream implements HttpCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    void okReply (HttpTransaction req) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        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
    41
        req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            System.out.println ("Server: sent response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static class MyCacheRequest extends CacheRequest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        private OutputStream buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        public MyCacheRequest(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            buf = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public OutputStream getBody() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         * Aborts the attempt to cache the response. If an IOException is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * encountered while reading the response or writing to the cache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         * the current cache store operation will be abandoned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public void abort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static class MyResponseCache extends ResponseCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public MyResponseCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public CacheRequest put(URI uri, URLConnection conn) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return new MyCacheRequest(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public byte[] getBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return buf.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        MyResponseCache cache = new MyResponseCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            ResponseCache.setDefault(cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            server = new HttpServer (new ResponseCacheStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.out.println ("Server: listening on port: " + server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            URL url = new URL ("http://127.0.0.1:"+server.getLocalPort()+"/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.out.println ("Client: connecting to " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.out.println("is is " + is.getClass() + ". And markSupported: " + is.markSupported());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (is.markSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                byte[] b = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                byte[] b2 = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                is.mark(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                len = is.read(b, 0, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    len = is.read(b, count, 40 - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    if (len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                } while (len > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                is.mark(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                len = is.read(b2, 0, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                len = is.read(b, count, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                is.mark(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                len = is.read(b2, 0, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    len = is.read(b, count, 1024 - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    if (len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                } while (len > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                String s1 = new String(b, 0 , count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                String s2 = new String(cache.getBuffer(), 0 , count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (! s1.equals(s2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    throw new RuntimeException("cache got corrupted!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}