public class HttpServer extends Thread
{
protected String Docroot; //
Root des Server-Dateisystems
protected String Index = "index.html";
protected String CgiProg; //
Cgi-Interpreter
protected String CgiDir; // Verzeichnis
der Cgi-Dateisystems
protected ServerSocket Socket; // Die
Socket des Servers
public HttpServer(String docroot, String index, int port,
String cgiprog, String cgidir) throws IOException
{
Docroot = docroot;
CgiProg = cgiprog;
CgiDir = cgidir;
Index = index;
Socket = new ServerSocket(port); //
Erzeugen der Socket
}
// Warten auf Requests
public void run()
{
while (true)
{ // Endlos auf Verbindungen warten
...
while (activeCount() > 100);
// max. 100 Threads starten, geg. warten
try
{
new HttpHandler(this,
Socket.accept()).start();
// fuer jede Verbindung
ein neuer Thread
}
catch (IOException e)
{ // Ganz gleich welcher Fehler
...
System.err.println("Exception: " + e );
}
}
}
static public String GuessContentType(String name)
{ // erraet den MIME-Type anhand der
Extension
if (name.endsWith(".html") || name.endsWith(".htm")) return
"text/html";
else if (name.endsWith(".txt") || name.endsWith(".java"))
return "text/plain";
else if (name.endsWith(".gif")) return "image/gif";
else if (name.endsWith(".class")) return "application/octet-stream";
else if (name.endsWith(".jpg") || name.endsWith(".jpeg"))
return "image/jpeg";
else return "text/plain";
}
static public boolean IsCgi(String name)
{
return (name.endsWith(".cgi"));
}
public String Translate(String name)
{
if (name.endsWith("/")) // evt. Indexfile
anhaengen
name += Index;
if (IsCgi(name))
return CgiDir + name;
else
return Docroot + name;
}
public Process CallCgi(String cgi, String[] env) throws IOException
{
return Runtime.getRuntime().exec(CgiProg
+ " " + cgi, env);
}
}