Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B8211420.java
41154 views
1
/*
2
* Copyright (c) 2018, 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 8211420
27
* @library /test/lib
28
* @run main/othervm B8211420
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true B8211420
30
* @summary
31
*/
32
33
import com.sun.net.httpserver.*;
34
35
import java.util.*;
36
import java.util.concurrent.*;
37
import java.util.logging.*;
38
import java.io.*;
39
import java.net.*;
40
41
import jdk.test.lib.net.URIBuilder;
42
43
public class B8211420 {
44
45
public static void main(String[] args) throws Exception {
46
Logger logger = Logger.getLogger("com.sun.net.httpserver");
47
ConsoleHandler c = new ConsoleHandler();
48
c.setLevel(Level.WARNING);
49
logger.addHandler(c);
50
logger.setLevel(Level.WARNING);
51
Handler handler = new Handler();
52
InetAddress loopback = InetAddress.getLoopbackAddress();
53
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
54
HttpServer server = HttpServer.create(addr, 0);
55
HttpContext ctx = server.createContext("/test", handler);
56
ExecutorService executor = Executors.newCachedThreadPool();
57
server.setExecutor(executor);
58
server.start();
59
60
URL url = URIBuilder.newBuilder()
61
.scheme("http")
62
.host(server.getAddress().getAddress())
63
.port(server.getAddress().getPort())
64
.path("/test/foo.html")
65
.toURL();
66
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
67
try {
68
InputStream is = urlc.getInputStream();
69
while (is.read()!= -1) ;
70
is.close ();
71
String prop = urlc.getHeaderField("Content-length");
72
System.out.println("Content-length = " + prop + " should be null");
73
if (prop != null)
74
throw new RuntimeException("Content-length was present");
75
76
urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
77
is = urlc.getInputStream();
78
while (is.read()!= -1) ;
79
is.close();
80
if (urlc.getResponseCode() != 304) // expected for 2nd test
81
throw new RuntimeException("wrong response code");
82
String clen = urlc.getHeaderField("Content-length");
83
System.out.println("Content-length = " + clen + " should be 99");
84
System.out.println("len = " + clen.length());
85
if (clen == null || !clen.equals("99"))
86
throw new RuntimeException("Content-length not present or has wrong value");
87
System.out.println("OK");
88
} finally {
89
server.stop(2);
90
executor.shutdown();
91
}
92
}
93
94
public static boolean error = false;
95
96
static class Handler implements HttpHandler {
97
volatile int invocation = 1;
98
public void handle (HttpExchange t)
99
throws IOException
100
{
101
InputStream is = t.getRequestBody();
102
Headers map = t.getRequestHeaders();
103
Headers rmap = t.getResponseHeaders();
104
while (is.read() != -1) ;
105
is.close();
106
if (invocation++ == 1) {
107
// send a 204 response with no body
108
t.sendResponseHeaders(204, -1);
109
t.close();
110
} else {
111
// send a 304 response with no body but with content - length
112
rmap.add("Content-length", "99");
113
t.sendResponseHeaders(304, -1);
114
t.close();
115
}
116
}
117
}
118
}
119
120