Path: blob/master/test/jdk/java/net/URLConnection/HttpContinueStackOverflow.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 425869725* @library /test/lib26* @summary Make sure that http CONTINUE status followed by invalid27* response doesn't cause HttpClient to recursively loop and28* eventually StackOverflow.29*30*/31import java.io.BufferedReader;32import java.io.InputStreamReader;33import java.io.IOException;34import java.io.PrintStream;35import java.net.InetAddress;36import java.net.ServerSocket;37import java.net.Socket;38import java.net.URL;39import java.net.HttpURLConnection;40import jdk.test.lib.net.URIBuilder;41import static java.net.Proxy.NO_PROXY;4243public class HttpContinueStackOverflow {4445static class Server implements Runnable {46ServerSocket serverSock ;4748Server() throws IOException {49serverSock = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());50}5152int getLocalPort() {53return serverSock.getLocalPort();54}5556public void run() {57Socket sock = null;58try {59serverSock.setSoTimeout(10000);60sock = serverSock.accept();6162/* setup streams and read http request */63BufferedReader in = new BufferedReader(64new InputStreamReader(sock.getInputStream()));65PrintStream out = new PrintStream( sock.getOutputStream() );66in.readLine();6768/* send continue followed by invalid response */69out.println("HTTP/1.1 100 Continue\r");70out.println("\r");71out.println("junk junk junk");72out.flush();73} catch (Exception e) {74e.printStackTrace();75} finally {76try { serverSock.close(); } catch (IOException unused) {}77try { sock.close(); } catch (IOException unused) {}78}79}80}8182HttpContinueStackOverflow() throws Exception {83/* create the server */84Server s = new Server();85(new Thread(s)).start();8687/* connect to server and get response code */88URL url = URIBuilder.newBuilder()89.scheme("http")90.loopback()91.port(s.getLocalPort())92.path("/anything.html")93.toURL();9495HttpURLConnection conn = (HttpURLConnection)url.openConnection(NO_PROXY);96conn.getResponseCode();97System.out.println("TEST PASSED");98}99100public static void main(String args[]) throws Exception {101System.out.println("Testing 100-Continue");102new HttpContinueStackOverflow();103}104}105106107