author | dfuchs |
Thu, 15 Aug 2019 12:58:27 +0100 | |
changeset 57760 | ec948d19180a |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57760
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2006, 2019, 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 6472250 |
|
38557 | 27 |
* @modules jdk.httpserver |
2 | 28 |
* @run main/othervm StreamingOutputStream |
29 |
* @summary HttpURLConnection.getOutputStream streaming mode bug when called multiple times |
|
30 |
*/ |
|
31 |
||
32 |
import java.net.*; |
|
33 |
import java.io.*; |
|
34 |
import com.sun.net.httpserver.*; |
|
35 |
||
36 |
/* |
|
37 |
* Calling HttpURLConnection.getOutputStream multiple times when streaming |
|
38 |
* should not create a new OutputStream each time. |
|
39 |
*/ |
|
40 |
||
41 |
public class StreamingOutputStream |
|
42 |
{ |
|
43 |
com.sun.net.httpserver.HttpServer httpServer; |
|
44 |
||
45 |
public static void main(String[] args) { |
|
46 |
new StreamingOutputStream(); |
|
47 |
} |
|
48 |
||
49 |
public StreamingOutputStream() { |
|
50 |
try { |
|
51 |
startHttpServer(); |
|
52 |
doClient(); |
|
53 |
} catch (IOException ioe) { |
|
54 |
ioe.printStackTrace(); |
|
55 |
} |
|
56 |
} |
|
57 |
||
58 |
void doClient() { |
|
59 |
try { |
|
60 |
InetSocketAddress address = httpServer.getAddress(); |
|
61 |
||
62 |
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/"); |
|
57760
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
63 |
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY); |
2 | 64 |
|
65 |
uc.setDoOutput(true); |
|
66 |
uc.setFixedLengthStreamingMode(1); |
|
67 |
OutputStream os1 = uc.getOutputStream(); |
|
68 |
OutputStream os2 = uc.getOutputStream(); |
|
69 |
||
70 |
if (os1 != os2) |
|
71 |
throw new RuntimeException("Failed: OutputStreams should reference the same object"); |
|
72 |
||
73 |
os2.write('b'); |
|
74 |
os2.close(); |
|
75 |
||
76 |
int resp = uc.getResponseCode(); |
|
77 |
if (resp != 200) |
|
78 |
throw new RuntimeException("Failed: Server should return 200 OK"); |
|
79 |
||
80 |
} catch (IOException e) { |
|
81 |
e.printStackTrace(); |
|
82 |
} finally { |
|
83 |
httpServer.stop(1); |
|
84 |
} |
|
85 |
} |
|
86 |
/** |
|
87 |
* Http Server |
|
88 |
*/ |
|
89 |
void startHttpServer() throws IOException { |
|
57760
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
90 |
InetAddress address = InetAddress.getLocalHost(); |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
91 |
if (!InetAddress.getByName(address.getHostName()).equals(address)) { |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
92 |
// if this happens then we should possibly change the client |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
93 |
// side to use the address literal in its URL instead of |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
94 |
// the host name. |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
95 |
throw new IOException(address.getHostName() |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
96 |
+ " resolves to " |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
97 |
+ InetAddress.getByName(address.getHostName()) |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
98 |
+ " not to " |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
99 |
+ address + ": check host configuration."); |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
100 |
} |
ec948d19180a
8229486: Replace wildcard address with loopback or local host in tests - part 21
dfuchs
parents:
47216
diff
changeset
|
101 |
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0); |
2 | 102 |
HttpContext ctx = httpServer.createContext("/test/", new MyHandler()); |
103 |
httpServer.start(); |
|
104 |
} |
|
105 |
||
106 |
class MyHandler implements HttpHandler { |
|
107 |
public void handle(HttpExchange t) throws IOException { |
|
108 |
InputStream is = t.getRequestBody(); |
|
109 |
while(is.read() != -1); |
|
110 |
is.close(); |
|
111 |
||
112 |
t.sendResponseHeaders(200, -1); |
|
113 |
t.close(); |
|
114 |
} |
|
115 |
} |
|
116 |
} |