1 /* 2 * Copyright (c) 2001, 2002, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * 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 { Server(ServerSocket server)37 Server (ServerSocket server) { 38 this.server = server; 39 } run()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 getMessage()67 synchronized String getMessage () { 68 return msg; 69 } 70 succeeded()71 synchronized boolean succeeded () { 72 return success; 73 } 74 success()75 synchronized void success () { 76 success = true; 77 } error(String s)78 synchronized void error (String s) { 79 success = false; 80 msg = s; 81 } 82 } 83 84 public class UserAgent { 85 main(String[] args)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 } 100