Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test4.java
38855 views
/*1* Copyright (c) 2005, 2006, 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*/2930import com.sun.net.httpserver.*;3132import java.util.*;33import java.util.concurrent.*;34import java.util.regex.*;35import java.io.*;36import java.net.*;37import java.security.*;38import javax.net.ssl.*;3940/**41* Test pipe-lining (block after read)42*/43public class Test4 extends Test {44static int count = 1;45public static void main (String[] args) throws Exception {46System.out.print ("Test4: ");47Handler handler = new Handler();48InetSocketAddress addr = new InetSocketAddress (0);49HttpServer server = HttpServer.create (addr, 0);50int port = server.getAddress().getPort();51HttpContext c2 = server.createContext ("/test", handler);52c2.getAttributes().put ("name", "This is the http handler");5354ExecutorService exec = Executors.newCachedThreadPool();55server.setExecutor (exec);56try {57server.start ();58doClient(port);59System.out.println ("OK");60} finally {61delay();62if (server != null)63server.stop(2);64if (exec != null)65exec.shutdown();66}67}6869static class Handler implements HttpHandler {70volatile int invocation = 0;71public void handle (HttpExchange t)72throws IOException73{74InputStream is = t.getRequestBody();75Headers map = t.getRequestHeaders();76Headers rmap = t.getResponseHeaders();77int x = invocation ++;78rmap.set ("XTest", Integer.toString (x));7980switch (x) {81case 0:82checkBody (is, body1);83try {Thread.sleep (2000); } catch (Exception e) {}84break;85case 1:86checkBody (is, body2);87try {Thread.sleep (1000); } catch (Exception e) {}88break;89case 2:90checkBody (is, body3);91break;92case 3:93checkBody (is, body4);94break;95}96t.sendResponseHeaders (200, -1);97t.close();98}99}100101static void checkBody (InputStream is, String cmp) throws IOException {102byte [] b = new byte [1024];103int count = 0, c;104while ((c=is.read(b, count, b.length-count)) != -1) {105count+=c;106}107is.close();108String s = new String (b, 0, count, "ISO8859_1");109if (!s.equals (cmp)) {110throw new RuntimeException ("strings not equal");111}112}113114static String body1 = "1234567890abcdefghij";115static String body2 = "2234567890abcdefghij0123456789";116static String body3 = "3wertyuiop";117static String body4 = "4234567890";118119static String result =120"HTTP/1.1 200 OK.*Xtest: 0.*"+121"HTTP/1.1 200 OK.*Xtest: 1.*"+122"HTTP/1.1 200 OK.*Xtest: 2.*"+123"HTTP/1.1 200 OK.*Xtest: 3.*";124125public static void doClient (int port) throws Exception {126String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+127"\r\n" +body1 +128"GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+129"\r\n"+ body2 +130"GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+131"\r\n"+ body3 +132"GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+133"\r\n"+body4;134135Socket socket = new Socket ("localhost", port);136OutputStream os = socket.getOutputStream();137os.write (s.getBytes());138InputStream is = socket.getInputStream();139int c, count=0;140byte[] b = new byte [1024];141while ((c=is.read(b, count, b.length-count)) != -1) {142count +=c;143}144is.close();145socket.close();146s = new String (b,0,count, "ISO8859_1");147if (!compare (s, result)) {148throw new RuntimeException ("wrong string result");149}150}151152static boolean compare (String s, String result) {153Pattern pattern = Pattern.compile (result,154Pattern.DOTALL|Pattern.CASE_INSENSITIVE155);156Matcher matcher = pattern.matcher (s);157return matcher.matches();158}159160}161162163