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