Path: blob/master/test/jdk/sun/net/www/http/HttpClient/B6726695.java
41154 views
/*1* Copyright (c) 2009, 2019, 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 should support 'Expect: 100-contimue' headers for PUT27* @library /test/lib28* @run main B672669529* @run main/othervm -Djava.net.preferIPv6Addresses=true B672669530*/3132import java.net.*;33import java.io.*;3435import jdk.test.lib.net.URIBuilder;3637public class B6726695 extends Thread {38private ServerSocket server = null;39private int port = 0;40private byte[] data = new byte[512];41private String boundary = "----------------7774563516523621";4243public static void main(String[] args) throws Exception {44B6726695 test = new B6726695();45// Exit even if server is still running46test.setDaemon(true);47// start server48test.start();49// run test50test.test();51}5253public B6726695() {54try {55server = new ServerSocket();56server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));57port = server.getLocalPort();58} catch (IOException e) {59e.printStackTrace();60}61}6263public void test() throws Exception {64/**65* This is a hardcoded test. The server side expects 3 requests with a66* Expect: 100-continue header. It will reject the 1st one and accept67* the second one. Thus allowing us to test both scenarios.68* The 3rd case is the simulation of a server that just plains ignore69* the Expect: 100-Continue header. So the POST should proceed after70* a timeout.71*/72URL url = URIBuilder.newBuilder()73.scheme("http")74.loopback()75.port(port)76.path("/foo")77.toURL();7879// 1st Connection. Should be rejected. I.E. get a ProtocolException80URLConnection con = url.openConnection(Proxy.NO_PROXY);81HttpURLConnection http = (HttpURLConnection) con;82http.setRequestMethod("POST");83http.setRequestProperty("Expect", "100-Continue");84http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);85http.setDoOutput(true);86http.setFixedLengthStreamingMode(512);87OutputStream out = null;88int errorCode = -1;89try {90out = http.getOutputStream();91} catch (ProtocolException e) {92errorCode = http.getResponseCode();93}94if (errorCode != 417) {95throw new RuntimeException("Didn't get the ProtocolException");96}9798// 2nd connection. Should be accepted by server.99http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);100http.setRequestMethod("POST");101http.setRequestProperty("Expect", "100-Continue");102http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);103http.setDoOutput(true);104http.setFixedLengthStreamingMode(data.length);105out = null;106try {107out = http.getOutputStream();108} catch (ProtocolException e) {109}110if (out == null) {111throw new RuntimeException("Didn't get an OutputStream");112}113out.write(data);114out.flush();115errorCode = http.getResponseCode();116if (errorCode != 200) {117throw new RuntimeException("Response code is " + errorCode);118}119out.close();120121// 3rd connection. Simulate a server that doesn't implement 100-continue122http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);123http.setRequestMethod("POST");124http.setRequestProperty("Expect", "100-Continue");125http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);126http.setDoOutput(true);127http.setFixedLengthStreamingMode(data.length);128out = null;129try {130out = http.getOutputStream();131} catch (ProtocolException e) {132}133if (out == null) {134throw new RuntimeException("Didn't get an OutputStream");135}136out.write(data);137out.flush();138out.close();139errorCode = http.getResponseCode();140if (errorCode != 200) {141throw new RuntimeException("Response code is " + errorCode);142}143}144145146@Override147public void run() {148try {149// Fist connection: don't accetpt the request150Socket s = server.accept();151serverReject(s);152// Second connection: accept the request (send 100-continue)153s = server.accept();154serverAccept(s);155// 3rd connection: just ignore the 'Expect:' header156s = server.accept();157serverIgnore(s);158} catch (IOException e) {159e.printStackTrace();160} finally {161try { server.close(); } catch (IOException unused) {}162}163}164165public void serverReject(Socket s) throws IOException {166BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));167PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));168String line = null;169do {170line = in.readLine();171} while (line != null && line.length() != 0);172173out.print("HTTP/1.1 417 Expectation Failed\r\n");174out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");175out.print("Connection: close\r\n");176out.print("Content-Length: 0\r\n");177out.print("\r\n");178out.flush();179out.close();180in.close();181}182183public void serverAccept(Socket s) throws IOException {184BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));185PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));186String line = null;187do {188line = in.readLine();189} while (line != null && line.length() != 0);190191// Send 100-Continue192out.print("HTTP/1.1 100 Continue\r\n");193out.print("\r\n");194out.flush();195// Then read the body196char[] cbuf = new char[512];197in.read(cbuf);198199/* Force the server to not respond for more that the expect 100-Continue200* timeout set by the HTTP handler (5000 millis). This ensures the201* timeout is correctly resets the default read timeout, infinity.202* See 6993490. */203System.out.println("server sleeping...");204try {Thread.sleep(6000); } catch (InterruptedException e) {}205206// finally send the 200 OK207out.print("HTTP/1.1 200 OK");208out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");209out.print("Connection: close\r\n");210out.print("Content-Length: 0\r\n");211out.print("\r\n");212out.flush();213out.close();214in.close();215}216217public void serverIgnore(Socket s) throws IOException {218BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));219PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));220String line = null;221do {222line = in.readLine();223} while (line != null && line.length() != 0);224225// Then read the body226char[] cbuf = new char[512];227int l = in.read(cbuf);228// finally send the 200 OK229out.print("HTTP/1.1 200 OK");230out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");231out.print("Content-Length: 0\r\n");232out.print("Connection: close\r\n");233out.print("\r\n");234out.flush();235out.close();236in.close();237}238}239240241