Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/B6369510.java
41159 views
1
/*
2
* Copyright (c) 2006, 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 6369510
27
* @modules jdk.httpserver
28
* @run main/othervm B6369510
29
* @summary HttpURLConnection sets Content-Type to application/x-www-form-urlencoded
30
*/
31
32
import java.net.*;
33
import java.util.*;
34
import java.io.*;
35
import com.sun.net.httpserver.*;
36
import java.util.concurrent.Executors;
37
import java.util.concurrent.ExecutorService;
38
39
public class B6369510
40
{
41
com.sun.net.httpserver.HttpServer httpServer;
42
ExecutorService executorService;
43
44
public static void main(String[] args) throws Exception
45
{
46
new B6369510();
47
}
48
49
public B6369510()
50
{
51
try {
52
startHttpServer();
53
doClient();
54
} catch (IOException ioe) {
55
System.err.println(ioe);
56
}
57
}
58
59
void doClient() {
60
try {
61
InetSocketAddress address = httpServer.getAddress();
62
String urlString = "http://" + InetAddress.getLocalHost().getHostName()
63
+ ":" + address.getPort() + "/test/";
64
System.out.println("URL == " + urlString);
65
66
// GET Request
67
URL url = new URL(urlString);
68
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
69
int resp = uc.getResponseCode();
70
if (resp != 200)
71
throw new RuntimeException("Failed: Response code from GET is not 200 RSP == " + resp);
72
73
System.out.println("Response code from GET = 200 OK");
74
75
//POST Request
76
uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
77
uc.setDoOutput(true);
78
uc.setRequestMethod("POST");
79
OutputStream os = uc.getOutputStream();
80
resp = uc.getResponseCode();
81
if (resp != 200)
82
throw new RuntimeException("Failed: Response code form POST is not 200 RSP == " + resp);
83
84
System.out.println("Response code from POST = 200 OK");
85
86
} catch (IOException e) {
87
e.printStackTrace();
88
throw new RuntimeException("Failed with IOException");
89
} finally {
90
httpServer.stop(1);
91
executorService.shutdown();
92
}
93
}
94
95
/**
96
* Http Server
97
*/
98
public void startHttpServer() throws IOException {
99
InetAddress localhost = InetAddress.getLocalHost();
100
httpServer = HttpServer.create(new InetSocketAddress(localhost, 0), 0);
101
102
// create HttpServer context
103
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
104
105
executorService = Executors.newCachedThreadPool();
106
httpServer.setExecutor(executorService);
107
httpServer.start();
108
}
109
110
class MyHandler implements HttpHandler {
111
public void handle(HttpExchange t) throws IOException {
112
InputStream is = t.getRequestBody();
113
Headers reqHeaders = t.getRequestHeaders();
114
Headers resHeaders = t.getResponseHeaders();
115
while (is.read () != -1) ;
116
is.close();
117
118
List<String> ct = reqHeaders.get("content-type");
119
String requestMethod = t.getRequestMethod();
120
121
if (requestMethod.equalsIgnoreCase("GET") && ct != null &&
122
ct.get(0).equals("application/x-www-form-urlencoded"))
123
t.sendResponseHeaders(400, -1);
124
125
else if (requestMethod.equalsIgnoreCase("POST") && ct != null &&
126
!ct.get(0).equals("application/x-www-form-urlencoded"))
127
t.sendResponseHeaders(400, -1);
128
129
t.sendResponseHeaders(200, -1);
130
t.close();
131
}
132
}
133
}
134
135