Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/UserAgent.java
38867 views
/*1* Copyright (c) 2001, 2002, 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/**24* @test25* @bug 451220026* @run main/othervm -Dhttp.agent=foo UserAgent27* @summary HTTP header "User-Agent" format incorrect28*/2930import java.io.*;31import java.util.*;32import java.net.*;33import sun.net.www.MessageHeader;3435class Server extends Thread {36Server (ServerSocket server) {37this.server = server;38}39public void run () {40try {41String version = System.getProperty ("java.version");42String expected = "foo Java/"+version;43Socket s = server.accept ();44MessageHeader header = new MessageHeader (s.getInputStream());45String v = header.findValue ("User-Agent");46if (!expected.equals (v)) {47error ("Got unexpected User-Agent: " + v);48} else {49success ();50}51OutputStream w = s.getOutputStream();52w.write("HTTP/1.1 200 OK\r\n".getBytes());53w.write("Content-Type: text/plain\r\n".getBytes());54w.write("Content-Length: 5\r\n".getBytes());55w.write("\r\n".getBytes());56w.write("12345\r\n".getBytes());57} catch (Exception e) {58error (e.toString());59}60}6162String msg;63ServerSocket server;64boolean success;6566synchronized String getMessage () {67return msg;68}6970synchronized boolean succeeded () {71return success;72}7374synchronized void success () {75success = true;76}77synchronized void error (String s) {78success = false;79msg = s;80}81}8283public class UserAgent {8485public static void main(String[] args) throws Exception {86ServerSocket server = new ServerSocket (0);87Server s = new Server (server);88s.start ();89int port = server.getLocalPort ();90URL url = new URL ("http://127.0.0.1:"+port);91URLConnection urlc = url.openConnection ();92urlc.getInputStream ();93s.join ();94if (!s.succeeded()) {95throw new RuntimeException (s.getMessage());96}97}98}99100101