2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1999, 2000, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
@bug 4213164
|
|
26 |
@summary setIfModifiedSince mehtod in HttpURLConnection sometimes fails
|
|
27 |
*/
|
|
28 |
import java.util.*;
|
|
29 |
import java.io.*;
|
|
30 |
import java.net.*;
|
|
31 |
import java.text.*;
|
|
32 |
|
|
33 |
public class SetIfModifiedSince implements Runnable {
|
|
34 |
|
|
35 |
ServerSocket serverSock;
|
|
36 |
|
|
37 |
public void run() {
|
|
38 |
try {
|
|
39 |
Socket s = serverSock.accept();
|
|
40 |
InputStream in = s.getInputStream();
|
|
41 |
byte b[] = new byte[4096];
|
|
42 |
|
|
43 |
// assume we read the entire http request
|
|
44 |
// (bad assumption but okay for test case)
|
|
45 |
int nread = in.read(b);
|
|
46 |
|
|
47 |
// check the date format by the position of the comma
|
|
48 |
String request = new String(b, 0, nread);
|
|
49 |
int pos = request.indexOf("If-Modified-Since:");
|
|
50 |
int respCode = 200;
|
|
51 |
if (pos != -1) {
|
|
52 |
pos += "If-Modified-Since:".length() + 4;
|
|
53 |
if (pos < nread) {
|
|
54 |
if (request.charAt(pos) == (char)',') {
|
|
55 |
respCode = 304;
|
|
56 |
}
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 |
OutputStream o = s.getOutputStream();
|
|
61 |
if (respCode == 304) {
|
|
62 |
o.write( "HTTP/1.1 304 Not Modified".getBytes() );
|
|
63 |
} else {
|
|
64 |
o.write( "HTTP/1.1 200 OK".getBytes() );
|
|
65 |
}
|
|
66 |
o.write( (byte)'\r' );
|
|
67 |
o.write( (byte)'\n' );
|
|
68 |
o.write( (byte)'\r' );
|
|
69 |
o.write( (byte)'\n' );
|
|
70 |
o.flush();
|
|
71 |
|
|
72 |
} catch (Exception e) { }
|
|
73 |
}
|
|
74 |
|
|
75 |
|
|
76 |
public SetIfModifiedSince() throws Exception {
|
|
77 |
|
|
78 |
serverSock = new ServerSocket(0);
|
|
79 |
int port = serverSock.getLocalPort();
|
|
80 |
|
|
81 |
Thread thr = new Thread(this);
|
|
82 |
thr.start();
|
|
83 |
|
|
84 |
Date date = new Date(new Date().getTime()-1440000); // this time yesterday
|
|
85 |
URL url;
|
|
86 |
HttpURLConnection con;
|
|
87 |
|
|
88 |
//url = new URL(args[0]);
|
|
89 |
url = new URL("http://localhost:" + String.valueOf(port) +
|
|
90 |
"/anything");
|
|
91 |
con = (HttpURLConnection)url.openConnection();
|
|
92 |
|
|
93 |
con.setIfModifiedSince(date.getTime());
|
|
94 |
con.connect();
|
|
95 |
int ret = con.getResponseCode();
|
|
96 |
|
|
97 |
if (ret == 304) {
|
|
98 |
System.out.println("Success!");
|
|
99 |
} else {
|
|
100 |
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");
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
public static void main(String args[]) throws Exception {
|
|
105 |
new SetIfModifiedSince();
|
|
106 |
}
|
|
107 |
}
|