|
1 /* |
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * @test |
|
26 * @bug 8068795 |
|
27 * @summary HttpServer missing tailing space for some response codes |
|
28 * @author lev.priima@oracle.com |
|
29 */ |
|
30 |
|
31 import java.net.InetSocketAddress; |
|
32 import java.io.InputStreamReader; |
|
33 import java.io.IOException; |
|
34 import java.io.BufferedReader; |
|
35 import java.io.OutputStreamWriter; |
|
36 import java.io.PrintWriter; |
|
37 import java.net.Socket; |
|
38 import java.util.concurrent.ExecutorService; |
|
39 import java.util.concurrent.Executors; |
|
40 import com.sun.net.httpserver.HttpExchange; |
|
41 import com.sun.net.httpserver.HttpHandler; |
|
42 import com.sun.net.httpserver.HttpServer; |
|
43 |
|
44 public class MissingTrailingSpace { |
|
45 |
|
46 private static final int noMsgCode = 207; |
|
47 private static final String someContext = "/context"; |
|
48 |
|
49 public static void main(String[] args) throws Exception { |
|
50 HttpServer server = HttpServer.create(new InetSocketAddress(0), 0); |
|
51 try { |
|
52 server.setExecutor(Executors.newFixedThreadPool(1)); |
|
53 server.createContext(someContext, new HttpHandler() { |
|
54 @Override |
|
55 public void handle(HttpExchange msg) { |
|
56 try { |
|
57 try { |
|
58 msg.sendResponseHeaders(noMsgCode, -1); |
|
59 } catch(IOException ioe) { |
|
60 ioe.printStackTrace(); |
|
61 } |
|
62 } finally { |
|
63 msg.close(); |
|
64 } |
|
65 } |
|
66 }); |
|
67 server.start(); |
|
68 System.out.println("Server started at port " |
|
69 + server.getAddress().getPort()); |
|
70 |
|
71 runRawSocketHttpClient("localhost", server.getAddress().getPort()); |
|
72 } finally { |
|
73 ((ExecutorService)server.getExecutor()).shutdown(); |
|
74 server.stop(0); |
|
75 } |
|
76 System.out.println("Server finished."); |
|
77 } |
|
78 |
|
79 static void runRawSocketHttpClient(String hostname, int port) |
|
80 throws Exception |
|
81 { |
|
82 Socket socket = null; |
|
83 PrintWriter writer = null; |
|
84 BufferedReader reader = null; |
|
85 final String CRLF = "\r\n"; |
|
86 try { |
|
87 socket = new Socket(hostname, port); |
|
88 writer = new PrintWriter(new OutputStreamWriter( |
|
89 socket.getOutputStream())); |
|
90 System.out.println("Client connected by socket: " + socket); |
|
91 |
|
92 writer.print("GET " + someContext + "/ HTTP/1.1" + CRLF); |
|
93 writer.print("User-Agent: Java/" |
|
94 + System.getProperty("java.version") |
|
95 + CRLF); |
|
96 writer.print("Host: " + hostname + CRLF); |
|
97 writer.print("Accept: */*" + CRLF); |
|
98 writer.print("Connection: keep-alive" + CRLF); |
|
99 writer.print(CRLF); // Important, else the server will expect that |
|
100 // there's more into the request. |
|
101 writer.flush(); |
|
102 System.out.println("Client wrote rquest to socket: " + socket); |
|
103 |
|
104 reader = new BufferedReader(new InputStreamReader( |
|
105 socket.getInputStream())); |
|
106 System.out.println("Client start reading from server:" ); |
|
107 String line = reader.readLine(); |
|
108 if ( !line.endsWith(" ") ) { |
|
109 throw new RuntimeException("respond to unknown code " |
|
110 + noMsgCode |
|
111 + " doesn't return space at the end of the first header.\n" |
|
112 + "Should be: " + "\"" + line + " \"" |
|
113 + ", but returns: " + "\"" + line + "\"."); |
|
114 } |
|
115 for (; line != null; line = reader.readLine()) { |
|
116 if (line.isEmpty()) { |
|
117 break; |
|
118 } |
|
119 System.out.println("\"" + line + "\""); |
|
120 } |
|
121 System.out.println("Client finished reading from server" ); |
|
122 } finally { |
|
123 if (reader != null) |
|
124 try { |
|
125 reader.close(); |
|
126 } catch (IOException logOrIgnore) { |
|
127 logOrIgnore.printStackTrace(); |
|
128 } |
|
129 if (writer != null) { |
|
130 writer.close(); |
|
131 } |
|
132 if (socket != null) { |
|
133 try { |
|
134 socket.close(); |
|
135 } catch (IOException logOrIgnore) { |
|
136 logOrIgnore.printStackTrace(); |
|
137 } |
|
138 } |
|
139 } |
|
140 System.out.println("Client finished." ); |
|
141 } |
|
142 } |
|
143 |