Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/sun/net/www/http/HttpClient/B6726695.java
+++ new/test/sun/net/www/http/HttpClient/B6726695.java
1 1 /*
2 2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23
24 24 /*
25 25 * @test
26 - * @bug 6726695
26 + * @bug 6726695 6993490
27 27 * @summary HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
28 28 */
29 29
30 30 import java.net.*;
31 31 import java.io.*;
32 32
33 33 public class B6726695 extends Thread {
34 34 private ServerSocket server = null;
35 35 private int port = 0;
36 36 private byte[] data = new byte[512];
37 37 private String boundary = "----------------7774563516523621";
38 38
39 39 public static void main(String[] args) throws Exception {
40 40 B6726695 test = new B6726695();
41 41 // Exit even if server is still running
42 42 test.setDaemon(true);
43 43 // start server
44 44 test.start();
45 45 // run test
46 46 test.test();
47 47 }
48 48
49 49 public B6726695() {
50 50 try {
51 51 server = new ServerSocket(0);
52 52 port = server.getLocalPort();
53 53 } catch (IOException e) {
54 54 e.printStackTrace();
55 55 }
56 56 }
57 57
58 58 public void test() throws Exception {
59 59 /**
60 60 * This is a hardcoded test. The server side expects 3 requests with a
61 61 * Expect: 100-continue header. It will reject the 1st one and accept
62 62 * the second one. Thus allowing us to test both scenarios.
63 63 * The 3rd case is the simulation of a server that just plains ignore
64 64 * the Expect: 100-Continue header. So the POST should proceed after
65 65 * a timeout.
66 66 */
67 67 URL url = new URL("http://localhost:" + port + "/foo");
68 68
69 69 // 1st Connection. Should be rejected. I.E. get a ProtocolException
70 70 URLConnection con = url.openConnection();
71 71 HttpURLConnection http = (HttpURLConnection) con;
72 72 http.setRequestMethod("POST");
73 73 http.setRequestProperty("Expect", "100-Continue");
74 74 http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
75 75 http.setDoOutput(true);
76 76 http.setFixedLengthStreamingMode(512);
77 77 OutputStream out = null;
78 78 int errorCode = -1;
79 79 try {
80 80 out = http.getOutputStream();
81 81 } catch (ProtocolException e) {
82 82 errorCode = http.getResponseCode();
83 83 }
84 84 if (errorCode != 417) {
85 85 throw new RuntimeException("Didn't get the ProtocolException");
86 86 }
87 87
88 88 // 2nd connection. Should be accepted by server.
89 89 http = (HttpURLConnection) url.openConnection();
90 90 http.setRequestMethod("POST");
91 91 http.setRequestProperty("Expect", "100-Continue");
92 92 http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
93 93 http.setDoOutput(true);
94 94 http.setFixedLengthStreamingMode(data.length);
95 95 out = null;
96 96 try {
97 97 out = http.getOutputStream();
98 98 } catch (ProtocolException e) {
99 99 }
100 100 if (out == null) {
101 101 throw new RuntimeException("Didn't get an OutputStream");
102 102 }
103 103 out.write(data);
104 104 out.flush();
105 105 errorCode = http.getResponseCode();
106 106 if (errorCode != 200) {
107 107 throw new RuntimeException("Response code is " + errorCode);
108 108 }
109 109 out.close();
110 110
111 111 // 3rd connection. Simulate a server that doesn't implement 100-continue
112 112 http = (HttpURLConnection) url.openConnection();
113 113 http.setRequestMethod("POST");
114 114 http.setRequestProperty("Expect", "100-Continue");
115 115 http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
116 116 http.setDoOutput(true);
117 117 http.setFixedLengthStreamingMode(data.length);
118 118 out = null;
119 119 try {
120 120 out = http.getOutputStream();
121 121 } catch (ProtocolException e) {
122 122 }
123 123 if (out == null) {
124 124 throw new RuntimeException("Didn't get an OutputStream");
125 125 }
126 126 out.write(data);
127 127 out.flush();
128 128 out.close();
129 129 errorCode = http.getResponseCode();
130 130 if (errorCode != 200) {
131 131 throw new RuntimeException("Response code is " + errorCode);
132 132 }
133 133 }
134 134
135 135
136 136 @Override
137 137 public void run() {
138 138 try {
139 139 // Fist connection: don't accetpt the request
140 140 Socket s = server.accept();
141 141 serverReject(s);
142 142 // Second connection: accept the request (send 100-continue)
143 143 s = server.accept();
144 144 serverAccept(s);
145 145 // 3rd connection: just ignore the 'Expect:' header
146 146 s = server.accept();
147 147 serverIgnore(s);
148 148 } catch (IOException e) {
149 149 e.printStackTrace();
150 150 } finally {
151 151 try { server.close(); } catch (IOException unused) {}
152 152 }
153 153 }
154 154
155 155 public void serverReject(Socket s) throws IOException {
156 156 BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
157 157 PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
158 158 String line = null;
159 159 do {
160 160 line = in.readLine();
161 161 } while (line != null && line.length() != 0);
162 162
163 163 out.print("HTTP/1.1 417 Expectation Failed\r\n");
164 164 out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
165 165 out.print("Connection: close\r\n");
166 166 out.print("Content-Length: 0\r\n");
167 167 out.print("\r\n");
168 168 out.flush();
169 169 out.close();
170 170 in.close();
171 171 }
172 172
173 173 public void serverAccept(Socket s) throws IOException {
174 174 BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
175 175 PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
176 176 String line = null;
↓ open down ↓ |
140 lines elided |
↑ open up ↑ |
177 177 do {
178 178 line = in.readLine();
179 179 } while (line != null && line.length() != 0);
180 180
181 181 // Send 100-Continue
182 182 out.print("HTTP/1.1 100 Continue\r\n");
183 183 out.print("\r\n");
184 184 out.flush();
185 185 // Then read the body
186 186 char[] cbuf = new char[512];
187 - int l = in.read(cbuf);
187 + in.read(cbuf);
188 +
189 + /* Force the server to not respond for more that the expect 100-Continue
190 + * timeout set by the HTTP handler (5000 millis). This ensures the
191 + * timeout is correctly resets the default read timeout, infinity.
192 + * See 6993490. */
193 + System.out.println("server sleeping...");
194 + try {Thread.sleep(6000); } catch (InterruptedException e) {}
195 +
188 196 // finally send the 200 OK
189 197 out.print("HTTP/1.1 200 OK");
190 198 out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
191 199 out.print("Connection: close\r\n");
192 200 out.print("Content-Length: 0\r\n");
193 201 out.print("\r\n");
194 202 out.flush();
195 203 out.close();
196 204 in.close();
197 205 }
198 206
199 207 public void serverIgnore(Socket s) throws IOException {
200 208 BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
201 209 PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
202 210 String line = null;
203 211 do {
204 212 line = in.readLine();
205 213 } while (line != null && line.length() != 0);
206 214
207 215 // Then read the body
208 216 char[] cbuf = new char[512];
209 217 int l = in.read(cbuf);
210 218 // finally send the 200 OK
211 219 out.print("HTTP/1.1 200 OK");
212 220 out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
213 221 out.print("Content-Length: 0\r\n");
214 222 out.print("Connection: close\r\n");
215 223 out.print("\r\n");
216 224 out.flush();
217 225 out.close();
218 226 in.close();
219 227 }
220 228 }
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX