741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
25 |
|
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
29 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
34 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
39 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
741 |
10 Oct 06 |
olle |
43 |
// |
741 |
10 Oct 06 |
olle |
44 |
// |
741 |
10 Oct 06 |
olle |
45 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
48 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
52 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## FOR FULL LICENSE, PLEASE READ "XERVER LICENSE". ## |
741 |
10 Oct 06 |
olle |
54 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
55 |
|
1652 |
22 May 07 |
gregory |
56 |
package se.lu.thep.coreftpd.webserver; |
741 |
10 Oct 06 |
olle |
57 |
|
791 |
23 Oct 06 |
olle |
58 |
import se.lu.thep.coreftpd.common.MyInteger; |
741 |
10 Oct 06 |
olle |
59 |
import java.io.BufferedInputStream; |
741 |
10 Oct 06 |
olle |
60 |
import java.io.DataInputStream; |
741 |
10 Oct 06 |
olle |
61 |
import java.io.DataOutputStream; |
741 |
10 Oct 06 |
olle |
62 |
import java.io.InputStream; |
741 |
10 Oct 06 |
olle |
63 |
import java.io.OutputStream; |
741 |
10 Oct 06 |
olle |
64 |
import java.text.DateFormat; |
741 |
10 Oct 06 |
olle |
65 |
import java.util.Date; |
741 |
10 Oct 06 |
olle |
66 |
|
741 |
10 Oct 06 |
olle |
67 |
/** |
1652 |
22 May 07 |
gregory |
68 |
* |
1652 |
22 May 07 |
gregory |
* <B>About this class:</B> <BR> |
1652 |
22 May 07 |
gregory |
* This class reads the STDOUT from a script and sends it to the browser. <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
72 |
* |
1652 |
22 May 07 |
gregory |
* <B>How to use:</B> <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>(new ReadInputStreamSTDOUT(InputStream argIs, OutputStream argOs, |
1652 |
22 May 07 |
gregory |
* String argScriptLocation, NewConnection argTheNewConnection, |
1652 |
22 May 07 |
gregory |
* ReadInputStreamSTDERR argStdErrThread)).start();</CODE> <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
78 |
* |
1652 |
22 May 07 |
gregory |
* <B>More info:</B> <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>argIs</CODE> = STDERR from a script. <BR> |
741 |
10 Oct 06 |
olle |
* <CODE>argOs</CODE> = Everything written to this will be sent to browser. |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>argScriptLocation</CODE> = Location of script as browser path |
1652 |
22 May 07 |
gregory |
* ("http://server.com/dir1/dir2/file.pl" ==> "/dir1/dir2/") <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>argTheNewConnection</CODE> = this shall be the <CODE>NewConnection</CODE> |
1652 |
22 May 07 |
gregory |
* that belongs to the specific request that started RunScript. We need this to |
1652 |
22 May 07 |
gregory |
* change the value off <CODE>s_errorStatus</CODE> to "500 Internal Server |
1652 |
22 May 07 |
gregory |
* Error" and to run <CODE>addThisHitToLog()</CODE>. <BR> |
1652 |
22 May 07 |
gregory |
* <CODE>argStdErrThread</CODE> = this is the <CODE>ReadInputStreamSTDERR</CODE> |
1652 |
22 May 07 |
gregory |
* thread which looks for error output in STDERR. If (and only if) there is |
1652 |
22 May 07 |
gregory |
* STDERR from the script, <CODE>stdErrThread.b_hasSentData==true</CODE>. |
1652 |
22 May 07 |
gregory |
* Otherwise <CODE>stdErrThread.b_hasSentData==false</CODE>. We use this |
1652 |
22 May 07 |
gregory |
* information to make sure that <CODE>ReadInputStreamSTDOUT</CODE> doesn't |
1652 |
22 May 07 |
gregory |
* close <CODE>argOs</CODE> while <CODE>ReadInputStreamSTDERR</CODE> is |
1652 |
22 May 07 |
gregory |
* writing information to it. <CODE>ReadInputStreamSTDERR</CODE> will close |
1652 |
22 May 07 |
gregory |
* <CODE>argOs</CODE> when all STDERR has been sent to browser. <BR> |
741 |
10 Oct 06 |
olle |
* <BR> |
1652 |
22 May 07 |
gregory |
98 |
* |
1652 |
22 May 07 |
gregory |
* <B>IMPORTANT:</B> <CODE>argOs</CODE> is closed ("<CODE>argOs.close()</CODE>") |
1652 |
22 May 07 |
gregory |
* by <CODE>ReadInputStreamSTDOUT</CODE> if (and only if) <CODE>ReadInputStreamSTDERR</CODE> |
1652 |
22 May 07 |
gregory |
* doesn't write to <CODE>argOs</CODE>. |
1652 |
22 May 07 |
gregory |
102 |
* |
741 |
10 Oct 06 |
olle |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @version 1.0 |
741 |
10 Oct 06 |
olle |
105 |
*/ |
741 |
10 Oct 06 |
olle |
106 |
|
1652 |
22 May 07 |
gregory |
107 |
final public class ReadInputStreamSTDOUT extends ReadInputStream { |
741 |
10 Oct 06 |
olle |
108 |
private DataOutputStream os; |
1652 |
22 May 07 |
gregory |
109 |
|
1652 |
22 May 07 |
gregory |
110 |
private final DateFormat df_dateFormat = DateFormat.getDateTimeInstance( |
1652 |
22 May 07 |
gregory |
111 |
DateFormat.LONG, DateFormat.LONG); |
1652 |
22 May 07 |
gregory |
112 |
|
741 |
10 Oct 06 |
olle |
113 |
private String s_scriptLocation; |
1652 |
22 May 07 |
gregory |
114 |
|
741 |
10 Oct 06 |
olle |
115 |
private NewConnection NC_theNewConnection; |
1652 |
22 May 07 |
gregory |
116 |
|
741 |
10 Oct 06 |
olle |
117 |
private ReadInputStreamSTDERR stdErrThread; |
1652 |
22 May 07 |
gregory |
118 |
|
741 |
10 Oct 06 |
olle |
119 |
private MyInteger whoShallRespond; |
1652 |
22 May 07 |
gregory |
120 |
|
741 |
10 Oct 06 |
olle |
121 |
RunScript rs_theRunScript; |
741 |
10 Oct 06 |
olle |
122 |
|
1652 |
22 May 07 |
gregory |
123 |
public ReadInputStreamSTDOUT() // now we can Extend ReadInputStream without |
1652 |
22 May 07 |
gregory |
// problem |
741 |
10 Oct 06 |
olle |
125 |
{ |
741 |
10 Oct 06 |
olle |
126 |
} |
741 |
10 Oct 06 |
olle |
127 |
|
1652 |
22 May 07 |
gregory |
128 |
public ReadInputStreamSTDOUT(InputStream argIs, OutputStream argOs, |
1652 |
22 May 07 |
gregory |
129 |
String argScriptLocation, NewConnection argTheNewConnection, |
1652 |
22 May 07 |
gregory |
130 |
ReadInputStreamSTDERR argStdErrThread, |
1652 |
22 May 07 |
gregory |
131 |
MyInteger argWhoShallRespond, RunScript argTheRunScript) { |
1652 |
22 May 07 |
gregory |
132 |
whoShallRespond = argWhoShallRespond; |
1652 |
22 May 07 |
gregory |
133 |
currentMode = INPUT_MODE; |
1652 |
22 May 07 |
gregory |
134 |
is = argIs; |
1652 |
22 May 07 |
gregory |
135 |
os = (DataOutputStream) argOs; |
1652 |
22 May 07 |
gregory |
136 |
s_scriptLocation = argScriptLocation; |
1652 |
22 May 07 |
gregory |
137 |
NC_theNewConnection = argTheNewConnection; |
1652 |
22 May 07 |
gregory |
138 |
stdErrThread = argStdErrThread; |
1652 |
22 May 07 |
gregory |
139 |
rs_theRunScript = argTheRunScript; |
741 |
10 Oct 06 |
olle |
140 |
} |
741 |
10 Oct 06 |
olle |
141 |
|
2386 |
14 Nov 07 |
gregory |
142 |
@Override |
1652 |
22 May 07 |
gregory |
143 |
public void sendDataInputMode() { |
1652 |
22 May 07 |
gregory |
144 |
try { |
1652 |
22 May 07 |
gregory |
145 |
is = new BufferedInputStream(new DataInputStream(is)); |
741 |
10 Oct 06 |
olle |
146 |
|
1652 |
22 May 07 |
gregory |
147 |
int n; |
1652 |
22 May 07 |
gregory |
148 |
boolean b_hasSentHeader = false; |
1652 |
22 May 07 |
gregory |
149 |
boolean b_hasReadHeader = false; |
1652 |
22 May 07 |
gregory |
150 |
byte[] buffer = new byte[8192]; |
741 |
10 Oct 06 |
olle |
151 |
|
1652 |
22 May 07 |
gregory |
152 |
/******************************************************************* |
1652 |
22 May 07 |
gregory |
* ALGORITHM TO READ STDOUT FROM A SCRIPT AND GET HEADERS IT SENDS |
1652 |
22 May 07 |
gregory |
* ********* What we are doing now is to read one large buffer (call |
1652 |
22 May 07 |
gregory |
* it BUFFER) from the script output and then make a copy of this to |
1652 |
22 May 07 |
gregory |
* a String (lets call it "STR"). Our algoritm: 1) Read BUFFER. |
1652 |
22 May 07 |
gregory |
* Create STR from BUFFER. 2) Remove all \r and \n from the |
1652 |
22 May 07 |
gregory |
* *beginning* of STR. 3) If no more \r's and no \n's are left in |
1652 |
22 May 07 |
gregory |
* STR we are done! Send the BUFFER to browser and now send all |
1652 |
22 May 07 |
gregory |
* other outputs from script to browser. 4) Create int FST, which |
1652 |
22 May 07 |
gregory |
* contain the index of the first \r OR \n in STR. Choose \r OR \n |
1652 |
22 May 07 |
gregory |
* so FST becomes as small as possible, but not -1. 5) Create a new |
1652 |
22 May 07 |
gregory |
* string ONELINE. It contains everything from index 0 to FST in |
1652 |
22 May 07 |
gregory |
* STR. Make STR shorter with ONELINE.length. |
1652 |
22 May 07 |
gregory |
* (oldSTR=ONELINE+newSTR) 6) Go into the IF's and see if ONELINE |
1652 |
22 May 07 |
gregory |
* starts with for example "Location:" or "content-type:". If it |
1652 |
22 May 07 |
gregory |
* does, take actions and send BUFFER and other outputs. If not, go |
1652 |
22 May 07 |
gregory |
* back to step 2. |
1652 |
22 May 07 |
gregory |
169 |
******************************************************************/ |
741 |
10 Oct 06 |
olle |
170 |
|
1652 |
22 May 07 |
gregory |
// Note, when I run Perl-scripts one my Win XP all \n-outputs are |
1652 |
22 May 07 |
gregory |
// converted to \r\n, which means that print "Content-type: |
1652 |
22 May 07 |
gregory |
// text/html\r\n\r\n"; will be sent as "Content-type: |
1652 |
22 May 07 |
gregory |
// text/html\r\r\n\r\r\n" by perl.exe. This is nothing we care |
1652 |
22 May 07 |
gregory |
// about, but it might be good to know... |
1652 |
22 May 07 |
gregory |
176 |
String s_thisLine = null; |
1652 |
22 May 07 |
gregory |
177 |
boolean b_waitForData = true; // We use this to be able to check |
1652 |
22 May 07 |
gregory |
// for data in the InputStream |
1652 |
22 May 07 |
gregory |
// twice. If we don't find anything |
1652 |
22 May 07 |
gregory |
// the first time we check, we check |
1652 |
22 May 07 |
gregory |
// another time a few milliseconds |
1652 |
22 May 07 |
gregory |
// later |
1652 |
22 May 07 |
gregory |
183 |
while (b_waitForData) { |
1652 |
22 May 07 |
gregory |
184 |
b_waitForData = false; |
1652 |
22 May 07 |
gregory |
185 |
while ((n = is.read(buffer)) != -1) { |
1652 |
22 May 07 |
gregory |
186 |
b_waitForData = true; |
1652 |
22 May 07 |
gregory |
187 |
String s_firstPartOfData = new String(buffer, 0, n); // s_firstPartOfData |
1652 |
22 May 07 |
gregory |
// might |
1652 |
22 May 07 |
gregory |
// conatin |
1652 |
22 May 07 |
gregory |
// data |
1652 |
22 May 07 |
gregory |
// since |
1652 |
22 May 07 |
gregory |
// last |
1652 |
22 May 07 |
gregory |
// loop, |
1652 |
22 May 07 |
gregory |
// add |
1652 |
22 May 07 |
gregory |
// the |
1652 |
22 May 07 |
gregory |
// new |
1652 |
22 May 07 |
gregory |
// string |
1652 |
22 May 07 |
gregory |
// to |
1652 |
22 May 07 |
gregory |
// this. |
1652 |
22 May 07 |
gregory |
200 |
while (!b_hasReadHeader) { |
1652 |
22 May 07 |
gregory |
201 |
while (s_firstPartOfData.startsWith("\n") |
1652 |
22 May 07 |
gregory |
202 |
|| s_firstPartOfData.startsWith("\r")) |
1652 |
22 May 07 |
gregory |
203 |
s_firstPartOfData = s_firstPartOfData.substring(1); // Lines |
1652 |
22 May 07 |
gregory |
// staring |
1652 |
22 May 07 |
gregory |
// with |
1652 |
22 May 07 |
gregory |
// a \r |
1652 |
22 May 07 |
gregory |
// or |
1652 |
22 May 07 |
gregory |
// \n |
1652 |
22 May 07 |
gregory |
// shall |
1652 |
22 May 07 |
gregory |
// be |
1652 |
22 May 07 |
gregory |
// removed, |
1652 |
22 May 07 |
gregory |
// we |
1652 |
22 May 07 |
gregory |
// don't |
1652 |
22 May 07 |
gregory |
// care |
1652 |
22 May 07 |
gregory |
// for |
1652 |
22 May 07 |
gregory |
// \r |
1652 |
22 May 07 |
gregory |
// and |
1652 |
22 May 07 |
gregory |
// \n |
741 |
10 Oct 06 |
olle |
219 |
|
1652 |
22 May 07 |
gregory |
220 |
int distForR = s_firstPartOfData.indexOf("\r"), distForN = s_firstPartOfData |
1652 |
22 May 07 |
gregory |
221 |
.indexOf("\n"); |
1652 |
22 May 07 |
gregory |
222 |
int minDistForNandR; |
741 |
10 Oct 06 |
olle |
223 |
|
1652 |
22 May 07 |
gregory |
224 |
if (distForN + distForR == -2) // both are -1 ==> no \r |
1652 |
22 May 07 |
gregory |
// or \n exists ==> we |
1652 |
22 May 07 |
gregory |
// are done locking for |
1652 |
22 May 07 |
gregory |
// headers... |
741 |
10 Oct 06 |
olle |
228 |
{ |
1652 |
22 May 07 |
gregory |
229 |
b_hasReadHeader = true; |
1652 |
22 May 07 |
gregory |
230 |
} else // Atleast one of distForN or distForR is != -1 |
741 |
10 Oct 06 |
olle |
231 |
{ |
1652 |
22 May 07 |
gregory |
// set minDistForNandR to the lowest value of |
1652 |
22 May 07 |
gregory |
// distForN and distForR, but don't set it to -1 |
1652 |
22 May 07 |
gregory |
234 |
if (distForR > distForN) |
1652 |
22 May 07 |
gregory |
235 |
if (distForN != -1) |
1652 |
22 May 07 |
gregory |
236 |
minDistForNandR = distForN; |
741 |
10 Oct 06 |
olle |
237 |
else |
1652 |
22 May 07 |
gregory |
238 |
minDistForNandR = distForR; |
1652 |
22 May 07 |
gregory |
239 |
else if (distForR != -1) |
1652 |
22 May 07 |
gregory |
240 |
minDistForNandR = distForR; |
741 |
10 Oct 06 |
olle |
241 |
else |
1652 |
22 May 07 |
gregory |
242 |
minDistForNandR = distForN; |
741 |
10 Oct 06 |
olle |
243 |
|
1652 |
22 May 07 |
gregory |
244 |
s_thisLine = s_firstPartOfData.substring(0, |
1652 |
22 May 07 |
gregory |
245 |
minDistForNandR); |
1652 |
22 May 07 |
gregory |
246 |
s_firstPartOfData = s_firstPartOfData |
1652 |
22 May 07 |
gregory |
247 |
.substring(minDistForNandR + 1); |
1652 |
22 May 07 |
gregory |
248 |
s_thisLine = s_thisLine.toLowerCase(); |
1652 |
22 May 07 |
gregory |
249 |
if (s_thisLine.startsWith("content-type:")) { |
1652 |
22 May 07 |
gregory |
// We don't take any special actions now, except |
1652 |
22 May 07 |
gregory |
// that we stop searching for "Location:" |
1652 |
22 May 07 |
gregory |
252 |
b_hasReadHeader = true; |
1652 |
22 May 07 |
gregory |
253 |
} else if (s_thisLine.startsWith("location:")) { |
1652 |
22 May 07 |
gregory |
// We change error code |
1652 |
22 May 07 |
gregory |
255 |
NC_theNewConnection.s_errorStatus = "302 Found"; |
1652 |
22 May 07 |
gregory |
256 |
b_hasReadHeader = true; |
741 |
10 Oct 06 |
olle |
257 |
} |
741 |
10 Oct 06 |
olle |
258 |
} |
741 |
10 Oct 06 |
olle |
259 |
} |
741 |
10 Oct 06 |
olle |
260 |
|
1652 |
22 May 07 |
gregory |
261 |
if (!b_hasSentHeader) { |
1652 |
22 May 07 |
gregory |
262 |
b_hasSentHeader = true; |
741 |
10 Oct 06 |
olle |
263 |
sendHeaderInfo(); |
741 |
10 Oct 06 |
olle |
264 |
} |
741 |
10 Oct 06 |
olle |
265 |
|
1652 |
22 May 07 |
gregory |
266 |
if (whoShallRespond.getValue() != RESPOND_STDERR) { |
1652 |
22 May 07 |
gregory |
267 |
NewConnection.totalNumberOfBytesDownloaded += n; |
1652 |
22 May 07 |
gregory |
268 |
os.write(buffer, 0, n); |
1652 |
22 May 07 |
gregory |
269 |
} else { |
1652 |
22 May 07 |
gregory |
270 |
break; // If STDERR is being sent right now, then we |
1652 |
22 May 07 |
gregory |
// stop here and don't send STDOUT |
741 |
10 Oct 06 |
olle |
272 |
} |
1652 |
22 May 07 |
gregory |
273 |
sleep(25); // Don't remove: without this it might happen |
1652 |
22 May 07 |
gregory |
// that we don't read the complete output (or |
1652 |
22 May 07 |
gregory |
// any output) from the process. |
741 |
10 Oct 06 |
olle |
276 |
} |
741 |
10 Oct 06 |
olle |
277 |
|
1652 |
22 May 07 |
gregory |
278 |
yield(); |
1652 |
22 May 07 |
gregory |
279 |
sleep(75); // Don't remove: without this it might happen that |
1652 |
22 May 07 |
gregory |
// we don't read the complete output (or any output) |
1652 |
22 May 07 |
gregory |
// from the process. |
741 |
10 Oct 06 |
olle |
282 |
} |
741 |
10 Oct 06 |
olle |
283 |
|
741 |
10 Oct 06 |
olle |
284 |
yield(); |
741 |
10 Oct 06 |
olle |
285 |
sleep(150); |
741 |
10 Oct 06 |
olle |
286 |
|
1652 |
22 May 07 |
gregory |
287 |
if (whoShallRespond.getValue() == RESPOND_STDOUT) // Only add to |
1652 |
22 May 07 |
gregory |
// log if we are |
1652 |
22 May 07 |
gregory |
// sending |
1652 |
22 May 07 |
gregory |
// STDOUT, |
1652 |
22 May 07 |
gregory |
// otherwise |
1652 |
22 May 07 |
gregory |
// ReadInputStreamSTDERR |
1652 |
22 May 07 |
gregory |
// will add to |
1652 |
22 May 07 |
gregory |
// log... |
1652 |
22 May 07 |
gregory |
295 |
{ |
1652 |
22 May 07 |
gregory |
296 |
NC_theNewConnection.addThisHitToLog(); // Log this visitor |
1652 |
22 May 07 |
gregory |
297 |
} |
741 |
10 Oct 06 |
olle |
298 |
|
1652 |
22 May 07 |
gregory |
299 |
if (!stdErrThread.b_hasSentData) // If ReadInputStreamSTDERR has |
1652 |
22 May 07 |
gregory |
// found output in the STDERR, |
1652 |
22 May 07 |
gregory |
// then we shall not close the |
1652 |
22 May 07 |
gregory |
// stream, as |
1652 |
22 May 07 |
gregory |
// ReadInputStreamSTDERR is |
1652 |
22 May 07 |
gregory |
// writing to it. |
1652 |
22 May 07 |
gregory |
305 |
{ |
1652 |
22 May 07 |
gregory |
306 |
yield(); |
1652 |
22 May 07 |
gregory |
307 |
sleep(125); // Be safe, don't close before all data has been |
1652 |
22 May 07 |
gregory |
// sent |
741 |
10 Oct 06 |
olle |
309 |
|
1652 |
22 May 07 |
gregory |
310 |
os.flush(); |
1652 |
22 May 07 |
gregory |
311 |
os.close(); |
1652 |
22 May 07 |
gregory |
312 |
} |
741 |
10 Oct 06 |
olle |
313 |
|
1652 |
22 May 07 |
gregory |
314 |
} catch (Exception e) { |
1652 |
22 May 07 |
gregory |
315 |
exceptionOccured(e); |
1652 |
22 May 07 |
gregory |
316 |
if (b_showErrors) |
1652 |
22 May 07 |
gregory |
317 |
System.out |
1652 |
22 May 07 |
gregory |
318 |
.println("An error has occured @ sendDataInputMode:\n" |
1652 |
22 May 07 |
gregory |
319 |
+ e.getMessage()); |
1652 |
22 May 07 |
gregory |
320 |
} |
741 |
10 Oct 06 |
olle |
321 |
} |
741 |
10 Oct 06 |
olle |
322 |
|
1652 |
22 May 07 |
gregory |
323 |
private void sendHeaderInfo() { |
741 |
10 Oct 06 |
olle |
324 |
try { |
1652 |
22 May 07 |
gregory |
325 |
if (whoShallRespond.getValue() != RESPOND_STDERR) { |
1652 |
22 May 07 |
gregory |
326 |
whoShallRespond.setValue(RESPOND_STDOUT); // This will make |
1652 |
22 May 07 |
gregory |
// sure so no STDERR |
1652 |
22 May 07 |
gregory |
// is sent to the |
1652 |
22 May 07 |
gregory |
// browser (a |
1652 |
22 May 07 |
gregory |
// reference of |
1652 |
22 May 07 |
gregory |
// "B_hasGotAResponse" |
1652 |
22 May 07 |
gregory |
// is also at |
1652 |
22 May 07 |
gregory |
// ReadInputStreamSTDERR, |
1652 |
22 May 07 |
gregory |
// which will |
1652 |
22 May 07 |
gregory |
// "block" it from |
1652 |
22 May 07 |
gregory |
// sending data now) |
1652 |
22 May 07 |
gregory |
337 |
os.writeBytes("" |
1652 |
22 May 07 |
gregory |
338 |
+ "HTTP/1.1 " |
1652 |
22 May 07 |
gregory |
339 |
+ NC_theNewConnection.s_errorStatus |
1652 |
22 May 07 |
gregory |
340 |
+ "\r\n" |
1652 |
22 May 07 |
gregory |
341 |
+ "Date: " |
1652 |
22 May 07 |
gregory |
342 |
+ df_dateFormat.format(new Date()) |
1652 |
22 May 07 |
gregory |
343 |
+ "\r\n" |
1652 |
22 May 07 |
gregory |
344 |
+ "Server: " |
1652 |
22 May 07 |
gregory |
345 |
+ XerverKernel.getXerverName() |
1652 |
22 May 07 |
gregory |
346 |
+ "\r\n" |
1652 |
22 May 07 |
gregory |
347 |
+ "Connection: close\r\n" |
1652 |
22 May 07 |
gregory |
348 |
+ ((NC_theNewConnection.s_errorStatus |
1652 |
22 May 07 |
gregory |
349 |
.equals("302 Found")) ? ("") : ("Location: " |
1652 |
22 May 07 |
gregory |
350 |
+ s_scriptLocation + "\r\n")) // If the script |
1652 |
22 May 07 |
gregory |
// has an |
1652 |
22 May 07 |
gregory |
// Location-line |
1652 |
22 May 07 |
gregory |
// as output, |
1652 |
22 May 07 |
gregory |
// don't send |
1652 |
22 May 07 |
gregory |
// our own |
1652 |
22 May 07 |
gregory |
// Location-line |
1652 |
22 May 07 |
gregory |
357 |
); |
741 |
10 Oct 06 |
olle |
358 |
} |
1652 |
22 May 07 |
gregory |
359 |
} catch (Exception e) { |
1652 |
22 May 07 |
gregory |
360 |
exceptionOccured(e); |
1652 |
22 May 07 |
gregory |
361 |
if (b_showErrors) |
1652 |
22 May 07 |
gregory |
362 |
System.out.println("An error has occured @ sendHeaderInfo\n" |
1652 |
22 May 07 |
gregory |
363 |
+ e); |
1652 |
22 May 07 |
gregory |
364 |
} |
741 |
10 Oct 06 |
olle |
365 |
} |
741 |
10 Oct 06 |
olle |
366 |
|
1652 |
22 May 07 |
gregory |
367 |
private void exceptionOccured(Exception e) { |
741 |
10 Oct 06 |
olle |
368 |
rs_theRunScript.killProcess(); |
741 |
10 Oct 06 |
olle |
369 |
} |
741 |
10 Oct 06 |
olle |
370 |
} |