author | sla |
Mon, 07 Apr 2014 08:09:55 +0200 | |
changeset 23723 | 46b29c0e9656 |
parent 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2002, 2010, 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 4533243 |
|
27 |
* @summary Closing a keep alive stream gives NullPointerException |
|
28 |
* @run main/othervm/timeout=30 KeepAliveStreamCloseWithWrongContentLength |
|
29 |
*/ |
|
30 |
||
31 |
import java.net.*; |
|
32 |
import java.io.*; |
|
33 |
||
34 |
public class KeepAliveStreamCloseWithWrongContentLength { |
|
35 |
||
36 |
static class XServer extends Thread { |
|
37 |
ServerSocket srv; |
|
38 |
Socket s; |
|
39 |
InputStream is; |
|
40 |
OutputStream os; |
|
41 |
||
42 |
XServer (ServerSocket s) { |
|
43 |
srv = s; |
|
44 |
} |
|
45 |
||
46 |
public void run() { |
|
47 |
try { |
|
48 |
s = srv.accept (); |
|
49 |
// read HTTP request from client |
|
50 |
InputStream is = s.getInputStream(); |
|
51 |
// read the first ten bytes |
|
52 |
for (int i=0; i<10; i++) { |
|
53 |
is.read(); |
|
54 |
} |
|
55 |
OutputStreamWriter ow = |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
56 |
new OutputStreamWriter((os = s.getOutputStream())); |
2 | 57 |
ow.write("HTTP/1.0 200 OK\n"); |
58 |
||
59 |
// Note: The client expects 10 bytes. |
|
60 |
ow.write("Content-Length: 10\n"); |
|
61 |
ow.write("Content-Type: text/html\n"); |
|
62 |
||
63 |
// Note: If this line is missing, everything works fine. |
|
64 |
ow.write("Connection: Keep-Alive\n"); |
|
65 |
ow.write("\n"); |
|
66 |
||
67 |
// Note: The (buggy) server only sends 9 bytes. |
|
68 |
ow.write("123456789"); |
|
69 |
ow.flush(); |
|
70 |
} catch (Exception e) { |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
71 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
72 |
try {if (os != null) { os.close(); }} catch (IOException e) {} |
2 | 73 |
} |
74 |
} |
|
75 |
} |
|
76 |
||
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
77 |
public static void main (String[] args) throws Exception { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
78 |
ServerSocket serversocket = new ServerSocket (0); |
2 | 79 |
try { |
80 |
int port = serversocket.getLocalPort (); |
|
81 |
XServer server = new XServer (serversocket); |
|
82 |
server.start (); |
|
83 |
URL url = new URL ("http://localhost:"+port); |
|
84 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); |
|
85 |
InputStream is = urlc.getInputStream (); |
|
86 |
int c = 0; |
|
87 |
while (c != -1) { |
|
88 |
try { |
|
89 |
c=is.read(); |
|
90 |
} catch (IOException ioe) { |
|
91 |
is.read (); |
|
92 |
break; |
|
93 |
} |
|
94 |
} |
|
95 |
is.close(); |
|
96 |
} catch (IOException e) { |
|
97 |
return; |
|
98 |
} catch (NullPointerException e) { |
|
99 |
throw new RuntimeException (e); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
100 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
101 |
if (serversocket != null) serversocket.close(); |
2 | 102 |
} |
103 |
} |
|
104 |
} |