1252N/A/*
3261N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
1252N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1252N/A *
1252N/A * This code is free software; you can redistribute it and/or modify it
1252N/A * under the terms of the GNU General Public License version 2 only, as
1252N/A * published by the Free Software Foundation.
1252N/A *
1252N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1252N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1252N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1252N/A * version 2 for more details (a copy is included in the LICENSE file that
1252N/A * accompanied this code).
1252N/A *
1252N/A * You should have received a copy of the GNU General Public License version
1252N/A * 2 along with this work; if not, write to the Free Software Foundation,
1252N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1252N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1252N/A */
1252N/A
1252N/A/*
1252N/A * @test
2981N/A * @bug 6726695 6993490
1252N/A * @summary HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
1252N/A*/
1252N/A
1252N/Aimport java.net.*;
1252N/Aimport java.io.*;
1252N/A
1252N/Apublic class B6726695 extends Thread {
1252N/A private ServerSocket server = null;
1252N/A private int port = 0;
1252N/A private byte[] data = new byte[512];
1252N/A private String boundary = "----------------7774563516523621";
1252N/A
1252N/A public static void main(String[] args) throws Exception {
1252N/A B6726695 test = new B6726695();
1252N/A // Exit even if server is still running
1252N/A test.setDaemon(true);
1252N/A // start server
1252N/A test.start();
1252N/A // run test
1252N/A test.test();
1252N/A }
1252N/A
1252N/A public B6726695() {
1252N/A try {
1252N/A server = new ServerSocket(0);
1252N/A port = server.getLocalPort();
1252N/A } catch (IOException e) {
1252N/A e.printStackTrace();
1252N/A }
1252N/A }
1252N/A
1252N/A public void test() throws Exception {
1252N/A /**
1252N/A * This is a hardcoded test. The server side expects 3 requests with a
1252N/A * Expect: 100-continue header. It will reject the 1st one and accept
1252N/A * the second one. Thus allowing us to test both scenarios.
1252N/A * The 3rd case is the simulation of a server that just plains ignore
1252N/A * the Expect: 100-Continue header. So the POST should proceed after
1252N/A * a timeout.
1252N/A */
1252N/A URL url = new URL("http://localhost:" + port + "/foo");
1252N/A
1252N/A // 1st Connection. Should be rejected. I.E. get a ProtocolException
1252N/A URLConnection con = url.openConnection();
1252N/A HttpURLConnection http = (HttpURLConnection) con;
1252N/A http.setRequestMethod("POST");
1252N/A http.setRequestProperty("Expect", "100-Continue");
1252N/A http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
1252N/A http.setDoOutput(true);
1252N/A http.setFixedLengthStreamingMode(512);
1252N/A OutputStream out = null;
1252N/A int errorCode = -1;
1252N/A try {
1252N/A out = http.getOutputStream();
1252N/A } catch (ProtocolException e) {
1252N/A errorCode = http.getResponseCode();
1252N/A }
1252N/A if (errorCode != 417) {
1252N/A throw new RuntimeException("Didn't get the ProtocolException");
1252N/A }
1252N/A
1252N/A // 2nd connection. Should be accepted by server.
1252N/A http = (HttpURLConnection) url.openConnection();
1252N/A http.setRequestMethod("POST");
1252N/A http.setRequestProperty("Expect", "100-Continue");
1252N/A http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
1252N/A http.setDoOutput(true);
1252N/A http.setFixedLengthStreamingMode(data.length);
1252N/A out = null;
1252N/A try {
1252N/A out = http.getOutputStream();
1252N/A } catch (ProtocolException e) {
1252N/A }
1252N/A if (out == null) {
1252N/A throw new RuntimeException("Didn't get an OutputStream");
1252N/A }
1252N/A out.write(data);
1252N/A out.flush();
1252N/A errorCode = http.getResponseCode();
1252N/A if (errorCode != 200) {
1252N/A throw new RuntimeException("Response code is " + errorCode);
1252N/A }
1252N/A out.close();
1252N/A
1252N/A // 3rd connection. Simulate a server that doesn't implement 100-continue
1252N/A http = (HttpURLConnection) url.openConnection();
1252N/A http.setRequestMethod("POST");
1252N/A http.setRequestProperty("Expect", "100-Continue");
1252N/A http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
1252N/A http.setDoOutput(true);
1252N/A http.setFixedLengthStreamingMode(data.length);
1252N/A out = null;
1252N/A try {
1252N/A out = http.getOutputStream();
1252N/A } catch (ProtocolException e) {
1252N/A }
1252N/A if (out == null) {
1252N/A throw new RuntimeException("Didn't get an OutputStream");
1252N/A }
1252N/A out.write(data);
1252N/A out.flush();
1252N/A out.close();
1252N/A errorCode = http.getResponseCode();
1252N/A if (errorCode != 200) {
1252N/A throw new RuntimeException("Response code is " + errorCode);
1252N/A }
1252N/A }
1252N/A
1252N/A
1252N/A @Override
1252N/A public void run() {
1252N/A try {
1252N/A // Fist connection: don't accetpt the request
1252N/A Socket s = server.accept();
1252N/A serverReject(s);
1252N/A // Second connection: accept the request (send 100-continue)
1252N/A s = server.accept();
1252N/A serverAccept(s);
1252N/A // 3rd connection: just ignore the 'Expect:' header
1252N/A s = server.accept();
1252N/A serverIgnore(s);
1252N/A } catch (IOException e) {
1252N/A e.printStackTrace();
2612N/A } finally {
2612N/A try { server.close(); } catch (IOException unused) {}
1252N/A }
1252N/A }
1252N/A
1252N/A public void serverReject(Socket s) throws IOException {
1252N/A BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
1252N/A PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
1252N/A String line = null;
1252N/A do {
1252N/A line = in.readLine();
1252N/A } while (line != null && line.length() != 0);
1252N/A
1252N/A out.print("HTTP/1.1 417 Expectation Failed\r\n");
1252N/A out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
1252N/A out.print("Connection: close\r\n");
1252N/A out.print("Content-Length: 0\r\n");
1252N/A out.print("\r\n");
1252N/A out.flush();
1252N/A out.close();
1252N/A in.close();
1252N/A }
1252N/A
1252N/A public void serverAccept(Socket s) throws IOException {
1252N/A BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
1252N/A PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
1252N/A String line = null;
1252N/A do {
1252N/A line = in.readLine();
1252N/A } while (line != null && line.length() != 0);
1252N/A
1252N/A // Send 100-Continue
1252N/A out.print("HTTP/1.1 100 Continue\r\n");
1252N/A out.print("\r\n");
1252N/A out.flush();
1252N/A // Then read the body
1252N/A char[] cbuf = new char[512];
2981N/A in.read(cbuf);
2981N/A
2981N/A /* Force the server to not respond for more that the expect 100-Continue
2981N/A * timeout set by the HTTP handler (5000 millis). This ensures the
2981N/A * timeout is correctly resets the default read timeout, infinity.
2981N/A * See 6993490. */
2981N/A System.out.println("server sleeping...");
2981N/A try {Thread.sleep(6000); } catch (InterruptedException e) {}
2981N/A
1252N/A // finally send the 200 OK
1252N/A out.print("HTTP/1.1 200 OK");
1252N/A out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
1252N/A out.print("Connection: close\r\n");
1252N/A out.print("Content-Length: 0\r\n");
1252N/A out.print("\r\n");
1252N/A out.flush();
1252N/A out.close();
1252N/A in.close();
1252N/A }
1252N/A
1252N/A public void serverIgnore(Socket s) throws IOException {
1252N/A BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
1252N/A PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
1252N/A String line = null;
1252N/A do {
1252N/A line = in.readLine();
1252N/A } while (line != null && line.length() != 0);
1252N/A
1252N/A // Then read the body
1252N/A char[] cbuf = new char[512];
1252N/A int l = in.read(cbuf);
1252N/A // finally send the 200 OK
1252N/A out.print("HTTP/1.1 200 OK");
1252N/A out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
1252N/A out.print("Content-Length: 0\r\n");
1252N/A out.print("Connection: close\r\n");
1252N/A out.print("\r\n");
1252N/A out.flush();
1252N/A out.close();
1252N/A in.close();
1252N/A }
1252N/A}