Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/B6226610.java
41161 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 6226610 6973030
27
* @summary HTTP tunnel connections send user headers to proxy
28
* @modules java.base/sun.net.www
29
* @run main/othervm B6226610
30
*/
31
32
/* This class includes a proxy server that processes the HTTP CONNECT request,
33
* and validates that the request does not have the user defined header in it.
34
* The proxy server always returns 400 Bad Request so that the Http client
35
* will not try to proceed with the connection as there is no back end http server.
36
*/
37
38
import java.io.*;
39
import java.net.*;
40
import sun.net.www.MessageHeader;
41
42
public class B6226610 {
43
static HeaderCheckerProxyTunnelServer proxy;
44
45
public static void main(String[] args) throws Exception
46
{
47
proxy = new HeaderCheckerProxyTunnelServer();
48
proxy.start();
49
50
InetAddress localHost = InetAddress.getLocalHost();
51
String hostname = localHost.getHostName();
52
String hostAddress = localHost.getHostAddress();
53
54
try {
55
URL u = new URL("https://" + hostname + "/");
56
System.out.println("Connecting to " + u);
57
InetSocketAddress proxyAddr = InetSocketAddress.createUnresolved(hostAddress, proxy.getLocalPort());
58
java.net.URLConnection c = u.openConnection(new Proxy(Proxy.Type.HTTP, proxyAddr));
59
60
/* I want this header to go to the destination server only, protected
61
* by SSL
62
*/
63
c.setRequestProperty("X-TestHeader", "value");
64
c.connect();
65
66
} catch (IOException e) {
67
if ( e.getMessage().equals("Unable to tunnel through proxy. Proxy returns \"HTTP/1.1 400 Bad Request\"") )
68
{
69
// OK. Proxy will always return 400 so that the main thread can terminate correctly.
70
}
71
else
72
System.out.println(e);
73
} finally {
74
if (proxy != null) proxy.shutdown();
75
}
76
77
if (HeaderCheckerProxyTunnelServer.failed)
78
throw new RuntimeException("Test failed; see output");
79
}
80
}
81
82
class HeaderCheckerProxyTunnelServer extends Thread
83
{
84
public static boolean failed = false;
85
86
private static ServerSocket ss = null;
87
88
// client requesting for a tunnel
89
private Socket clientSocket = null;
90
91
/*
92
* Origin server's address and port that the client
93
* wants to establish the tunnel for communication.
94
*/
95
private InetAddress serverInetAddr;
96
private int serverPort;
97
98
public HeaderCheckerProxyTunnelServer() throws IOException
99
{
100
if (ss == null) {
101
ss = new ServerSocket();
102
ss.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
103
}
104
}
105
106
void shutdown() {
107
try { ss.close(); } catch (IOException e) {}
108
}
109
110
public void run()
111
{
112
try {
113
clientSocket = ss.accept();
114
processRequests();
115
} catch (IOException e) {
116
System.out.println("Proxy Failed: " + e);
117
e.printStackTrace();
118
try {
119
ss.close();
120
}
121
catch (IOException excep) {
122
System.out.println("ProxyServer close error: " + excep);
123
excep.printStackTrace();
124
}
125
}
126
}
127
128
/**
129
* Returns the port on which the proxy is accepting connections.
130
*/
131
public int getLocalPort() {
132
return ss.getLocalPort();
133
}
134
135
/*
136
* Processes the CONNECT request
137
*/
138
private void processRequests() throws IOException
139
{
140
InputStream in = clientSocket.getInputStream();
141
MessageHeader mheader = new MessageHeader(in);
142
String statusLine = mheader.getValue(0);
143
144
if (statusLine.startsWith("CONNECT")) {
145
// retrieve the host and port info from the status-line
146
retrieveConnectInfo(statusLine);
147
148
if (mheader.findValue("X-TestHeader") != null) {
149
System.out.println("Proxy should not receive user defined headers for tunneled requests");
150
failed = true;
151
}
152
153
// 6973030
154
String value;
155
if ((value = mheader.findValue("Proxy-Connection")) == null ||
156
!value.equals("keep-alive")) {
157
System.out.println("Proxy-Connection:keep-alive not being sent");
158
failed = true;
159
}
160
161
//This will allow the main thread to terminate without trying to perform the SSL handshake.
162
send400();
163
164
in.close();
165
clientSocket.close();
166
ss.close();
167
}
168
else {
169
System.out.println("proxy server: processes only "
170
+ "CONNECT method requests, recieved: "
171
+ statusLine);
172
}
173
}
174
175
private void send400() throws IOException
176
{
177
OutputStream out = clientSocket.getOutputStream();
178
PrintWriter pout = new PrintWriter(out);
179
180
pout.println("HTTP/1.1 400 Bad Request");
181
pout.println();
182
pout.flush();
183
}
184
185
private void restart() throws IOException {
186
(new Thread(this)).start();
187
}
188
189
/*
190
* This method retrieves the hostname and port of the destination
191
* that the connect request wants to establish a tunnel for
192
* communication.
193
* The input, connectStr is of the form:
194
* CONNECT server-name:server-port HTTP/1.x
195
*/
196
private void retrieveConnectInfo(String connectStr) throws IOException {
197
198
int starti;
199
int endi;
200
String connectInfo;
201
String serverName = null;
202
try {
203
starti = connectStr.indexOf(' ');
204
endi = connectStr.lastIndexOf(' ');
205
connectInfo = connectStr.substring(starti+1, endi).trim();
206
// retrieve server name and port
207
endi = connectInfo.indexOf(':');
208
serverName = connectInfo.substring(0, endi);
209
serverPort = Integer.parseInt(connectInfo.substring(endi+1));
210
} catch (Exception e) {
211
throw new IOException("Proxy recieved a request: "
212
+ connectStr, e);
213
}
214
serverInetAddr = InetAddress.getByName(serverName);
215
}
216
}
217
218