/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* CGIClientException is thrown when an error is detected
* in a client's request.
*/
super(s);
}
}
/**
* CGIServerException is thrown when an error occurs here on the server.
*/
super(s);
}
}
/**
* CGICommandHandler is the interface to an object that handles a
* particular supported command.
*/
interface CGICommandHandler {
/**
* Return the string form of the command
* to be recognized in the query string.
*/
/**
* Execute the command with the given string as parameter.
*/
}
/**
* The CGIHandler class contains methods for executing as a CGI program.
* The main function interprets the query string as a command of the form
* "<command>=<parameters>".
*
* This class depends on the CGI 1.0 environment variables being set as
* properties of the same name in this Java VM.
*
* All data and methods of this class are static because they are specific
* to this particular CGI process.
*/
public final class CGIHandler {
/* get CGI parameters that we need */
static int ContentLength;
static int ServerPort;
static {
return null;
}
});
}
/* list of handlers for supported commands */
new CGIForwardCommand(),
new CGIGethostnameCommand(),
new CGIPingCommand(),
new CGITryHostnameCommand()
};
/* construct table mapping command strings to handlers */
static {
commandLookup = new Hashtable<>();
}
/* prevent instantiation of this class */
private CGIHandler() {}
/**
* Execute command given in query string on URL. The string before
* the first '=' is interpreted as the command name, and the string
* after the first '=' is the parameters to the command.
*/
{
try {
if (delim == -1) {
param = "";
}
else {
}
try {
} catch (CGIClientException e) {
returnClientError(e.getMessage());
} catch (CGIServerException e) {
returnServerError(e.getMessage());
}
else
returnClientError("invalid command.");
} catch (Exception e) {
}
}
/**
* Return an HTML error message indicating there was error in
* the client's request.
*/
{
"<HEAD><TITLE>Java RMI Client Error" +
"</TITLE></HEAD>" +
"<BODY>");
}
/**
* Return an HTML error message indicating an error occurred
* here on the server.
*/
{
"<HEAD><TITLE>Java RMI Server Error" +
"</TITLE></HEAD>" +
"<BODY>");
}
}
/**
* "forward" command: Forward request body to local port on the server,
* and send response back to client.
*/
return "forward";
}
@SuppressWarnings("deprecation")
}
{
throw new CGIClientException("can only forward POST requests");
int port;
try {
} catch (NumberFormatException e) {
throw new CGIClientException("invalid port number.");
}
if (port < 1024)
throw new CGIClientException("permission denied for port: " +
port);
byte buffer[];
try {
} catch (IOException e) {
throw new CGIServerException("could not connect to local port");
}
/*
* read client's request body
*/
try {
} catch (EOFException e) {
throw new CGIClientException("unexpected EOF reading request body");
} catch (IOException e) {
throw new CGIClientException("error reading request body");
}
/*
* send to local server in HTTP
*/
try {
} catch (IOException e) {
throw new CGIServerException("error writing to server");
}
/*
* read response
*/
try {
} catch (IOException e) {
throw new CGIServerException("error reading from server");
}
boolean contentLengthFound = false;
int responseContentLength = -1;
do {
try {
} catch (IOException e) {
throw new CGIServerException("error reading from server");
}
throw new CGIServerException(
"unexpected EOF reading server response");
if (contentLengthFound) {
throw new CGIServerException(
"Multiple Content-length entries found.");
} else {
contentLengthFound = true;
}
}
throw new CGIServerException(
"missing or invalid content length in server response");
buffer = new byte[responseContentLength];
try {
} catch (EOFException e) {
throw new CGIServerException(
"unexpected EOF reading server response");
} catch (IOException e) {
throw new CGIServerException("error reading from server");
}
/*
* send response back to client
*/
try {
} catch (IOException e) {
throw new CGIServerException("error writing response");
}
}
}
/**
* "gethostname" command: Return the host name of the server as the
* response body
*/
return "gethostname";
}
{
}
}
/**
* "ping" command: Return an OK status to indicate that connection
* was successful.
*/
return "ping";
}
{
}
}
/**
* "tryhostname" command: Return a human readable message describing
* what host name is available to local Java VMs.
*/
return "tryhostname";
}
{
"<HEAD><TITLE>Java RMI Server Hostname Info" +
"</TITLE></HEAD>" +
"<BODY>");
try {
} catch (UnknownHostException e) {
}
System.out.println("<H2>Server host information obtained through CGI interface from HTTP server:</H2>");
}
}