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