Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test5.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 Test528*/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 (no block)42*/4344public class Test5 extends Test {45static int count = 1;46public static void main (String[] args) throws Exception {47System.out.print ("Test5: ");48Handler handler = new Handler();49InetSocketAddress addr = new InetSocketAddress (0);50HttpServer server = HttpServer.create (addr, 0);51int port = server.getAddress().getPort();52HttpContext c2 = server.createContext ("/test", handler);53c2.getAttributes().put ("name", "This is the http handler");5455ExecutorService exec = Executors.newCachedThreadPool();56server.setExecutor (exec);57try {58server.start ();59doClient(port);60System.out.println ("OK");61} finally {62delay ();63if (server != null)64server.stop(2);65if (exec != null)66exec.shutdown();67}68}6970static class Handler implements HttpHandler {71volatile int invocation = 0;72public void handle (HttpExchange t)73throws IOException74{75InputStream is = t.getRequestBody();76Headers map = t.getRequestHeaders();77Headers rmap = t.getResponseHeaders();78int x = invocation ++;79rmap.set ("XTest", Integer.toString (x));8081switch (x) {82case 0:83checkBody (is, body1);84break;85case 1:86checkBody (is, body2);87break;88case 2:89checkBody (is, body3);90break;91case 3:92checkBody (is, body4);93break;94}95t.sendResponseHeaders (200, -1);96t.close();97}98}99100static void checkBody (InputStream is, String cmp) throws IOException {101byte [] b = new byte [1024];102int count = 0, c;103while ((c=is.read(b, count, b.length-count)) != -1) {104count+=c;105}106is.close();107String s = new String (b, 0, count, "ISO8859_1");108if (!s.equals (cmp)) {109throw new RuntimeException ("strings not equal");110}111}112113static String body1 = "1234567890abcdefghij";114static String body2 = "2234567890abcdefghij0123456789";115static String body3 = "3wertyuiop";116static String body4 = "4234567890";117118static String result =119"HTTP/1.1 200 OK.*Xtest: 0.*"+120"HTTP/1.1 200 OK.*Xtest: 1.*"+121"HTTP/1.1 200 OK.*Xtest: 2.*"+122"HTTP/1.1 200 OK.*Xtest: 3.*";123124public static void doClient (int port) throws Exception {125String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+126"\r\n" +body1 +127"GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+128"\r\n"+ body2 +129"GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+130"\r\n"+ body3 +131"GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+132"\r\n"+body4;133134Socket socket = new Socket ("localhost", port);135OutputStream os = socket.getOutputStream();136os.write (s.getBytes());137InputStream is = socket.getInputStream();138int c, count=0;139byte[] b = new byte [1024];140while ((c=is.read(b, count, b.length-count)) != -1) {141count +=c;142}143is.close();144socket.close();145s = new String (b,0,count, "ISO8859_1");146if (!compare (s, result)) {147throw new RuntimeException ("wrong string result");148}149}150151static boolean compare (String s, String result) {152Pattern pattern = Pattern.compile (result,153Pattern.DOTALL|Pattern.CASE_INSENSITIVE154);155Matcher matcher = pattern.matcher (s);156return matcher.matches();157}158}159160161