Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/SetIfModifiedSince.java
38867 views
/*1* Copyright (c) 1999, 2000, 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 421316425@summary setIfModifiedSince mehtod in HttpURLConnection sometimes fails26*/27import java.util.*;28import java.io.*;29import java.net.*;30import java.text.*;3132public class SetIfModifiedSince implements Runnable {3334ServerSocket serverSock;3536public void run() {37try {38Socket s = serverSock.accept();39InputStream in = s.getInputStream();40byte b[] = new byte[4096];4142// assume we read the entire http request43// (bad assumption but okay for test case)44int nread = in.read(b);4546// check the date format by the position of the comma47String request = new String(b, 0, nread);48int pos = request.indexOf("If-Modified-Since:");49int respCode = 200;50if (pos != -1) {51pos += "If-Modified-Since:".length() + 4;52if (pos < nread) {53if (request.charAt(pos) == (char)',') {54respCode = 304;55}56}57}5859OutputStream o = s.getOutputStream();60if (respCode == 304) {61o.write( "HTTP/1.1 304 Not Modified".getBytes() );62} else {63o.write( "HTTP/1.1 200 OK".getBytes() );64}65o.write( (byte)'\r' );66o.write( (byte)'\n' );67o.write( (byte)'\r' );68o.write( (byte)'\n' );69o.flush();7071} catch (Exception e) { }72}737475public SetIfModifiedSince() throws Exception {7677serverSock = new ServerSocket(0);78int port = serverSock.getLocalPort();7980Thread thr = new Thread(this);81thr.start();8283Date date = new Date(new Date().getTime()-1440000); // this time yesterday84URL url;85HttpURLConnection con;8687//url = new URL(args[0]);88url = new URL("http://localhost:" + String.valueOf(port) +89"/anything");90con = (HttpURLConnection)url.openConnection();9192con.setIfModifiedSince(date.getTime());93con.connect();94int ret = con.getResponseCode();9596if (ret == 304) {97System.out.println("Success!");98} else {99throw 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");100}101}102103public static void main(String args[]) throws Exception {104new SetIfModifiedSince();105}106}107108109