while (true) {
// read
one line from the cached document
str = readLine(Cache_File);
// if
there is nothing to read, we are done
if (str.equals(""))
break;
// if
this line contains a SSI variable, substitute it
if (str.indexOf("<!--#echo
var=\"") != -1) {
str = substitute(str,
Client_IP_Address, Client_Name);
}
// output
the line into the new document
byte buf[] = Convert(str);
write(New_File, buf, buf.length);
}
// let the ActiveCache
return the new document
return 1;
}
// read one line from an opened file,
fd is file descriptor
private static String readLine(int fd)
{
StringBuffer strbuf = new StringBuffer(0);
byte buf[] = new byte[1];
while (read(fd, buf, 1)==1) {
strbuf.append((char)buf[0]);
// read
until a newline character
if (buf[0] == '\n') break;
}
return new String(strbuf);
}
// convert a String to a byte array
private static byte[] Convert(String str)
{
int i;
byte buf[] = new byte[str.length()];
for (i=0; i<str.length(); i++) {
buf[i] = (byte)(str.charAt(i));
}
return buf;
}
// substitute a SSI line to its corresponding
value of the variable
// a SSI pattern is like: <!--#echo
var="VARIABLE_NAME"->
private static String substitute(String str,
String Client_IP_Address,
String Client_Name)
{
int pos1, pos2;
StringBuffer newStr = new StringBuffer(0);
// find the left part of the
pattern
pos1 = str.indexOf("<!--#echo var=\"");
// pattern not exist, return
the original line
if (pos1 == -1) return str;
// copy the part before the left
part of pattern
newStr.append(str.substring(0, pos1));
// find the right part of the
pattern
pos1 += 15;
pos2 = str.indexOf("\"->", pos1);
// pattern not exist, return
the original line
if (pos2 == -1) return str;
// get the variable name from
the pattern
String variable = str.substring(pos1, pos2);
// append the value of the variable
here
if (variable.equals("DATE_GMT")) {
newStr.append(curtime());
}
else if (variable.equals("DATE_LOCAL")) {
newStr.append(curtime());
}
else if (variable.equals("REMOTE_ADDRESS")) {
newStr.append(Client_IP_Address);
}
else if (variable.equals("REMOTE_HOST")) {
newStr.append(Client_Name);
}
else return str;
// append the part after the
right part of parttern
newStr.append(str.substring(pos2+3, str.length()));
return new String(newStr);
}
}