001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: CgiProcessor.java,v 1.2 2006-06-15 14:07:00 sinisa Exp $
022: */
023:
024: package org.enhydra.servlet.servlets;
025:
026: import java.io.BufferedReader;
027: import java.io.File;
028: import java.io.IOException;
029: import java.io.InputStreamReader;
030: import java.util.Vector;
031:
032: import javax.servlet.ServletOutputStream;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035:
036: /**
037: * This Servlet is for the purpose of executing a CGI program that resides on
038: * the system. In order to use this servlet in the server please
039: * remember to specify the fully qualified classname
040: * org.enhydra.servlet.servlets.CGIServlet in the Classname field and specify
041: * the directory of the CGI to run in the DocRoot field.
042: *
043: * @version $Revision: 1.2 $
044: * @author Kent Henneuse
045: * @author Paul Morgan
046: */
047: class CgiProcessor {
048:
049: /**
050: * Buffer size for writeing one stream to another.
051: */
052: private static final int buffer_size = 1024;
053:
054: /**
055: * Html text to send clent for a file not found.
056: */
057: private final static String NOT_FOUND_MSG = "<TITLE>Not Found</TITLE><H1>Not Found</H1> "
058: + "The requested object does not exist on this Enhydra Server. "
059: + "The link you followed is either outdated, inaccurate, "
060: + "or the server has been instructed not to let you have it.";
061:
062: /**
063: *
064: */
065: public void processCgiRequest(HttpServletRequest request,
066: HttpServletResponse response, String scriptPath,
067: String pathInfo, String scriptName) throws IOException {
068:
069: ServletOutputStream out;
070: String strBuffer;
071: String name, value;
072: byte buffer[] = new byte[buffer_size];
073: int bytesRead;
074:
075: // Execute the CGI & report on errors...
076: Process process = null;
077: Runtime runner = Runtime.getRuntime();
078: String envVars[] = getCgiEnv(request, scriptPath, pathInfo,
079: scriptName);
080: File script = new File(scriptPath);
081: if (!(script.exists() && script.isFile())) {
082: // script doesn't exist
083: response.sendError(response.SC_NOT_FOUND, NOT_FOUND_MSG);
084: return;
085: }
086: process = runner.exec(scriptPath, envVars);
087:
088: InputStreamReader tempIn = new InputStreamReader(process
089: .getInputStream());
090: BufferedReader in = new BufferedReader(tempIn);
091:
092: strBuffer = in.readLine();
093: if (scriptName.toLowerCase().startsWith("nph-")) {
094: String code = strBuffer.substring(strBuffer.indexOf(" "));
095: response.setStatus(Integer.parseInt(code));
096: strBuffer = in.readLine();
097: }
098:
099: // Set the response header fields...
100: while ((strBuffer != null) && (strBuffer.length() != 0)) {
101: int colon = strBuffer.indexOf(":");
102: if (colon != -1) {
103: name = strBuffer.substring(0, colon);
104: value = strBuffer.substring(colon + 1);
105: String lName = name.toLowerCase();
106: if (lName.equals("status")) {
107: String tValue = value.trim();
108: if (tValue.indexOf(" ") != -1) {
109: String nValue = tValue.substring(0, tValue
110: .indexOf(" "));
111: response.setStatus(Integer.parseInt(nValue));
112: } else {
113: response.setStatus(Integer.parseInt(tValue));
114: }
115: } else if (lName.equals("content-type")) {
116: String tValue = value.trim();
117: response.setContentType(tValue);
118: } else {
119: response.setHeader(name, value);
120: }
121: }
122: strBuffer = in.readLine();
123: }
124:
125: bytesRead = in.read();
126: // then write the data of the response
127: out = response.getOutputStream();
128:
129: while (bytesRead != -1) {
130: out.write(bytesRead);
131: bytesRead = in.read();
132: }
133: in.close();
134: tempIn.close();
135: out.flush();
136: out.close();
137: }
138:
139: /**
140: *
141: */
142: private String[] getCgiEnv(HttpServletRequest request,
143: String scriptPath, String pathInfo, String scriptName) {
144:
145: Vector envList = new Vector();
146: String strBuffer = null;
147:
148: // Get all of the Environment Variables that can not be gotten from
149: // the servlet request
150:
151: // FIXME: The following code is commented out currently as
152: // it may not be required. Have not found a CGI to really test
153: // this functionality
154: /*
155: process = runner.exec("env");
156: tempIn = new InputStreamReader(process.getInputStream());
157: in = new BufferedReader(tempIn);
158:
159: strBuffer = in.readLine();
160: while(strBuffer != null) {
161: envList.addElement(strBuffer);
162: strBuffer = in.readLine();
163: }
164: */
165:
166: // Add the request variables to the list to be passed
167: strBuffer = request.getServerName();
168: if (strBuffer != null) {
169: envList.addElement("SERVER_NAME=" + strBuffer);
170: }
171:
172: strBuffer = request.getProtocol();
173: if (strBuffer != null) {
174: envList.addElement("SERVER_PROTOCOL=" + strBuffer);
175: }
176:
177: envList.addElement("SERVER_PORT=" + request.getServerPort());
178:
179: strBuffer = request.getMethod();
180: if (strBuffer != null) {
181: envList.addElement("REQUEST_METHOD=" + strBuffer);
182: }
183:
184: if (pathInfo != null && pathInfo.length() != 0) {
185: envList.addElement("PATH_INFO=" + pathInfo);
186: }
187:
188: strBuffer = request.getPathTranslated();
189: if (strBuffer != null) {
190: envList.addElement("PATH_TRANSLATED=" + strBuffer);
191: envList.addElement("SCRIPT_FILENAME=" + strBuffer);
192: }
193:
194: if (scriptPath != null || scriptPath.equals("")) {
195: envList.addElement("SCRIPT_NAME="
196: + request.getServletPath() + scriptName);
197: }
198:
199: strBuffer = request.getRealPath("/");
200: if (strBuffer != null) {
201: envList.addElement("DOCUMENT_ROOT=" + strBuffer);
202: }
203:
204: strBuffer = request.getQueryString();
205: if (strBuffer != null) {
206: envList.addElement("QUERY_STRING=" + strBuffer);
207: }
208:
209: strBuffer = request.getRemoteHost();
210: if (strBuffer != null) {
211: envList.addElement("REMOTE_HOST=" + strBuffer);
212: }
213:
214: strBuffer = request.getRemoteAddr();
215: if (strBuffer != null) {
216: envList.addElement("REMOTE_ADDR=" + strBuffer);
217: }
218:
219: strBuffer = request.getAuthType();
220: if (strBuffer != null) {
221: envList.addElement("AUTH_TYPE=" + strBuffer);
222: }
223:
224: strBuffer = request.getRemoteUser();
225: if (strBuffer != null) {
226: envList.addElement("REMOTE_USER=" + strBuffer);
227: }
228:
229: if (request.getContentLength() != -1) {
230: envList.addElement("CONTENT_LENGTH="
231: + request.getContentLength());
232: }
233:
234: strBuffer = request.getContentType();
235: if (strBuffer != null) {
236: envList.addElement("CONTENT_TYPE=" + strBuffer);
237: }
238:
239: strBuffer = request.getHeader("Accept");
240: if (strBuffer != null) {
241: envList.addElement("HTTP_ACCEPT=" + strBuffer);
242: }
243:
244: strBuffer = request.getHeader("User-Agent");
245: if (strBuffer != null) {
246: envList.addElement("HTTP_USER_AGENT=" + strBuffer);
247: }
248:
249: envList.addElement("SERVER_SOFTWARE=Enhydra7.0");
250:
251: // Create the needed array of environment variables
252: String envVars[] = new String[envList.size()];
253: envList.copyInto(envVars);
254: return envVars;
255: }
256:
257: /**
258: *
259: */
260: public static void cgiError(HttpServletResponse response)
261: throws IOException {
262:
263: ServletOutputStream out = response.getOutputStream();
264: response.setContentType("text/html");
265:
266: out.println("<HTML>");
267: out.println("<HEAD>");
268: out.println("<TITLE>CGIServlet Error</TITLE>\n</HEAD>");
269: out.println("<BODY>");
270: out.println("<H1>CGIServlet Error</H1>");
271: out
272: .println("A CGI program to execute was not specified or could not be found.");
273: out.println("</BODY>\n</HTML>");
274: out.flush();
275: out.close();
276: }
277:
278: }
|