Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/CloseOptionHeader.java
41159 views
1
/*
2
* Copyright (c) 2004, 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 6189206
27
* @modules java.base/sun.net.www
28
* @library /test/lib
29
* @run main/othervm -Dhttp.keepAlive=false CloseOptionHeader
30
* @summary HTTP client should set "Connection: close" header in request when keepalive is disabled
31
*/
32
33
import java.net.*;
34
import java.util.*;
35
import java.io.*;
36
import sun.net.www.MessageHeader;
37
import jdk.test.lib.net.URIBuilder;
38
39
public class CloseOptionHeader implements Runnable {
40
static ServerSocket ss;
41
static boolean hasCloseHeader = false;
42
43
/*
44
* "Our" http server
45
*/
46
public void run() {
47
try {
48
Socket s = ss.accept();
49
50
/* check the request to find close connection option header */
51
InputStream is = s.getInputStream ();
52
MessageHeader mh = new MessageHeader(is);
53
String connHeader = mh.findValue("Connection");
54
if (connHeader != null && connHeader.equalsIgnoreCase("close")) {
55
hasCloseHeader = true;
56
}
57
58
PrintStream out = new PrintStream(
59
new BufferedOutputStream(
60
s.getOutputStream() ));
61
62
/* response 200 */
63
out.print("HTTP/1.1 200 OK\r\n");
64
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
65
out.print("Content-Length: 0\r\n");
66
out.print("Connection: close\r\n");
67
out.print("\r\n");
68
out.print("\r\n");
69
70
out.flush();
71
72
s.close();
73
ss.close();
74
} catch (Exception e) {
75
e.printStackTrace();
76
}
77
}
78
79
public static void main(String args[]) throws Exception {
80
Thread tester = new Thread(new CloseOptionHeader());
81
82
/* start the server */
83
InetAddress loopback = InetAddress.getLoopbackAddress();
84
ss = new ServerSocket();
85
ss.bind(new InetSocketAddress(loopback, 0));
86
tester.start();
87
88
/* connect to the server just started
89
* server then check the request to see whether
90
* there is a close connection option header in it
91
*/
92
URL url = URIBuilder.newBuilder()
93
.scheme("http")
94
.host(ss.getInetAddress())
95
.port(ss.getLocalPort())
96
.toURL();
97
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
98
huc.connect();
99
huc.getResponseCode();
100
huc.disconnect();
101
102
tester.join();
103
104
if (!hasCloseHeader) {
105
throw new RuntimeException("Test failed : should see 'close' connection header");
106
}
107
}
108
109
}
110
111