Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/SetIfModifiedSince.java
41159 views
1
/*
2
* Copyright (c) 1999, 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 4213164 8172253
26
* @library /test/lib
27
* @summary setIfModifiedSince method in HttpURLConnection sometimes fails
28
*/
29
import java.util.*;
30
import java.io.*;
31
import java.net.*;
32
33
import jdk.test.lib.net.URIBuilder;
34
35
public class SetIfModifiedSince implements Runnable {
36
37
ServerSocket serverSock;
38
39
public void run() {
40
try {
41
Socket s = serverSock.accept();
42
InputStream in = s.getInputStream();
43
byte b[] = new byte[4096];
44
45
// assume we read the entire http request
46
// (bad assumption but okay for test case)
47
int nread = in.read(b);
48
49
// check the date format by the position of the comma
50
String request = new String(b, 0, nread);
51
int pos = request.indexOf("If-Modified-Since:");
52
int respCode = 200;
53
if (pos != -1) {
54
pos += "If-Modified-Since:".length() + 4;
55
if (pos < nread) {
56
if (request.charAt(pos) == (char)',') {
57
respCode = 304;
58
}
59
}
60
}
61
62
OutputStream o = s.getOutputStream();
63
if (respCode == 304) {
64
o.write( "HTTP/1.1 304 Not Modified".getBytes() );
65
} else {
66
o.write( "HTTP/1.1 200 OK".getBytes() );
67
}
68
o.write( (byte)'\r' );
69
o.write( (byte)'\n' );
70
o.write( (byte)'\r' );
71
o.write( (byte)'\n' );
72
o.flush();
73
74
} catch (Exception e) { }
75
}
76
77
78
public SetIfModifiedSince() throws Exception {
79
80
serverSock = new ServerSocket();
81
serverSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
82
int port = serverSock.getLocalPort();
83
84
Thread thr = new Thread(this);
85
thr.start();
86
87
Date date = new Date(new Date().getTime()-1440000); // this time yesterday
88
URL url;
89
HttpURLConnection con;
90
91
//url = new URL(args[0]);
92
url = URIBuilder.newBuilder()
93
.scheme("http")
94
.loopback()
95
.port(port)
96
.path("/anything")
97
.toURL();
98
con = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
99
100
con.setIfModifiedSince(date.getTime());
101
con.connect();
102
int ret = con.getResponseCode();
103
104
if (ret == 304) {
105
System.out.println("Success!");
106
} else {
107
throw new RuntimeException("Test failed! Http return code using setIfModified method is:"+ret+"\nNOTE:some web servers are not implemented according to RFC, thus a failed test does not necessarily indicate a bug in setIfModifiedSince method");
108
}
109
}
110
111
public static void main(String args[]) throws Exception {
112
new SetIfModifiedSince();
113
}
114
}
115
116