Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/HttpContinueStackOverflow.java
38811 views
/*1* Copyright (c) 2000, 2010, 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* @summary Make sure that http CONTINUE status followed by invalid26* response doesn't cause HttpClient to recursively loop and27* eventually StackOverflow.28*29*/30import java.io.BufferedReader;31import java.io.InputStreamReader;32import java.io.IOException;33import java.io.PrintStream;34import java.net.ServerSocket;35import java.net.Socket;36import java.net.URL;37import java.net.HttpURLConnection;3839public class HttpContinueStackOverflow {4041static class Server implements Runnable {42int port;43ServerSocket serverSock ;4445Server() throws IOException {46serverSock = new ServerSocket(0);47}4849int getLocalPort() {50return serverSock.getLocalPort();51}5253public void run() {54Socket sock = null;55try {56serverSock.setSoTimeout(10000);57sock = serverSock.accept();5859/* setup streams and read http request */60BufferedReader in = new BufferedReader(61new InputStreamReader(sock.getInputStream()));62PrintStream out = new PrintStream( sock.getOutputStream() );63in.readLine();6465/* send continue followed by invalid response */66out.println("HTTP/1.1 100 Continue\r");67out.println("\r");68out.println("junk junk junk");69out.flush();70} catch (Exception e) {71e.printStackTrace();72} finally {73try { serverSock.close(); } catch (IOException unused) {}74try { sock.close(); } catch (IOException unused) {}75}76}77}7879HttpContinueStackOverflow() throws Exception {80/* create the server */81Server s = new Server();82(new Thread(s)).start();8384/* connect to server, connect to server and get response code */85URL url = new URL("http", "localhost", s.getLocalPort(), "anything.html");86HttpURLConnection conn = (HttpURLConnection)url.openConnection();87conn.getResponseCode();88System.out.println("TEST PASSED");89}9091public static void main(String args[]) throws Exception {92System.out.println("Testing 100-Continue");93new HttpContinueStackOverflow();94}95}969798