Path: blob/master/test/jdk/sun/net/www/protocol/http/SetIfModifiedSince.java
41159 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4213164 817225325* @library /test/lib26* @summary setIfModifiedSince method in HttpURLConnection sometimes fails27*/28import java.util.*;29import java.io.*;30import java.net.*;3132import jdk.test.lib.net.URIBuilder;3334public class SetIfModifiedSince implements Runnable {3536ServerSocket serverSock;3738public void run() {39try {40Socket s = serverSock.accept();41InputStream in = s.getInputStream();42byte b[] = new byte[4096];4344// assume we read the entire http request45// (bad assumption but okay for test case)46int nread = in.read(b);4748// check the date format by the position of the comma49String request = new String(b, 0, nread);50int pos = request.indexOf("If-Modified-Since:");51int respCode = 200;52if (pos != -1) {53pos += "If-Modified-Since:".length() + 4;54if (pos < nread) {55if (request.charAt(pos) == (char)',') {56respCode = 304;57}58}59}6061OutputStream o = s.getOutputStream();62if (respCode == 304) {63o.write( "HTTP/1.1 304 Not Modified".getBytes() );64} else {65o.write( "HTTP/1.1 200 OK".getBytes() );66}67o.write( (byte)'\r' );68o.write( (byte)'\n' );69o.write( (byte)'\r' );70o.write( (byte)'\n' );71o.flush();7273} catch (Exception e) { }74}757677public SetIfModifiedSince() throws Exception {7879serverSock = new ServerSocket();80serverSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));81int port = serverSock.getLocalPort();8283Thread thr = new Thread(this);84thr.start();8586Date date = new Date(new Date().getTime()-1440000); // this time yesterday87URL url;88HttpURLConnection con;8990//url = new URL(args[0]);91url = URIBuilder.newBuilder()92.scheme("http")93.loopback()94.port(port)95.path("/anything")96.toURL();97con = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);9899con.setIfModifiedSince(date.getTime());100con.connect();101int ret = con.getResponseCode();102103if (ret == 304) {104System.out.println("Success!");105} else {106throw 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");107}108}109110public static void main(String args[]) throws Exception {111new SetIfModifiedSince();112}113}114115116