Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/B6660405.java
38867 views
1
/*
2
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
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
*
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.
22
*/
23
24
/*
25
* @test
26
* @bug 6660405
27
* @summary HttpURLConnection returns the wrong InputStream
28
*/
29
30
import java.net.*;
31
import java.util.*;
32
import java.io.*;
33
import com.sun.net.httpserver.*;
34
import java.util.concurrent.Executors;
35
import java.util.concurrent.ExecutorService;
36
37
public class B6660405
38
{
39
com.sun.net.httpserver.HttpServer httpServer;
40
ExecutorService executorService;
41
42
static class MyCacheResponse extends CacheResponse {
43
private byte[] buf = new byte[1024];
44
45
public MyCacheResponse() {
46
}
47
48
@Override
49
public Map<String, List<String>> getHeaders() throws IOException
50
{
51
Map<String, List<String>> h = new HashMap<String, List<String>>();
52
ArrayList<String> l = new ArrayList<String>();
53
l.add("HTTP/1.1 200 OK");
54
h.put(null, l);
55
l = new ArrayList<String>();
56
l.add("1024");
57
h.put("Content-Length", l);
58
return h;
59
}
60
61
@Override
62
public InputStream getBody() throws IOException
63
{
64
return new ByteArrayInputStream(buf);
65
}
66
67
}
68
static class MyResponseCache extends ResponseCache {
69
70
public MyResponseCache() {
71
}
72
73
@Override
74
public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException
75
{
76
if (uri.getPath().equals("/redirect/index.html")) {
77
return new MyCacheResponse();
78
}
79
return null;
80
}
81
82
@Override
83
public CacheRequest put(URI uri, URLConnection conn) throws IOException
84
{
85
return null;
86
}
87
88
}
89
90
public static void main(String[] args)
91
{
92
new B6660405();
93
}
94
95
public B6660405()
96
{
97
try {
98
startHttpServer();
99
doClient();
100
} catch (IOException ioe) {
101
System.err.println(ioe);
102
}
103
}
104
105
void doClient() {
106
ResponseCache.setDefault(new MyResponseCache());
107
try {
108
InetSocketAddress address = httpServer.getAddress();
109
110
// GET Request
111
URL url = new URL("http://localhost:" + address.getPort() + "/test/index.html");
112
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
113
int code = uc.getResponseCode();
114
System.err.println("response code = " + code);
115
int l = uc.getContentLength();
116
System.err.println("content-length = " + l);
117
InputStream in = uc.getInputStream();
118
int i = 0;
119
// Read till end of stream
120
do {
121
i = in.read();
122
} while (i != -1);
123
in.close();
124
} catch (IOException e) {
125
throw new RuntimeException("Got the wrong InputStream after checking headers");
126
} finally {
127
httpServer.stop(1);
128
executorService.shutdown();
129
}
130
}
131
132
/**
133
* Http Server
134
*/
135
public void startHttpServer() throws IOException {
136
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
137
138
// create HttpServer context
139
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
140
141
executorService = Executors.newCachedThreadPool();
142
httpServer.setExecutor(executorService);
143
httpServer.start();
144
}
145
146
class MyHandler implements HttpHandler {
147
public void handle(HttpExchange t) throws IOException {
148
InputStream is = t.getRequestBody();
149
Headers reqHeaders = t.getRequestHeaders();
150
Headers resHeaders = t.getResponseHeaders();
151
152
int i = 0;
153
// Read till end of stream
154
do {
155
i = is.read();
156
} while (i != -1);
157
is.close();
158
resHeaders.add("Location", "http://foo.bar/redirect/index.html");
159
t.sendResponseHeaders(302, -1);
160
t.close();
161
}
162
}
163
}
164
165