Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/sun/rmi/transport/proxy/CGIHandler.java
+++ new/src/share/classes/sun/rmi/transport/proxy/CGIHandler.java
1 1 /*
2 2 * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25 package sun.rmi.transport.proxy;
↓ open down ↓ |
25 lines elided |
↑ open up ↑ |
26 26
27 27 import java.io.*;
28 28 import java.net.*;
29 29 import java.util.Hashtable;
30 30
31 31 /**
32 32 * CGIClientException is thrown when an error is detected
33 33 * in a client's request.
34 34 */
35 35 class CGIClientException extends Exception {
36 + private static final long serialVersionUID = 8147981687059865216L;
36 37
37 38 public CGIClientException(String s) {
38 39 super(s);
39 40 }
40 41 }
41 42
42 43 /**
43 44 * CGIServerException is thrown when an error occurs here on the server.
44 45 */
45 46 class CGIServerException extends Exception {
46 47
48 + private static final long serialVersionUID = 6928425456704527017L;
49 +
47 50 public CGIServerException(String s) {
48 51 super(s);
49 52 }
50 53 }
51 54
52 55 /**
53 56 * CGICommandHandler is the interface to an object that handles a
54 57 * particular supported command.
55 58 */
56 59 interface CGICommandHandler {
57 60
58 61 /**
59 62 * Return the string form of the command
60 63 * to be recognized in the query string.
61 64 */
62 65 public String getName();
63 66
64 67 /**
65 68 * Execute the command with the given string as parameter.
66 69 */
67 70 public void execute(String param) throws CGIClientException, CGIServerException;
68 71 }
69 72
70 73 /**
71 74 * The CGIHandler class contains methods for executing as a CGI program.
72 75 * The main function interprets the query string as a command of the form
73 76 * "<command>=<parameters>".
74 77 *
75 78 * This class depends on the CGI 1.0 environment variables being set as
76 79 * properties of the same name in this Java VM.
77 80 *
78 81 * All data and methods of this class are static because they are specific
79 82 * to this particular CGI process.
80 83 */
81 84 public final class CGIHandler {
82 85
83 86 /* get CGI parameters that we need */
84 87 static int ContentLength;
85 88 static String QueryString;
86 89 static String RequestMethod;
87 90 static String ServerName;
88 91 static int ServerPort;
89 92
90 93 static {
91 94 java.security.AccessController.doPrivileged(
92 95 new java.security.PrivilegedAction<Void>() {
93 96 public Void run() {
94 97 ContentLength =
95 98 Integer.getInteger("CONTENT_LENGTH", 0).intValue();
96 99 QueryString = System.getProperty("QUERY_STRING", "");
97 100 RequestMethod = System.getProperty("REQUEST_METHOD", "");
98 101 ServerName = System.getProperty("SERVER_NAME", "");
99 102 ServerPort = Integer.getInteger("SERVER_PORT", 0).intValue();
100 103 return null;
101 104 }
102 105 });
103 106 }
↓ open down ↓ |
47 lines elided |
↑ open up ↑ |
104 107
105 108 /* list of handlers for supported commands */
106 109 private static CGICommandHandler commands[] = {
107 110 new CGIForwardCommand(),
108 111 new CGIGethostnameCommand(),
109 112 new CGIPingCommand(),
110 113 new CGITryHostnameCommand()
111 114 };
112 115
113 116 /* construct table mapping command strings to handlers */
114 - private static Hashtable commandLookup;
117 + private static Hashtable<String, CGICommandHandler> commandLookup;
115 118 static {
116 - commandLookup = new Hashtable();
119 + commandLookup = new Hashtable<>();
117 120 for (int i = 0; i < commands.length; ++ i)
118 121 commandLookup.put(commands[i].getName(), commands[i]);
119 122 }
120 123
121 124 /* prevent instantiation of this class */
122 125 private CGIHandler() {}
123 126
124 127 /**
125 128 * Execute command given in query string on URL. The string before
126 129 * the first '=' is interpreted as the command name, and the string
127 130 * after the first '=' is the parameters to the command.
128 131 */
129 132 public static void main(String args[])
130 133 {
131 134 try {
132 135 String command, param;
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
133 136 int delim = QueryString.indexOf("=");
134 137 if (delim == -1) {
135 138 command = QueryString;
136 139 param = "";
137 140 }
138 141 else {
139 142 command = QueryString.substring(0, delim);
140 143 param = QueryString.substring(delim + 1);
141 144 }
142 145 CGICommandHandler handler =
143 - (CGICommandHandler) commandLookup.get(command);
146 + commandLookup.get(command);
144 147 if (handler != null)
145 148 try {
146 149 handler.execute(param);
147 150 } catch (CGIClientException e) {
148 151 returnClientError(e.getMessage());
149 152 } catch (CGIServerException e) {
150 153 returnServerError(e.getMessage());
151 154 }
152 155 else
153 156 returnClientError("invalid command: " + command);
154 157 } catch (Exception e) {
155 158 returnServerError("internal error: " + e.getMessage());
156 159 }
157 160 System.exit(0);
158 161 }
159 162
160 163 /**
161 164 * Return an HTML error message indicating there was error in
162 165 * the client's request.
163 166 */
164 167 private static void returnClientError(String message)
165 168 {
166 169 System.out.println("Status: 400 Bad Request: " + message);
167 170 System.out.println("Content-type: text/html");
168 171 System.out.println("");
169 172 System.out.println("<HTML>" +
170 173 "<HEAD><TITLE>Java RMI Client Error" +
171 174 "</TITLE></HEAD>" +
172 175 "<BODY>");
173 176 System.out.println("<H1>Java RMI Client Error</H1>");
174 177 System.out.println("");
175 178 System.out.println(message);
176 179 System.out.println("</BODY></HTML>");
177 180 System.exit(1);
178 181 }
179 182
180 183 /**
181 184 * Return an HTML error message indicating an error occurred
182 185 * here on the server.
183 186 */
184 187 private static void returnServerError(String message)
185 188 {
186 189 System.out.println("Status: 500 Server Error: " + message);
187 190 System.out.println("Content-type: text/html");
188 191 System.out.println("");
189 192 System.out.println("<HTML>" +
190 193 "<HEAD><TITLE>Java RMI Server Error" +
191 194 "</TITLE></HEAD>" +
192 195 "<BODY>");
↓ open down ↓ |
39 lines elided |
↑ open up ↑ |
193 196 System.out.println("<H1>Java RMI Server Error</H1>");
194 197 System.out.println("");
195 198 System.out.println(message);
196 199 System.out.println("</BODY></HTML>");
197 200 System.exit(1);
198 201 }
199 202 }
200 203
201 204 /**
202 205 * "forward" command: Forward request body to local port on the server,
203 - * and send reponse back to client.
206 + * and send response back to client.
204 207 */
205 208 final class CGIForwardCommand implements CGICommandHandler {
206 209
207 210 public String getName() {
208 211 return "forward";
209 212 }
213 +
214 + @SuppressWarnings("deprecation")
215 + private String getLine (DataInputStream socketIn) throws IOException {
216 + return socketIn.readLine();
217 + }
210 218
211 219 public void execute(String param) throws CGIClientException, CGIServerException
212 220 {
213 221 if (!CGIHandler.RequestMethod.equals("POST"))
214 222 throw new CGIClientException("can only forward POST requests");
215 223
216 224 int port;
217 225 try {
218 226 port = Integer.parseInt(param);
219 227 } catch (NumberFormatException e) {
220 228 throw new CGIClientException("invalid port number: " + param);
221 229 }
222 230 if (port <= 0 || port > 0xFFFF)
223 231 throw new CGIClientException("invalid port: " + port);
224 232 if (port < 1024)
225 233 throw new CGIClientException("permission denied for port: " +
226 234 port);
227 235
228 236 byte buffer[];
229 237 Socket socket;
230 238 try {
231 239 socket = new Socket(InetAddress.getLocalHost(), port);
232 240 } catch (IOException e) {
233 241 throw new CGIServerException("could not connect to local port");
234 242 }
235 243
236 244 /*
237 245 * read client's request body
238 246 */
239 247 DataInputStream clientIn = new DataInputStream(System.in);
240 248 buffer = new byte[CGIHandler.ContentLength];
241 249 try {
242 250 clientIn.readFully(buffer);
243 251 } catch (EOFException e) {
244 252 throw new CGIClientException("unexpected EOF reading request body");
245 253 } catch (IOException e) {
246 254 throw new CGIClientException("error reading request body");
247 255 }
248 256
249 257 /*
250 258 * send to local server in HTTP
251 259 */
252 260 try {
253 261 DataOutputStream socketOut =
254 262 new DataOutputStream(socket.getOutputStream());
255 263 socketOut.writeBytes("POST / HTTP/1.0\r\n");
256 264 socketOut.writeBytes("Content-length: " +
257 265 CGIHandler.ContentLength + "\r\n\r\n");
258 266 socketOut.write(buffer);
259 267 socketOut.flush();
260 268 } catch (IOException e) {
261 269 throw new CGIServerException("error writing to server");
262 270 }
263 271
264 272 /*
265 273 * read response
266 274 */
267 275 DataInputStream socketIn;
268 276 try {
↓ open down ↓ |
49 lines elided |
↑ open up ↑ |
269 277 socketIn = new DataInputStream(socket.getInputStream());
270 278 } catch (IOException e) {
271 279 throw new CGIServerException("error reading from server");
272 280 }
273 281 String key = "Content-length:".toLowerCase();
274 282 boolean contentLengthFound = false;
275 283 String line;
276 284 int responseContentLength = -1;
277 285 do {
278 286 try {
279 - line = socketIn.readLine();
287 + line = getLine(socketIn);
280 288 } catch (IOException e) {
281 289 throw new CGIServerException("error reading from server");
282 290 }
283 291 if (line == null)
284 292 throw new CGIServerException(
285 293 "unexpected EOF reading server response");
286 294
287 295 if (line.toLowerCase().startsWith(key)) {
288 - if (contentLengthFound)
289 - ; // what would we want to do in this case??
296 + // if contentLengthFound is true
297 + // we should probably do something here
290 298 responseContentLength =
291 299 Integer.parseInt(line.substring(key.length()).trim());
292 300 contentLengthFound = true;
293 301 }
294 302 } while ((line.length() != 0) &&
295 303 (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
296 304
297 305 if (!contentLengthFound || responseContentLength < 0)
298 306 throw new CGIServerException(
299 307 "missing or invalid content length in server response");
300 308 buffer = new byte[responseContentLength];
301 309 try {
302 310 socketIn.readFully(buffer);
303 311 } catch (EOFException e) {
304 312 throw new CGIServerException(
305 313 "unexpected EOF reading server response");
306 314 } catch (IOException e) {
307 315 throw new CGIServerException("error reading from server");
308 316 }
309 317
310 318 /*
311 319 * send response back to client
312 320 */
313 321 System.out.println("Status: 200 OK");
314 322 System.out.println("Content-type: application/octet-stream");
315 323 System.out.println("");
316 324 try {
317 325 System.out.write(buffer);
318 326 } catch (IOException e) {
319 327 throw new CGIServerException("error writing response");
320 328 }
321 329 System.out.flush();
322 330 }
323 331 }
324 332
325 333 /**
326 334 * "gethostname" command: Return the host name of the server as the
327 335 * response body
328 336 */
329 337 final class CGIGethostnameCommand implements CGICommandHandler {
330 338
331 339 public String getName() {
332 340 return "gethostname";
333 341 }
334 342
335 343 public void execute(String param)
336 344 {
337 345 System.out.println("Status: 200 OK");
338 346 System.out.println("Content-type: application/octet-stream");
339 347 System.out.println("Content-length: " +
340 348 CGIHandler.ServerName.length());
341 349 System.out.println("");
342 350 System.out.print(CGIHandler.ServerName);
343 351 System.out.flush();
344 352 }
345 353 }
346 354
347 355 /**
348 356 * "ping" command: Return an OK status to indicate that connection
349 357 * was successful.
350 358 */
351 359 final class CGIPingCommand implements CGICommandHandler {
352 360
353 361 public String getName() {
354 362 return "ping";
355 363 }
356 364
357 365 public void execute(String param)
358 366 {
359 367 System.out.println("Status: 200 OK");
360 368 System.out.println("Content-type: application/octet-stream");
361 369 System.out.println("Content-length: 0");
362 370 System.out.println("");
363 371 }
364 372 }
365 373
366 374 /**
367 375 * "tryhostname" command: Return a human readable message describing
368 376 * what host name is available to local Java VMs.
369 377 */
370 378 final class CGITryHostnameCommand implements CGICommandHandler {
371 379
372 380 public String getName() {
373 381 return "tryhostname";
374 382 }
375 383
376 384 public void execute(String param)
377 385 {
378 386 System.out.println("Status: 200 OK");
379 387 System.out.println("Content-type: text/html");
380 388 System.out.println("");
381 389 System.out.println("<HTML>" +
382 390 "<HEAD><TITLE>Java RMI Server Hostname Info" +
383 391 "</TITLE></HEAD>" +
384 392 "<BODY>");
385 393 System.out.println("<H1>Java RMI Server Hostname Info</H1>");
386 394 System.out.println("<H2>Local host name available to Java VM:</H2>");
387 395 System.out.print("<P>InetAddress.getLocalHost().getHostName()");
388 396 try {
389 397 String localHostName = InetAddress.getLocalHost().getHostName();
390 398
391 399 System.out.println(" = " + localHostName);
392 400 } catch (UnknownHostException e) {
393 401 System.out.println(" threw java.net.UnknownHostException");
394 402 }
395 403
396 404 System.out.println("<H2>Server host information obtained through CGI interface from HTTP server:</H2>");
397 405 System.out.println("<P>SERVER_NAME = " + CGIHandler.ServerName);
398 406 System.out.println("<P>SERVER_PORT = " + CGIHandler.ServerPort);
399 407 System.out.println("</BODY></HTML>");
400 408 }
401 409 }
↓ open down ↓ |
102 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX