Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/MissingTrailingSpace.java
41152 views
1
/*
2
* Copyright (c) 2015, 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 8068795
27
* @summary HttpServer missing tailing space for some response codes
28
* @run main MissingTrailingSpace
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true MissingTrailingSpace
30
* @author [email protected]
31
*/
32
33
import java.net.InetAddress;
34
import java.net.InetSocketAddress;
35
import java.io.InputStreamReader;
36
import java.io.IOException;
37
import java.io.BufferedReader;
38
import java.io.OutputStreamWriter;
39
import java.io.PrintWriter;
40
import java.net.Socket;
41
import java.util.concurrent.ExecutorService;
42
import java.util.concurrent.Executors;
43
import com.sun.net.httpserver.HttpExchange;
44
import com.sun.net.httpserver.HttpHandler;
45
import com.sun.net.httpserver.HttpServer;
46
47
public class MissingTrailingSpace {
48
49
private static final int noMsgCode = 207;
50
private static final String someContext = "/context";
51
52
public static void main(String[] args) throws Exception {
53
InetAddress loopback = InetAddress.getLoopbackAddress();
54
HttpServer server = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
55
try {
56
server.setExecutor(Executors.newFixedThreadPool(1));
57
server.createContext(someContext, new HttpHandler() {
58
@Override
59
public void handle(HttpExchange msg) {
60
try {
61
try {
62
msg.sendResponseHeaders(noMsgCode, -1);
63
} catch(IOException ioe) {
64
ioe.printStackTrace();
65
}
66
} finally {
67
msg.close();
68
}
69
}
70
});
71
server.start();
72
System.out.println("Server started at port "
73
+ server.getAddress().getPort());
74
75
runRawSocketHttpClient(loopback, server.getAddress().getPort());
76
} finally {
77
((ExecutorService)server.getExecutor()).shutdown();
78
server.stop(0);
79
}
80
System.out.println("Server finished.");
81
}
82
83
static void runRawSocketHttpClient(InetAddress address, int port)
84
throws Exception
85
{
86
Socket socket = null;
87
PrintWriter writer = null;
88
BufferedReader reader = null;
89
final String CRLF = "\r\n";
90
try {
91
socket = new Socket(address, port);
92
writer = new PrintWriter(new OutputStreamWriter(
93
socket.getOutputStream()));
94
System.out.println("Client connected by socket: " + socket);
95
96
writer.print("GET " + someContext + "/ HTTP/1.1" + CRLF);
97
writer.print("User-Agent: Java/"
98
+ System.getProperty("java.version")
99
+ CRLF);
100
writer.print("Host: " + address.getHostName() + CRLF);
101
writer.print("Accept: */*" + CRLF);
102
writer.print("Connection: keep-alive" + CRLF);
103
writer.print(CRLF); // Important, else the server will expect that
104
// there's more into the request.
105
writer.flush();
106
System.out.println("Client wrote rquest to socket: " + socket);
107
108
reader = new BufferedReader(new InputStreamReader(
109
socket.getInputStream()));
110
System.out.println("Client start reading from server:" );
111
String line = reader.readLine();
112
if ( !line.endsWith(" ") ) {
113
throw new RuntimeException("respond to unknown code "
114
+ noMsgCode
115
+ " doesn't return space at the end of the first header.\n"
116
+ "Should be: " + "\"" + line + " \""
117
+ ", but returns: " + "\"" + line + "\".");
118
}
119
for (; line != null; line = reader.readLine()) {
120
if (line.isEmpty()) {
121
break;
122
}
123
System.out.println("\"" + line + "\"");
124
}
125
System.out.println("Client finished reading from server" );
126
} finally {
127
if (reader != null)
128
try {
129
reader.close();
130
} catch (IOException logOrIgnore) {
131
logOrIgnore.printStackTrace();
132
}
133
if (writer != null) {
134
writer.close();
135
}
136
if (socket != null) {
137
try {
138
socket.close();
139
} catch (IOException logOrIgnore) {
140
logOrIgnore.printStackTrace();
141
}
142
}
143
}
144
System.out.println("Client finished." );
145
}
146
}
147
148