Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/B6726695.java
38868 views
/*1* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6726695 699349026* @summary HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT27*/2829import java.net.*;30import java.io.*;3132public class B6726695 extends Thread {33private ServerSocket server = null;34private int port = 0;35private byte[] data = new byte[512];36private String boundary = "----------------7774563516523621";3738public static void main(String[] args) throws Exception {39B6726695 test = new B6726695();40// Exit even if server is still running41test.setDaemon(true);42// start server43test.start();44// run test45test.test();46}4748public B6726695() {49try {50server = new ServerSocket(0);51port = server.getLocalPort();52} catch (IOException e) {53e.printStackTrace();54}55}5657public void test() throws Exception {58/**59* This is a hardcoded test. The server side expects 3 requests with a60* Expect: 100-continue header. It will reject the 1st one and accept61* the second one. Thus allowing us to test both scenarios.62* The 3rd case is the simulation of a server that just plains ignore63* the Expect: 100-Continue header. So the POST should proceed after64* a timeout.65*/66URL url = new URL("http://localhost:" + port + "/foo");6768// 1st Connection. Should be rejected. I.E. get a ProtocolException69URLConnection con = url.openConnection();70HttpURLConnection http = (HttpURLConnection) con;71http.setRequestMethod("POST");72http.setRequestProperty("Expect", "100-Continue");73http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);74http.setDoOutput(true);75http.setFixedLengthStreamingMode(512);76OutputStream out = null;77int errorCode = -1;78try {79out = http.getOutputStream();80} catch (ProtocolException e) {81errorCode = http.getResponseCode();82}83if (errorCode != 417) {84throw new RuntimeException("Didn't get the ProtocolException");85}8687// 2nd connection. Should be accepted by server.88http = (HttpURLConnection) url.openConnection();89http.setRequestMethod("POST");90http.setRequestProperty("Expect", "100-Continue");91http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);92http.setDoOutput(true);93http.setFixedLengthStreamingMode(data.length);94out = null;95try {96out = http.getOutputStream();97} catch (ProtocolException e) {98}99if (out == null) {100throw new RuntimeException("Didn't get an OutputStream");101}102out.write(data);103out.flush();104errorCode = http.getResponseCode();105if (errorCode != 200) {106throw new RuntimeException("Response code is " + errorCode);107}108out.close();109110// 3rd connection. Simulate a server that doesn't implement 100-continue111http = (HttpURLConnection) url.openConnection();112http.setRequestMethod("POST");113http.setRequestProperty("Expect", "100-Continue");114http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);115http.setDoOutput(true);116http.setFixedLengthStreamingMode(data.length);117out = null;118try {119out = http.getOutputStream();120} catch (ProtocolException e) {121}122if (out == null) {123throw new RuntimeException("Didn't get an OutputStream");124}125out.write(data);126out.flush();127out.close();128errorCode = http.getResponseCode();129if (errorCode != 200) {130throw new RuntimeException("Response code is " + errorCode);131}132}133134135@Override136public void run() {137try {138// Fist connection: don't accetpt the request139Socket s = server.accept();140serverReject(s);141// Second connection: accept the request (send 100-continue)142s = server.accept();143serverAccept(s);144// 3rd connection: just ignore the 'Expect:' header145s = server.accept();146serverIgnore(s);147} catch (IOException e) {148e.printStackTrace();149} finally {150try { server.close(); } catch (IOException unused) {}151}152}153154public void serverReject(Socket s) throws IOException {155BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));156PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));157String line = null;158do {159line = in.readLine();160} while (line != null && line.length() != 0);161162out.print("HTTP/1.1 417 Expectation Failed\r\n");163out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");164out.print("Connection: close\r\n");165out.print("Content-Length: 0\r\n");166out.print("\r\n");167out.flush();168out.close();169in.close();170}171172public void serverAccept(Socket s) throws IOException {173BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));174PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));175String line = null;176do {177line = in.readLine();178} while (line != null && line.length() != 0);179180// Send 100-Continue181out.print("HTTP/1.1 100 Continue\r\n");182out.print("\r\n");183out.flush();184// Then read the body185char[] cbuf = new char[512];186in.read(cbuf);187188/* Force the server to not respond for more that the expect 100-Continue189* timeout set by the HTTP handler (5000 millis). This ensures the190* timeout is correctly resets the default read timeout, infinity.191* See 6993490. */192System.out.println("server sleeping...");193try {Thread.sleep(6000); } catch (InterruptedException e) {}194195// finally send the 200 OK196out.print("HTTP/1.1 200 OK");197out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");198out.print("Connection: close\r\n");199out.print("Content-Length: 0\r\n");200out.print("\r\n");201out.flush();202out.close();203in.close();204}205206public void serverIgnore(Socket s) throws IOException {207BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));208PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));209String line = null;210do {211line = in.readLine();212} while (line != null && line.length() != 0);213214// Then read the body215char[] cbuf = new char[512];216int l = in.read(cbuf);217// finally send the 200 OK218out.print("HTTP/1.1 200 OK");219out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");220out.print("Content-Length: 0\r\n");221out.print("Connection: close\r\n");222out.print("\r\n");223out.flush();224out.close();225in.close();226}227}228229230