Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6529200.java
41155 views
1
/*
2
* Copyright (c) 2007, 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 6529200
27
* @run main/othervm B6529200
28
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6529200
29
* @summary lightweight http server does not work with http1.0 clients
30
*/
31
32
import com.sun.net.httpserver.*;
33
34
import java.util.*;
35
import java.util.concurrent.*;
36
import java.io.*;
37
import java.net.*;
38
import java.security.*;
39
import java.security.cert.*;
40
import javax.net.ssl.*;
41
42
public class B6529200 {
43
44
public static void main (String[] args) throws Exception {
45
Handler handler = new Handler();
46
InetAddress loopback = InetAddress.getLoopbackAddress();
47
InetSocketAddress addr = new InetSocketAddress (loopback, 0);
48
HttpServer server = HttpServer.create (addr, 0);
49
HttpContext ctx = server.createContext ("/test", handler);
50
51
ExecutorService executor = Executors.newCachedThreadPool();
52
server.setExecutor (executor);
53
server.start ();
54
55
/* test 1: keep-alive */
56
57
Socket sock = new Socket (loopback, server.getAddress().getPort());
58
OutputStream os = sock.getOutputStream();
59
System.out.println ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");
60
os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());
61
os.flush();
62
InputStream is = sock.getInputStream();
63
StringBuffer s = new StringBuffer();
64
boolean finished = false;
65
66
sock.setSoTimeout (10 * 1000);
67
try {
68
while (!finished) {
69
char c = (char) is.read();
70
s.append (c);
71
finished = s.indexOf ("\r\n\r\nhello") != -1;
72
/* test will timeout otherwise */
73
}
74
} catch (SocketTimeoutException e) {
75
server.stop (2);
76
executor.shutdown ();
77
throw new RuntimeException ("Test failed in test1");
78
}
79
80
System.out.println (new String (s));
81
82
/* test 2: even though we request keep-alive, server must close
83
* because it is sending unknown content length response */
84
85
System.out.println("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");
86
os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());
87
os.flush();
88
int i=0,c;
89
byte [] buf = new byte [8*1024];
90
try {
91
while ((c=is.read()) != -1) {
92
buf[i++] = (byte)c;
93
}
94
} catch (SocketTimeoutException e) {
95
server.stop (2);
96
executor.shutdown ();
97
throw new RuntimeException ("Test failed in test2");
98
}
99
100
String ss = new String (buf, "ISO-8859-1");
101
if (ss.indexOf ("\r\n\r\nhello world") == -1) {
102
server.stop (2);
103
executor.shutdown ();
104
throw new RuntimeException ("Test failed in test2: wrong string");
105
}
106
System.out.println (ss);
107
is.close ();
108
server.stop (2);
109
executor.shutdown();
110
}
111
112
113
static class Handler implements HttpHandler {
114
int invocation = 1;
115
public void handle (HttpExchange t)
116
throws IOException
117
{
118
InputStream is;
119
OutputStream os;
120
switch (invocation++) {
121
case 1:
122
is = t.getRequestBody();
123
while (is.read() != -1) ;
124
is.close();
125
t.sendResponseHeaders (200, "hello".length());
126
os = t.getResponseBody();
127
os.write ("hello".getBytes());
128
os.close();
129
break;
130
case 2:
131
is = t.getRequestBody();
132
while (is.read() != -1) ;
133
is.close();
134
t.sendResponseHeaders (200, 0);
135
os = t.getResponseBody();
136
os.write ("hello world".getBytes());
137
os.close();
138
break;
139
}
140
}
141
}
142
}
143
144