author | aeubanks |
Wed, 27 Mar 2019 09:06:43 -0700 | |
changeset 54314 | 46cf212cdcca |
parent 47216 | 71c04702a3d5 |
child 54681 | edd709e64ea1 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
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 | 28 |
* @modules java.base/sun.net.www |
2 | 29 |
* @library ../../httptest/ |
13788 | 30 |
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction |
2 | 31 |
* @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream |
32 |
* @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load |
|
33 |
*/ |
|
34 |
||
35 |
import java.net.*; |
|
36 |
import java.io.*; |
|
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 | 39 |
|
40 |
public class ResponseCacheStream implements HttpCallback { |
|
41 |
||
42 |
void okReply (HttpTransaction req) throws IOException { |
|
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."); |
|
44 |
req.sendResponse (200, "Ok"); |
|
45 |
System.out.println ("Server: sent response"); |
|
46 |
req.orderlyClose(); |
|
47 |
} |
|
48 |
||
49 |
public void request (HttpTransaction req) { |
|
50 |
try { |
|
51 |
okReply (req); |
|
52 |
} catch (IOException e) { |
|
53 |
e.printStackTrace(); |
|
54 |
} |
|
55 |
} |
|
56 |
||
57 |
static class MyCacheRequest extends CacheRequest { |
|
58 |
private OutputStream buf = null; |
|
59 |
||
60 |
public MyCacheRequest(OutputStream out) { |
|
61 |
buf = out; |
|
62 |
} |
|
63 |
||
64 |
public OutputStream getBody() throws IOException { |
|
65 |
return buf; |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Aborts the attempt to cache the response. If an IOException is |
|
70 |
* encountered while reading the response or writing to the cache, |
|
71 |
* the current cache store operation will be abandoned. |
|
72 |
*/ |
|
73 |
public void abort() { |
|
74 |
} |
|
75 |
||
76 |
} |
|
77 |
||
78 |
static class MyResponseCache extends ResponseCache { |
|
79 |
private ByteArrayOutputStream buf = new ByteArrayOutputStream(1024); |
|
80 |
||
81 |
public MyResponseCache() { |
|
82 |
} |
|
83 |
||
84 |
public CacheRequest put(URI uri, URLConnection conn) throws IOException { |
|
85 |
return new MyCacheRequest(buf); |
|
86 |
} |
|
87 |
||
88 |
public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException { |
|
89 |
return null; |
|
90 |
} |
|
91 |
||
92 |
public byte[] getBuffer() { |
|
93 |
return buf.toByteArray(); |
|
94 |
} |
|
95 |
} |
|
96 |
||
13788 | 97 |
static TestHttpServer server; |
2 | 98 |
|
99 |
public static void main(String[] args) throws Exception { |
|
100 |
MyResponseCache cache = new MyResponseCache(); |
|
101 |
try { |
|
102 |
ResponseCache.setDefault(cache); |
|
13788 | 103 |
server = new TestHttpServer (new ResponseCacheStream()); |
2 | 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 | 111 |
System.out.println ("Client: connecting to " + url); |
112 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); |
|
113 |
InputStream is = urlc.getInputStream(); |
|
114 |
System.out.println("is is " + is.getClass() + ". And markSupported: " + is.markSupported()); |
|
115 |
if (is.markSupported()) { |
|
116 |
byte[] b = new byte[1024]; |
|
117 |
byte[] b2 = new byte[32]; |
|
118 |
int len; |
|
119 |
int count; |
|
120 |
is.mark(10); |
|
121 |
len = is.read(b, 0, 10); |
|
122 |
is.reset(); |
|
123 |
len = 0; |
|
124 |
count = 0; |
|
125 |
do { |
|
126 |
len = is.read(b, count, 40 - count); |
|
127 |
if (len > 0) |
|
128 |
count += len; |
|
129 |
} while (len > 0); |
|
130 |
is.mark(20); |
|
131 |
len = is.read(b2, 0, 20); |
|
132 |
is.reset(); |
|
133 |
len = is.read(b, count, 10); |
|
134 |
count += len; |
|
135 |
is.mark(20); |
|
136 |
len = is.read(b2, 0, 20); |
|
137 |
is.reset(); |
|
138 |
do { |
|
139 |
len = is.read(b, count, 1024 - count); |
|
140 |
if (len > 0) |
|
141 |
count += len; |
|
142 |
} while (len > 0); |
|
143 |
is.close(); |
|
144 |
String s1 = new String(b, 0 , count); |
|
145 |
String s2 = new String(cache.getBuffer(), 0 , count); |
|
146 |
if (! s1.equals(s2)) |
|
147 |
throw new RuntimeException("cache got corrupted!"); |
|
148 |
} |
|
149 |
} catch (Exception e) { |
|
150 |
if (server != null) { |
|
151 |
server.terminate(); |
|
152 |
} |
|
153 |
throw e; |
|
154 |
} |
|
155 |
server.terminate(); |
|
156 |
} |
|
157 |
} |