Path: blob/master/test/jdk/com/sun/net/httpserver/Test4.java
41152 views
/*1* Copyright (c) 2005, 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 627001526* @summary Light weight HTTP server27* @run main/othervm -Dsun.net.httpserver.idleInterval=4 Test428* @run main/othervm -Djava.net.preferIPv6Addresses=true29* -Dsun.net.httpserver.idleInterval=4 Test430*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.util.regex.*;37import java.io.*;38import java.net.*;39import java.security.*;40import javax.net.ssl.*;4142/**43* Test pipe-lining (block after read)44*/45public class Test4 extends Test {46static int count = 1;47public static void main (String[] args) throws Exception {48System.out.print ("Test4: ");49Handler handler = new Handler();50InetAddress loopback = InetAddress.getLoopbackAddress();51InetSocketAddress addr = new InetSocketAddress(loopback, 0);52HttpServer server = HttpServer.create (addr, 0);53int port = server.getAddress().getPort();54HttpContext c2 = server.createContext ("/test", handler);55c2.getAttributes().put ("name", "This is the http handler");5657ExecutorService exec = Executors.newCachedThreadPool();58server.setExecutor (exec);59try {60server.start ();61doClient(port);62System.out.println ("OK");63} finally {64delay();65if (server != null)66server.stop(2);67if (exec != null)68exec.shutdown();69}70}7172static class Handler implements HttpHandler {73volatile int invocation = 0;74public void handle (HttpExchange t)75throws IOException76{77InputStream is = t.getRequestBody();78Headers map = t.getRequestHeaders();79Headers rmap = t.getResponseHeaders();80int x = invocation ++;81rmap.set ("XTest", Integer.toString (x));8283switch (x) {84case 0:85checkBody (is, body1);86try {Thread.sleep (2000); } catch (Exception e) {}87break;88case 1:89checkBody (is, body2);90try {Thread.sleep (1000); } catch (Exception e) {}91break;92case 2:93checkBody (is, body3);94break;95case 3:96checkBody (is, body4);97break;98}99t.sendResponseHeaders (200, -1);100t.close();101}102}103104static void checkBody (InputStream is, String cmp) throws IOException {105byte [] b = new byte [1024];106int count = 0, c;107while ((c=is.read(b, count, b.length-count)) != -1) {108count+=c;109}110is.close();111String s = new String (b, 0, count, "ISO8859_1");112if (!s.equals (cmp)) {113throw new RuntimeException ("strings not equal");114}115}116117static String body1 = "1234567890abcdefghij";118static String body2 = "2234567890abcdefghij0123456789";119static String body3 = "3wertyuiop";120static String body4 = "4234567890";121122static String result =123"HTTP/1.1 200 OK.*Xtest: 0.*"+124"HTTP/1.1 200 OK.*Xtest: 1.*"+125"HTTP/1.1 200 OK.*Xtest: 2.*"+126"HTTP/1.1 200 OK.*Xtest: 3.*";127128public static void doClient (int port) throws Exception {129String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+130"\r\n" +body1 +131"GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+132"\r\n"+ body2 +133"GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+134"\r\n"+ body3 +135"GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+136"\r\n"+body4;137138Socket socket = new Socket (InetAddress.getLoopbackAddress(), port);139OutputStream os = socket.getOutputStream();140os.write (s.getBytes());141InputStream is = socket.getInputStream();142int c, count=0;143byte[] b = new byte [1024];144while ((c=is.read(b, count, b.length-count)) != -1) {145count +=c;146}147is.close();148socket.close();149s = new String (b,0,count, "ISO8859_1");150if (!compare (s, result)) {151throw new RuntimeException ("wrong string result");152}153}154155static boolean compare (String s, String result) {156Pattern pattern = Pattern.compile (result,157Pattern.DOTALL|Pattern.CASE_INSENSITIVE158);159Matcher matcher = pattern.matcher (s);160return matcher.matches();161}162163}164165166