Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLConnection/SetIfModifiedSince.java
41149 views
1
/*
2
* Copyright (c) 2000, 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
/* @test
25
* @bug 4397096
26
* @library /test/lib
27
* @run main/othervm SetIfModifiedSince
28
* @summary setIfModifiedSince() of HttpURLConnection sets invalid date of default locale
29
*/
30
31
import java.net.*;
32
import java.io.*;
33
import java.util.*;
34
35
import jdk.test.lib.net.URIBuilder;
36
37
public class SetIfModifiedSince {
38
static volatile boolean successfulHeaderCheck = false;
39
static final String MARKER = "A-test-name";
40
41
static class XServer extends Thread {
42
ServerSocket srv;
43
Socket s;
44
InputStream is;
45
OutputStream os;
46
47
XServer (ServerSocket s) {
48
srv = s;
49
}
50
51
Socket getSocket () {
52
return (s);
53
}
54
55
public void run() {
56
boolean foundMarker = false;
57
while (!foundMarker) {
58
String x;
59
try {
60
s = srv.accept();
61
System.out.println("Server: accepting connection from: " + s);
62
is = s.getInputStream ();
63
} catch (IOException io) {
64
System.err.println("Server: Failed to accept connection: " + io);
65
io.printStackTrace();
66
try { srv.close(); } catch (IOException ioc) { }
67
break;
68
}
69
try {
70
BufferedReader r = new BufferedReader(new InputStreamReader(is));
71
os = s.getOutputStream ();
72
boolean foundHeader;
73
while ((x=r.readLine()) != null) {
74
String testname = MARKER + ": ";
75
String header = "If-Modified-Since: ";
76
if (x.startsWith(header)) {
77
foundHeader = true;
78
System.out.println("Server: found header: " + x);
79
if (x.charAt(header.length()) == '?') {
80
s.close ();
81
srv.close (); // or else the HTTPURLConnection will retry
82
throw new RuntimeException
83
("Invalid HTTP date specification");
84
}
85
if (foundMarker) break;
86
} else if (x.startsWith(testname)) {
87
foundMarker = true;
88
System.out.println("Server: found marker: " + x);
89
}
90
}
91
successfulHeaderCheck = true;
92
s.close ();
93
// only close server if connected from this test.
94
if (foundMarker) {
95
srv.close (); // or else the HTTPURLConnection will retry
96
}
97
} catch (IOException e) {}
98
}
99
}
100
}
101
102
public static void main(String[] args) throws Exception {
103
Locale reservedLocale = Locale.getDefault();
104
try {
105
Locale.setDefault(Locale.JAPAN);
106
ServerSocket serversocket = new ServerSocket();
107
serversocket.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
108
int port = serversocket.getLocalPort ();
109
XServer server = new XServer (serversocket);
110
server.start ();
111
Thread.sleep (2000);
112
URL url = URIBuilder.newBuilder()
113
.scheme("http")
114
.loopback()
115
.port(port)
116
.path("/index.html")
117
.toURLUnchecked();
118
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
119
urlc.setRequestProperty(MARKER, "SetIfModifiedSince");
120
urlc.setIfModifiedSince (10000000);
121
InputStream is = urlc.getInputStream ();
122
int i = 0, c;
123
Thread.sleep(5000);
124
if (!successfulHeaderCheck) {
125
throw new RuntimeException("Header check was unsuccessful");
126
}
127
} catch (SocketException ce) {
128
if (!successfulHeaderCheck) {
129
throw ce;
130
}
131
System.out.println("ConnectionException expected on successful check of If-modified-since header");
132
} finally {
133
// restore the reserved locale
134
Locale.setDefault(reservedLocale);
135
}
136
137
}
138
}
139
140