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