Path: blob/master/test/jdk/sun/net/www/protocol/http/UserAgent.java
41159 views
/*1* Copyright (c) 2001, 2019, 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* @library /test/lib27* @modules java.base/sun.net.www28* @run main/othervm -Dhttp.agent=foo UserAgent29* @run main/othervm -Dhttp.agent=foo -Djava.net.preferIPv6Addresses=true UserAgent30* @summary HTTP header "User-Agent" format incorrect31*/3233import java.io.*;34import java.util.*;35import java.net.*;36import jdk.test.lib.net.URIBuilder;37import sun.net.www.MessageHeader;3839class Server extends Thread {40Server (ServerSocket server) {41this.server = server;42}43public void run () {44try {45String version = System.getProperty ("java.version");46String expected = "foo Java/"+version;47Socket s = server.accept ();48MessageHeader header = new MessageHeader (s.getInputStream());49String v = header.findValue ("User-Agent");50if (!expected.equals (v)) {51error ("Got unexpected User-Agent: " + v);52} else {53success ();54}55OutputStream w = s.getOutputStream();56w.write("HTTP/1.1 200 OK\r\n".getBytes());57w.write("Content-Type: text/plain\r\n".getBytes());58w.write("Content-Length: 5\r\n".getBytes());59w.write("\r\n".getBytes());60w.write("12345\r\n".getBytes());61} catch (Exception e) {62error (e.toString());63}64}6566String msg;67ServerSocket server;68boolean success;6970synchronized String getMessage () {71return msg;72}7374synchronized boolean succeeded () {75return success;76}7778synchronized void success () {79success = true;80}81synchronized void error (String s) {82success = false;83msg = s;84}85}8687public class UserAgent {8889public static void main(String[] args) throws Exception {90InetAddress loopback = InetAddress.getLoopbackAddress();91ServerSocket server = new ServerSocket ();92server.bind(new InetSocketAddress(loopback, 0));93Server s = new Server (server);94s.start ();95int port = server.getLocalPort ();96URL url = URIBuilder.newBuilder()97.scheme("http")98.loopback()99.port(port)100.toURL();101System.out.println("URL: " + url);102URLConnection urlc = url.openConnection (Proxy.NO_PROXY);103urlc.getInputStream ();104s.join ();105if (!s.succeeded()) {106throw new RuntimeException (s.getMessage());107}108}109}110111112