author | aeubanks |
Wed, 27 Mar 2019 09:06:43 -0700 | |
changeset 54314 | 46cf212cdcca |
parent 47216 | 71c04702a3d5 |
child 55522 | b2cbe2f674f0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 4512200 |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
27 |
* @library /test/lib |
30820 | 28 |
* @modules java.base/sun.net.www |
2 | 29 |
* @run main/othervm -Dhttp.agent=foo UserAgent |
30 |
* @summary HTTP header "User-Agent" format incorrect |
|
31 |
*/ |
|
32 |
||
33 |
import java.io.*; |
|
34 |
import java.util.*; |
|
35 |
import java.net.*; |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
36 |
import jdk.test.lib.net.URIBuilder; |
2 | 37 |
import sun.net.www.MessageHeader; |
38 |
||
39 |
class Server extends Thread { |
|
40 |
Server (ServerSocket server) { |
|
41 |
this.server = server; |
|
42 |
} |
|
43 |
public void run () { |
|
44 |
try { |
|
45 |
String version = System.getProperty ("java.version"); |
|
46 |
String expected = "foo Java/"+version; |
|
47 |
Socket s = server.accept (); |
|
48 |
MessageHeader header = new MessageHeader (s.getInputStream()); |
|
49 |
String v = header.findValue ("User-Agent"); |
|
50 |
if (!expected.equals (v)) { |
|
51 |
error ("Got unexpected User-Agent: " + v); |
|
52 |
} else { |
|
53 |
success (); |
|
54 |
} |
|
55 |
OutputStream w = s.getOutputStream(); |
|
56 |
w.write("HTTP/1.1 200 OK\r\n".getBytes()); |
|
57 |
w.write("Content-Type: text/plain\r\n".getBytes()); |
|
58 |
w.write("Content-Length: 5\r\n".getBytes()); |
|
59 |
w.write("\r\n".getBytes()); |
|
60 |
w.write("12345\r\n".getBytes()); |
|
61 |
} catch (Exception e) { |
|
62 |
error (e.toString()); |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
String msg; |
|
67 |
ServerSocket server; |
|
68 |
boolean success; |
|
69 |
||
70 |
synchronized String getMessage () { |
|
71 |
return msg; |
|
72 |
} |
|
73 |
||
74 |
synchronized boolean succeeded () { |
|
75 |
return success; |
|
76 |
} |
|
77 |
||
78 |
synchronized void success () { |
|
79 |
success = true; |
|
80 |
} |
|
81 |
synchronized void error (String s) { |
|
82 |
success = false; |
|
83 |
msg = s; |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
public class UserAgent { |
|
88 |
||
89 |
public static void main(String[] args) throws Exception { |
|
90 |
ServerSocket server = new ServerSocket (0); |
|
91 |
Server s = new Server (server); |
|
92 |
s.start (); |
|
93 |
int port = server.getLocalPort (); |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
94 |
URL url = URIBuilder.newBuilder() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
95 |
.scheme("http") |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
96 |
.loopback() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
97 |
.port(port) |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
98 |
.toURL(); |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
99 |
System.out.println("URL: " + url); |
2 | 100 |
URLConnection urlc = url.openConnection (); |
101 |
urlc.getInputStream (); |
|
102 |
s.join (); |
|
103 |
if (!s.succeeded()) { |
|
104 |
throw new RuntimeException (s.getMessage()); |
|
105 |
} |
|
106 |
} |
|
107 |
} |