-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCgiServlet.java
More file actions
351 lines (291 loc) · 13.5 KB
/
CgiServlet.java
File metadata and controls
351 lines (291 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package javaxt.http.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;
//******************************************************************************
//** CGI Servlet
//******************************************************************************
/**
* Http Servlet used to run CGI programs. Based on CgiServlet.java, v1.8
* developed by Jef Poskanzer (acme.com).
*
******************************************************************************/
public class CgiServlet extends HttpServlet {
private java.io.File executable;
//**************************************************************************
//** Constructor
//**************************************************************************
public CgiServlet(java.io.File executable){
this.executable = executable;
}
//**************************************************************************
//** getServletInfo
//**************************************************************************
/** Returns a string containing information about the author, version, and
* copyright of the servlet.
*/
public String getServletInfo() {
return "JavaXT CGI Servlet";
}
//**************************************************************************
//** getParameters
//**************************************************************************
/** Returns a list of parameters that are used to instantiate the CGI
* application.
*/
protected java.util.ArrayList<String> getParameters(HttpServletRequest request){
java.util.ArrayList<String> env = new java.util.ArrayList<String>();
//env.add("PATH=" + "/usr/local/bin:/usr/ucb:/bin:/usr/bin");
env.add("GATEWAY_INTERFACE=" + "CGI/1.1");
env.add("SERVER_SOFTWARE=" + getServletContext().getServerInfo());
env.add("SERVER_PROTOCOL=" + request.getProtocol());
env.add("SERVER_NAME=" + request.getServerName());
env.add("SERVER_PORT=" + request.getServerPort());
env.add("REMOTE_ADDR=" + request.getRemoteAddr());
env.add("REMOTE_HOST=" + request.getRemoteHost());
env.add("REQUEST_METHOD=" + request.getMethod());
env.add("SCRIPT_NAME=" + request.getServletPath());
int contentLength = request.getContentLength();
if (contentLength!=-1) env.add("CONTENT_LENGTH=" + contentLength);
String contentType = request.getContentType();
if (contentType!=null) env.add("CONTENT_TYPE=" + contentType);
String pathInfo = request.getPathInfo();
if (pathInfo!=null) env.add("PATH_INFO=" + pathInfo);
String pathTranslated = request.getPathTranslated();
if (pathTranslated!=null) env.add("PATH_TRANSLATED=" + pathTranslated);
String queryString = request.getQueryString();
if (queryString!=null) env.add("QUERY_STRING=" + queryString);
String remoteUser = request.getRemoteUser();
if (remoteUser!=null) env.add("REMOTE_USER=" + remoteUser);
String authType = request.getAuthType();
if (authType!=null) env.add("AUTH_TYPE=" + authType);
java.util.Enumeration<String> hnEnum = request.getHeaderNames();
while (hnEnum.hasMoreElements()) {
String name = hnEnum.nextElement();
String value = request.getHeader(name);
if (value == null) value = "";
env.add("HTTP_" + name.toUpperCase().replace('-', '_') + "=" + value);
}
return env;
}
//**************************************************************************
//** processRequest
//**************************************************************************
/** Services a single request from the client.
* @param request the servlet request
* @param response the servlet response
* @exception ServletException when an exception has occurred
*/
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getMethod().toUpperCase();
if (!(method.equals("GET") || method.equals("POST"))) {
response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
return;
}
//Generate a list of parameters used to instantiate the CGI application
java.util.ArrayList<String> env = getParameters(request);
String[] parameters = new String[env.size()+1];
parameters[0] = executable.toString();
for (int i=0; i<parameters.length; i++){
if (i>0) parameters[i] = env.get(i-1);
}
try{
//Run executable via Command Line
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(parameters, null, executable.getParentFile());
//If this is a POST, pass the body of the request to the process
if (method.equals("POST")) {
OutputStream outputStream = process.getOutputStream();
InputStream inputStream = request.getInputStream();
byte[] b = new byte[1024];
int x=0;
while ( (x = inputStream.read(b)) != -1) {
outputStream.write(b,0,x);
}
inputStream.close();
outputStream.close();
}
//Parse output streams
StreamReader s1 = new StreamReader(process.getInputStream(), response);
ErrorStreamReader s2 = new ErrorStreamReader(process.getErrorStream());
s1.start();
s2.start();
process.waitFor();
s1.join();
s2.join();
//Explicitly clean up every the process by calling close on each stream
try{process.getInputStream().close();} catch(Exception ex){}
try{process.getErrorStream().close();} catch(Exception ex){}
try{process.getOutputStream().close();} catch(Exception ex){}
//Explicitly destroy the process even if the process is already terminated
try{process.destroy();} catch(Exception ex){}
process = null;
}
catch(IOException e){
throw e;
}
catch(InterruptedException e){
//throw e;
return;
}
}
//**************************************************************************
//** StreamReader Class
//**************************************************************************
/** Thread used to process the standard output stream. */
private class StreamReader implements Runnable {
private InputStream is;
private HttpServletResponse response;
private Thread thread;
private byte[] b = new byte[1];
public StreamReader(InputStream is, HttpServletResponse response){
this.is = is;
this.response = response;
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
try {
//Parse the list few lines returned from the executable. These
//may contain HTTP response headers
boolean firstLine = true;
while (true) {
String line = readLine();
if (line==null) break;
line = line.trim();
if (line.equals("")) break;
int colon = line.indexOf(":");
if (colon == -1) {
// No colon. If it's the first line, parse it for status.
if (firstLine) {
StringTokenizer tok = new StringTokenizer(line, " ");
try {
switch (tok.countTokens()) {
case 2:
tok.nextToken();
response.setStatus(Integer.parseInt(tok.nextToken()));
break;
case 3:
tok.nextToken();
response.setStatus(Integer.parseInt(tok.nextToken()), tok.nextToken());
break;
}
} catch (NumberFormatException ignore) {
}
} else {
// No colon and it's not the first line? Ignore.
}
} else {
// There's a colon. Check for certain special headers.
String name = line.substring(0, colon);
String value = line.substring(colon + 1).trim();
if (name.equalsIgnoreCase("Status")) {
StringTokenizer tok = new StringTokenizer(value, " ");
try {
switch (tok.countTokens()) {
case 1:
response.setStatus(Integer.parseInt(tok.nextToken()));
break;
case 2:
response.setStatus(Integer.parseInt(tok.nextToken()), tok.nextToken());
break;
}
} catch (NumberFormatException ignore) {
}
} else if (name.equalsIgnoreCase("Content-type")) {
response.setContentType(value);
} else if (name.equalsIgnoreCase("Content-length")) {
try {
response.setContentLength(Integer.parseInt(value));
} catch (NumberFormatException ignore) {
}
} else if (name.equalsIgnoreCase("Location")) {
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader(name, value);
} else if (name.equalsIgnoreCase("Set-Cookie")) {
int x = value.indexOf("=");
if (x > 0) {
String n = value.substring(0, x);
String v = value.substring(x + 1).trim();
response.addCookie(new Cookie(n, v));
}
} else {
// Not a special header. Just set it.
response.setHeader(name, value);
}
}
}
//Set transfer encoding
response.setHeader("Transfer-Encoding", "Chunked");
//Tranfer remaining bytes from the standard output stream to
//the servlet output stream
OutputStream outputStream = response.getOutputStream();
byte[] b = new byte[1024];
int x=0;
while ( (x = is.read(b)) != -1) {
outputStream.write(b,0,x);
}
//Close the input and output streams
outputStream.close();
is.close();
}
catch (IOException e) {
// response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
// There's some weird bug in Java, when reading from a Process
// you get a spurious IOException. We have to ignore it.
}
catch (Exception e) {
e.printStackTrace();
return;
}
}
private String readLine() throws IOException {
StringBuffer str = new StringBuffer();
while (true){
if (is.read(b)==-1) break;
byte c = b[0];
if (c=='\n') break;
str.append((char) c);
}
return str.toString();
}
public void join() throws InterruptedException {
thread.join();
}
} //End StreamReader Class
//**************************************************************************
//** ErrorStreamReader Class
//**************************************************************************
/** Thread used to read the standard output streams. */
private class ErrorStreamReader implements Runnable {
private InputStream is;
private Thread thread;
private byte[] b = new byte[1];
public ErrorStreamReader(InputStream is){
this.is = is;
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
try {
while (true) {
if (is.read(b)==-1) break;
}
is.close();
}
catch (Exception e) {
//System.out.println ("Problem reading stream... :" + ex);
e.printStackTrace();
return;
}
}
public void join() throws InterruptedException {
thread.join();
}
} //End ErrorStreamReader Class
}