Below is a rather lengthy bit of code that provides the attributes and behavior of a webserver for OpenCyc, all of which is on my local machine. I need to know how the source code (HTML) from each page the server returns to the client (me) can be displayed. Ideally it will be displayed automatically within a separate text file, without going to a menu and selecting "view source." Thanks in advance.
*************************
PHP Code:
/**
* Class WebServer is simple multithreaded HTTP server
* with CGI limited to a Cyc connection on default port 3600.
*/
public class WebServer extends Thread {
/**
* Singleton WebServer instance.
*/
public static WebServer current;
}
/**
* Processes an HTTP GET method.
* @param httpGetPath the path of the file to get.
*/
private void processHttpGet(String httpGetPath) throws IOException {
int i = httpGetPath.indexOf(' ');
if (i > 0)
httpGetPath = httpGetPath.substring(0, i);
Log.current.println(methodLine + " from " + sock.getInetAddress().getHostName());
i = httpGetPath.indexOf("cg?");
if (i > 0) {
cycHtmlRequest(httpGetPath.substring(i + 3));
return;
}
notFoundPath = httpGetPath;
i = httpGetPath.indexOf('/');
if (i < 0 || map == null) {
if (map == null || httpGetPath.endsWith(".jar")) {
for (int j = 0; j < dirs.size(); j++) {
String dir = (String) dirs.get(j);
String nativePath = dir + httpGetPath;
nativePath = nativePath.replace('/', File.separatorChar);
if (fileCache.containsKey(nativePath)) {
writeDataBytes((byte[]) fileCache.get(nativePath));
Log.current.println("...cached");
nbrCacheHits++;
nbrFilesServed++;
return;
}
try {
File f = new File(nativePath);
byte[] fileBytes = getBytes(new FileInputStream(f), f.length());
writeDataBytes(fileBytes);
if (fileCache.size() >= CACHE_CAPACITY)
fileCache.clear();
fileCache.put(nativePath, fileBytes);
Log.current.println("...from " + nativePath);
nbrFilesServed++;
return;
}
catch (IOException e) {
}
}
}
throw new IOException();
}
/**
* Processes an HTTP POST method.
*/
private void processHttpPost() throws IOException {
Log.current.println("POST " + bodyLine + " from " + sock.getInetAddress().getHostName());
cycHtmlRequest(bodyLine);
}
/**
* Reads the specified number of bytes and always close the stream.
* @param in the file to be read for subsequent downloading.
* @return An array of bytes from the file.
*/
private byte[] getBytes(InputStream in, long length) throws IOException {
DataInputStream din = new DataInputStream(in);
byte[] bytes = new byte[ (int) length];
try {
din.readFully(bytes);
}
finally {
din.close();
}
return bytes;
}
/**
* Sends the HTML request to Cyc.
* @param cycPath the portion of the URL which is given to the Cyc HTML server.
*
*/
private void cycHtmlRequest(String cycPath) {
String request = sock.getInetAddress().getHostName() + "&" + cycPath + "#";
System.out.println("request=" + request);
ArrayList bytes = new ArrayList(10000);
try {
cycHtmlSocket = new Socket(cycHost, cycPort);
System.out.println("cycHost=" + cycHost + " cycPort=" + cycPort);
BufferedReader cycIn = new BufferedReader(new InputStreamReader(cycHtmlSocket.getInputStream()));
PrintWriter cycOut = new PrintWriter(cycHtmlSocket.getOutputStream(), true);
cycOut.println(request);
cycOut.flush();
int ch = 0;
while (ch >= 0) {
ch = cycIn.read();
bytes.add(new Integer(ch));
}
}
/**
* Responds to the HTTP client with data content from the requested URL.
* @param bytes the array of bytes from the URL.
*/
public void writeDataBytes(byte[] bytes) throws IOException {
out.writeBytes("HTTP/1.1 200 OK\r\n");
out.writeBytes("Server: Cyc WebServer\r\n");
out.writeBytes("Connection: close\r\n");
out.writeBytes("Content-Length: " + bytes.length + "\r\n");
String prefix = (new String(bytes)).toLowerCase();
if (prefix.indexOf("<html>") > -1)
out.writeBytes("Content-Type: text/html\r\n\r\n");
else
out.writeBytes("Content-Type: application/java\r\n\r\n");
out.write(bytes);
out.flush();
}
/**
* Respond to the HTTP client with text content from the requested URL.
* @param bytes the array of bytes from the URL.
*/
public void writeTextBytes(byte[] bytes) throws IOException {
out.writeBytes("HTTP/1.1 200 OK\r\n");
out.writeBytes("Server: Cyc WebServer\r\n");
out.writeBytes("Connection: close\r\n");
out.writeBytes("Content-Length: " + bytes.length + "\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.write(bytes);
out.flush();
}
}
/**
* Gets properties governing the web server's behavior.
*/
private void getProperties() {
port = DEFAULT_PORT;
String portProperty = System.getProperty("org.opencyc.webserver.port", "");
if (! portProperty.equalsIgnoreCase(""))
port = (new Integer(portProperty)).intValue();
Log.current.println("Listening on port " + port);
cycPort = DEFAULT_CYC_PORT;
String cycPortProperty = System.getProperty("org.opencyc.webserver.cycPort", "");
if (! cycPortProperty.equalsIgnoreCase(""))
cycPort = (new Integer(cycPortProperty)).intValue();
Log.current.println("Cyc connections directed to port " + cycPort);
String dirsProperty = System.getProperty("org.opencyc.webserver.dirs", "");
dirs = new ArrayList(3);
StringTokenizer st = new StringTokenizer(dirsProperty, ";", false);
while (st.hasMoreTokens()) {
String dir = st.nextToken();
dirs.add(dir);
}
trees = false;
String treesProperty = System.getProperty("org.opencyc.webserver.trees", "");
if (! treesProperty.equalsIgnoreCase(""))
trees = true;
traceRequests = false;
String traceRequestsProperty = System.getProperty("org.opencyc.webserver.traceRequests", "");
if (! traceRequestsProperty.equalsIgnoreCase("")) {
traceRequests = true;
Log.current.println("tracing requests");
}
}
/**
* Administrative accessor method that obtains list of directories from which files are served.
*/
public ArrayList getDirs() {
return dirs;
}
/**
* Administrative method that updates the list of directories from which files are served.
*/
public synchronized void setDirs(ArrayList dirs) throws IOException {
this.dirs = dirs;
fileCache.clear();
processDirectories();
}
/**
* Processes the directories from which files are served, expanding jar trees if
* directed.
* @exception IOException if problem occurs while processing the jar files.
*/
private void processDirectories() throws IOException {
if (dirs.size() == 0)
if (File.separatorChar == '\\')
dirs.add(DEFAULT_WIN_DIR);
else
dirs.add(DEFAULT_DIR);
Iterator directories = dirs.iterator();
while (directories.hasNext())
Log.current.println("Serving from " + directories.next());
if (trees) {
map = new HashMap();
for (int j = 0; j < dirs.size(); j++) {
String dir = (String) dirs.get(j);
String[] files = new File(dir).list();
for (int i = 0; i < files.length; i++) {
String jar = files[i];
if (!jar.endsWith(".jar"))
continue;
ArrayList jfs = new ArrayList(1);
addJar(jar, jfs, dir);
map.put(jar.substring(0, jar.length() - 4), jfs.toArray(new JarFile[jfs.size()]));
}
}
}
}
public static void main(String[] args) {
Log.makeLog();
System.out.println("OpenCyc Web Server");
try {
// Launch thread to accept HTTP connections.
current = new WebServer();
current.start();
}
catch (IOException e) {
e.printStackTrace();
}
}
}


Reply With Quote


Bookmarks