blob: 40a6c9cb446087024e6beb46dd77cafd5f59792b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 6270015
27 * @summary Light weight HTTP server
28 * @run main/othervm -Dsun.net.httpserver.idleInterval=4 Test3
29 */
30
31import com.sun.net.httpserver.*;
32
33import java.util.*;
34import java.util.concurrent.*;
35import java.util.regex.*;
36import java.util.regex.Pattern.*;
37import java.io.*;
38import java.net.*;
39import java.security.*;
40import javax.net.ssl.*;
41
42/**
43 * Test pipe-lining over http
44 */
45
46public class Test3 extends Test {
47 static int count = 1;
48 public static void main (String[] args) throws Exception {
49 System.out.print ("Test3: ");
50 Handler handler = new Handler();
51 InetSocketAddress addr = new InetSocketAddress (0);
52 HttpServer server = HttpServer.create (addr, 0);
53 int port = server.getAddress().getPort();
54 HttpContext c2 = server.createContext ("/test", handler);
55 c2.getAttributes().put ("name", "This is the http handler");
56
57 ExecutorService exec = Executors.newCachedThreadPool();
58 server.setExecutor (exec);
59 try {
60 server.start ();
61 doClient(port);
62 System.out.println ("OK");
63 } finally {
64 delay();
65 server.stop(2);
66 exec.shutdown();
67 }
68 }
69
70 static class Handler implements HttpHandler {
71 volatile int invocation = 0;
72 public void handle (HttpExchange t)
73 throws IOException
74 {
75 InputStream is = t.getRequestBody();
76 Headers map = t.getRequestHeaders();
77 Headers rmap = t.getResponseHeaders();
78 int x = invocation ++;
79 rmap.set ("XTest", Integer.toString (x));
80
81 switch (x) {
82 case 0:
83 try {Thread.sleep (2000); } catch (Exception e) {}
84 checkBody (is, body1);
85 break;
86 case 1:
87 try {Thread.sleep (1000); } catch (Exception e) {}
88 checkBody (is, body2);
89 break;
90 case 2:
91 checkBody (is, body3);
92 break;
93 case 3:
94 checkBody (is, body4);
95 break;
96 }
97 t.sendResponseHeaders (200, -1);
98 t.close();
99 }
100 }
101
102 static void checkBody (InputStream is, String cmp) throws IOException {
103 byte [] b = new byte [1024];
104 int count = 0, c;
105 while ((c=is.read(b, count, b.length-count)) != -1) {
106 count+=c;
107 }
108 is.close();
109 String s = new String (b, 0, count, "ISO8859_1");
110 if (!s.equals (cmp)) {
111 throw new RuntimeException ("strings not equal");
112 }
113 }
114
115 static String body1 = "1234567890abcdefghij";
116 static String body2 = "2234567890abcdefghij0123456789";
117 static String body3 = "3wertyuiop";
118 static String body4 = "4234567890";
119
120 static String result =
121 "HTTP/1.1 200 OK.*Xtest: 0.*"+
122 "HTTP/1.1 200 OK.*Xtest: 1.*"+
123 "HTTP/1.1 200 OK.*Xtest: 2.*"+
124 "HTTP/1.1 200 OK.*Xtest: 3.*";
125
126 public static void doClient (int port) throws Exception {
127 String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+
128 "\r\n" +body1 +
129 "GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+
130 "\r\n"+ body2 +
131 "GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+
132 "\r\n"+ body3 +
133 "GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+
134 "\r\n"+body4;
135
136 Socket socket = new Socket ("localhost", port);
137 OutputStream os = socket.getOutputStream();
138 os.write (s.getBytes());
139 InputStream is = socket.getInputStream();
140 int c, count=0;
141 byte[] b = new byte [1024];
142 while ((c=is.read(b, count, b.length-count)) != -1) {
143 count +=c;
144 }
145 is.close();
146 socket.close();
147 s = new String (b,0,count, "ISO8859_1");
148 if (!compare (s, result)) {
149 throw new RuntimeException ("wrong string result");
150 }
151 }
152
153 static boolean compare (String s, String result) {
154 Pattern pattern = Pattern.compile (result,
155 Pattern.DOTALL|Pattern.CASE_INSENSITIVE
156 );
157 Matcher matcher = pattern.matcher (s);
158 return matcher.matches();
159 }
160}