J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001-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 | /** |
| 25 | * @test |
| 26 | * @bug 4512200 |
| 27 | * @run main/othervm -Dhttp.agent=foo UserAgent |
| 28 | * @summary HTTP header "User-Agent" format incorrect |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | import java.util.*; |
| 33 | import java.net.*; |
| 34 | import sun.net.www.MessageHeader; |
| 35 | |
| 36 | class Server extends Thread { |
| 37 | Server (ServerSocket server) { |
| 38 | this.server = server; |
| 39 | } |
| 40 | public void run () { |
| 41 | try { |
| 42 | String version = System.getProperty ("java.version"); |
| 43 | String expected = "foo Java/"+version; |
| 44 | Socket s = server.accept (); |
| 45 | MessageHeader header = new MessageHeader (s.getInputStream()); |
| 46 | String v = header.findValue ("User-Agent"); |
| 47 | if (!expected.equals (v)) { |
| 48 | error ("Got unexpected User-Agent: " + v); |
| 49 | } else { |
| 50 | success (); |
| 51 | } |
| 52 | OutputStream w = s.getOutputStream(); |
| 53 | w.write("HTTP/1.1 200 OK\r\n".getBytes()); |
| 54 | w.write("Content-Type: text/plain\r\n".getBytes()); |
| 55 | w.write("Content-Length: 5\r\n".getBytes()); |
| 56 | w.write("\r\n".getBytes()); |
| 57 | w.write("12345\r\n".getBytes()); |
| 58 | } catch (Exception e) { |
| 59 | error (e.toString()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | String msg; |
| 64 | ServerSocket server; |
| 65 | boolean success; |
| 66 | |
| 67 | synchronized String getMessage () { |
| 68 | return msg; |
| 69 | } |
| 70 | |
| 71 | synchronized boolean succeeded () { |
| 72 | return success; |
| 73 | } |
| 74 | |
| 75 | synchronized void success () { |
| 76 | success = true; |
| 77 | } |
| 78 | synchronized void error (String s) { |
| 79 | success = false; |
| 80 | msg = s; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public class UserAgent { |
| 85 | |
| 86 | public static void main(String[] args) throws Exception { |
| 87 | ServerSocket server = new ServerSocket (0); |
| 88 | Server s = new Server (server); |
| 89 | s.start (); |
| 90 | int port = server.getLocalPort (); |
| 91 | URL url = new URL ("http://127.0.0.1:"+port); |
| 92 | URLConnection urlc = url.openConnection (); |
| 93 | urlc.getInputStream (); |
| 94 | s.join (); |
| 95 | if (!s.succeeded()) { |
| 96 | throw new RuntimeException (s.getMessage()); |
| 97 | } |
| 98 | } |
| 99 | } |