00001
package com.quadcap.http.servlets.cgi;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.File;
00042
import java.io.FileInputStream;
00043
import java.io.IOException;
00044
import java.io.OutputStream;
00045
00046
import java.util.Enumeration;
00047
import java.util.Hashtable;
00048
00049
import javax.servlet.ServletConfig;
00050
import javax.servlet.ServletException;
00051
00052
import javax.servlet.http.HttpServlet;
00053
import javax.servlet.http.HttpServletRequest;
00054
import javax.servlet.http.HttpServletResponse;
00055
00056
import com.quadcap.util.Debug;
00057
00058
00059
00060
00061
00062
00063
00064
00065 public class CgiServlet extends HttpServlet {
00066 Hashtable
scripts =
new Hashtable();
00067 Hashtable
extMap =
new Hashtable();
00068 String
defaultInterp;
00069
00070
00071
00072
00073
00074
00075
00076
00077 public void init(ServletConfig config)
throws ServletException {
00078 super.init(config);
00079 Enumeration e = config.getInitParameterNames();
00080
defaultInterp = config.getInitParameter(
"defaultInterp");
00081
while (e.hasMoreElements()) {
00082 String name = e.nextElement().toString();
00083
if (name.startsWith(
".")) {
00084
extMap.put(name.substring(1), config.getInitParameter(name));
00085 }
00086 }
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 protected void service(HttpServletRequest req, HttpServletResponse res)
00101
throws ServletException, IOException
00102 {
00103
CgiScript file =
getFileForRequest(req);
00104
if (file == null) {
00105 res.sendError(HttpServletResponse.SC_NOT_FOUND);
00106 }
else {
00107 file.
service(req, res);
00108 }
00109 }
00110
00111
00112
00113
00114
00115
00116
00117 public String
getFileNameForRequest(HttpServletRequest req) {
00118 String uri = req.getRequestURI();
00119
int idx = uri.indexOf(
'?');
00120
if (idx >= 0) uri = uri.substring(0, idx);
00121
return uri;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 public CgiScript getFileForRequest(HttpServletRequest req)
00134
throws ServletException
00135 {
00136 String name = getServletContext().getRealPath(req.getServletPath());
00137
CgiScript script = null;
00138
00139
synchronized (
scripts) {
00140 script = (
CgiScript)
scripts.get(name);
00141
00142
if (script == null) {
00143
int idx = name.lastIndexOf(
'.');
00144 String ext = null;
00145
if (idx > 0) ext = name.substring(idx+1);
00146 String interp = null;
00147
if (ext != null) interp = (String)
extMap.get(ext);
00148
if (interp == null) interp =
defaultInterp;
00149
if (interp == null) {
00150
throw new ServletException(
"No interpreter for " + name);
00151 }
00152
00153 File f =
new File(name);
00154
if (!f.exists())
return null;
00155
00156 script =
new CgiScript(
this, interp, f);
00157
scripts.put(name, script);
00158
return script;
00159 }
00160 }
00161
return script;
00162 }
00163 }