J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2002 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4258697 |
| 26 | * @summary Make sure that http CONTINUE status followed by invalid |
| 27 | * response doesn't cause HttpClient to recursively loop and |
| 28 | * eventually StackOverflow. |
| 29 | * |
| 30 | */ |
| 31 | import java.io.BufferedReader; |
| 32 | import java.io.InputStreamReader; |
| 33 | import java.io.OutputStreamWriter; |
| 34 | import java.io.PrintStream; |
| 35 | import java.net.ServerSocket; |
| 36 | import java.net.Socket; |
| 37 | import java.net.URL; |
| 38 | import java.net.HttpURLConnection; |
| 39 | |
| 40 | public class HttpContinueStackOverflow { |
| 41 | |
| 42 | static class Server implements Runnable { |
| 43 | int port; |
| 44 | |
| 45 | Server(int port) { |
| 46 | this.port = port; |
| 47 | } |
| 48 | |
| 49 | public void run() { |
| 50 | try { |
| 51 | /* bind to port and wait for connection */ |
| 52 | ServerSocket serverSock = new ServerSocket( port ); |
| 53 | serverSock.setSoTimeout(10000); |
| 54 | Socket sock = serverSock.accept(); |
| 55 | |
| 56 | /* setup streams and read http request */ |
| 57 | BufferedReader in = new BufferedReader( |
| 58 | new InputStreamReader(sock.getInputStream())); |
| 59 | PrintStream out = new PrintStream( sock.getOutputStream() ); |
| 60 | String request = in.readLine(); |
| 61 | |
| 62 | /* send continue followed by invalid response */ |
| 63 | out.println("HTTP/1.1 100 Continue\r"); |
| 64 | out.println("\r"); |
| 65 | out.println("junk junk junk"); |
| 66 | out.flush(); |
| 67 | |
| 68 | sock.close(); |
| 69 | } catch (Exception e) { |
| 70 | e.printStackTrace(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | HttpContinueStackOverflow(int port) throws Exception { |
| 76 | /* create the server */ |
| 77 | Server s = new Server(port); |
| 78 | Thread thr = new Thread(s); |
| 79 | thr.start(); |
| 80 | |
| 81 | /* wait for server to bind to port */ |
| 82 | try { |
| 83 | Thread.currentThread().sleep(2000); |
| 84 | } catch (Exception e) { } |
| 85 | |
| 86 | /* connect to server, connect to server and get response code */ |
| 87 | URL url = new URL("http", "localhost", port, "anything.html"); |
| 88 | HttpURLConnection conn = (HttpURLConnection)url.openConnection(); |
| 89 | int respCode = conn.getResponseCode(); |
| 90 | System.out.println("TEST PASSED"); |
| 91 | } |
| 92 | |
| 93 | public static void main(String args[]) throws Exception { |
| 94 | int port = 4090; |
| 95 | if (args.length > 0) { |
| 96 | port = Integer.parseInt(args[0]); |
| 97 | } |
| 98 | System.out.println("Testing 100-Continue"); |
| 99 | new HttpContinueStackOverflow(port); |
| 100 | } |
| 101 | } |