{
// first, take care of error stream
(new ProcessErrorReader(process)).start();
// No header script don't deserve attention:
if ( checkNoheaderFlag() ) {
Reply reply = request.makeReply(HTTP.NOHEADER) ;
reply.setStream (process.getInputStream()) ;
return reply ;
}
// Check for debugging mode:
if ( checkCgiDebug() ) {
Reply reply = request.makeReply(HTTP.OK);
reply.setContentType(MimeType.TEXT_PLAIN);
reply.setStream(process.getInputStream());
return reply;
}
// We MUST parse at least one header:
MimeParser p = new MimeParser(process.getInputStream(),
new CGIHeaderHolderFactory());
Reply reply = null ;
try {
CGIHeaderHolder h = (CGIHeaderHolder) p.parse();
// Check for a status code:
String svalue = h.getStatus();
String location = h.getLocation();
if ( svalue != null ) {
int status = -1;
try {
String _st = svalue.trim();
int _space = _st.indexOf(' ');
if (_space != -1) {
_st = _st.substring(0, _space);
}
status = Integer.parseInt(_st);
} catch (Exception ex) {
// This script has emited an invalid status line:
String msg = ("Emited an invalid status line ["+
svalue + "].");
getServer().errlog(this, msg);
// Throw an HTTPException:
reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
reply.setContent("CGI script emited invalid status.");
throw new HTTPException(reply);
}
// we will use the default for this frame, but remove
// the length calculated from the script size.
reply = createDefaultReply(request, status);
reply.setContentLength(-1);
} else {
// No status code available, any location header ?
if (location != null) {
reply = request.makeReply(HTTP.FOUND);
} else {
reply = createDefaultReply(request, HTTP.OK);
reply.setContentLength(-1);
}
}
// Set up the location header if needed:
if ( location != null ) {
try {
reply.setLocation(new URL(getURL(request), location));
} catch (MalformedURLException ex) {
// This should really not happen:
getServer().errlog(this, "unable to create location url "+
location+
" in base "+getURL(request));
}
}
// And then, the remaining headers:
Enumeration e = h.enumerateHeaders();
if ( e != null ) {
while ( e.hasMoreElements() ) {
String hname = (String) e.nextElement();
reply.setValue(hname, (String) h.getValue(hname));
}
}
reply.setStream(p.getInputStream()) ;
} catch (IOException ex) {
ex.printStackTrace();
} catch (MimeParserException ex) {
// This script has generated invalid output:
String msg = (getURL(request)
+": emited invalid output ["+
ex.getMessage() +"]");
getServer().errlog(this, msg);
// Throw an HTTPException:
Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR) ;
error.setContent("CGI error: unable to parse script headers.") ;
throw new HTTPException (error) ;
}
if (reply != null) {
reply.setDynamic(true);
}