Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test3.java
38855 views
1
/*
2
* Copyright (c) 2005, 2006, 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
*/
30
31
import com.sun.net.httpserver.*;
32
33
import java.util.*;
34
import java.util.concurrent.*;
35
import java.util.regex.*;
36
import java.util.regex.Pattern.*;
37
import java.io.*;
38
import java.net.*;
39
import java.security.*;
40
import javax.net.ssl.*;
41
42
/**
43
* Test pipe-lining over http
44
*/
45
46
public class Test3 extends Test {
47
static int count = 1;
48
public static void main (String[] args) throws Exception {
49
System.out.print ("Test3: ");
50
Handler handler = new Handler();
51
InetSocketAddress addr = new InetSocketAddress (0);
52
HttpServer server = HttpServer.create (addr, 0);
53
int port = server.getAddress().getPort();
54
HttpContext c2 = server.createContext ("/test", handler);
55
c2.getAttributes().put ("name", "This is the http handler");
56
57
ExecutorService exec = Executors.newCachedThreadPool();
58
server.setExecutor (exec);
59
try {
60
server.start ();
61
doClient(port);
62
System.out.println ("OK");
63
} finally {
64
delay();
65
if (server != null)
66
server.stop(2);
67
if (exec != null)
68
exec.shutdown();
69
}
70
}
71
72
static class Handler implements HttpHandler {
73
volatile int invocation = 0;
74
public void handle (HttpExchange t)
75
throws IOException
76
{
77
InputStream is = t.getRequestBody();
78
Headers map = t.getRequestHeaders();
79
Headers rmap = t.getResponseHeaders();
80
int x = invocation ++;
81
rmap.set ("XTest", Integer.toString (x));
82
83
switch (x) {
84
case 0:
85
try {Thread.sleep (2000); } catch (Exception e) {}
86
checkBody (is, body1);
87
break;
88
case 1:
89
try {Thread.sleep (1000); } catch (Exception e) {}
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 ("localhost", 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
throw new RuntimeException ("wrong string result");
152
}
153
}
154
155
static boolean compare (String s, String result) {
156
Pattern pattern = Pattern.compile (result,
157
Pattern.DOTALL|Pattern.CASE_INSENSITIVE
158
);
159
Matcher matcher = pattern.matcher (s);
160
return matcher.matches();
161
}
162
}
163
164