Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/Test3.java
41152 views
1
/*
2
* Copyright (c) 2005, 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 6270015
27
* @summary Light weight HTTP server
28
* @run main/othervm -Dsun.net.httpserver.idleInterval=4 Test3
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true
30
* -Dsun.net.httpserver.idleInterval=4 Test3
31
*/
32
33
import com.sun.net.httpserver.*;
34
35
import java.util.*;
36
import java.util.concurrent.*;
37
import java.util.regex.*;
38
import java.util.regex.Pattern.*;
39
import java.io.*;
40
import java.net.*;
41
import java.security.*;
42
import javax.net.ssl.*;
43
44
/**
45
* Test pipe-lining over http
46
*/
47
48
public class Test3 extends Test {
49
static int count = 1;
50
public static void main (String[] args) throws Exception {
51
System.out.print ("Test3: ");
52
Handler handler = new Handler();
53
InetAddress loopback = InetAddress.getLoopbackAddress();
54
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
55
HttpServer server = HttpServer.create (addr, 0);
56
int port = server.getAddress().getPort();
57
HttpContext c2 = server.createContext ("/test", handler);
58
c2.getAttributes().put ("name", "This is the http handler");
59
60
ExecutorService exec = Executors.newCachedThreadPool();
61
server.setExecutor (exec);
62
try {
63
server.start ();
64
doClient(port);
65
System.out.println ("OK");
66
} finally {
67
delay();
68
if (server != null)
69
server.stop(2);
70
if (exec != null)
71
exec.shutdown();
72
}
73
}
74
75
static class Handler implements HttpHandler {
76
volatile int invocation = 0;
77
public void handle (HttpExchange t)
78
throws IOException
79
{
80
InputStream is = t.getRequestBody();
81
Headers map = t.getRequestHeaders();
82
Headers rmap = t.getResponseHeaders();
83
int x = invocation ++;
84
rmap.set ("XTest", Integer.toString (x));
85
86
switch (x) {
87
case 0:
88
try {Thread.sleep (2000); } catch (Exception e) {}
89
checkBody (is, body1);
90
break;
91
case 1:
92
try {Thread.sleep (1000); } catch (Exception e) {}
93
checkBody (is, body2);
94
break;
95
case 2:
96
checkBody (is, body3);
97
break;
98
case 3:
99
checkBody (is, body4);
100
break;
101
}
102
t.sendResponseHeaders (200, -1);
103
t.close();
104
}
105
}
106
107
static void checkBody (InputStream is, String cmp) throws IOException {
108
byte [] b = new byte [1024];
109
int count = 0, c;
110
while ((c=is.read(b, count, b.length-count)) != -1) {
111
count+=c;
112
}
113
is.close();
114
String s = new String (b, 0, count, "ISO8859_1");
115
if (!s.equals (cmp)) {
116
throw new RuntimeException ("strings not equal");
117
}
118
}
119
120
static String body1 = "1234567890abcdefghij";
121
static String body2 = "2234567890abcdefghij0123456789";
122
static String body3 = "3wertyuiop";
123
static String body4 = "4234567890";
124
125
static String result =
126
"HTTP/1.1 200 OK.*Xtest: 0.*"+
127
"HTTP/1.1 200 OK.*Xtest: 1.*"+
128
"HTTP/1.1 200 OK.*Xtest: 2.*"+
129
"HTTP/1.1 200 OK.*Xtest: 3.*";
130
131
public static void doClient (int port) throws Exception {
132
String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+
133
"\r\n" +body1 +
134
"GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+
135
"\r\n"+ body2 +
136
"GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+
137
"\r\n"+ body3 +
138
"GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+
139
"\r\n"+body4;
140
141
Socket socket = new Socket (InetAddress.getLoopbackAddress(), port);
142
OutputStream os = socket.getOutputStream();
143
os.write (s.getBytes());
144
InputStream is = socket.getInputStream();
145
int c, count=0;
146
byte[] b = new byte [1024];
147
while ((c=is.read(b, count, b.length-count)) != -1) {
148
count +=c;
149
}
150
is.close();
151
socket.close();
152
s = new String (b,0,count, "ISO8859_1");
153
if (!compare (s, result)) {
154
throw new RuntimeException ("wrong string result");
155
}
156
}
157
158
static boolean compare (String s, String result) {
159
Pattern pattern = Pattern.compile (result,
160
Pattern.DOTALL|Pattern.CASE_INSENSITIVE
161
);
162
Matcher matcher = pattern.matcher (s);
163
return matcher.matches();
164
}
165
}
166
167