0N/A/*
2362N/A * Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <stdio.h>
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include <process.h>
0N/A
0N/A/*
0N/A * This is a primitive bootstrapping utility for executing Java CGI
0N/A * programs, specifically Java RMI's CGI HTTP forwarding mechanism
0N/A *
0N/A * It executes the Java interpreter with options to define
0N/A * properties corresponding to the environment variables set by the
0N/A * CGI 1.0 specification and runs the target class.
0N/A *
0N/A * The following assumptions are made:
0N/A * - the Java interpreter can be located by the system
0N/A * PATH variable
0N/A * - for RMI 1.1 prebeta release, the target class can be located
0N/A * using the system CLASSPATH variable
0N/A */
0N/A
0N/A/* name of Java interpreter executable */
0N/A#define JAVA_NAME "java"
0N/A
0N/A/* name of Java class to execute with interpreter */
0N/A#define CLASS_NAME "sun.rmi.transport.proxy.CGIHandler"
0N/A
0N/A/* names of environment variables set in CGI 1.0 interface */
0N/Astatic char *var_names[] = {
0N/A "AUTH_TYPE",
0N/A "CONTENT_LENGTH",
0N/A "CONTENT_TYPE",
0N/A "GATEWAY_INTERFACE",
0N/A "HTTP_ACCEPT",
0N/A "PATH_INFO",
0N/A "PATH_TRANSLATED",
0N/A "QUERY_STRING",
0N/A "REMOTE_ADDR",
0N/A "REMOTE_HOST",
0N/A "REMOTE_IDENT",
0N/A "REMOTE_USER",
0N/A "REQUEST_METHOD",
0N/A "SCRIPT_NAME",
0N/A "SERVER_NAME",
0N/A "SERVER_PORT",
0N/A "SERVER_PROTOCOL",
0N/A "SERVER_SOFTWARE"
0N/A};
0N/A
0N/A#define NUM_VARS (sizeof(var_names) / sizeof(var_names[0]))
0N/A
0N/A/* file static functions */
0N/Astatic void server_error(char *);
0N/A
0N/A/*
0N/A * Program entry point: set up arguments and invoke Java interpreter.
0N/A */
0N/Aint
0N/Amain(
0N/A int argc,
0N/A char *argv[]
0N/A)
0N/A{
0N/A int i; /* loop index variable */
0N/A char **args; /* array to store arguments to interpreter */
0N/A int n = 0; /* next index to fill in argument array */
0N/A
0N/A /* allocate space for argument list */
0N/A args = (char **) /* allocate space for: */
0N/A malloc((1 /* executable name */
0N/A + NUM_VARS /* property definition for each variable */
0N/A + 1 /* class name */
0N/A + 1) /* terminating NULL */
0N/A * sizeof(*args));
0N/A if (args == NULL) {
0N/A server_error("memory allocation failure");
0N/A return 1;
0N/A }
0N/A
0N/A /* first argument: name of java interpreter */
0N/A args[n ++] = JAVA_NAME;
0N/A
0N/A /* next arguments: define CGI variables as properties to Java VM */
0N/A for (i = 0; i < NUM_VARS; ++ i) {
0N/A char *name = var_names[i]; /* name of variable */
0N/A char *value; /* value of variable */
0N/A char *buffer; /* buffer to store argument string */
0N/A
0N/A value = getenv(name);
0N/A if (value == NULL) /* if variable undefined, */
0N/A value = ""; /* use empty string */
0N/A
0N/A buffer = (char *) /* allocate space for: */
0N/A malloc((2 /* "-D" */
0N/A + strlen(name) /* variable name */
0N/A + 2 /* "=\"" */
0N/A + strlen(value) /* variable value */
0N/A + 2) /* "\"" and terminating '\0' */
0N/A * sizeof(*buffer));
0N/A if (buffer == NULL) {
0N/A server_error("memory allocation failure");
0N/A return 1;
0N/A }
0N/A
0N/A /* construct property definition parameter */
0N/A sprintf(buffer, "-D%s=\"%s\"", name, value);
0N/A
0N/A args[n ++] = buffer; /* add to argument list */
0N/A }
0N/A
0N/A /* last argument: name of class to execute */
0N/A args[n ++] = CLASS_NAME;
0N/A
0N/A args[n ++] = NULL; /* terminate argument list */
0N/A
0N/A _execvp(JAVA_NAME, args); /* execute java interpreter */
0N/A
0N/A /* if exec call returns, there was an error */
0N/A server_error("interpreter execution failure");
0N/A return 1;
0N/A}
0N/A
0N/A/*
0N/A * Return primitive error message to server because of some failure in
0N/A * this program. (This could be embellished to an HTML formatted error
0N/A * message.)
0N/A */
0N/Astatic void
0N/Aserver_error(
0N/A char *message
0N/A)
0N/A{
0N/A /*
0N/A * NOTE: CGI 1.0 spec uses "\n" (unlike "\r\n"
0N/A * for HTTP 1.0) for line termination
0N/A */
0N/A printf("Status: 500 Server Error: %s\n", message);
0N/A printf("Content-type: text/plain\n");
0N/A printf("\n");
0N/A printf("%s", message);
0N/A}