Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/Test8a.java
41152 views
1
/*
2
* Copyright (c) 2005, 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 6270015
27
* @library /test/lib
28
* @build jdk.test.lib.net.SimpleSSLContext jdk.test.lib.net.URIBuilder
29
* @run main/othervm Test8a
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test8a
31
* @summary Light weight HTTP server
32
*/
33
34
import com.sun.net.httpserver.*;
35
36
import java.util.concurrent.*;
37
import java.io.*;
38
import java.net.*;
39
import javax.net.ssl.*;
40
import jdk.test.lib.net.SimpleSSLContext;
41
import jdk.test.lib.net.URIBuilder;
42
43
/**
44
* Test POST large file via fixed len encoding
45
*/
46
47
public class Test8a extends Test {
48
49
public static void main (String[] args) throws Exception {
50
//Logger log = Logger.getLogger ("com.sun.net.httpserver");
51
//ConsoleHandler h = new ConsoleHandler();
52
//h.setLevel (Level.INFO);
53
//log.addHandler (h);
54
//log.setLevel (Level.INFO);
55
HttpsServer server = null;
56
ExecutorService executor = null;
57
try {
58
Handler handler = new Handler();
59
InetAddress loopback = InetAddress.getLoopbackAddress();
60
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
61
server = HttpsServer.create (addr, 0);
62
HttpContext ctx = server.createContext ("/test", handler);
63
executor = Executors.newCachedThreadPool();
64
SSLContext ssl = new SimpleSSLContext().get();
65
server.setHttpsConfigurator(new HttpsConfigurator (ssl));
66
server.setExecutor (executor);
67
server.start ();
68
69
URL url = URIBuilder.newBuilder()
70
.scheme("https")
71
.loopback()
72
.port(server.getAddress().getPort())
73
.path("/test/foo.html")
74
.toURL();
75
76
System.out.print ("Test8a: " );
77
HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection(Proxy.NO_PROXY);
78
urlc.setDoOutput (true);
79
urlc.setRequestMethod ("POST");
80
urlc.setHostnameVerifier (new DummyVerifier());
81
urlc.setSSLSocketFactory (ssl.getSocketFactory());
82
OutputStream os = new BufferedOutputStream (urlc.getOutputStream(), 8000);
83
for (int i=0; i<SIZE; i++) {
84
os.write (i % 250);
85
}
86
os.close();
87
int resp = urlc.getResponseCode();
88
if (resp != 200) {
89
throw new RuntimeException ("test failed response code");
90
}
91
InputStream is = urlc.getInputStream ();
92
for (int i=0; i<SIZE; i++) {
93
int f = is.read();
94
if (f != (i % 250)) {
95
System.out.println ("Setting error(" +f +")("+i+")" );
96
error = true;
97
break;
98
}
99
}
100
is.close();
101
} finally {
102
delay();
103
if (server != null) server.stop(2);
104
if (executor != null) executor.shutdown();
105
}
106
if (error) {
107
throw new RuntimeException ("test failed error");
108
}
109
System.out.println ("OK");
110
111
}
112
113
public static boolean error = false;
114
//final static int SIZE = 999999;
115
final static int SIZE = 9999;
116
117
static class Handler implements HttpHandler {
118
int invocation = 1;
119
public void handle (HttpExchange t)
120
throws IOException
121
{
122
System.out.println ("Handler.handle");
123
InputStream is = t.getRequestBody();
124
Headers map = t.getRequestHeaders();
125
Headers rmap = t.getResponseHeaders();
126
int c, count=0;
127
while ((c=is.read ()) != -1) {
128
if (c != (count % 250)) {
129
System.out.println ("Setting error 1");
130
error = true;
131
break;
132
}
133
count ++;
134
}
135
if (count != SIZE) {
136
System.out.println ("Setting error 2");
137
error = true;
138
}
139
is.close();
140
t.sendResponseHeaders (200, SIZE);
141
System.out.println ("Sending 200 OK");
142
OutputStream os = new BufferedOutputStream(t.getResponseBody(), 8000);
143
for (int i=0; i<SIZE; i++) {
144
os.write (i % 250);
145
}
146
os.close();
147
System.out.println ("Finished");
148
}
149
}
150
}
151
152