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