Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test3.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 Test328*/2930import com.sun.net.httpserver.*;3132import java.util.*;33import java.util.concurrent.*;34import java.util.regex.*;35import java.util.regex.Pattern.*;36import java.io.*;37import java.net.*;38import java.security.*;39import javax.net.ssl.*;4041/**42* Test pipe-lining over http43*/4445public class Test3 extends Test {46static int count = 1;47public static void main (String[] args) throws Exception {48System.out.print ("Test3: ");49Handler handler = new Handler();50InetSocketAddress addr = new InetSocketAddress (0);51HttpServer server = HttpServer.create (addr, 0);52int port = server.getAddress().getPort();53HttpContext c2 = server.createContext ("/test", handler);54c2.getAttributes().put ("name", "This is the http handler");5556ExecutorService exec = Executors.newCachedThreadPool();57server.setExecutor (exec);58try {59server.start ();60doClient(port);61System.out.println ("OK");62} finally {63delay();64if (server != null)65server.stop(2);66if (exec != null)67exec.shutdown();68}69}7071static class Handler implements HttpHandler {72volatile int invocation = 0;73public void handle (HttpExchange t)74throws IOException75{76InputStream is = t.getRequestBody();77Headers map = t.getRequestHeaders();78Headers rmap = t.getResponseHeaders();79int x = invocation ++;80rmap.set ("XTest", Integer.toString (x));8182switch (x) {83case 0:84try {Thread.sleep (2000); } catch (Exception e) {}85checkBody (is, body1);86break;87case 1:88try {Thread.sleep (1000); } catch (Exception e) {}89checkBody (is, body2);90break;91case 2:92checkBody (is, body3);93break;94case 3:95checkBody (is, body4);96break;97}98t.sendResponseHeaders (200, -1);99t.close();100}101}102103static void checkBody (InputStream is, String cmp) throws IOException {104byte [] b = new byte [1024];105int count = 0, c;106while ((c=is.read(b, count, b.length-count)) != -1) {107count+=c;108}109is.close();110String s = new String (b, 0, count, "ISO8859_1");111if (!s.equals (cmp)) {112throw new RuntimeException ("strings not equal");113}114}115116static String body1 = "1234567890abcdefghij";117static String body2 = "2234567890abcdefghij0123456789";118static String body3 = "3wertyuiop";119static String body4 = "4234567890";120121static String result =122"HTTP/1.1 200 OK.*Xtest: 0.*"+123"HTTP/1.1 200 OK.*Xtest: 1.*"+124"HTTP/1.1 200 OK.*Xtest: 2.*"+125"HTTP/1.1 200 OK.*Xtest: 3.*";126127public static void doClient (int port) throws Exception {128String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+129"\r\n" +body1 +130"GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+131"\r\n"+ body2 +132"GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+133"\r\n"+ body3 +134"GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+135"\r\n"+body4;136137Socket socket = new Socket ("localhost", port);138OutputStream os = socket.getOutputStream();139os.write (s.getBytes());140InputStream is = socket.getInputStream();141int c, count=0;142byte[] b = new byte [1024];143while ((c=is.read(b, count, b.length-count)) != -1) {144count +=c;145}146is.close();147socket.close();148s = new String (b,0,count, "ISO8859_1");149if (!compare (s, result)) {150throw new RuntimeException ("wrong string result");151}152}153154static boolean compare (String s, String result) {155Pattern pattern = Pattern.compile (result,156Pattern.DOTALL|Pattern.CASE_INSENSITIVE157);158Matcher matcher = pattern.matcher (s);159return matcher.matches();160}161}162163164