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