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