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