Tinyiko
Posts: 1
|
Posted: 05/18/2004, 2:47 AM |
|
I've designed a simple "web server" using java. This "web server" receives a request and sends a response. My problem is that the web server processes text,html and xml files but it can't process php files,please look at this code:
package fileport;
/**
* <p>Title: XML.Fileport</p>
* <p>Description: File airport for XML.Relation suite</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Starship Systems</p>
* @author Herman van Heerden
* @version 1.0
*/
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
public class oFileServerClient extends Thread {
private Socket soc;
private String sServerPath;
private boolean bDebug;
private boolean bServeOnce;
// === CONSTRUCTOR ===========================================================
public oFileServerClient(Socket socClient, String sINOUTPath) {
soc = socClient;
sServerPath = sINOUTPath;
bDebug = false;
bServeOnce = false;
}
// --- thread: run -----------------------------------------------------------
public void run() {
String sOut;
if (bDebug) System.out.println("serving...");
try {
// Access in/out
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream out = new DataOutputStream(soc.getOutputStream());
out.writeBytes("HTTP/1.0 200 Document Follows\r\n");
//out.flush();
if (bDebug) System.out.println(" first HTTP output sent...");
String sRequest = in.readLine();
StringTokenizer sRequestSplit = new StringTokenizer(sRequest);
if (sRequestSplit.nextToken().equals("GET")){
if (bDebug) System.out.println(" HTTP GET response...");
String fileName = sRequestSplit.nextToken();
if (fileName.startsWith("/") == true) fileName = fileName.substring(1);
File file = new File(sServerPath + fileName);
if ((file.canRead()) && (file.isFile())) {
if (ServeTXT(fileName,file,out) && bDebug) System.out.println(" Serverd XML");
}
else {
if (bDebug) System.out.println(" file not found...");
sOut = "<?xml version=\"1.0\" ?><access xmlrver=\"1.0\"><cube name=\"fileporterror\" action=\"data\"><error type=\"text\">The file you requested does not exist on the server.</error></cube></access>";
out.writeBytes("Content-Type: text/xml\r\n");
out.writeBytes("Content-Length: " + sOut.length() + "\r\n");
out.writeBytes("\r\n");
out.writeBytes(sOut);
}
}
else {
if (bDebug) System.out.println(" HTTP GET not sent...");
sOut = "<?xml version=\"1.0\" ?><access xmlrver=\"1.0\"><cube name=\"fileporterror\" action=\"data\"><error type=\"text\">Incorrect HTTP action specified.</error></cube></access>";
out.writeBytes("Content-Type: text/xml\r\n");
out.writeBytes("Content-Length: " + sOut.length() + "\r\n");
out.writeBytes("\r\n");
out.writeBytes(sOut);
}
soc.close();
}
catch (Exception e) {
if (bDebug) System.out.println("debug. Client serving error: " + e.toString());
}
if (bDebug) System.out.println("serverd.");
}
// === METHODS ===============================================================
// --- DebugOn ---------------------------------------------------------------
public void DebugOn() {
bDebug = true;
}
// --- DebugOff --------------------------------------------------------------
public void DebugOff() {
bDebug = false;
}
// --- ServeOnceOn -----------------------------------------------------------
public void ServeOnceOn() {
bServeOnce = true;
}
// --- ServeOnceOff ----------------------------------------------------------
public void ServeOnceOff() {
bServeOnce = false;
}
// === PRIVATE ===
// --- ServeText ---
private boolean ServeText(String Type,String fileName, File file, DataOutputStream out) {
try {
FileInputStream inFile = new FileInputStream(sServerPath + fileName);
out.writeBytes("Content-Type: text/" + Type + "\r\n");
out.writeBytes("Content-Length: " + file.length() + "\r\n");
out.writeBytes("\r\n");
byte[] fileInBytes = new byte[(int)file.length()];
inFile.read(fileInBytes);
if (bDebug) System.out.println(" file found, now serving " + file.length() + " bytes...");
out.write(fileInBytes);
out.flush();
out.close();
inFile.close(); // SUCCESS!
if (bServeOnce) {
if (bDebug) System.out.println(" served once! deleting file...");
file.delete();
}
return true;
}
catch (Exception e) {
if (bDebug) System.out.println(" ServeText error: " + e.toString());
return false;
}
}
// --- ServePHP ---
private void ServePHP(String sFile) {
}
// --- ServeHTML ---
private boolean ServeHTML(String fileName, File file, DataOutputStream out) {
return ServeText("html",fileName,file,out);
}
// --- ServerXML ---
private boolean ServeXML(String fileName, File file, DataOutputStream out) {
return ServeText("xml",fileName,file,out);
}
// --- ServerTXT ---
private boolean ServeTXT(String fileName, File file, DataOutputStream out) {
return ServeText("text",fileName,file,out);
}
}
|
|
|
|