Web Server
Applet
Explanation
import java.net.*;
import java.io.*;
public class WebServe
{
// Parses received string looking for requested file name
// Strips "GET" and "HTTP" version, default filename is "index.html"
static String getPath(String msg)
{
if (msg.length() == 0 || !msg.substring(0,3).equals("GET")) return null;
String path = msg.substring(msg.indexOf(' ')+1);
path = path.substring(0, path.indexOf(' '));
if (path.equals("")) return "index.html";
if (path.charAt(path.length()-1) == '/') path += "index.html";
return path;
}
// Constructs header of served message to client
static void fileFoundHeader(PrintWriter os, int filelength)
{
os.print("HTTP:/1.0 200 OK\n");
os.print("MIME-Version: 1.0\n");
os.print("Content-type: text/html\n");
os.print("Content-length: "+filelength+"\n");
os.print("\n");
}
// Constructs body of reply to client
static void sendReply(PrintWriter os, BufferedReader in, int flen)
{
try
{
char buffer[] = new char[flen];
in.read(buffer, 0, flen);
os.write(buffer, 0, flen);
in.close();
}
catch (Exception e) { System.out.println(e); }
}
// In case of error - sends an error header only
static void errorHeader(PrintWriter os, String err_message)
{
os.print("HTTP:/1.0 404 Not Found\n");
os.print("Content-type: text/html\n");
os.print("Content-length: "+err_message.length()+"\n");
os.print("\n");
os.print(err_message+"\n");
}
// Opens a file. Some "/" may need to be stripped.
static File OpenFile(String filename)
{
File file = new File(filename);
if (file.exists()) return file;
if (filename.charAt(0) != '/') return file;
return new File(filename.substring(1));
}
public static void main(String arg[])
{
String path, a; BufferedReader in, is; PrintWriter os;
try
{
// Creates server socket at port 8080
ServerSocket server = new ServerSocket(8080);
System.out.println("Webserver is fully operational and listening to port 8080...");
while (true)
{
// Starts listening on port 8080
Socket client = server.accept();
String destname = client.getInetAddress().getHostName();
System.out.println("Connect:"+client.getInetAddress()); /***/
// If a client connects, opens I/O streams to client
os = new PrintWriter(client.getOutputStream());
InputStream ins = client.getInputStream();
is = new BufferedReader(new InputStreamReader(ins));
// Reads the first (GET) line of request header
String il = is.readLine();
System.out.println("Got:"+il); /***/
// Reads the remainder of the request header for laughs
a = is.readLine();
while (!a.equals(""))
{
System.out.println("Got:"+a); /***/
a = is.readLine();
}
System.out.println("----------------------------"); /***/
if ((path = getPath(il)) != null)
{
File file = OpenFile(path);
if (file.exists())
{
try
{
// If requested file can be opened send it
FileInputStream fis = new FileInputStream(file);
in = new BufferedReader(new InputStreamReader(fis));
fileFoundHeader(os, (int)file.length());
sendReply(os, in, (int)file.length());
}
catch (Exception e)
{
// If not, send error header
errorHeader(os, "< h2>Can't Read "+path+"< /h2>");
}
os.flush();
}
else // If file does not exist, send error header
errorHeader(os, "< h2>Not Found "+path+"< /h2>");
}
client.close();
}
}
catch (IOException e) { System.out.println(e); }
}
}