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