Path: blob/master/test/jdk/com/sun/net/httpserver/Test3.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 Test328* @run main/othervm -Djava.net.preferIPv6Addresses=true29* -Dsun.net.httpserver.idleInterval=4 Test330*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.util.regex.*;37import java.util.regex.Pattern.*;38import java.io.*;39import java.net.*;40import java.security.*;41import javax.net.ssl.*;4243/**44* Test pipe-lining over http45*/4647public class Test3 extends Test {48static int count = 1;49public static void main (String[] args) throws Exception {50System.out.print ("Test3: ");51Handler handler = new Handler();52InetAddress loopback = InetAddress.getLoopbackAddress();53InetSocketAddress addr = new InetSocketAddress(loopback, 0);54HttpServer server = HttpServer.create (addr, 0);55int port = server.getAddress().getPort();56HttpContext c2 = server.createContext ("/test", handler);57c2.getAttributes().put ("name", "This is the http handler");5859ExecutorService exec = Executors.newCachedThreadPool();60server.setExecutor (exec);61try {62server.start ();63doClient(port);64System.out.println ("OK");65} finally {66delay();67if (server != null)68server.stop(2);69if (exec != null)70exec.shutdown();71}72}7374static class Handler implements HttpHandler {75volatile int invocation = 0;76public void handle (HttpExchange t)77throws IOException78{79InputStream is = t.getRequestBody();80Headers map = t.getRequestHeaders();81Headers rmap = t.getResponseHeaders();82int x = invocation ++;83rmap.set ("XTest", Integer.toString (x));8485switch (x) {86case 0:87try {Thread.sleep (2000); } catch (Exception e) {}88checkBody (is, body1);89break;90case 1:91try {Thread.sleep (1000); } catch (Exception e) {}92checkBody (is, body2);93break;94case 2:95checkBody (is, body3);96break;97case 3:98checkBody (is, body4);99break;100}101t.sendResponseHeaders (200, -1);102t.close();103}104}105106static void checkBody (InputStream is, String cmp) throws IOException {107byte [] b = new byte [1024];108int count = 0, c;109while ((c=is.read(b, count, b.length-count)) != -1) {110count+=c;111}112is.close();113String s = new String (b, 0, count, "ISO8859_1");114if (!s.equals (cmp)) {115throw new RuntimeException ("strings not equal");116}117}118119static String body1 = "1234567890abcdefghij";120static String body2 = "2234567890abcdefghij0123456789";121static String body3 = "3wertyuiop";122static String body4 = "4234567890";123124static String result =125"HTTP/1.1 200 OK.*Xtest: 0.*"+126"HTTP/1.1 200 OK.*Xtest: 1.*"+127"HTTP/1.1 200 OK.*Xtest: 2.*"+128"HTTP/1.1 200 OK.*Xtest: 3.*";129130public static void doClient (int port) throws Exception {131String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+132"\r\n" +body1 +133"GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+134"\r\n"+ body2 +135"GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+136"\r\n"+ body3 +137"GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+138"\r\n"+body4;139140Socket socket = new Socket (InetAddress.getLoopbackAddress(), port);141OutputStream os = socket.getOutputStream();142os.write (s.getBytes());143InputStream is = socket.getInputStream();144int c, count=0;145byte[] b = new byte [1024];146while ((c=is.read(b, count, b.length-count)) != -1) {147count +=c;148}149is.close();150socket.close();151s = new String (b,0,count, "ISO8859_1");152if (!compare (s, result)) {153throw new RuntimeException ("wrong string result");154}155}156157static boolean compare (String s, String result) {158Pattern pattern = Pattern.compile (result,159Pattern.DOTALL|Pattern.CASE_INSENSITIVE160);161Matcher matcher = pattern.matcher (s);162return matcher.matches();163}164}165166167